Skip to content

Commit ef79b92

Browse files
chuliangpierreprinettistephenfin
authored
identity: Add Get endpoint by ID
Co-Authored-By: chuliang <contact@julien-tanguy.com> Co-Authored-By: Pierre Prinetti <pierreprinetti@redhat.com> Co-Authored-By: Stephen Finucane <stephenfin@redhat.com>
1 parent 2e6154e commit ef79b92

File tree

5 files changed

+137
-70
lines changed

5 files changed

+137
-70
lines changed

internal/acceptance/openstack/identity/v3/endpoint_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ func TestEndpointsList(t *testing.T) {
3939
th.AssertEquals(t, found, true)
4040
}
4141

42+
func TestEndpointsGet(t *testing.T) {
43+
clients.RequireAdmin(t)
44+
45+
client, err := clients.NewIdentityV3Client()
46+
th.AssertNoErr(t, err)
47+
48+
allPages, err := endpoints.List(client, nil).AllPages(context.TODO())
49+
th.AssertNoErr(t, err)
50+
51+
allEndpoints, err := endpoints.ExtractEndpoints(allPages)
52+
th.AssertNoErr(t, err)
53+
54+
endpoint := allEndpoints[0]
55+
e, err := endpoints.Get(context.TODO(), client, endpoint.ID).Extract()
56+
if err != nil {
57+
t.Fatalf("Unable to get endpoint: %v", err)
58+
}
59+
60+
tools.PrintResource(t, e)
61+
62+
th.AssertEquals(t, e.Name, e.Name)
63+
}
64+
4265
func TestEndpointsNavigateCatalog(t *testing.T) {
4366
clients.RequireAdmin(t)
4467

openstack/identity/v3/endpoints/requests.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pa
9090
})
9191
}
9292

93+
// Get retrieves details on a single endpoint, by ID.
94+
func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
95+
resp, err := client.Get(ctx, endpointURL(client, id), &r.Body, nil)
96+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
97+
return
98+
}
99+
93100
// UpdateOptsBuilder allows extensions to add parameters to the Update request.
94101
type UpdateOptsBuilder interface {
95102
ToEndpointUpdateMap() (map[string]any, error)

openstack/identity/v3/endpoints/results.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ func (r commonResult) Extract() (*Endpoint, error) {
1919
return s.Endpoint, err
2020
}
2121

22+
// GetResult is the response from a Get operation. Call its Extract method
23+
// to interpret it as an Endpoint.
24+
type GetResult struct {
25+
commonResult
26+
}
27+
2228
// CreateResult is the response from a Create operation. Call its Extract
2329
// method to interpret it as an Endpoint.
2430
type CreateResult struct {

openstack/identity/v3/endpoints/testing/requests_test.go

Lines changed: 100 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,31 @@ func TestCreateSuccessful(t *testing.T) {
2020
th.Mux.HandleFunc("/endpoints", func(w http.ResponseWriter, r *http.Request) {
2121
th.TestMethod(t, r, "POST")
2222
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
23-
th.TestJSONRequest(t, r, `
24-
{
25-
"endpoint": {
26-
"interface": "public",
27-
"name": "the-endiest-of-points",
28-
"region": "underground",
29-
"url": "https://1.2.3.4:9000/",
30-
"service_id": "asdfasdfasdfasdf"
31-
}
32-
}
33-
`)
23+
th.TestJSONRequest(t, r, `{
24+
"endpoint": {
25+
"interface": "public",
26+
"name": "the-endiest-of-points",
27+
"region": "underground",
28+
"url": "https://1.2.3.4:9000/",
29+
"service_id": "asdfasdfasdfasdf"
30+
}
31+
}`)
3432

3533
w.WriteHeader(http.StatusCreated)
36-
fmt.Fprint(w, `
37-
{
38-
"endpoint": {
39-
"id": "12",
40-
"interface": "public",
41-
"enabled": true,
42-
"links": {
43-
"self": "https://localhost:5000/v3/endpoints/12"
44-
},
45-
"name": "the-endiest-of-points",
46-
"region": "underground",
47-
"service_id": "asdfasdfasdfasdf",
48-
"url": "https://1.2.3.4:9000/"
49-
}
50-
}
51-
`)
34+
fmt.Fprint(w, `{
35+
"endpoint": {
36+
"id": "12",
37+
"interface": "public",
38+
"enabled": true,
39+
"links": {
40+
"self": "https://localhost:5000/v3/endpoints/12"
41+
},
42+
"name": "the-endiest-of-points",
43+
"region": "underground",
44+
"service_id": "asdfasdfasdfasdf",
45+
"url": "https://1.2.3.4:9000/"
46+
}
47+
}`)
5248
})
5349

5450
actual, err := endpoints.Create(context.TODO(), client.ServiceClient(), endpoints.CreateOpts{
@@ -82,40 +78,38 @@ func TestListEndpoints(t *testing.T) {
8278
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
8379

8480
w.Header().Add("Content-Type", "application/json")
85-
fmt.Fprint(w, `
86-
{
87-
"endpoints": [
88-
{
89-
"id": "12",
90-
"interface": "public",
91-
"enabled": true,
92-
"links": {
93-
"self": "https://localhost:5000/v3/endpoints/12"
94-
},
95-
"name": "the-endiest-of-points",
96-
"region": "underground",
97-
"service_id": "asdfasdfasdfasdf",
98-
"url": "https://1.2.3.4:9000/"
81+
fmt.Fprint(w, `{
82+
"endpoints": [
83+
{
84+
"id": "12",
85+
"interface": "public",
86+
"enabled": true,
87+
"links": {
88+
"self": "https://localhost:5000/v3/endpoints/12"
9989
},
100-
{
101-
"id": "13",
102-
"interface": "internal",
103-
"enabled": false,
104-
"links": {
105-
"self": "https://localhost:5000/v3/endpoints/13"
106-
},
107-
"name": "shhhh",
108-
"region": "underground",
109-
"service_id": "asdfasdfasdfasdf",
110-
"url": "https://1.2.3.4:9001/"
111-
}
112-
],
113-
"links": {
114-
"next": null,
115-
"previous": null
90+
"name": "the-endiest-of-points",
91+
"region": "underground",
92+
"service_id": "asdfasdfasdfasdf",
93+
"url": "https://1.2.3.4:9000/"
94+
},
95+
{
96+
"id": "13",
97+
"interface": "internal",
98+
"enabled": false,
99+
"links": {
100+
"self": "https://localhost:5000/v3/endpoints/13"
101+
},
102+
"name": "shhhh",
103+
"region": "underground",
104+
"service_id": "asdfasdfasdfasdf",
105+
"url": "https://1.2.3.4:9001/"
116106
}
107+
],
108+
"links": {
109+
"next": null,
110+
"previous": null
117111
}
118-
`)
112+
}`)
119113
})
120114

121115
count := 0
@@ -154,24 +148,62 @@ func TestListEndpoints(t *testing.T) {
154148
th.AssertEquals(t, 1, count)
155149
}
156150

151+
func TestGetEndpoint(t *testing.T) {
152+
th.SetupHTTP()
153+
defer th.TeardownHTTP()
154+
155+
th.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) {
156+
th.TestMethod(t, r, "GET")
157+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
158+
159+
fmt.Fprint(w, `{
160+
"endpoint": {
161+
"id": "12",
162+
"interface": "public",
163+
"enabled": true,
164+
"links": {
165+
"self": "https://localhost:5000/v3/endpoints/12"
166+
},
167+
"name": "the-endiest-of-points",
168+
"region": "underground",
169+
"service_id": "asdfasdfasdfasdf",
170+
"url": "https://1.2.3.4:9000/"
171+
}
172+
}`)
173+
})
174+
175+
actual, err := endpoints.Get(context.TODO(), client.ServiceClient(), "12").Extract()
176+
if err != nil {
177+
t.Fatalf("Unexpected error from Get: %v", err)
178+
}
179+
180+
expected := &endpoints.Endpoint{
181+
ID: "12",
182+
Availability: gophercloud.AvailabilityPublic,
183+
Enabled: true,
184+
Name: "the-endiest-of-points",
185+
Region: "underground",
186+
ServiceID: "asdfasdfasdfasdf",
187+
URL: "https://1.2.3.4:9000/",
188+
}
189+
th.AssertDeepEquals(t, expected, actual)
190+
}
191+
157192
func TestUpdateEndpoint(t *testing.T) {
158193
th.SetupHTTP()
159194
defer th.TeardownHTTP()
160195

161196
th.Mux.HandleFunc("/endpoints/12", func(w http.ResponseWriter, r *http.Request) {
162197
th.TestMethod(t, r, "PATCH")
163198
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
164-
th.TestJSONRequest(t, r, `
165-
{
166-
"endpoint": {
167-
"name": "renamed",
199+
th.TestJSONRequest(t, r, `{
200+
"endpoint": {
201+
"name": "renamed",
168202
"region": "somewhere-else"
169-
}
170-
}
171-
`)
203+
}
204+
}`)
172205

173-
fmt.Fprint(w, `
174-
{
206+
fmt.Fprint(w, `{
175207
"endpoint": {
176208
"id": "12",
177209
"interface": "public",
@@ -184,8 +216,7 @@ func TestUpdateEndpoint(t *testing.T) {
184216
"service_id": "asdfasdfasdfasdf",
185217
"url": "https://1.2.3.4:9000/"
186218
}
187-
}
188-
`)
219+
}`)
189220
})
190221

191222
actual, err := endpoints.Update(context.TODO(), client.ServiceClient(), "12", endpoints.UpdateOpts{

openstack/identity/v3/projects/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Example to List all tags of a Project
7373
panic(err)
7474
}
7575
76-
Example to modify all tags of a Project
76+
Example to modify all tags of a Project
7777
7878
projectID := "966b3c7d36a24facaf20b7e458bf2192"
7979
tags := ["foo", "bar"]

0 commit comments

Comments
 (0)