From 4562440b04c075e5e220db66dfb7846946dc37d2 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Mon, 22 Apr 2019 17:09:34 +0800 Subject: [PATCH 1/3] Add RDS tag v1 --- openstack/client.go | 8 ++++ openstack/rds/v1/tags/requests.go | 79 +++++++++++++++++++++++++++++++ openstack/rds/v1/tags/results.go | 39 +++++++++++++++ openstack/rds/v1/tags/urls.go | 7 +++ 4 files changed, 133 insertions(+) create mode 100644 openstack/rds/v1/tags/requests.go create mode 100644 openstack/rds/v1/tags/results.go create mode 100644 openstack/rds/v1/tags/urls.go diff --git a/openstack/client.go b/openstack/client.go index 1bd88905b..6583c4f27 100644 --- a/openstack/client.go +++ b/openstack/client.go @@ -689,6 +689,14 @@ func NewComputeV1(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) ( 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) + sc.Endpoint = sc.Endpoint + "v1/" + sc.ResourceBase = sc.Endpoint + client.ProjectID + "/rds/" + return sc, err +} + //NewAutoScalingService creates a ServiceClient that may be used to access the //auto-scaling service of huawei public cloud func NewAutoScalingService(client *golangsdk.ProviderClient, eo golangsdk.EndpointOpts) (*golangsdk.ServiceClient, error) { diff --git a/openstack/rds/v1/tags/requests.go b/openstack/rds/v1/tags/requests.go new file mode 100644 index 000000000..7c5e45c09 --- /dev/null +++ b/openstack/rds/v1/tags/requests.go @@ -0,0 +1,79 @@ +package tags + +import ( + "github.com/huaweicloud/golangsdk" +) + +var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ + MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, +} + +// Tag is a structure of key value pair. +type CreateOpts struct { + //tag key + Key string `json:"key" required:"true"` + //tag value + Value string `json:"value" required:"true"` +} + +type DeleteOpts struct { + //tag key + Key string `json:"key" required:"true"` +} + +// ToCreateMap assembles a request body based on the contents of a +// CreateOpts. +func (opts CreateOpts) ToCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "tag") +} + +// ToDeleteMap assembles a request body based on the contents of a +// DeleteOpts. +func (opts DeleteOpts) ToDeleteMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "") +} + +type CreateOptsBuilder interface { + ToCreateMap() (map[string]interface{}, error) +} + +type DeleteOptsBuilder interface { + ToDeleteMap() (map[string]interface{}, error) +} + +// Create implements tag create request. +func Create(client *golangsdk.ServiceClient, id string, opts CreateOptsBuilder) (r CreateResult) { + b, err := opts.ToCreateMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Post(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, + }) + return +} + +// Delete implements tag delete request. +func Delete(client *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) { + b, err := opts.ToDeleteMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Post(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, + }) + return +} + +// Get implements tag get request. +func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { + _, r.Err = client.Get(resourceURL(client, id), &r.Body, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, + }) + return +} diff --git a/openstack/rds/v1/tags/results.go b/openstack/rds/v1/tags/results.go new file mode 100644 index 000000000..5bbd734be --- /dev/null +++ b/openstack/rds/v1/tags/results.go @@ -0,0 +1,39 @@ +package tags + +import ( + "github.com/huaweicloud/golangsdk" +) + +type Tag struct { + //tag key + Key string `json:"key"` + //tag value + Value string `json:"value"` +} + +type RespTags struct { + //contains list of tags, i.e.key value pair + Tags []Tag `json:"tags"` +} + +type commonResult struct { + golangsdk.Result +} + +type CreateResult struct { + commonResult +} + +type DeleteResult struct { + golangsdk.Result +} + +type GetResult struct { + commonResult +} + +func (r commonResult) Extract() (*RespTags, error) { + var response RespTags + err := r.ExtractInto(&response) + return &response, err +} diff --git a/openstack/rds/v1/tags/urls.go b/openstack/rds/v1/tags/urls.go new file mode 100644 index 000000000..e3f20e73e --- /dev/null +++ b/openstack/rds/v1/tags/urls.go @@ -0,0 +1,7 @@ +package tags + +import "github.com/huaweicloud/golangsdk" + +func resourceURL(c *golangsdk.ServiceClient, id string) string { + return c.ServiceURL(id, "tags") +} From a36d5b72059385b244741986317e58244fd65b7c Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Thu, 25 Apr 2019 10:25:33 +0800 Subject: [PATCH 2/3] Format code --- openstack/rds/v1/tags/requests.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openstack/rds/v1/tags/requests.go b/openstack/rds/v1/tags/requests.go index 7c5e45c09..b84fc04c1 100644 --- a/openstack/rds/v1/tags/requests.go +++ b/openstack/rds/v1/tags/requests.go @@ -49,7 +49,7 @@ func Create(client *golangsdk.ServiceClient, id string, opts CreateOptsBuilder) return } _, r.Err = client.Post(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ - OkCodes: []int{200}, + OkCodes: []int{200}, MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, }) return @@ -63,7 +63,7 @@ func Delete(client *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) return } _, r.Err = client.Post(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ - OkCodes: []int{200}, + OkCodes: []int{200}, MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, }) return From 3b3c577f2a8af9ec5a0f09fe0577cf4656714d0f Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Fri, 26 Apr 2019 14:15:31 +0800 Subject: [PATCH 3/3] Fix request issues --- openstack/rds/v1/tags/requests.go | 4 ++-- openstack/rds/v1/tags/results.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/openstack/rds/v1/tags/requests.go b/openstack/rds/v1/tags/requests.go index b84fc04c1..a5388f37d 100644 --- a/openstack/rds/v1/tags/requests.go +++ b/openstack/rds/v1/tags/requests.go @@ -48,7 +48,7 @@ func Create(client *golangsdk.ServiceClient, id string, opts CreateOptsBuilder) r.Err = err return } - _, r.Err = client.Post(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ + _, r.Err = client.Post(resourceURL(client, id), b, nil, &golangsdk.RequestOpts{ OkCodes: []int{200}, MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, }) @@ -62,7 +62,7 @@ func Delete(client *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) r.Err = err return } - _, r.Err = client.Post(resourceURL(client, id), b, &r.Body, &golangsdk.RequestOpts{ + _, r.Err = client.DeleteWithBody(resourceURL(client, id), b, &golangsdk.RequestOpts{ OkCodes: []int{200}, MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil, }) diff --git a/openstack/rds/v1/tags/results.go b/openstack/rds/v1/tags/results.go index 5bbd734be..6bd27da54 100644 --- a/openstack/rds/v1/tags/results.go +++ b/openstack/rds/v1/tags/results.go @@ -21,11 +21,11 @@ type commonResult struct { } type CreateResult struct { - commonResult + golangsdk.ErrResult } type DeleteResult struct { - golangsdk.Result + golangsdk.ErrResult } type GetResult struct {