Skip to content

Commit 7885c6f

Browse files
identity/service: add name and description fields
1 parent fba717b commit 7885c6f

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

openstack/identity/v3/services/requests.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ type CreateOptsBuilder interface {
1515

1616
// CreateOpts provides options used to create a service.
1717
type CreateOpts struct {
18+
// Name is the name of the service.
19+
Name string `json:"name,omitempty"`
20+
21+
// Description is the description of the service.
22+
Description string `json:"description,omitempty"`
23+
1824
// Type is the type of the service.
1925
Type string `json:"type"`
2026

@@ -108,6 +114,12 @@ type UpdateOptsBuilder interface {
108114

109115
// UpdateOpts provides options for updating a service.
110116
type UpdateOpts struct {
117+
// Name is an updated name for the service.
118+
Name *string `json:"name,omitempty"`
119+
120+
// Description is an update description for the service.
121+
Description *string `json:"description,omitempty"`
122+
111123
// Type is the type of the service.
112124
Type string `json:"type,omitempty"`
113125

openstack/identity/v3/services/results.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ type Service struct {
5050
// ID is the unique ID of the service.
5151
ID string `json:"id"`
5252

53+
// Name is the name of the service.
54+
Name string `json:"name"`
55+
56+
// Description is the description of the service.
57+
Description string `json:"description"`
58+
5359
// Type is the type of the service.
5460
Type string `json:"type"`
5561

@@ -75,8 +81,6 @@ func (r *Service) UnmarshalJSON(b []byte) error {
7581
}
7682
*r = Service(s.tmp)
7783

78-
// Collect other fields and bundle them into Extra
79-
// but only if a field titled "extra" wasn't sent.
8084
if s.Extra != nil {
8185
r.Extra = s.Extra
8286
} else {
@@ -85,8 +89,19 @@ func (r *Service) UnmarshalJSON(b []byte) error {
8589
if err != nil {
8690
return err
8791
}
92+
8893
if resultMap, ok := result.(map[string]any); ok {
8994
r.Extra = gophercloud.RemainingKeys(Service{}, resultMap)
95+
96+
// the following code is required for backward compatibility with the
97+
// old behavior, when both description and name were in extra
98+
if description, ok := resultMap["description"]; ok {
99+
r.Extra["description"] = description
100+
}
101+
102+
if name, ok := resultMap["name"]; ok {
103+
r.Extra["name"] = name
104+
}
90105
}
91106
}
92107

openstack/identity/v3/services/testing/fixtures_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ const ListOutput = `
2323
"links": {
2424
"self": "https://example.com/identity/v3/services/1234"
2525
},
26+
"name": "service-one",
2627
"type": "identity",
2728
"enabled": false,
2829
"extra": {
29-
"name": "service-one",
30-
"description": "Service One"
30+
"email": "service-one@example.com"
3131
}
3232
},
3333
{
3434
"id": "9876",
3535
"links": {
3636
"self": "https://example.com/identity/v3/services/9876"
3737
},
38+
"name": "service-two",
39+
"description": "Service Two",
3840
"type": "compute",
3941
"enabled": false,
4042
"extra": {
41-
"name": "service-two",
42-
"description": "Service Two",
4343
"email": "service@example.com"
4444
}
4545
}
@@ -55,11 +55,11 @@ const GetOutput = `
5555
"links": {
5656
"self": "https://example.com/identity/v3/services/9876"
5757
},
58+
"name": "service-two",
59+
"description": "Service Two",
5860
"type": "compute",
5961
"enabled": false,
6062
"extra": {
61-
"name": "service-two",
62-
"description": "Service Two",
6363
"email": "service@example.com"
6464
}
6565
}
@@ -96,11 +96,11 @@ const UpdateOutput = `
9696
"links": {
9797
"self": "https://example.com/identity/v3/services/9876"
9898
},
99+
"name": "service-two",
100+
"description": "Service Two Updated",
99101
"type": "compute2",
100102
"enabled": false,
101103
"extra": {
102-
"name": "service-two",
103-
"description": "Service Two Updated",
104104
"email": "service@example.com"
105105
}
106106
}
@@ -113,11 +113,11 @@ var FirstService = services.Service{
113113
Links: map[string]any{
114114
"self": "https://example.com/identity/v3/services/1234",
115115
},
116+
Name: "service-one",
116117
Type: "identity",
117118
Enabled: false,
118119
Extra: map[string]any{
119-
"name": "service-one",
120-
"description": "Service One",
120+
"email": "service-one@example.com",
121121
},
122122
}
123123

@@ -127,12 +127,12 @@ var SecondService = services.Service{
127127
Links: map[string]any{
128128
"self": "https://example.com/identity/v3/services/9876",
129129
},
130-
Type: "compute",
131-
Enabled: false,
130+
Name: "service-two",
131+
Description: "Service Two",
132+
Type: "compute",
133+
Enabled: false,
132134
Extra: map[string]any{
133-
"name": "service-two",
134-
"description": "Service Two",
135-
"email": "service@example.com",
135+
"email": "service@example.com",
136136
},
137137
}
138138

@@ -142,12 +142,12 @@ var SecondServiceUpdated = services.Service{
142142
Links: map[string]any{
143143
"self": "https://example.com/identity/v3/services/9876",
144144
},
145-
Type: "compute2",
146-
Enabled: false,
145+
Name: "service-two",
146+
Description: "Service Two Updated",
147+
Type: "compute2",
148+
Enabled: false,
147149
Extra: map[string]any{
148-
"name": "service-two",
149-
"description": "Service Two Updated",
150-
"email": "service@example.com",
150+
"email": "service@example.com",
151151
},
152152
}
153153

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ func TestCreateSuccessful(t *testing.T) {
1717
HandleCreateServiceSuccessfully(t, fakeServer)
1818

1919
createOpts := services.CreateOpts{
20-
Type: "compute",
20+
Name: "service-two",
21+
Description: "Service Two",
22+
Type: "compute",
2123
Extra: map[string]any{
22-
"name": "service-two",
23-
"description": "Service Two",
24-
"email": "service@example.com",
24+
"email": "service@example.com",
2525
},
2626
}
2727

@@ -60,7 +60,7 @@ func TestListServicesAllPages(t *testing.T) {
6060
actual, err := services.ExtractServices(allPages)
6161
th.AssertNoErr(t, err)
6262
th.CheckDeepEquals(t, ExpectedServicesSlice, actual)
63-
th.AssertEquals(t, ExpectedServicesSlice[0].Extra["name"], "service-one")
63+
th.AssertEquals(t, ExpectedServicesSlice[0].Extra["email"], "service-one@example.com")
6464
th.AssertEquals(t, ExpectedServicesSlice[1].Extra["email"], "service@example.com")
6565
}
6666

@@ -81,16 +81,15 @@ func TestUpdateSuccessful(t *testing.T) {
8181
defer fakeServer.Teardown()
8282
HandleUpdateServiceSuccessfully(t, fakeServer)
8383

84+
updatedDescription := "Service Two Updated"
8485
updateOpts := services.UpdateOpts{
85-
Type: "compute2",
86-
Extra: map[string]any{
87-
"description": "Service Two Updated",
88-
},
86+
Description: &updatedDescription,
87+
Type: "compute2",
8988
}
9089
actual, err := services.Update(context.TODO(), client.ServiceClient(fakeServer), "9876", updateOpts).Extract()
9190
th.AssertNoErr(t, err)
9291
th.CheckDeepEquals(t, SecondServiceUpdated, *actual)
93-
th.AssertEquals(t, SecondServiceUpdated.Extra["description"], "Service Two Updated")
92+
th.AssertEquals(t, SecondServiceUpdated.Description, "Service Two Updated")
9493
}
9594

9695
func TestDeleteSuccessful(t *testing.T) {

0 commit comments

Comments
 (0)