From 3ef04a912a34b0bcc347cd3ee22507638857cea3 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Tue, 14 Jan 2020 02:17:44 +0000 Subject: [PATCH] Add new cloudserver tags support --- openstack/client.go | 5 ++ openstack/ecs/v1/cloudservertags/requests.go | 61 ++++++++++++++++++++ openstack/ecs/v1/cloudservertags/results.go | 28 +++++++++ openstack/ecs/v1/cloudservertags/urls.go | 17 ++++++ 4 files changed, 111 insertions(+) create mode 100644 openstack/ecs/v1/cloudservertags/requests.go create mode 100644 openstack/ecs/v1/cloudservertags/results.go create mode 100644 openstack/ecs/v1/cloudservertags/urls.go diff --git a/openstack/client.go b/openstack/client.go index d5e8026c8..a2033d99b 100644 --- a/openstack/client.go +++ b/openstack/client.go @@ -732,6 +732,11 @@ func NewComputeV11(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) return sc, err } +func NewEcsV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) { + sc, err := initClientOpts(client, eo, "ecs") + return sc, err +} + func NewRdsTagV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) { sc, err := initClientOpts(client, eo, "network") sc.Endpoint = strings.Replace(sc.Endpoint, "vpc", "rds", 1) diff --git a/openstack/ecs/v1/cloudservertags/requests.go b/openstack/ecs/v1/cloudservertags/requests.go new file mode 100644 index 000000000..548fc836a --- /dev/null +++ b/openstack/ecs/v1/cloudservertags/requests.go @@ -0,0 +1,61 @@ +package cloudservertags + +import ( + "github.com/huaweicloud/golangsdk" +) + +// Tag is a structure of key value pair. +type Tag struct { + //tag key + Key string `json:"key" required:"true"` + //tag value + Value string `json:"value" required:"true"` +} + +// BatchOptsBuilder allows extensions to add additional parameters to the +// BatchAction request. +type BatchOptsBuilder interface { + ToTagsBatchMap() (map[string]interface{}, error) +} + +// BatchOpts contains all the values needed to perform BatchAction on the instance tags. +type BatchOpts struct { + //List of tags to perform batch operation + Tags []Tag `json:"tags,omitempty"` + //Operator , Possible values are:create, update,delete + Action ActionType `json:"action" required:"true"` +} + +//ActionType specifies the type of batch operation action to be performed +type ActionType string + +var ( + // ActionCreate is used to set action operator to create + ActionCreate ActionType = "create" + // ActionDelete is used to set action operator to delete + ActionDelete ActionType = "delete" +) + +// ToTagsBatchMap builds a BatchAction request body from BatchOpts. +func (opts BatchOpts) ToTagsBatchMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "") +} + +//BatchAction is used to create ,update or delete the tags of a specified instance. +func BatchAction(client *golangsdk.ServiceClient, serverID string, opts BatchOptsBuilder) (r ActionResults) { + b, err := opts.ToTagsBatchMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Post(actionURL(client, serverID), b, nil, &golangsdk.RequestOpts{ + OkCodes: []int{204}, + }) + return +} + +// Get retrieves the tags of a specific instance. +func Get(client *golangsdk.ServiceClient, serverID string) (r GetResult) { + _, r.Err = client.Get(resourceURL(client, serverID), &r.Body, nil) + return +} diff --git a/openstack/ecs/v1/cloudservertags/results.go b/openstack/ecs/v1/cloudservertags/results.go new file mode 100644 index 000000000..f35c37044 --- /dev/null +++ b/openstack/ecs/v1/cloudservertags/results.go @@ -0,0 +1,28 @@ +package cloudservertags + +import ( + "github.com/huaweicloud/golangsdk" +) + +type RespTags struct { + //contains list of tags, i.e.key value pair + Tags []Tag `json:"tags"` +} + +type commonResult struct { + golangsdk.Result +} + +type ActionResults struct { + commonResult +} + +type GetResult struct { + commonResult +} + +func (r commonResult) Extract() (*RespTags, error) { + var response RespTags + err := r.ExtractInto(&response) + return &response, err +} diff --git a/openstack/ecs/v1/cloudservertags/urls.go b/openstack/ecs/v1/cloudservertags/urls.go new file mode 100644 index 000000000..04f7ad69c --- /dev/null +++ b/openstack/ecs/v1/cloudservertags/urls.go @@ -0,0 +1,17 @@ +package cloudservertags + +import "github.com/huaweicloud/golangsdk" + +const ( + rootPath = "cloudservers" + resourcePath = "tags" + actionPath = "tags/action" +) + +func actionURL(c *golangsdk.ServiceClient, id string) string { + return c.ServiceURL(rootPath, id, actionPath) +} + +func resourceURL(c *golangsdk.ServiceClient, id string) string { + return c.ServiceURL(rootPath, id, resourcePath) +}