From 6204b16c8082a4695564e989f5d39f986ef3b1d3 Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Thu, 14 May 2020 15:02:21 +0800 Subject: [PATCH 1/2] Add APIs for vpc tags --- openstack/networking/v1/tags/requests.go | 63 ++++++++++++++++++++++++ openstack/networking/v1/tags/results.go | 45 +++++++++++++++++ openstack/networking/v1/tags/urls.go | 18 +++++++ 3 files changed, 126 insertions(+) create mode 100644 openstack/networking/v1/tags/requests.go create mode 100644 openstack/networking/v1/tags/results.go create mode 100644 openstack/networking/v1/tags/urls.go diff --git a/openstack/networking/v1/tags/requests.go b/openstack/networking/v1/tags/requests.go new file mode 100644 index 000000000..3b73e9cf9 --- /dev/null +++ b/openstack/networking/v1/tags/requests.go @@ -0,0 +1,63 @@ +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, nil) + 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/networking/v1/tags/results.go b/openstack/networking/v1/tags/results.go new file mode 100644 index 000000000..7ba71c148 --- /dev/null +++ b/openstack/networking/v1/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/networking/v1/tags/urls.go b/openstack/networking/v1/tags/urls.go new file mode 100644 index 000000000..33ac7a2bd --- /dev/null +++ b/openstack/networking/v1/tags/urls.go @@ -0,0 +1,18 @@ +package tags + +import ( + "github.com/huaweicloud/golangsdk" +) + +// supported resourceType: "vpcs", "subnets", "publicips" +func actionURL(c *golangsdk.ServiceClient, resourceType, id string) string { + return c.ServiceURL(c.ProjectID, resourceType, id, "tags/action") +} + +func getURL(c *golangsdk.ServiceClient, resourceType, id string) string { + return c.ServiceURL(c.ProjectID, resourceType, id, "tags") +} + +func listURL(c *golangsdk.ServiceClient, resourceType string) string { + return c.ServiceURL(c.ProjectID, resourceType, "tags") +} From 251586bd36256860e4c7b6eb21566f150bb5772e Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Fri, 15 May 2020 17:14:42 +0800 Subject: [PATCH 2/2] Add APIs for dns tags --- openstack/dns/v2/tags/requests.go | 63 +++++++++++++++++++++++++++++++ openstack/dns/v2/tags/results.go | 45 ++++++++++++++++++++++ openstack/dns/v2/tags/urls.go | 19 ++++++++++ 3 files changed, 127 insertions(+) create mode 100644 openstack/dns/v2/tags/requests.go create mode 100644 openstack/dns/v2/tags/results.go create mode 100644 openstack/dns/v2/tags/urls.go diff --git a/openstack/dns/v2/tags/requests.go b/openstack/dns/v2/tags/requests.go new file mode 100644 index 000000000..3b73e9cf9 --- /dev/null +++ b/openstack/dns/v2/tags/requests.go @@ -0,0 +1,63 @@ +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, nil) + 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/dns/v2/tags/results.go b/openstack/dns/v2/tags/results.go new file mode 100644 index 000000000..7ba71c148 --- /dev/null +++ b/openstack/dns/v2/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/dns/v2/tags/urls.go b/openstack/dns/v2/tags/urls.go new file mode 100644 index 000000000..04a176e45 --- /dev/null +++ b/openstack/dns/v2/tags/urls.go @@ -0,0 +1,19 @@ +package tags + +import ( + "github.com/huaweicloud/golangsdk" +) + +// supported resourceType: "DNS-public_zone", "DNS-private_zone", +// "DNS-public_recordset", "DNS-private_recordset", "DNS-ptr_record" +func actionURL(c *golangsdk.ServiceClient, resourceType, id string) string { + return c.ServiceURL(c.ProjectID, resourceType, id, "tags/action") +} + +func getURL(c *golangsdk.ServiceClient, resourceType, id string) string { + return c.ServiceURL(c.ProjectID, resourceType, id, "tags") +} + +func listURL(c *golangsdk.ServiceClient, resourceType string) string { + return c.ServiceURL(c.ProjectID, resourceType, "tags") +}