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