Update existing Services.
diff --git a/openstack/identity/v3/services/requests.go b/openstack/identity/v3/services/requests.go
index 7b1a250..fa55d06 100644
--- a/openstack/identity/v3/services/requests.go
+++ b/openstack/identity/v3/services/requests.go
@@ -8,16 +8,16 @@
"github.com/rackspace/gophercloud/openstack/utils"
)
+type response struct {
+ Service ServiceResult `json:"service"`
+}
+
// Create adds a new service of the requested type to the catalog.
func Create(client *gophercloud.ServiceClient, serviceType string) (*ServiceResult, error) {
type request struct {
Type string `json:"type"`
}
- type response struct {
- Service ServiceResult `json:"service"`
- }
-
req := request{Type: serviceType}
var resp response
@@ -70,10 +70,6 @@
// Info returns additional information about a service, given its ID.
func Info(client *gophercloud.ServiceClient, serviceID string) (*ServiceResult, error) {
- type response struct {
- Service ServiceResult `json:"service"`
- }
-
var resp response
_, err := perigee.Request("GET", getServiceURL(client, serviceID), perigee.Options{
MoreHeaders: client.Provider.AuthenticatedHeaders(),
@@ -85,3 +81,25 @@
}
return &resp.Service, nil
}
+
+// Update changes the service type of an existing service.s
+func Update(client *gophercloud.ServiceClient, serviceID string, serviceType string) (*ServiceResult, error) {
+ type request struct {
+ Type string `json:"type"`
+ }
+
+ req := request{Type: serviceType}
+
+ var resp response
+ _, err := perigee.Request("PATCH", getServiceURL(client, serviceID), perigee.Options{
+ MoreHeaders: client.Provider.AuthenticatedHeaders(),
+ ReqBody: &req,
+ Results: &resp,
+ OkCodes: []int{200},
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ return &resp.Service, nil
+}
diff --git a/openstack/identity/v3/services/requests_test.go b/openstack/identity/v3/services/requests_test.go
index 1d30540..b2249e6 100644
--- a/openstack/identity/v3/services/requests_test.go
+++ b/openstack/identity/v3/services/requests_test.go
@@ -9,13 +9,24 @@
"github.com/rackspace/gophercloud/testhelper"
)
+const tokenID = "111111"
+
+func serviceClient() *gophercloud.ServiceClient {
+ return &gophercloud.ServiceClient{
+ Provider: &gophercloud.ProviderClient{
+ TokenID: tokenID,
+ },
+ Endpoint: testhelper.Endpoint(),
+ }
+}
+
func TestCreateSuccessful(t *testing.T) {
testhelper.SetupHTTP()
defer testhelper.TeardownHTTP()
testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "POST")
- testhelper.TestHeader(t, r, "X-Auth-Token", "1111")
+ testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
testhelper.TestJSONRequest(t, r, `{ "type": "compute" }`)
w.Header().Add("Content-Type", "application/json")
@@ -30,14 +41,9 @@
}`)
})
- client := gophercloud.ServiceClient{
- Provider: &gophercloud.ProviderClient{
- TokenID: "1111",
- },
- Endpoint: testhelper.Endpoint(),
- }
+ client := serviceClient()
- result, err := Create(&client, "compute")
+ result, err := Create(client, "compute")
if err != nil {
t.Fatalf("Unexpected error from Create: %v", err)
}
@@ -62,7 +68,7 @@
testhelper.Mux.HandleFunc("/services", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "GET")
- testhelper.TestHeader(t, r, "X-Auth-Token", "1111")
+ testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, `
@@ -89,14 +95,9 @@
`)
})
- client := gophercloud.ServiceClient{
- Provider: &gophercloud.ProviderClient{
- TokenID: "1111",
- },
- Endpoint: testhelper.Endpoint(),
- }
+ client := serviceClient()
- result, err := List(&client, ListOpts{})
+ result, err := List(client, ListOpts{})
if err != nil {
t.Fatalf("Error listing services: %v", err)
}
@@ -124,14 +125,14 @@
testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
testhelper.TestMethod(t, r, "GET")
- testhelper.TestHeader(t, r, "X-Auth-Token", "1111")
+ testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, `
{
"service": {
"description": "Service One",
- "id": "1234",
+ "id": "12345",
"name": "service-one",
"type": "identity"
}
@@ -139,19 +140,14 @@
`)
})
- client := gophercloud.ServiceClient{
- Provider: &gophercloud.ProviderClient{
- TokenID: "1111",
- },
- Endpoint: testhelper.Endpoint(),
- }
+ client := serviceClient()
- result, err := Info(&client, "12345")
+ result, err := Info(client, "12345")
if err != nil {
t.Fatalf("Error fetching service information: %v", err)
}
- if result.ID != "1234" {
+ if result.ID != "12345" {
t.Errorf("Unexpected service ID: %s", result.ID)
}
if *result.Description != "Service One" {
@@ -164,3 +160,35 @@
t.Errorf("Unexpected service type: [%s]", result.Type)
}
}
+
+func TestUpdateSuccessful(t *testing.T) {
+ testhelper.SetupHTTP()
+ defer testhelper.TeardownHTTP()
+
+ testhelper.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) {
+ testhelper.TestMethod(t, r, "PATCH")
+ testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
+ testhelper.TestJSONRequest(t, r, `{ "type": "lasermagic" }`)
+
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, `
+ {
+ "service": {
+ "id": "12345",
+ "type": "lasermagic"
+ }
+ }
+ `)
+ })
+
+ client := serviceClient()
+
+ result, err := Update(client, "12345", "lasermagic")
+ if err != nil {
+ t.Fatalf("Unable to update service")
+ }
+
+ if result.ID != "12345" {
+
+ }
+}