Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07253c9

Browse files
authored
Add support for update of Compute service (gophercloud#1902)
* Add resource provider traits API for placement * Add support for updating os-services in Compute API * Add support for updating os-services in Compute API * use string const for status * Add support for updating os-services in Compute API * use string const for status * use list opts in list services call
1 parent fbb6b8b commit 07253c9

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-0
lines changed

acceptance/openstack/compute/v2/services_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,60 @@ func TestServicesListWithOpts(t *testing.T) {
6363

6464
th.AssertEquals(t, found, true)
6565
}
66+
67+
func TestServicesUpdate(t *testing.T) {
68+
clients.RequireAdmin(t)
69+
70+
client, err := clients.NewComputeV2Client()
71+
th.AssertNoErr(t, err)
72+
73+
listOpts := services.ListOpts{
74+
Binary: "nova-compute",
75+
}
76+
77+
client.Microversion = "2.53"
78+
allPages, err := services.List(client, listOpts).AllPages()
79+
th.AssertNoErr(t, err)
80+
81+
allServices, err := services.ExtractServices(allPages)
82+
th.AssertNoErr(t, err)
83+
84+
// disable all services
85+
for _, service := range allServices {
86+
opts := services.UpdateOpts{
87+
Status: services.ServiceDisabled,
88+
}
89+
updated, err := services.Update(client, service.ID, opts).Extract()
90+
th.AssertNoErr(t, err)
91+
92+
th.AssertEquals(t, updated.ID, service.ID)
93+
}
94+
95+
// verify all services are disabled
96+
allPages, err = services.List(client, listOpts).AllPages()
97+
th.AssertNoErr(t, err)
98+
99+
allServices, err = services.ExtractServices(allPages)
100+
th.AssertNoErr(t, err)
101+
102+
for _, service := range allServices {
103+
th.AssertEquals(t, service.Status, "disabled")
104+
}
105+
106+
// reenable all services
107+
allPages, err = services.List(client, listOpts).AllPages()
108+
th.AssertNoErr(t, err)
109+
110+
allServices, err = services.ExtractServices(allPages)
111+
th.AssertNoErr(t, err)
112+
113+
for _, service := range allServices {
114+
opts := services.UpdateOpts{
115+
Status: services.ServiceEnabled,
116+
}
117+
updated, err := services.Update(client, service.ID, opts).Extract()
118+
th.AssertNoErr(t, err)
119+
120+
th.AssertEquals(t, updated.ID, service.ID)
121+
}
122+
}

openstack/compute/v2/extensions/services/doc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ Example of Retrieving list of all services
2121
for _, service := range allServices {
2222
fmt.Printf("%+v\n", service)
2323
}
24+
25+
Example of updating a service
26+
27+
opts := services.UpdateOpts{
28+
Status: services.ServiceDisabled,
29+
}
30+
31+
updated, err := services.Update(client, serviceID, opts).Extract()
32+
if err != nil {
33+
panic(err)
34+
}
2435
*/
2536

2637
package services

openstack/compute/v2/extensions/services/requests.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,44 @@ func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pa
3838
return ServicePage{pagination.SinglePageBase(r)}
3939
})
4040
}
41+
42+
type ServiceStatus string
43+
44+
const (
45+
// ServiceEnabled is used to mark a service as being enabled.
46+
ServiceEnabled ServiceStatus = "enabled"
47+
48+
// ServiceDisabled is used to mark a service as being disabled.
49+
ServiceDisabled ServiceStatus = "disabled"
50+
)
51+
52+
// UpdateOpts specifies the base attributes that may be updated on a service.
53+
type UpdateOpts struct {
54+
// Status represents the new service status. One of enabled or disabled.
55+
Status ServiceStatus `json:"status,omitempty"`
56+
57+
// DisabledReason represents the reason for disabling a service.
58+
DisabledReason string `json:"disabled_reason,omitempty"`
59+
60+
// ForcedDown is a manual override to tell nova that the service in question
61+
// has been fenced manually by the operations team.
62+
ForcedDown bool `json:"forced_down,omitempty"`
63+
}
64+
65+
// ToServiceUpdateMap formats an UpdateOpts structure into a request body.
66+
func (opts UpdateOpts) ToServiceUpdateMap() (map[string]interface{}, error) {
67+
return gophercloud.BuildRequestBody(opts, "")
68+
}
69+
70+
// Update requests that various attributes of the indicated service be changed.
71+
func Update(client *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
72+
b, err := opts.ToServiceUpdateMap()
73+
if err != nil {
74+
r.Err = err
75+
return
76+
}
77+
_, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
78+
OkCodes: []int{200},
79+
})
80+
return
81+
}

openstack/compute/v2/extensions/services/results.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type Service struct {
1818
// The reason for disabling a service.
1919
DisabledReason string `json:"disabled_reason"`
2020

21+
// Whether or not service was forced down manually.
22+
ForcedDown bool `json:"forced_down"`
23+
2124
// The name of the host.
2225
Host string `json:"host"`
2326

@@ -69,6 +72,25 @@ func (r *Service) UnmarshalJSON(b []byte) error {
6972
return nil
7073
}
7174

75+
type serviceResult struct {
76+
gophercloud.Result
77+
}
78+
79+
// Extract interprets any UpdateResult as a service, if possible.
80+
func (r serviceResult) Extract() (*Service, error) {
81+
var s struct {
82+
Service Service `json:"service"`
83+
}
84+
err := r.ExtractInto(&s)
85+
return &s.Service, err
86+
}
87+
88+
// UpdateResult is the response from an Update operation. Call its Extract
89+
// method to interpret it as a Server.
90+
type UpdateResult struct {
91+
serviceResult
92+
}
93+
7294
// ServicePage represents a single page of all Services from a List request.
7395
type ServicePage struct {
7496
pagination.SinglePageBase

openstack/compute/v2/extensions/services/testing/fixtures.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,37 @@ var (
220220
}
221221
)
222222

223+
// ServiceUpdate represents a raw service from the Compute service update API
224+
const ServiceUpdate = `
225+
{
226+
"service":
227+
{
228+
"id": 1,
229+
"binary": "nova-scheduler",
230+
"disabled_reason": "test1",
231+
"host": "host1",
232+
"state": "up",
233+
"status": "disabled",
234+
"updated_at": "2012-10-29T13:42:02.000000",
235+
"forced_down": false,
236+
"zone": "internal"
237+
}
238+
}
239+
`
240+
241+
//FakeServiceUpdateBody represents the updated service
242+
var FakeServiceUpdateBody = services.Service{
243+
Binary: "nova-scheduler",
244+
DisabledReason: "test1",
245+
ForcedDown: false,
246+
Host: "host1",
247+
ID: "1",
248+
State: "up",
249+
Status: "disabled",
250+
UpdatedAt: time.Date(2012, 10, 29, 13, 42, 2, 0, time.UTC),
251+
Zone: "internal",
252+
}
253+
223254
// HandleListPre253Successfully configures the test server to respond to a List
224255
// request to a Compute server API pre 2.53 microversion release.
225256
func HandleListPre253Successfully(t *testing.T) {
@@ -243,3 +274,17 @@ func HandleListSuccessfully(t *testing.T) {
243274
fmt.Fprintf(w, ServiceListBody)
244275
})
245276
}
277+
278+
// HandleUpdateSuccessfully configures the test server to respond to a Update
279+
// request to a Compute server with Pike+ release.
280+
func HandleUpdateSuccessfully(t *testing.T) {
281+
th.Mux.HandleFunc("/os-services/fake-service-id", func(w http.ResponseWriter, r *http.Request) {
282+
th.TestMethod(t, r, "PUT")
283+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
284+
th.TestHeader(t, r, "Accept", "application/json")
285+
th.TestHeader(t, r, "Content-Type", "application/json")
286+
th.TestJSONRequest(t, r, `{"status": "disabled"}`)
287+
288+
fmt.Fprintf(w, ServiceUpdate)
289+
})
290+
}

openstack/compute/v2/extensions/services/testing/requests_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,17 @@ func TestListServices(t *testing.T) {
7676
t.Errorf("Expected 1 page, saw %d", pages)
7777
}
7878
}
79+
80+
func TestUpdateService(t *testing.T) {
81+
testhelper.SetupHTTP()
82+
defer testhelper.TeardownHTTP()
83+
HandleUpdateSuccessfully(t)
84+
85+
client := client.ServiceClient()
86+
actual, err := services.Update(client, "fake-service-id", services.UpdateOpts{Status: services.ServiceDisabled}).Extract()
87+
if err != nil {
88+
t.Fatalf("Unexpected Update error: %v", err)
89+
}
90+
91+
testhelper.CheckDeepEquals(t, FakeServiceUpdateBody, *actual)
92+
}

openstack/compute/v2/extensions/services/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ import "github.com/gophercloud/gophercloud"
55
func listURL(c *gophercloud.ServiceClient) string {
66
return c.ServiceURL("os-services")
77
}
8+
9+
func updateURL(c *gophercloud.ServiceClient, id string) string {
10+
return c.ServiceURL("os-services", id)
11+
}

0 commit comments

Comments
 (0)