github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/iam/iamv1/user_policies.go (about) 1 package iamv1 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/url" 7 8 "github.com/IBM-Cloud/bluemix-go/client" 9 "github.com/IBM-Cloud/bluemix-go/models" 10 "github.com/IBM-Cloud/bluemix-go/rest" 11 ) 12 13 const ( 14 _UserPoliciesPathTemplate = "/acms/v1/scopes/%s/users/%s/policies" 15 _UserPolicyPathTemplate = "/acms/v1/scopes/%s/users/%s/policies/%s" 16 ) 17 18 //go:generate counterfeiter . UserPolicyRepository 19 type UserPolicyRepository interface { 20 List(scope string, ibmUniqueID string) ([]models.Policy, error) 21 Get(scope string, ibmUniqueID string, policyID string) (models.Policy, error) 22 Create(scope string, ibmUniqueID string, policy models.Policy) (models.Policy, error) 23 Update(scope string, ibmUniqueID string, policyID string, policy models.Policy, version string) (models.Policy, error) 24 Delete(scope string, ibmUniqueID string, policyID string) error 25 } 26 27 type userPolicyRepository struct { 28 client *client.Client 29 } 30 31 func NewUserPolicyRepository(c *client.Client) UserPolicyRepository { 32 return &userPolicyRepository{ 33 client: c, 34 } 35 } 36 37 type PoliciesQueryResult struct { 38 Policies []models.Policy `json:"policies"` 39 } 40 41 func (r *userPolicyRepository) List(scope string, ibmUniqueID string) ([]models.Policy, error) { 42 result := PoliciesQueryResult{} 43 resp, err := r.client.Get(r.generateURLPath(_UserPoliciesPathTemplate, scope, ibmUniqueID), &result) 44 45 if resp.StatusCode == http.StatusNotFound { 46 return []models.Policy{}, nil 47 } 48 49 if err != nil { 50 return nil, err 51 } 52 53 return result.Policies, nil 54 } 55 56 func (r *userPolicyRepository) Get(scope string, ibmUniqueID string, policyID string) (models.Policy, error) { 57 policy := models.Policy{} 58 resp, err := r.client.Get(r.generateURLPath(_UserPolicyPathTemplate, scope, ibmUniqueID, policyID), &policy) 59 if err != nil { 60 return models.Policy{}, err 61 } 62 policy.Version = resp.Header.Get("Etag") 63 return policy, nil 64 } 65 66 func (r *userPolicyRepository) Create(scope string, ibmUniqueID string, policy models.Policy) (models.Policy, error) { 67 policyCreated := models.Policy{} 68 resp, err := r.client.Post(r.generateURLPath(_UserPoliciesPathTemplate, scope, ibmUniqueID), &policy, &policyCreated) 69 if err != nil { 70 return models.Policy{}, err 71 } 72 policyCreated.Version = resp.Header.Get("Etag") 73 return policyCreated, nil 74 } 75 76 func (r *userPolicyRepository) Update(scope string, ibmUniqueID string, policyID string, policy models.Policy, version string) (models.Policy, error) { 77 policyUpdated := models.Policy{} 78 79 request := rest.PutRequest(*r.client.Config.Endpoint + r.generateURLPath(_UserPolicyPathTemplate, scope, ibmUniqueID, policyID)) 80 request = request.Set("If-Match", version).Body(&policy) 81 82 resp, err := r.client.SendRequest(request, &policyUpdated) 83 if err != nil { 84 return models.Policy{}, err 85 } 86 policyUpdated.Version = resp.Header.Get("Etag") 87 return policyUpdated, nil 88 } 89 90 func (r *userPolicyRepository) Delete(scope string, ibmUniqueID string, policyID string) error { 91 _, err := r.client.Delete(r.generateURLPath(_UserPolicyPathTemplate, scope, ibmUniqueID, policyID)) 92 return err 93 } 94 95 func (r *userPolicyRepository) generateURLPath(template string, parameters ...string) string { 96 escaped := []interface{}{} 97 for _, parameter := range parameters { 98 escaped = append(escaped, url.QueryEscape(parameter)) 99 } 100 return fmt.Sprintf(template, escaped...) 101 }