From 6ebbebaf517c7963924daa0657ab389a5e844bf6 Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Mon, 13 Jul 2020 14:00:37 +0300 Subject: [PATCH] Add urls for common tags --- openstack/common/tags/requests.go | 66 +++++++++++++++++++++++++++++++ openstack/common/tags/results.go | 45 +++++++++++++++++++++ openstack/common/tags/urls.go | 42 ++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 openstack/common/tags/requests.go create mode 100644 openstack/common/tags/results.go create mode 100644 openstack/common/tags/urls.go diff --git a/openstack/common/tags/requests.go b/openstack/common/tags/requests.go new file mode 100644 index 000000000..57b3460b2 --- /dev/null +++ b/openstack/common/tags/requests.go @@ -0,0 +1,66 @@ +package tags + +import ( + "github.com/huaweicloud/golangsdk" +) + +//ActionOptsBuilder is an interface from which can build the request of creating/deleting tags +type ActionOptsBuilder interface { + ToTagsActionMap() (map[string]interface{}, error) +} + +//ActionOpts is a struct contains the parameters of creating/deleting tags +type ActionOpts struct { + Action string `json:"action" required:"ture"` + Tags []ResourceTag `json:"tags" required:"true"` +} + +//ToTagsActionMap build the action request in json format +func (opts ActionOpts) ToTagsActionMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "") +} + +func doAction(client *golangsdk.ServiceClient, srvType, id string, opts ActionOptsBuilder) (r ActionResult) { + b, err := opts.ToTagsActionMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Post(actionURL(client, srvType, id), b, nil, &golangsdk.RequestOpts{ + OkCodes: []int{200, 204}, + }) + return +} + +//Create is a method of creating tags by id +func Create(client *golangsdk.ServiceClient, srvType, id string, tags []ResourceTag) (r ActionResult) { + opts := ActionOpts{ + Tags: tags, + Action: "create", + } + return doAction(client, srvType, id, opts) +} + +//Delete is a method of deleting tags by id +func Delete(client *golangsdk.ServiceClient, srvType, id string, tags []ResourceTag) (r ActionResult) { + opts := ActionOpts{ + Tags: tags, + Action: "delete", + } + return doAction(client, srvType, id, opts) +} + +//Get is a method of getting the tags by id +func Get(client *golangsdk.ServiceClient, srvType, id string) (r GetResult) { + _, r.Err = client.Get(getURL(client, srvType, id), &r.Body, &golangsdk.RequestOpts{ + OkCodes: []int{202, 200}, + MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, + }) + return +} + +//List is a method of getting the tags of all service +func List(client *golangsdk.ServiceClient, srvType string) (r ListResult) { + _, r.Err = client.Get(listURL(client, srvType), &r.Body, nil) + return +} diff --git a/openstack/common/tags/results.go b/openstack/common/tags/results.go new file mode 100644 index 000000000..7ba71c148 --- /dev/null +++ b/openstack/common/tags/results.go @@ -0,0 +1,45 @@ +package tags + +import ( + "github.com/huaweicloud/golangsdk" +) + +//ResourceTags represents the tags response +type ResourceTags struct { + Tags []ResourceTag `json:"tags"` +} + +//ResourceTag is in key-value format +type ResourceTag struct { + Key string `json:"key" required:"ture"` + Value string `json:"value,omitempty"` +} + +//ActionResult is the action result which is the result of create or delete operations +type ActionResult struct { + golangsdk.ErrResult +} + +//GetResult contains the body of getting detailed tags request +type GetResult struct { + golangsdk.Result +} + +//Extract method will parse the result body into ResourceTags struct +func (r GetResult) Extract() (ResourceTags, error) { + var tags ResourceTags + err := r.Result.ExtractInto(&tags) + return tags, err +} + +//ListResult contains the body of getting all tags request +type ListResult struct { + golangsdk.Result +} + +//Extract method will parse the result body into ResourceTags struct +func (r ListResult) Extract() (ResourceTags, error) { + var tags ResourceTags + err := r.Result.ExtractInto(&tags) + return tags, err +} diff --git a/openstack/common/tags/urls.go b/openstack/common/tags/urls.go new file mode 100644 index 000000000..52d450c1c --- /dev/null +++ b/openstack/common/tags/urls.go @@ -0,0 +1,42 @@ +package tags + +import ( + "strings" + + "github.com/huaweicloud/golangsdk" +) + +// supported resourceType: "vpcs", "subnets", "publicips" +// "DNS-public_zone", "DNS-private_zone", "DNS-ptr_record" +// "DNS-public_recordset", "DNS-private_recordset" +func actionURL(c *golangsdk.ServiceClient, resourceType, id string) string { + if hasProjectID(c) { + return c.ServiceURL(resourceType, id, "tags/action") + } + return c.ServiceURL(c.ProjectID, resourceType, id, "tags/action") +} + +func getURL(c *golangsdk.ServiceClient, resourceType, id string) string { + if hasProjectID(c) { + return c.ServiceURL(resourceType, id, "tags") + } + return c.ServiceURL(c.ProjectID, resourceType, id, "tags") +} + +func listURL(c *golangsdk.ServiceClient, resourceType string) string { + if hasProjectID(c) { + return c.ServiceURL(resourceType, "tags") + } + return c.ServiceURL(c.ProjectID, resourceType, "tags") +} + +func hasProjectID(c *golangsdk.ServiceClient) bool { + url := c.ResourceBaseURL() + array := strings.Split(url, "/") + + // the baseURL must be end with "/" + if array[len(array)-2] == c.ProjectID { + return true + } + return false +}