Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 0f6f5cb

Browse files
committed
Add identity polices support
1 parent 8d6995f commit 0f6f5cb

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package policies
2+
3+
import (
4+
"github.com/huaweicloud/golangsdk"
5+
)
6+
7+
type Role struct {
8+
ID string `json:"id"`
9+
Name string `json:"display_name"`
10+
Catalog string `json:"catalog"`
11+
Description string `json:"description"`
12+
Type string `json:"type"`
13+
Policy Policy `json:"policy" required:"true"`
14+
DomainId string `json:"domain_id"`
15+
References int `json:"references"`
16+
}
17+
18+
type roleResult struct {
19+
golangsdk.Result
20+
}
21+
22+
// GetResult is the response from a Get operation. Call its Extract method
23+
// to interpret it as a Role.
24+
type GetResult struct {
25+
roleResult
26+
}
27+
28+
// CreateResult is the response from a Create operation. Call its Extract method
29+
// to interpret it as a Role
30+
type CreateResult struct {
31+
roleResult
32+
}
33+
34+
// UpdateResult is the response from an Update operation. Call its Extract
35+
// method to interpret it as a Role.
36+
type UpdateResult struct {
37+
roleResult
38+
}
39+
40+
// DeleteResult is the response from a Delete operation. Call its ExtractErr to
41+
// determine if the request succeeded or failed.
42+
type DeleteResult struct {
43+
golangsdk.ErrResult
44+
}
45+
46+
// Extract interprets any roleResults as a Role.
47+
func (r roleResult) Extract() (*Role, error) {
48+
var s struct {
49+
Role *Role `json:"role"`
50+
}
51+
err := r.ExtractInto(&s)
52+
return s.Role, err
53+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package policies
2+
3+
import "github.com/huaweicloud/golangsdk"
4+
5+
const (
6+
rootPath = "OS-ROLE"
7+
rolePath = "roles"
8+
)
9+
10+
func rootURL(client *golangsdk.ServiceClient) string {
11+
return client.ServiceURL(rootPath, rolePath)
12+
}
13+
14+
func resourceURL(client *golangsdk.ServiceClient, roleID string) string {
15+
return client.ServiceURL(rootPath, rolePath, roleID)
16+
}

0 commit comments

Comments
 (0)