Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New resource: aws_lakeformation_lf_tag #19523

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/19523.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_lakeformation_lf_tag
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,7 @@ func Provider() *schema.Provider {
"aws_kms_replica_key": kms.ResourceReplicaKey(),

"aws_lakeformation_data_lake_settings": lakeformation.ResourceDataLakeSettings(),
"aws_lakeformation_lf_tag": lakeformation.ResourceLFTag(),
"aws_lakeformation_permissions": lakeformation.ResourcePermissions(),
"aws_lakeformation_resource": lakeformation.ResourceResource(),

Expand Down
5 changes: 5 additions & 0 deletions internal/service/lakeformation/lakeformation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func TestAccLakeFormation_serial(t *testing.T) {
"wildcardSelectOnly": testAccPermissions_twcWildcardSelectOnly,
"wildcardSelectPlus": testAccPermissions_twcWildcardSelectPlus,
},
"LFTags": {
"basic": testAccLFTag_basic,
"disappears": testAccLFTag_disappears,
"values": testAccLFTag_values,
},
}

for group, m := range testCases {
Expand Down
185 changes: 185 additions & 0 deletions internal/service/lakeformation/lf_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package lakeformation

import (
"fmt"
"log"
"regexp"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lakeformation"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
)

func ResourceLFTag() *schema.Resource {
return &schema.Resource{
Create: resourceLFTagCreate,
Read: resourceLFTagRead,
Update: resourceLFTagUpdate,
Delete: resourceLFTagDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"catalog_id": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Computed: true,
},
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 128),
},
"values": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
MaxItems: 15,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateLFTagValues(),
},
Set: schema.HashString,
},
},
}
}

func resourceLFTagCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).LakeFormationConn

tagKey := d.Get("key").(string)
tagValues := d.Get("values").(*schema.Set)

var catalogID string
if v, ok := d.GetOk("catalog_id"); ok {
catalogID = v.(string)
} else {
catalogID = meta.(*conns.AWSClient).AccountID
}

input := &lakeformation.CreateLFTagInput{
CatalogId: aws.String(catalogID),
TagKey: aws.String(tagKey),
TagValues: flex.ExpandStringSet(tagValues),
}

_, err := conn.CreateLFTag(input)
if err != nil {
return fmt.Errorf("Error creating Lake Formation LF-Tag: %w", err)
}

d.SetId(fmt.Sprintf("%s:%s", catalogID, tagKey))

return resourceLFTagRead(d, meta)
}

func resourceLFTagRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).LakeFormationConn

catalogID, tagKey, err := ReadLFTagID(d.Id())
if err != nil {
return err
}

input := &lakeformation.GetLFTagInput{
CatalogId: aws.String(catalogID),
TagKey: aws.String(tagKey),
}

output, err := conn.GetLFTag(input)
if err != nil {
if tfawserr.ErrCodeEquals(err, lakeformation.ErrCodeEntityNotFoundException) {
log.Printf("[WARN] Lake Formation LF-Tag (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

return fmt.Errorf("Error reading Lake Formation LF-Tag: %s", err.Error())
}

d.Set("key", output.TagKey)
d.Set("values", flex.FlattenStringSet(output.TagValues))
d.Set("catalog_id", output.CatalogId)

return nil
}

func resourceLFTagUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).LakeFormationConn

catalogID, tagKey, err := ReadLFTagID(d.Id())
if err != nil {
return err
}

o, n := d.GetChange("values")
os := o.(*schema.Set)
ns := n.(*schema.Set)
toAdd := flex.ExpandStringSet(ns.Difference(os))
toDelete := flex.ExpandStringSet(os.Difference(ns))

input := &lakeformation.UpdateLFTagInput{
CatalogId: aws.String(catalogID),
TagKey: aws.String(tagKey),
}

if len(toAdd) > 0 {
input.TagValuesToAdd = toAdd
}

if len(toDelete) > 0 {
input.TagValuesToDelete = toDelete
}

_, err = conn.UpdateLFTag(input)
if err != nil {
return fmt.Errorf("Error updating Lake Formation LF-Tag (%s): %w", d.Id(), err)
}

return resourceLFTagRead(d, meta)
}

func resourceLFTagDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).LakeFormationConn

catalogID, tagKey, err := ReadLFTagID(d.Id())
if err != nil {
return err
}

input := &lakeformation.DeleteLFTagInput{
CatalogId: aws.String(catalogID),
TagKey: aws.String(tagKey),
}

_, err = conn.DeleteLFTag(input)
if err != nil {
return fmt.Errorf("Error deleting Lake Formation LF-Tag (%s): %w", d.Id(), err)
}

return nil
}

func ReadLFTagID(id string) (catalogID string, tagKey string, err error) {
idParts := strings.Split(id, ":")
if len(idParts) != 2 {
return "", "", fmt.Errorf("Unexpected format of ID (%q), expected CATALOG-ID:TAG-KEY", id)
}
return idParts[0], idParts[1], nil
}

func validateLFTagValues() schema.SchemaValidateFunc {
return validation.All(
validation.StringLenBetween(1, 255),
validation.StringMatch(regexp.MustCompile(`^([\p{L}\p{Z}\p{N}_.:\*\/=+\-@%]*)$`), ""),
)
}
Loading