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