/
resource_tags_data_source.go
60 lines (46 loc) · 1.39 KB
/
resource_tags_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package organizations
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/cloudposse/terraform-provider-awsutils/internal/conns"
tftags "github.com/cloudposse/terraform-provider-awsutils/internal/tags"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceResourceTags() *schema.Resource {
return &schema.Resource{
Read: dataSourceResourceTagsRead,
Schema: map[string]*schema.Schema{
"resource_id": {
Type: schema.TypeString,
Required: true,
},
"tags": tftags.TagsSchemaComputed(),
},
}
}
func dataSourceResourceTagsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).OrganizationsConn
resource_id := d.Get("resource_id").(string)
params := &organizations.ListTagsForResourceInput{
ResourceId: aws.String(resource_id),
}
var tags []*organizations.Tag
err := conn.ListTagsForResourcePages(params,
func(page *organizations.ListTagsForResourceOutput, lastPage bool) bool {
tags = append(tags, page.Tags...)
return !lastPage
})
if err != nil {
return fmt.Errorf("error listing tags for resource (%s): %w", resource_id, err)
}
d.SetId(resource_id)
if tags != nil {
if err := d.Set("tags", KeyValueTags(tags).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}
} else {
d.Set("tags", nil)
}
return nil
}