github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3.0/policies/requests.go (about) 1 package policies 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/golangsdk/pagination" 6 ) 7 8 // Get retrieves details on a single policy, by ID. 9 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 10 _, r.Err = client.Get(resourceURL(client, id), &r.Body, &golangsdk.RequestOpts{ 11 OkCodes: []int{200}, 12 MoreHeaders: map[string]string{"Content-Type": "application/json"}, 13 }) 14 return 15 } 16 17 // List retrieves all custom policies. 18 func List(client *golangsdk.ServiceClient) pagination.Pager { 19 pager := pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page { 20 return RolePage{pagination.LinkedPageBase{PageResult: r}} 21 }) 22 pager.Headers = map[string]string{"Content-Type": "application/json;charset=utf8"} 23 24 return pager 25 } 26 27 // CreateOptsBuilder allows extensions to add additional parameters to 28 // the Create request. 29 type CreateOptsBuilder interface { 30 ToPolicyCreateMap() (map[string]interface{}, error) 31 } 32 33 // Policy contains the content of a custom policy. 34 type Policy struct { 35 Version string `json:"Version" required:"true"` 36 Statement []Statement `json:"Statement" required:"true"` 37 } 38 39 // Statement represents the Statement of a custom policy. 40 type Statement struct { 41 Action []string `json:"Action" required:"true"` 42 Effect string `json:"Effect" required:"true"` 43 Condition map[string]interface{} `json:"Condition,omitempty"` 44 Resource interface{} `json:"Resource,omitempty"` 45 } 46 47 // CreateOpts provides options used to create a policy. 48 type CreateOpts struct { 49 Name string `json:"display_name" required:"true"` 50 Type string `json:"type" required:"true"` 51 Description string `json:"description" required:"true"` 52 Policy Policy `json:"policy" required:"true"` 53 } 54 55 // ToPolicyCreateMap formats a CreateOpts into a create request. 56 func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) { 57 b, err := golangsdk.BuildRequestBody(opts, "role") 58 if err != nil { 59 return nil, err 60 } 61 62 return b, nil 63 } 64 65 // Create creates a new Policy. 66 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 67 b, err := opts.ToPolicyCreateMap() 68 if err != nil { 69 r.Err = err 70 return 71 } 72 _, r.Err = client.Post(rootURL(client), &b, &r.Body, &golangsdk.RequestOpts{ 73 OkCodes: []int{201}, 74 MoreHeaders: map[string]string{"Content-Type": "application/json"}, 75 }) 76 return 77 } 78 79 // Update updates an existing Policy. 80 func Update(client *golangsdk.ServiceClient, roleID string, opts CreateOptsBuilder) (r UpdateResult) { 81 b, err := opts.ToPolicyCreateMap() 82 if err != nil { 83 r.Err = err 84 return 85 } 86 _, r.Err = client.Patch(resourceURL(client, roleID), &b, &r.Body, &golangsdk.RequestOpts{ 87 OkCodes: []int{200}, 88 MoreHeaders: map[string]string{"Content-Type": "application/json"}, 89 }) 90 return 91 } 92 93 // Delete deletes a policy. 94 func Delete(client *golangsdk.ServiceClient, roleID string) (r DeleteResult) { 95 _, r.Err = client.Delete(resourceURL(client, roleID), &golangsdk.RequestOpts{ 96 OkCodes: []int{200}, 97 MoreHeaders: map[string]string{"Content-Type": "application/json"}, 98 }) 99 return 100 }