Skip to content

Commit 514c0e8

Browse files
gcnathanLudovic Lamarche
authored andcommitted
endpoint groups feature: add list, get & association
1 parent b31f60b commit 514c0e8

File tree

6 files changed

+494
-0
lines changed

6 files changed

+494
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//go:build acceptance || identity || endpointgroups
2+
// +build acceptance identity endpointgroups
3+
4+
package v3
5+
6+
import (
7+
"testing"
8+
9+
"github.com/gophercloud/gophercloud/acceptance/clients"
10+
"github.com/gophercloud/gophercloud/openstack/identity/v3/extensions/endpointgroups"
11+
th "github.com/gophercloud/gophercloud/testhelper"
12+
)
13+
14+
func TestEndpointGroupsList(t *testing.T) {
15+
clients.SkipRelease(t, "stable/mitaka")
16+
clients.SkipRelease(t, "stable/newton")
17+
clients.SkipRelease(t, "stable/ocata")
18+
clients.SkipRelease(t, "stable/pike")
19+
clients.SkipRelease(t, "stable/queens")
20+
clients.RequireAdmin(t)
21+
22+
client, err := clients.NewIdentityV3Client()
23+
th.AssertNoErr(t, err)
24+
25+
allPages, err := endpointgroups.List(client, nil).AllPages()
26+
th.AssertNoErr(t, err)
27+
28+
allEndpointGroups, err := endpointgroups.ExtractEndpointGroups(allPages)
29+
th.AssertNoErr(t, err)
30+
th.AssertEquals(t, len(allEndpointGroups), 0)
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Package endpointgroups enables management of OpenStack Identity Endpoint Groups
3+
and Endpoint associations.
4+
5+
Example to Get an Endpoint Group
6+
7+
err := endpointgroups.Get(identityClient, endpointGroupID).ExtractErr()
8+
if err != nil {
9+
panic(err)
10+
}
11+
12+
Example to List all Endpoint Groups by name
13+
14+
listOpts := endpointgropus.ListOpts{
15+
Name: "mygroup",
16+
}
17+
18+
allPages, err := endpointgroups.List(identityClient, listOpts).AllPages()
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
allGroups, err := endpointgroups.ExtractEndpointGroups(allPages)
24+
if err != nil {
25+
panic(err)
26+
}
27+
28+
for _, endpointgroup := range allGroups {
29+
fmt.Printf("%+v\n", endpointgroup)
30+
}
31+
*/
32+
package endpointgroups
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package endpointgroups
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gophercloud/gophercloud"
7+
"github.com/gophercloud/gophercloud/pagination"
8+
)
9+
10+
// Get retrieves details on a single endpoint group, by ID.
11+
func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
12+
_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
13+
return
14+
}
15+
16+
// ListOptsBuilder allows extensions to add additional parameters to
17+
// the List request
18+
type ListOptsBuilder interface {
19+
ToEndpointGroupListQuery() (string, error)
20+
}
21+
22+
// ListOpts provides options to filter the List results.
23+
type ListOpts struct {
24+
// Name filters the response by endpoint group name.
25+
Name string `q:"name"`
26+
}
27+
28+
// ToEndpointGroupListQuery formats a ListOpts into a query string.
29+
func (opts ListOpts) ToEndpointGroupListQuery() (string, error) {
30+
q, err := gophercloud.BuildQueryString(opts)
31+
return q.String(), err
32+
}
33+
34+
// List enumerates the endpoint groups.
35+
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
36+
url := rootURL(client)
37+
if opts != nil {
38+
query, err := opts.ToEndpointGroupListQuery()
39+
if err != nil {
40+
return pagination.Pager{Err: err}
41+
}
42+
url += query
43+
}
44+
45+
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
46+
return EndpointGroupPage{pagination.LinkedPageBase{PageResult: r}}
47+
})
48+
}
49+
50+
// ListForProjects enumerates the endpoint groups associated to a project.
51+
func ListForProjects(client *gophercloud.ServiceClient, projectId string) pagination.Pager {
52+
return pagination.NewPager(client, listEndpointGroupsAssociationURL(client, projectId), func(r pagination.PageResult) pagination.Page {
53+
return EndpointGroupPage{pagination.LinkedPageBase{PageResult: r}}
54+
})
55+
}
56+
57+
// CreateProjectAssociation creates an endpoint group to a project association.
58+
func CreateProjectAssociation(client *gophercloud.ServiceClient, id string, projectId string) (r CreateProjectAssociationResult) {
59+
resp, err := client.Put(projectAssociationURL(client, id, projectId), nil, nil, &gophercloud.RequestOpts{
60+
OkCodes: []int{http.StatusNoContent},
61+
})
62+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
63+
return
64+
}
65+
66+
// CheckProjectAssociation checks if an endpoint group is associated to a project.
67+
func CheckProjectAssociation(client *gophercloud.ServiceClient, id string, projectId string) (r CheckProjectAssociationResult) {
68+
resp, err := client.Head(projectAssociationURL(client, id, projectId), nil)
69+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
70+
return
71+
}
72+
73+
// DeleteProjectAssociation deletes an endpoint group to a project association.
74+
func DeleteProjectAssociation(client *gophercloud.ServiceClient, id string, projectId string) (r DeleteProjectAssociationResult) {
75+
resp, err := client.Delete(projectAssociationURL(client, id, projectId), nil)
76+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
77+
return
78+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package endpointgroups
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
"github.com/gophercloud/gophercloud/pagination"
6+
)
7+
8+
type commonResult struct {
9+
gophercloud.Result
10+
}
11+
12+
// Extract interprets a GetResult, CreateResult or UpdateResult as a concrete
13+
// EndpointGroup. An error is returned if the original call or the extraction
14+
// failed.
15+
func (r commonResult) Extract() (*EndpointGroup, error) {
16+
var s struct {
17+
EndpointGroup *EndpointGroup `json:"endpoint_group"`
18+
}
19+
err := r.ExtractInto(&s)
20+
return s.EndpointGroup, err
21+
}
22+
23+
// GetResult is the response from a Get operation. Call its Extract method
24+
// to interpret it as an EndpointGroup.
25+
type GetResult struct {
26+
commonResult
27+
}
28+
29+
// EndpointGroup represents a group of endpoints matching one or several filter
30+
// criteria.
31+
type EndpointGroup struct {
32+
// ID is the unique ID of the endpoint group.
33+
ID string `json:"id"`
34+
35+
// Name is the name of the new endpoint group.
36+
Name string `json:"name"`
37+
38+
// Filters is a map type describing the filter criteria
39+
Filters map[string]interface{} `json:"filters"`
40+
41+
// Description is the description of the endpoint group
42+
Description string `json:"description"`
43+
}
44+
45+
// EndpointGroupPage is a single page of EndpointGroup results.
46+
type EndpointGroupPage struct {
47+
pagination.LinkedPageBase
48+
}
49+
50+
// IsEmpty returns true if no EndpointGroups were returned.
51+
func (r EndpointGroupPage) IsEmpty() (bool, error) {
52+
es, err := ExtractEndpointGroups(r)
53+
return len(es) == 0, err
54+
}
55+
56+
// ExtractEndpointGroups extracts an EndpointGroup slice from a Page.
57+
func ExtractEndpointGroups(r pagination.Page) ([]EndpointGroup, error) {
58+
var s struct {
59+
EndpointGroups []EndpointGroup `json:"endpoint_groups"`
60+
}
61+
err := (r.(EndpointGroupPage)).ExtractInto(&s)
62+
return s.EndpointGroups, err
63+
}
64+
65+
type CreateProjectAssociationResult struct {
66+
gophercloud.ErrResult
67+
}
68+
69+
type CheckProjectAssociationResult struct {
70+
gophercloud.ErrResult
71+
}
72+
73+
type DeleteProjectAssociationResult struct {
74+
gophercloud.ErrResult
75+
}

0 commit comments

Comments
 (0)