github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/iamuum/iamuumv2/access_group.go (about) 1 package iamuumv2 2 3 import ( 4 "fmt" 5 "net/url" 6 7 "github.com/IBM-Cloud/bluemix-go/client" 8 "github.com/IBM-Cloud/bluemix-go/helpers" 9 "github.com/IBM-Cloud/bluemix-go/models" 10 "github.com/IBM-Cloud/bluemix-go/rest" 11 ) 12 13 type Groups struct { 14 PaginationFields 15 Groups []models.AccessGroupV2 `json:"groups"` 16 } 17 18 func (g *Groups) Resources() []interface{} { 19 r := make([]interface{}, len(g.Groups)) 20 for i := range g.Groups { 21 r[i] = g.Groups[i] 22 } 23 return r 24 } 25 26 type AccessGroupRepository interface { 27 List(accountID string, queryParams ...string) ([]models.AccessGroupV2, error) 28 Create(group models.AccessGroupV2, accountID string) (*models.AccessGroupV2, error) 29 FindByName(name string, accountID string) ([]models.AccessGroupV2, error) 30 Delete(accessGroupID string, recursive bool) error 31 Update(accessGroupID string, group AccessGroupUpdateRequest, revision string) (models.AccessGroupV2, error) 32 Get(accessGroupID string) (group *models.AccessGroupV2, revision string, err error) 33 } 34 35 type accessGroupRepository struct { 36 client *client.Client 37 } 38 39 type AccessGroupUpdateRequest struct { 40 Name string `json:"name,omitempty"` 41 Description string `json:"description,omitempty"` 42 } 43 44 func NewAccessGroupRepository(c *client.Client) AccessGroupRepository { 45 return &accessGroupRepository{ 46 client: c, 47 } 48 } 49 50 func (r *accessGroupRepository) List(accountID string, queryParams ...string) ([]models.AccessGroupV2, error) { 51 var groups []models.AccessGroupV2 52 var err error 53 if len(queryParams) != 0 { 54 _, err = r.client.GetPaginated(fmt.Sprintf("/v2/groups?account_id=%s&iam_id=%s", url.QueryEscape(accountID), queryParams[0]), NewPaginatedResourcesHandler(&Groups{}), func(v interface{}) bool { 55 groups = append(groups, v.(models.AccessGroupV2)) 56 return true 57 }) 58 } else { 59 _, err = r.client.GetPaginated(fmt.Sprintf("/v2/groups?account_id=%s", url.QueryEscape(accountID)), NewPaginatedResourcesHandler(&Groups{}), func(v interface{}) bool { 60 groups = append(groups, v.(models.AccessGroupV2)) 61 return true 62 }) 63 } 64 if err != nil { 65 return []models.AccessGroupV2{}, err 66 } 67 return groups, err 68 } 69 70 func (r *accessGroupRepository) Create(accessGroup models.AccessGroupV2, accountID string) (*models.AccessGroupV2, error) { 71 req := rest.PostRequest(helpers.GetFullURL(*r.client.Config.Endpoint, "/v2/groups")).Query("account_id", accountID).Body(accessGroup) 72 73 newAccessGroup := models.AccessGroupV2{} 74 _, err := r.client.SendRequest(req, &newAccessGroup) 75 if err != nil { 76 return nil, err 77 } 78 return &newAccessGroup, nil 79 } 80 81 func (r *accessGroupRepository) FindByName(name string, accountID string) ([]models.AccessGroupV2, error) { 82 var groups []models.AccessGroupV2 83 _, err := r.client.GetPaginated(fmt.Sprintf("/v2/groups?account=%s", url.QueryEscape(accountID)), NewPaginatedResourcesHandler(&Groups{}), func(v interface{}) bool { 84 if v.(models.AccessGroupV2).AccessGroup.Name == name { 85 groups = append(groups, v.(models.AccessGroupV2)) 86 } 87 return true 88 }) 89 if err != nil { 90 return []models.AccessGroupV2{}, err 91 } 92 return groups, err 93 } 94 95 func (r *accessGroupRepository) Delete(accessGroupID string, recursive bool) error { 96 req := rest.DeleteRequest((helpers.GetFullURL(*r.client.Config.Endpoint, "/v2/groups/"+accessGroupID))) 97 98 if recursive { 99 req = req.Query("force", "true") 100 } 101 _, err := r.client.SendRequest(req, nil) 102 return err 103 } 104 105 func (r *accessGroupRepository) Update(accessGroupID string, group AccessGroupUpdateRequest, revision string) (models.AccessGroupV2, error) { 106 req := rest.PatchRequest((helpers.GetFullURL(*r.client.Config.Endpoint, "/v2/groups/"+accessGroupID))).Body(group).Add("If-Match", revision) 107 resp := models.AccessGroupV2{} 108 _, err := r.client.SendRequest(req, &resp) 109 if err != nil { 110 return resp, err 111 } 112 return resp, nil 113 } 114 115 func (r *accessGroupRepository) Get(accessGroupID string) (*models.AccessGroupV2, string, error) { 116 group := models.AccessGroupV2{} 117 response, err := r.client.Get("/v2/groups/"+url.PathEscape(accessGroupID), &group) 118 if err != nil { 119 return &group, "", err 120 } 121 return &group, response.Header.Get("Etag"), nil 122 }