Skip to content

Commit bef463f

Browse files
reenjiiLudovic Lamarche
authored and
Ludovic Lamarche
committed
feat(identity): add create, update and delete endpoint group
1 parent a4a7ffc commit bef463f

File tree

3 files changed

+127
-6
lines changed

3 files changed

+127
-6
lines changed

openstack/identity/v3/extensions/endpointgroups/requests.go

+91
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,94 @@ func DeleteProjectAssociation(client *gophercloud.ServiceClient, id string, proj
7676
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
7777
return
7878
}
79+
80+
// CreateOptsBuilder allows extensions to add additional parameters to
81+
// the Create request.
82+
type CreateOptsBuilder interface {
83+
ToGroupCreateMap() (map[string]interface{}, error)
84+
}
85+
86+
// CreateOpts provides options used to create a group.
87+
type CreateOpts struct {
88+
// Name is the name of the new endpoint group.
89+
Name string `json:"name" required:"true"`
90+
91+
// Description is a description of the endpoint group.
92+
Description *string `json:"description,omitempty"`
93+
94+
// Filters are the filters of the endpoint group.
95+
Filters map[string]interface{} `json:"filters,omitempty"`
96+
}
97+
98+
// ToGroupCreateMap formats a CreateOpts into a create request.
99+
func (opts CreateOpts) ToGroupCreateMap() (map[string]interface{}, error) {
100+
b, err := gophercloud.BuildRequestBody(opts, "endpoint_group")
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
return b, nil
106+
}
107+
108+
// Create creates a new endpoint group.
109+
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
110+
b, err := opts.ToGroupCreateMap()
111+
if err != nil {
112+
r.Err = err
113+
return
114+
}
115+
resp, err := client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
116+
OkCodes: []int{201},
117+
})
118+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
119+
return
120+
}
121+
122+
// UpdateOptsBuilder allows extensions to add additional parameters to
123+
// the Update request.
124+
type UpdateOptsBuilder interface {
125+
ToGroupUpdateMap() (map[string]interface{}, error)
126+
}
127+
128+
// UpdateOpts provides options for updating an endpoint group.
129+
type UpdateOpts struct {
130+
// Name is the name of the endpoint group.
131+
Name string `json:"name,omitempty"`
132+
133+
// Description is a description of the endpoint group.
134+
Description *string `json:"description,omitempty"`
135+
136+
// Filters are the filters of the endpoint group.
137+
Filters map[string]interface{} `json:"filters,omitempty"`
138+
}
139+
140+
// ToGroupUpdateMap formats a UpdateOpts into an update request.
141+
func (opts UpdateOpts) ToGroupUpdateMap() (map[string]interface{}, error) {
142+
b, err := gophercloud.BuildRequestBody(opts, "endpoint_group")
143+
if err != nil {
144+
return nil, err
145+
}
146+
147+
return b, nil
148+
}
149+
150+
// Update updates an existing Endpoint Group.
151+
func Update(client *gophercloud.ServiceClient, groupID string, opts UpdateOptsBuilder) (r UpdateResult) {
152+
b, err := opts.ToGroupUpdateMap()
153+
if err != nil {
154+
r.Err = err
155+
return
156+
}
157+
resp, err := client.Patch(updateURL(client, groupID), &b, &r.Body, &gophercloud.RequestOpts{
158+
OkCodes: []int{200},
159+
})
160+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
161+
return
162+
}
163+
164+
// Delete deletes an endpoint group.
165+
func Delete(client *gophercloud.ServiceClient, groupID string) (r DeleteResult) {
166+
resp, err := client.Delete(deleteURL(client, groupID), nil)
167+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
168+
return
169+
}

openstack/identity/v3/extensions/endpointgroups/results.go

+18
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,21 @@ type CheckProjectAssociationResult struct {
7373
type DeleteProjectAssociationResult struct {
7474
gophercloud.ErrResult
7575
}
76+
77+
// CreateResult is the response from a Create operation. Call its Extract method
78+
// to interpret it as an Endpoint Group.
79+
type CreateResult struct {
80+
commonResult
81+
}
82+
83+
// UpdateResult is the response from an Update operation. Call its Extract
84+
// method to interpret it as an Endpoint Group.
85+
type UpdateResult struct {
86+
commonResult
87+
}
88+
89+
// DeleteResult is the response from a Delete operation. Call its ExtractErr to
90+
// determine if the request succeeded or failed.
91+
type DeleteResult struct {
92+
gophercloud.ErrResult
93+
}

openstack/identity/v3/extensions/endpointgroups/urls.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,26 @@ func rootURL(client *gophercloud.ServiceClient) string {
1111
return client.ServiceURL(endpointGroupPath)
1212
}
1313

14-
func resourceURL(client *gophercloud.ServiceClient, endointGroupID string) string {
15-
return client.ServiceURL(endpointGroupPath, endointGroupID)
14+
func resourceURL(client *gophercloud.ServiceClient, endpointGroupID string) string {
15+
return client.ServiceURL(endpointGroupPath, endpointGroupID)
1616
}
1717

18-
func projectAssociationURL(client *gophercloud.ServiceClient, endpointGroupId string, projectId string) string {
19-
return client.ServiceURL(endpointGroupPath, endpointGroupId, "projects", projectId)
18+
func projectAssociationURL(client *gophercloud.ServiceClient, endpointGroupID string, projectID string) string {
19+
return client.ServiceURL(endpointGroupPath, endpointGroupID, "projects", projectID)
2020
}
2121

22-
func listEndpointGroupsAssociationURL(client *gophercloud.ServiceClient, projectId string) string {
23-
return client.ServiceURL(endpointGroupsAssociationPath, projectId, "endpoint_groups")
22+
func listEndpointGroupsAssociationURL(client *gophercloud.ServiceClient, projectID string) string {
23+
return client.ServiceURL(endpointGroupsAssociationPath, projectID, "endpoint_groups")
24+
}
25+
26+
func createURL(client *gophercloud.ServiceClient) string {
27+
return client.ServiceURL(endpointGroupPath)
28+
}
29+
30+
func updateURL(client *gophercloud.ServiceClient, endpointGroupID string) string {
31+
return client.ServiceURL(endpointGroupPath, endpointGroupID)
32+
}
33+
34+
func deleteURL(client *gophercloud.ServiceClient, endpointGroupID string) string {
35+
return client.ServiceURL(endpointGroupPath, endpointGroupID)
2436
}

0 commit comments

Comments
 (0)