From f5d8a5bd1de70bcfef7c27d8404bf5289cb05f18 Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Wed, 16 Oct 2019 20:13:14 +0800 Subject: [PATCH] Add ims v2 tags interface Signed-off-by: ShiChangkuo --- openstack/ims/v2/tags/requests.go | 61 +++++++++++++++++++++++++++++++ openstack/ims/v2/tags/results.go | 28 ++++++++++++++ openstack/ims/v2/tags/urls.go | 17 +++++++++ 3 files changed, 106 insertions(+) create mode 100644 openstack/ims/v2/tags/requests.go create mode 100644 openstack/ims/v2/tags/results.go create mode 100644 openstack/ims/v2/tags/urls.go diff --git a/openstack/ims/v2/tags/requests.go b/openstack/ims/v2/tags/requests.go new file mode 100644 index 000000000..c0028a435 --- /dev/null +++ b/openstack/ims/v2/tags/requests.go @@ -0,0 +1,61 @@ +package tags + +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 image tags. +type BatchOpts struct { + //List of tags to perform batch operation + Tags []Tag `json:"tags,omitempty"` + //Operator , Possible values are:create or 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 image. +func BatchAction(client *golangsdk.ServiceClient, imageID string, opts BatchOptsBuilder) (r ActionResults) { + b, err := opts.ToTagsBatchMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Post(actionURL(client, imageID), b, nil, &golangsdk.RequestOpts{ + OkCodes: []int{204}, + }) + return +} + +// Get retrieves the tags of a specific image. +func Get(client *golangsdk.ServiceClient, imageID string) (r GetResult) { + _, r.Err = client.Get(resourceURL(client, imageID), &r.Body, nil) + return +} diff --git a/openstack/ims/v2/tags/results.go b/openstack/ims/v2/tags/results.go new file mode 100644 index 000000000..206e2ce2b --- /dev/null +++ b/openstack/ims/v2/tags/results.go @@ -0,0 +1,28 @@ +package tags + +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/ims/v2/tags/urls.go b/openstack/ims/v2/tags/urls.go new file mode 100644 index 000000000..53488a00b --- /dev/null +++ b/openstack/ims/v2/tags/urls.go @@ -0,0 +1,17 @@ +package tags + +import "github.com/huaweicloud/golangsdk" + +const ( + rootPath = "images" + resourcePath = "tags" + actionPath = "tags/action" +) + +func actionURL(c *golangsdk.ServiceClient, id string) string { + return c.ServiceURL(c.ProjectID, rootPath, id, actionPath) +} + +func resourceURL(c *golangsdk.ServiceClient, id string) string { + return c.ServiceURL(c.ProjectID, rootPath, id, resourcePath) +}