Skip to content

Commit

Permalink
fix(Global Tagging): add 'update' option to attach_tag operation (#337)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Leoni <fabrizio_leoni@it.ibm.com>
  • Loading branch information
fabrizio-leoni committed Jun 21, 2024
1 parent 5349179 commit fb7c355
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
24 changes: 21 additions & 3 deletions globaltaggingv1/global_tagging_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ import (
// create tags in two formats: `key:value` or `label`. The tagging API supports three types of tag: `user` `service`,
// and `access` tags. `service` tags cannot be attached to IMS resources. `service` tags must be in the form
// `service_prefix:tag_label` where `service_prefix` identifies the Service owning the tag. `access` tags cannot be
// attached to IMS and Cloud Foundry resources. They must be in the form `key:value`.
// attached to IMS and Cloud Foundry resources. They must be in the form `key:value`. You can replace all resource's
// tags using the `replace` query parameter in the attach operation. You can update the `value` of a resource's tag in
// the format `key:value`, using the `update` query parameter in the attach operation.
//
// API Version: 1.2.0
type GlobalTaggingV1 struct {
Expand Down Expand Up @@ -582,6 +584,9 @@ func (globalTagging *GlobalTaggingV1) AttachTagWithContext(ctx context.Context,
if attachTagOptions.Replace != nil {
builder.AddQuery("replace", fmt.Sprint(*attachTagOptions.Replace))
}
if attachTagOptions.Update != nil {
builder.AddQuery("update", fmt.Sprint(*attachTagOptions.Update))
}

body := make(map[string]interface{})
if attachTagOptions.Resources != nil {
Expand Down Expand Up @@ -756,10 +761,17 @@ type AttachTagOptions struct {
// for IMS resources.
TagType *string `json:"tag_type,omitempty"`

// Flag to request replacement of all attached tags. Set 'true' if you want to replace all the list of tags attached to
// the resource. Default value is false.
// Flag to request replacement of all attached tags. Set `true` if you want to replace all tags attached to the
// resource with the current ones. Default value is false.
Replace *bool `json:"replace,omitempty"`

// Flag to request update of attached tags in the format `key:value`. Here's how it works for each tag in the request
// body: If the tag to attach is in the format `key:value`, the System will atomically detach all existing tags
// starting with `key:` and attach the new `key:value` tag. If no such tags exist, a new `key:value` tag will be
// attached. If the tag is not in the `key:value` format (e.g., a simple label), the System will attach the label as
// usual. The update query parameter is available for user and access management tags, but not for service tags.
Update *bool `json:"update,omitempty"`

// Allows users to set headers on API requests
Headers map[string]string
}
Expand Down Expand Up @@ -828,6 +840,12 @@ func (_options *AttachTagOptions) SetReplace(replace bool) *AttachTagOptions {
return _options
}

// SetUpdate : Allow user to set Update
func (_options *AttachTagOptions) SetUpdate(update bool) *AttachTagOptions {
_options.Update = core.BoolPtr(update)
return _options
}

// SetHeaders : Allow user to set Headers
func (options *AttachTagOptions) SetHeaders(param map[string]string) *AttachTagOptions {
options.Headers = param
Expand Down
10 changes: 10 additions & 0 deletions globaltaggingv1/global_tagging_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ var _ = Describe(`GlobalTaggingV1`, func() {
Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"}))
Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"}))
// TODO: Add check for replace query parameter
// TODO: Add check for update query parameter
res.Header().Set("Content-type", "application/json")
res.WriteHeader(200)
fmt.Fprint(res, `} this is not valid json {`)
Expand Down Expand Up @@ -1291,6 +1292,7 @@ var _ = Describe(`GlobalTaggingV1`, func() {
attachTagOptionsModel.AccountID = core.StringPtr("testString")
attachTagOptionsModel.TagType = core.StringPtr("user")
attachTagOptionsModel.Replace = core.BoolPtr(false)
attachTagOptionsModel.Update = core.BoolPtr(false)
attachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"}
// Expect response parsing to fail since we are receiving a text/plain response
result, response, operationErr := globalTaggingService.AttachTag(attachTagOptionsModel)
Expand Down Expand Up @@ -1344,6 +1346,7 @@ var _ = Describe(`GlobalTaggingV1`, func() {
Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"}))
Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"}))
// TODO: Add check for replace query parameter
// TODO: Add check for update query parameter
// Sleep a short time to support a timeout test
time.Sleep(100 * time.Millisecond)

Expand Down Expand Up @@ -1377,6 +1380,7 @@ var _ = Describe(`GlobalTaggingV1`, func() {
attachTagOptionsModel.AccountID = core.StringPtr("testString")
attachTagOptionsModel.TagType = core.StringPtr("user")
attachTagOptionsModel.Replace = core.BoolPtr(false)
attachTagOptionsModel.Update = core.BoolPtr(false)
attachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"}

// Invoke operation with a Context to test a timeout error
Expand Down Expand Up @@ -1436,6 +1440,7 @@ var _ = Describe(`GlobalTaggingV1`, func() {
Expect(req.URL.Query()["account_id"]).To(Equal([]string{"testString"}))
Expect(req.URL.Query()["tag_type"]).To(Equal([]string{"user"}))
// TODO: Add check for replace query parameter
// TODO: Add check for update query parameter
// Set mock response
res.Header().Set("Content-type", "application/json")
res.WriteHeader(200)
Expand Down Expand Up @@ -1471,6 +1476,7 @@ var _ = Describe(`GlobalTaggingV1`, func() {
attachTagOptionsModel.AccountID = core.StringPtr("testString")
attachTagOptionsModel.TagType = core.StringPtr("user")
attachTagOptionsModel.Replace = core.BoolPtr(false)
attachTagOptionsModel.Update = core.BoolPtr(false)
attachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"}

// Invoke operation with valid options model (positive test)
Expand Down Expand Up @@ -1503,6 +1509,7 @@ var _ = Describe(`GlobalTaggingV1`, func() {
attachTagOptionsModel.AccountID = core.StringPtr("testString")
attachTagOptionsModel.TagType = core.StringPtr("user")
attachTagOptionsModel.Replace = core.BoolPtr(false)
attachTagOptionsModel.Update = core.BoolPtr(false)
attachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"}
// Invoke operation with empty URL (negative test)
err := globalTaggingService.SetServiceURL("")
Expand Down Expand Up @@ -1556,6 +1563,7 @@ var _ = Describe(`GlobalTaggingV1`, func() {
attachTagOptionsModel.AccountID = core.StringPtr("testString")
attachTagOptionsModel.TagType = core.StringPtr("user")
attachTagOptionsModel.Replace = core.BoolPtr(false)
attachTagOptionsModel.Update = core.BoolPtr(false)
attachTagOptionsModel.Headers = map[string]string{"x-custom-header": "x-custom-value"}

// Invoke operation
Expand Down Expand Up @@ -1914,6 +1922,7 @@ var _ = Describe(`GlobalTaggingV1`, func() {
attachTagOptionsModel.SetAccountID("testString")
attachTagOptionsModel.SetTagType("user")
attachTagOptionsModel.SetReplace(false)
attachTagOptionsModel.SetUpdate(false)
attachTagOptionsModel.SetHeaders(map[string]string{"foo": "bar"})
Expect(attachTagOptionsModel).ToNot(BeNil())
Expect(attachTagOptionsModel.Resources).To(Equal([]globaltaggingv1.Resource{*resourceModel}))
Expand All @@ -1924,6 +1933,7 @@ var _ = Describe(`GlobalTaggingV1`, func() {
Expect(attachTagOptionsModel.AccountID).To(Equal(core.StringPtr("testString")))
Expect(attachTagOptionsModel.TagType).To(Equal(core.StringPtr("user")))
Expect(attachTagOptionsModel.Replace).To(Equal(core.BoolPtr(false)))
Expect(attachTagOptionsModel.Update).To(Equal(core.BoolPtr(false)))
Expect(attachTagOptionsModel.Headers).To(Equal(map[string]string{"foo": "bar"}))
})
It(`Invoke NewCreateTagOptions successfully`, func() {
Expand Down

0 comments on commit fb7c355

Please sign in to comment.