Skip to content

Commit

Permalink
Compute v2: Create Or Update Aggregate Metadata (#756)
Browse files Browse the repository at this point in the history
* Add support create/update metadata

* Add unit test

* Update doc.go

* Add acceptance test

* Fix doc.go

* Change `Metadata` field type to map[string]interface{}

* Add delete key to acceptance test
  • Loading branch information
dstdfx authored and jtopjian committed Feb 10, 2018
1 parent 104e257 commit 6da026c
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 4 deletions.
35 changes: 35 additions & 0 deletions acceptance/openstack/compute/v2/aggregates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,41 @@ func TestAggregatesAddRemoveHost(t *testing.T) {
tools.PrintResource(t, aggregateWithRemovedHost)
}

func TestAggregatesSetRemoveMetadata(t *testing.T) {
client, err := clients.NewComputeV2Client()
if err != nil {
t.Fatalf("Unable to create a compute client: %v", err)
}

createdAggregate, err := CreateAggregate(t, client)
if err != nil {
t.Fatalf("Unable to create an aggregate: %v", err)
}
defer DeleteAggregate(t, client, createdAggregate)

opts := aggregates.SetMetadataOpts{
Metadata: map[string]interface{}{"key": "value"},
}

aggregateWithMetadata, err := aggregates.SetMetadata(client, createdAggregate.ID, opts).Extract()
if err != nil {
t.Fatalf("Unable to set metadata to aggregate: %v", err)
}

tools.PrintResource(t, aggregateWithMetadata)

optsToRemove := aggregates.SetMetadataOpts{
Metadata: map[string]interface{}{"key": nil},
}

aggregateWithRemovedKey, err := aggregates.SetMetadata(client, createdAggregate.ID, optsToRemove).Extract()
if err != nil {
t.Fatalf("Unable to set metadata to aggregate: %v", err)
}

tools.PrintResource(t, aggregateWithRemovedKey)
}

func getHypervisor(t *testing.T, client *gophercloud.ServiceClient) (*hypervisors.Hypervisor, error) {
allPages, err := hypervisors.List(client).AllPages()
if err != nil {
Expand Down
17 changes: 15 additions & 2 deletions openstack/compute/v2/extensions/aggregates/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Example of Add Host
aggregateID := 22
opts := aggregates.AddHostOpts{
Host: "newhost-cmp1"
Host: "newhost-cmp1",
}
aggregate, err := aggregates.AddHost(computeClient, aggregateID, opts).Extract()
Expand All @@ -79,7 +79,7 @@ Example of Remove Host
aggregateID := 22
opts := aggregates.RemoveHostOpts{
Host: "newhost-cmp1"
Host: "newhost-cmp1",
}
aggregate, err := aggregates.RemoveHost(computeClient, aggregateID, opts).Extract()
Expand All @@ -88,5 +88,18 @@ Example of Remove Host
}
fmt.Printf("%+v\n", aggregate)
Example of Create or Update Metadata
aggregateID := 22
opts := aggregates.SetMetadata{
Metadata: map[string]string{"key": "value"},
}
aggregate, err := aggregates.SetMetadata(computeClient, aggregateID, opts).Extract()
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", aggregate)
*/
package aggregates
23 changes: 23 additions & 0 deletions openstack/compute/v2/extensions/aggregates/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,26 @@ func RemoveHost(client *gophercloud.ServiceClient, aggregateID int, opts RemoveH
})
return
}

type SetMetadataOpts struct {
Metadata map[string]interface{} `json:"metadata" required:"true"`
}

func (opts SetMetadataOpts) ToSetMetadataMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "set_metadata")
}

// SetMetadata makes a request against the API to set metadata to a specific aggregate.
func SetMetadata(client *gophercloud.ServiceClient, aggregateID int, opts SetMetadataOpts) (r ActionResult) {
v := strconv.Itoa(aggregateID)

b, err := opts.ToSetMetadataMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(aggregatesSetMetadataURL(client, v), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return
}
44 changes: 44 additions & 0 deletions openstack/compute/v2/extensions/aggregates/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ const AggregateRemoveHostBody = `
}
`

const AggregateSetMetadataBody = `
{
"aggregate": {
"name": "test-aggregate2",
"availability_zone": "test-az",
"deleted": false,
"created_at": "2017-12-22T10:16:07.000000",
"updated_at": "2017-12-23T10:18:00.000000",
"hosts": [
"cmp0"
],
"deleted_at": null,
"id": 4,
"metadata": {
"availability_zone": "test-az",
"key": "value"
}
}
}
`

var (
// First aggregate from the AggregateListBody
FirstFakeAggregate = aggregates.Aggregate{
Expand Down Expand Up @@ -222,6 +243,18 @@ var (
DeletedAt: time.Time{},
Deleted: false,
}

AggregateWithUpdatedMetadata = aggregates.Aggregate{
AvailabilityZone: "test-az",
Hosts: []string{"cmp0"},
ID: 4,
Metadata: map[string]string{"availability_zone": "test-az", "key": "value"},
Name: "test-aggregate2",
CreatedAt: time.Date(2017, 12, 22, 10, 16, 7, 0, time.UTC),
UpdatedAt: time.Date(2017, 12, 23, 10, 18, 0, 0, time.UTC),
DeletedAt: time.Time{},
Deleted: false,
}
)

// HandleListSuccessfully configures the test server to respond to a List request.
Expand Down Expand Up @@ -298,3 +331,14 @@ func HandleRemoveHostSuccessfully(t *testing.T) {
fmt.Fprintf(w, AggregateRemoveHostBody)
})
}

func HandleSetMetadataSuccessfully(t *testing.T) {
v := strconv.Itoa(AggregateWithUpdatedMetadata.ID)
th.Mux.HandleFunc("/os-aggregates/"+v+"/action", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "POST")
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)

w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, AggregateSetMetadataBody)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ func TestAddHostAggregate(t *testing.T) {
func TestRemoveHostAggregate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleAddHostSuccessfully(t)
HandleRemoveHostSuccessfully(t)

expected := AggregateWithAddedHost
expected := AggregateWithRemovedHost

opts := aggregates.RemoveHostOpts{
Host: "cmp1",
Expand All @@ -130,3 +130,20 @@ func TestRemoveHostAggregate(t *testing.T) {

th.AssertDeepEquals(t, &expected, actual)
}

func TestSetMetadataAggregate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleSetMetadataSuccessfully(t)

expected := AggregateWithUpdatedMetadata

opts := aggregates.SetMetadataOpts{
Metadata: map[string]interface{}{"key": "value"},
}

actual, err := aggregates.SetMetadata(client.ServiceClient(), expected.ID, opts).Extract()
th.AssertNoErr(t, err)

th.AssertDeepEquals(t, &expected, actual)
}
4 changes: 4 additions & 0 deletions openstack/compute/v2/extensions/aggregates/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func aggregatesAddHostURL(c *gophercloud.ServiceClient, aggregateID string) stri
func aggregatesRemoveHostURL(c *gophercloud.ServiceClient, aggregateID string) string {
return c.ServiceURL("os-aggregates", aggregateID, "action")
}

func aggregatesSetMetadataURL(c *gophercloud.ServiceClient, aggregateID string) string {
return c.ServiceURL("os-aggregates", aggregateID, "action")
}

0 comments on commit 6da026c

Please sign in to comment.