github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3/groups/requests.go (about) 1 package groups 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/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 // Get retrieves details on a single group, by ID. 45 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 46 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 47 return 48 } 49 50 // CreateOptsBuilder allows extensions to add additional parameters to 51 // the Create request. 52 type CreateOptsBuilder interface { 53 ToGroupCreateMap() (map[string]interface{}, error) 54 } 55 56 // CreateOpts provides options used to create a group. 57 type CreateOpts struct { 58 // Name is the name of the new group. 59 Name string `json:"name" required:"true"` 60 61 // Description is a description of the group. 62 Description string `json:"description,omitempty"` 63 64 // DomainID is the ID of the domain the group belongs to. 65 DomainID string `json:"domain_id,omitempty"` 66 67 Extra map[string]interface{} `json:"-"` 68 } 69 70 // ToGroupCreateMap formats a CreateOpts into a create request. 71 func (opts CreateOpts) ToGroupCreateMap() (map[string]interface{}, error) { 72 b, err := golangsdk.BuildRequestBody(opts, "group") 73 if err != nil { 74 return nil, err 75 } 76 77 if opts.Extra != nil { 78 if v, ok := b["group"].(map[string]interface{}); ok { 79 for key, value := range opts.Extra { 80 v[key] = value 81 } 82 } 83 } 84 85 return b, nil 86 } 87 88 // Create creates a new Group. 89 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 90 b, err := opts.ToGroupCreateMap() 91 if err != nil { 92 r.Err = err 93 return 94 } 95 _, r.Err = client.Post(createURL(client), &b, &r.Body, &golangsdk.RequestOpts{}) 96 return 97 } 98 99 // UpdateOptsBuilder allows extensions to add additional parameters to 100 // the Update request. 101 type UpdateOptsBuilder interface { 102 ToGroupUpdateMap() (map[string]interface{}, error) 103 } 104 105 // UpdateOpts provides options for updating a group. 106 type UpdateOpts struct { 107 // Name is the name of the new group. 108 Name string `json:"name,omitempty"` 109 110 // Description is a description of the group. 111 Description string `json:"description,omitempty"` 112 113 // DomainID is the ID of the domain the group belongs to. 114 DomainID string `json:"domain_id,omitempty"` 115 116 // Extra is free-form extra key/value pairs to describe the group. 117 Extra map[string]interface{} `json:"-"` 118 } 119 120 // ToGroupUpdateMap formats a UpdateOpts into an update request. 121 func (opts UpdateOpts) ToGroupUpdateMap() (map[string]interface{}, error) { 122 b, err := golangsdk.BuildRequestBody(opts, "group") 123 if err != nil { 124 return nil, err 125 } 126 127 if opts.Extra != nil { 128 if v, ok := b["group"].(map[string]interface{}); ok { 129 for key, value := range opts.Extra { 130 v[key] = value 131 } 132 } 133 } 134 135 return b, nil 136 } 137 138 // Update updates an existing Group. 139 func Update(client *golangsdk.ServiceClient, groupID string, opts UpdateOptsBuilder) (r UpdateResult) { 140 b, err := opts.ToGroupUpdateMap() 141 if err != nil { 142 r.Err = err 143 return 144 } 145 _, r.Err = client.Patch(updateURL(client, groupID), &b, &r.Body, &golangsdk.RequestOpts{ 146 OkCodes: []int{200}, 147 }) 148 return 149 } 150 151 // Delete deletes a group. 152 func Delete(client *golangsdk.ServiceClient, groupID string) (r DeleteResult) { 153 _, r.Err = client.Delete(deleteURL(client, groupID), nil) 154 return 155 }