Skip to content

Commit

Permalink
tag Attach - added new replace flag (IBM-Cloud#5192)
Browse files Browse the repository at this point in the history
* updated code

* added UT

* added UT

* fix typo

* added DiffSuppressFunc: flex.ApplyOnce,

* added Default:false,

* added Default:false,

* updated documentation

* updated documentation

* check acceptance test

* check acceptance test

* check acceptance test

* check acceptance test
  • Loading branch information
francescadecicco authored Mar 14, 2024
1 parent 75e79b7 commit e75d10e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ require (
github.com/IBM/event-notifications-go-admin-sdk v0.4.0
github.com/IBM/eventstreams-go-sdk v1.4.0
github.com/IBM/go-sdk-core/v3 v3.2.4
github.com/IBM/go-sdk-core/v5 v5.15.1
github.com/IBM/go-sdk-core/v5 v5.15.3
github.com/IBM/ibm-cos-sdk-go v1.10.0
github.com/IBM/ibm-cos-sdk-go-config v1.2.0
github.com/IBM/ibm-hpcs-tke-sdk v0.0.0-20211109141421-a4b61b05f7d1
github.com/IBM/ibm-hpcs-uko-sdk v0.0.20-beta
github.com/IBM/keyprotect-go-client v0.12.2
github.com/IBM/networking-go-sdk v0.45.0
github.com/IBM/platform-services-go-sdk v0.59.0
github.com/IBM/platform-services-go-sdk v0.60.0
github.com/IBM/project-go-sdk v0.2.1
github.com/IBM/push-notifications-go-sdk v0.0.0-20210310100607-5790b96c47f5
github.com/IBM/scc-go-sdk/v5 v5.1.5
Expand All @@ -38,7 +38,7 @@ require (
github.com/apache/openwhisk-client-go v0.0.0-20200201143223-a804fb82d105
github.com/apparentlymart/go-cidr v1.1.0
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/go-openapi/strfmt v0.22.0
github.com/go-openapi/strfmt v0.22.1
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
Expand Down Expand Up @@ -202,7 +202,7 @@ require (
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
go.mongodb.org/mongo-driver v1.14.0 // indirect; indirec
go.mongodb.org/mongo-driver v1.14.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.uber.org/ratelimit v0.2.0 // indirect
Expand Down
14 changes: 7 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ github.com/IBM/go-sdk-core/v5 v5.7.0/go.mod h1:+YbdhrjCHC84ls4MeBp+Hj4NZCni+tDAc
github.com/IBM/go-sdk-core/v5 v5.9.2/go.mod h1:YlOwV9LeuclmT/qi/LAK2AsobbAP42veV0j68/rlZsE=
github.com/IBM/go-sdk-core/v5 v5.9.5/go.mod h1:YlOwV9LeuclmT/qi/LAK2AsobbAP42veV0j68/rlZsE=
github.com/IBM/go-sdk-core/v5 v5.10.2/go.mod h1:WZPFasUzsKab/2mzt29xPcfruSk5js2ywAPwW4VJjdI=
github.com/IBM/go-sdk-core/v5 v5.15.1 h1:XOzNZbBgnlxOGK1JMMBtZJYSVguK4TFPJiYutuzFmdA=
github.com/IBM/go-sdk-core/v5 v5.15.1/go.mod h1:so2mbdIgSp6X8Zm/qLV+whdchTGgi04c3j4xFMsqlCc=
github.com/IBM/go-sdk-core/v5 v5.15.3 h1:yBSSYLuQSO9Ip+j3mONsTcymoYQyxarQ6rh3aU9cVt8=
github.com/IBM/go-sdk-core/v5 v5.15.3/go.mod h1:ee+AZaB15yUwZigJdRCwZZ3u7HIvEQzxNUdxVpnJHY8=
github.com/IBM/ibm-cos-sdk-go v1.3.1/go.mod h1:YLBAYobEA8bD27P7xpMwSQeNQu6W3DNBtBComXrRzRY=
github.com/IBM/ibm-cos-sdk-go v1.10.0 h1:/2VIev2/jBei39OqU2+nSZQnoWJ+KtkiSAIDkqsd7uU=
github.com/IBM/ibm-cos-sdk-go v1.10.0/go.mod h1:C8KRTRaoD3CWPPBOa6FCOpdh0ZMlUjKAAA4i3F+Q/sc=
Expand All @@ -158,8 +158,8 @@ github.com/IBM/mqcloud-go-sdk v0.0.4 h1:gqMpoU5a0qJ0GETG4PQrkgeEEoaQLvbxRJnEe6yt
github.com/IBM/mqcloud-go-sdk v0.0.4/go.mod h1:gQptHC6D+rxfg0muRFFGvTDmvl4YfiDE0uXkaRRewRk=
github.com/IBM/networking-go-sdk v0.45.0 h1:tYgDhVDpgKvELNY7tcodbZ4ny9fatpEWM6PwtQcDe20=
github.com/IBM/networking-go-sdk v0.45.0/go.mod h1:NnJPA1e5GWr5opJe+5Hs6e1G6RcBIFz64TrkZsdnSp8=
github.com/IBM/platform-services-go-sdk v0.59.0 h1:FSRM3oKHxzShLCsIIb6Dl+JSaVOXpBWnfWFITJR6DDk=
github.com/IBM/platform-services-go-sdk v0.59.0/go.mod h1:+U6Kg7o5u/Bh4ZkLxjymSgfdpVsaWAtsMtzhwclUry0=
github.com/IBM/platform-services-go-sdk v0.60.0 h1:DXCp5hAtFO6quUmb5qKeosQw0ln/fYE5nrKjgXbiSBU=
github.com/IBM/platform-services-go-sdk v0.60.0/go.mod h1:fcmUb29QKLjMM0UWrR5bAidC7qfKWrf96H0xxmGJHdE=
github.com/IBM/project-go-sdk v0.2.1 h1:Xo7ITrfyfVm0eCsaC2SADlhcEjqjx9rtU37fwnzGMCI=
github.com/IBM/project-go-sdk v0.2.1/go.mod h1:lqe0M4cKvABI1iHR1b+KfasVcxQL6nl2VJ8eOyQs8Ig=
github.com/IBM/push-notifications-go-sdk v0.0.0-20210310100607-5790b96c47f5 h1:NPUhkoOCRuv3OFWt19PmwjXGGTKlvmbuPg9fUrBUNe4=
Expand Down Expand Up @@ -552,8 +552,8 @@ github.com/go-openapi/strfmt v0.21.1/go.mod h1:I/XVKeLc5+MM5oPNN7P6urMOpuLXEcNrC
github.com/go-openapi/strfmt v0.21.2/go.mod h1:I/XVKeLc5+MM5oPNN7P6urMOpuLXEcNrCX/rPGuWb0k=
github.com/go-openapi/strfmt v0.21.3/go.mod h1:k+RzNO0Da+k3FrrynSNN8F7n/peCmQQqbbXjtDfvmGg=
github.com/go-openapi/strfmt v0.21.7/go.mod h1:adeGTkxE44sPyLk0JV235VQAO/ZXUr8KAzYjclFs3ew=
github.com/go-openapi/strfmt v0.22.0 h1:Ew9PnEYc246TwrEspvBdDHS4BVKXy/AOVsfqGDgAcaI=
github.com/go-openapi/strfmt v0.22.0/go.mod h1:HzJ9kokGIju3/K6ap8jL+OlGAbjpSv27135Yr9OivU4=
github.com/go-openapi/strfmt v0.22.1 h1:5Ky8cybT4576C6Ffc+8gYji/wRXCo6Ozm8RaWjPI6jc=
github.com/go-openapi/strfmt v0.22.1/go.mod h1:OfVoytIXJasDkkGvkb1Cceb3BPyMOwk1FgmyyEw7NYg=
github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I=
github.com/go-openapi/swag v0.17.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg=
github.com/go-openapi/swag v0.18.0/go.mod h1:AByQ+nYG6gQg71GINrmuDXCPWdL640yX49/kXLo40Tg=
Expand Down Expand Up @@ -1280,7 +1280,7 @@ github.com/onsi/gomega v1.27.7/go.mod h1:1p8OOlwo2iUUDsHnOrjE5UKYJ+e3W8eQ3qSlRah
github.com/onsi/gomega v1.27.8/go.mod h1:2J8vzI/s+2shY9XHRApDkdgPo1TKT7P2u6fXeJKFnNQ=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
Expand Down
20 changes: 17 additions & 3 deletions ibm/service/globaltagging/resource_ibm_resource_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ const (
tags = "tags"
resourceType = "resource_type"
tagType = "tag_type"
acccountID = "acccount_id"
accountID = "account_id"
service = "service"
replace = "replace"
)

func ResourceIBMResourceTag() *schema.Resource {
Expand Down Expand Up @@ -68,11 +69,18 @@ func ResourceIBMResourceTag() *schema.Resource {
ValidateFunc: validate.InvokeValidator("ibm_resource_tag", tagType),
Description: "Type of the tag. Only allowed values are: user, or service or access (default value : user)",
},
acccountID: {
accountID: {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the account that owns the resources to be tagged (required if tag-type is set to service)",
},
replace: {
Type: schema.TypeBool,
DiffSuppressFunc: flex.ApplyOnce,
Optional: true,
Default: false,
Description: "If true, it indicates that the attaching operation is a replacement operation",
},
},
}
}
Expand Down Expand Up @@ -154,6 +162,12 @@ func resourceIBMResourceTagCreate(d *schema.ResourceData, meta interface{}) erro
}
}

if v, ok := d.GetOk(replace); ok && v != nil {
replace := v.(bool)
AttachTagOptions.Replace = &replace

}

// Fetch tags from schematics only if they are user tags
if strings.TrimSpace(tagType) == "" || tagType == "user" {
schematicTags := os.Getenv("IC_ENV_TAGS")
Expand Down Expand Up @@ -207,7 +221,7 @@ func resourceIBMResourceTagRead(d *schema.ResourceData, meta interface{}) error
tType = v.(string)

if tType == service {
d.Set(acccountID, acctID)
d.Set(accountID, acctID)
}
}

Expand Down
43 changes: 43 additions & 0 deletions ibm/service/globaltagging/resource_ibm_resource_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,46 @@ func testAccCheckResourceTagCreate(name, managed_from string) string {
}
`, name, managed_from)
}

func TestAccResourceTag_replace_Basic(t *testing.T) {
name := fmt.Sprintf("tf-cos-%d", acctest.RandIntRange(10, 100))

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{

{
Config: testAccCheckResourceTagCreate_replace(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckResourceTagExists("ibm_resource_tag.tag"),
resource.TestCheckResourceAttr("ibm_resource_tag.tag", "tags.#", "1"),
),
},
{
ResourceName: "ibm_resource_tag.tag",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckResourceTagCreate_replace(name string) string {
return fmt.Sprintf(`
resource "ibm_resource_instance" "resource_1" {
name = "%s"
service = "cloud-object-storage"
plan = "lite"
location = "global"
}
resource "ibm_resource_tag" "tag" {
resource_id = resource.ibm_resource_instance.resource_1.crn
tags = ["test:test"]
replace = true
}
`, name)
}
1 change: 1 addition & 0 deletions website/docs/r/resource_tag.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Review the argument references that you can specify for your resource.
- `resource_type` - (Optional, String) The resource type on which the tags should be attached.
- `tag_type` - (Optional, String) Type of the tag. Supported values are: `user`, `service`, or `access`. The default value is user.
- `tags` - (Required, Array of strings) List of tags associated with resource instance.
- `replace` - (Optional, Bool) If true, it indicates that the attaching operation is a replacement operation

## Attributes reference
In addition to all argument reference list, you can access the following attribute reference after your resource is created.
Expand Down

0 comments on commit e75d10e

Please sign in to comment.