Skip to content

Commit

Permalink
Merge pull request #13267 from gmazelier/lakeformation_resource
Browse files Browse the repository at this point in the history
Lake Formation Resource
  • Loading branch information
YakDriver authored Dec 11, 2020
2 parents 226e9a5 + 870c35d commit 29fcdc9
Show file tree
Hide file tree
Showing 5 changed files with 452 additions and 0 deletions.
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ func Provider() *schema.Provider {
"aws_kms_grant": resourceAwsKmsGrant(),
"aws_kms_key": resourceAwsKmsKey(),
"aws_kms_ciphertext": resourceAwsKmsCiphertext(),
"aws_lakeformation_resource": resourceAwsLakeFormationResource(),
"aws_lambda_alias": resourceAwsLambdaAlias(),
"aws_lambda_code_signing_config": resourceAwsLambdaCodeSigningConfig(),
"aws_lambda_event_source_mapping": resourceAwsLambdaEventSourceMapping(),
Expand Down
119 changes: 119 additions & 0 deletions aws/resource_aws_lakeformation_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package aws

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lakeformation"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceAwsLakeFormationResource() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLakeFormationResourceCreate,
Read: resourceAwsLakeFormationResourceRead,
Delete: resourceAwsLakeFormationResourceDelete,

Schema: map[string]*schema.Schema{
"last_modified": {
Type: schema.TypeString,
Computed: true,
},
"resource_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"role_arn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validateArn,
},
},
}
}

func resourceAwsLakeFormationResourceCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lakeformationconn
resourceArn := d.Get("resource_arn").(string)

input := &lakeformation.RegisterResourceInput{
ResourceArn: aws.String(resourceArn),
}

if v, ok := d.GetOk("role_arn"); ok {
input.RoleArn = aws.String(v.(string))
} else {
input.UseServiceLinkedRole = aws.Bool(true)
}

_, err := conn.RegisterResource(input)

if tfawserr.ErrCodeEquals(err, lakeformation.ErrCodeAlreadyExistsException) {
log.Printf("[WARN] Lake Formation Resource (%s) already exists", resourceArn)
} else if err != nil {
return fmt.Errorf("error registering Lake Formation Resource (%s): %s", resourceArn, err)
}

d.SetId(resourceArn)
return resourceAwsLakeFormationResourceRead(d, meta)
}

func resourceAwsLakeFormationResourceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lakeformationconn
resourceArn := d.Get("resource_arn").(string)

input := &lakeformation.DescribeResourceInput{
ResourceArn: aws.String(resourceArn),
}

output, err := conn.DescribeResource(input)

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, lakeformation.ErrCodeEntityNotFoundException) {
log.Printf("[WARN] Lake Formation Resource (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error getting Lake Formation Resource (%s): %w", d.Id(), err)
}

if output == nil || output.ResourceInfo == nil {
return fmt.Errorf("error getting Lake Formation Resource (%s): empty response", d.Id())
}

if err != nil {
return fmt.Errorf("error reading Lake Formation Resource (%s): %w", d.Id(), err)
}

// d.Set("resource_arn", output.ResourceInfo.ResourceArn) // output not including resource arn currently
d.Set("role_arn", output.ResourceInfo.RoleArn)
if output.ResourceInfo.LastModified != nil { // output not including last modified currently
d.Set("last_modified", output.ResourceInfo.LastModified.Format(time.RFC3339))
}

return nil
}

func resourceAwsLakeFormationResourceDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lakeformationconn
resourceArn := d.Get("resource_arn").(string)

input := &lakeformation.DeregisterResourceInput{
ResourceArn: aws.String(resourceArn),
}

_, err := conn.DeregisterResource(input)
if err != nil {
return fmt.Errorf("error deregistering Lake Formation Resource (%s): %w", d.Id(), err)
}

return nil
}
Loading

0 comments on commit 29fcdc9

Please sign in to comment.