github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/identity/v3/groups/requests.go (about) 1 package groups 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to 9 // the List request 10 type ListOptsBuilder interface { 11 ToGroupListQuery() (string, error) 12 } 13 14 // ListOpts provides options to filter the List results. 15 type ListOpts struct { 16 // DomainID filters the response by a domain ID. 17 DomainID string `q:"domain_id"` 18 19 // Name filters the response by group name. 20 Name string `q:"name"` 21 } 22 23 // ToGroupListQuery formats a ListOpts into a query string. 24 func (opts ListOpts) ToGroupListQuery() (string, error) { 25 q, err := golangsdk.BuildQueryString(opts) 26 return q.String(), err 27 } 28 29 // List enumerates the Groups to which the current token has access. 30 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 31 url := listURL(client) 32 if opts != nil { 33 query, err := opts.ToGroupListQuery() 34 if err != nil { 35 return pagination.Pager{Err: err} 36 } 37 url += query 38 } 39 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 40 return GroupPage{pagination.LinkedPageBase{PageResult: r}} 41 }) 42 } 43 44 // List users of the group 45 func ListUsers(client *golangsdk.ServiceClient, groupID string) (r UserResult) { 46 _, r.Err = client.Get(listUsersURL(client, groupID), &r.Body, nil) 47 return 48 } 49 50 // Get retrieves details on a single group, by ID. 51 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 52 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 53 return 54 } 55 56 // CreateOptsBuilder allows extensions to add additional parameters to 57 // the Create request. 58 type CreateOptsBuilder interface { 59 ToGroupCreateMap() (map[string]interface{}, error) 60 } 61 62 // CreateOpts provides options used to create a group. 63 type CreateOpts struct { 64 // Name is the name of the new group. 65 Name string `json:"name" required:"true"` 66 67 // Description is a description of the group. 68 Description string `json:"description,omitempty"` 69 70 // DomainID is the ID of the domain the group belongs to. 71 DomainID string `json:"domain_id,omitempty"` 72 73 Extra map[string]interface{} `json:"-"` 74 } 75 76 // ToGroupCreateMap formats a CreateOpts into a create request. 77 func (opts CreateOpts) ToGroupCreateMap() (map[string]interface{}, error) { 78 b, err := golangsdk.BuildRequestBody(opts, "group") 79 if err != nil { 80 return nil, err 81 } 82 83 if opts.Extra != nil { 84 if v, ok := b["group"].(map[string]interface{}); ok { 85 for key, value := range opts.Extra { 86 v[key] = value 87 } 88 } 89 } 90 91 return b, nil 92 } 93 94 // Create creates a new Group. 95 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 96 b, err := opts.ToGroupCreateMap() 97 if err != nil { 98 r.Err = err 99 return 100 } 101 _, r.Err = client.Post(createURL(client), &b, &r.Body, &golangsdk.RequestOpts{}) 102 return 103 } 104 105 // UpdateOptsBuilder allows extensions to add additional parameters to 106 // the Update request. 107 type UpdateOptsBuilder interface { 108 ToGroupUpdateMap() (map[string]interface{}, error) 109 } 110 111 // UpdateOpts provides options for updating a group. 112 type UpdateOpts struct { 113 // Name is the name of the new group. 114 Name string `json:"name,omitempty"` 115 116 // Description is a description of the group. 117 Description string `json:"description,omitempty"` 118 119 // DomainID is the ID of the domain the group belongs to. 120 DomainID string `json:"domain_id,omitempty"` 121 122 // Extra is free-form extra key/value pairs to describe the group. 123 Extra map[string]interface{} `json:"-"` 124 } 125 126 // ToGroupUpdateMap formats a UpdateOpts into an update request. 127 func (opts UpdateOpts) ToGroupUpdateMap() (map[string]interface{}, error) { 128 b, err := golangsdk.BuildRequestBody(opts, "group") 129 if err != nil { 130 return nil, err 131 } 132 133 if opts.Extra != nil { 134 if v, ok := b["group"].(map[string]interface{}); ok { 135 for key, value := range opts.Extra { 136 v[key] = value 137 } 138 } 139 } 140 141 return b, nil 142 } 143 144 // Update updates an existing Group. 145 func Update(client *golangsdk.ServiceClient, groupID string, opts UpdateOptsBuilder) (r UpdateResult) { 146 b, err := opts.ToGroupUpdateMap() 147 if err != nil { 148 r.Err = err 149 return 150 } 151 _, r.Err = client.Patch(updateURL(client, groupID), &b, &r.Body, &golangsdk.RequestOpts{ 152 OkCodes: []int{200}, 153 }) 154 return 155 } 156 157 // Delete deletes a group. 158 func Delete(client *golangsdk.ServiceClient, groupID string) (r DeleteResult) { 159 _, r.Err = client.Delete(deleteURL(client, groupID), nil) 160 return 161 }