Skip to content

Commit

Permalink
Fix #3287: retrieve access_tags using ibm_resource_tag datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
kavya498 authored and hkantare committed Dec 15, 2021
1 parent 3fea8f1 commit 45eff95
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
25 changes: 20 additions & 5 deletions ibm/data_source_ibm_resource_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ibm

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand All @@ -16,7 +17,7 @@ func dataSourceIBMResourceTag() *schema.Resource {
Schema: map[string]*schema.Schema{
"resource_id": {
Type: schema.TypeString,
Required: true,
Optional: true,
ValidateFunc: InvokeValidator("ibm_resource_tag", resourceID),
Description: "CRN of the resource on which the tags should be attached",
},
Expand All @@ -32,27 +33,41 @@ func dataSourceIBMResourceTag() *schema.Resource {
Optional: true,
Description: "Resource type on which the tags should be fetched",
},
"tag_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: InvokeValidator("ibm_resource_tag", "tag_type"),
Description: "Tag type on which the tags should be fetched",
Default: "user",
},
},
}
}

func dataSourceIBMResourceTagRead(d *schema.ResourceData, meta interface{}) error {
var rID, rType string
rID = d.Get("resource_id").(string)

if r, ok := d.GetOk("resource_id"); ok && r != nil {
rID = r.(string)
}
if v, ok := d.GetOk(resourceType); ok && v != nil {
rType = v.(string)
}
tType := ""
if t, ok := d.GetOk("tag_type"); ok && t != nil {
tType = t.(string)
}

tags, err := GetGlobalTagsUsingCRN(meta, rID, rType, "")
tags, err := GetGlobalTagsUsingCRN(meta, rID, rType, tType)
if err != nil {
return fmt.Errorf(
"Error on get of resource tags (%s) tags: %s", d.Id(), err)
}

d.SetId(rID)
d.SetId(time.Now().UTC().String())
d.Set("resource_id", rID)
d.Set("resource_type", rType)
d.Set("tags", tags)

d.Set("tag_type", tType)
return nil
}
25 changes: 25 additions & 0 deletions ibm/data_source_ibm_resource_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ func TestAccResourceTagDataSource_basic(t *testing.T) {
},
})
}
func TestAccResourceTagDataSourceTagType(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckResourceTagwithTagType(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_resource_tag.access_tags", "id"),
resource.TestCheckResourceAttrSet("data.ibm_resource_tag.tags", "id"),
),
},
},
})
}
func testAccCheckResourceTagwithTagType() string {
return fmt.Sprintf(`
data "ibm_resource_tag" "access_tags" {
tag_type ="access"
}
data "ibm_resource_tag" "tags" {
}
`)
}

func testAccCheckResourceTagReadDataSource(name, managed_from string) string {
return fmt.Sprintf(`
Expand Down
11 changes: 9 additions & 2 deletions ibm/resource_ibm_resource_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func resourceIBMResourceTag() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validateAllowedStringValue([]string{"service", "access", "user"}),
ValidateFunc: InvokeValidator("ibm_resource_tag", "tag_type"),
Description: "Type of the tag. Only allowed values are: user, or service or access (default value : user)",
},
acccountID: {
Expand All @@ -78,7 +78,7 @@ func resourceIBMResourceTag() *schema.Resource {
}

func resourceIBMResourceTagValidator() *ResourceValidator {

tagTypeAllowedValues := "service,access,user"
validateSchema := make([]ValidateSchema, 0)

validateSchema = append(validateSchema,
Expand All @@ -99,6 +99,13 @@ func resourceIBMResourceTagValidator() *ResourceValidator {
Regexp: `^[A-Za-z0-9:_ .-]+$`,
MinValueLength: 1,
MaxValueLength: 128})
validateSchema = append(validateSchema,
ValidateSchema{
Identifier: "tag_type",
ValidateFunctionIdentifier: ValidateAllowedStringValue,
Type: TypeString,
Optional: true,
AllowedValues: tagTypeAllowedValues})

ibmResourceTagValidator := ResourceValidator{ResourceName: "ibm_resource_tag", Schema: validateSchema}
return &ibmResourceTagValidator
Expand Down
4 changes: 3 additions & 1 deletion ibm/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,9 @@ func GetGlobalTagsUsingCRN(meta interface{}, resourceID, resourceType, tagType s
}

ListTagsOptions := &globaltaggingv1.ListTagsOptions{}
ListTagsOptions.AttachedTo = &resourceID
if resourceID != "" {
ListTagsOptions.AttachedTo = &resourceID
}
ListTagsOptions.Providers = providers
if len(tagType) > 0 {
ListTagsOptions.TagType = ptrToString(tagType)
Expand Down
21 changes: 17 additions & 4 deletions website/docs/d/resource_tag.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ subcategory: "Global Tagging"
layout: "ibm"
page_title: "IBM : resource_tag"
description: |-
Manages resource tags.
Retrieve available tags on the account.
---

# ibm_resource_tag

Retreive information about an existing resource tags as a read-only data source. For more information, about resource tags, see [controlling access to resources by using tags](https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial).
Retreive information about an existing resource or access tags as a read-only data source. For more information, about resource tags, see [controlling access to resources by using tags](https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial).

## Example usage

Expand All @@ -24,13 +24,26 @@ data "ibm_resource_tag" "read_tag" {
resource_id = data.ibm_satellite_location.location.crn
}
```
### Retrieve access tags

```terraform
data "ibm_resource_tag" "access_tags" {
tag_type ="access"
}
```
### Retrieve user tags

```terraform
data "ibm_resource_tag" "user_tags" {
}
```

## Argument reference
Review the argument references that you can specify for your data source.

- `resource_id` - (Required, String) The CRN of the resource on which the tags should be attached.
- `resource_id` - (Optional, String) The CRN of the resource on which the tags should be attached.
- `resource_type` - (Optional, String) The resource type on which the tags to be attached.

- `tag_type` - (Optional, String) Type of the tag. Supported values are: `user`, `service`, or `access`. Default: `user`
## Attributes reference
In addition to all argument reference list, you can access the following attribute references after your data source is created.

Expand Down

0 comments on commit 45eff95

Please sign in to comment.