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