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