Skip to content

Commit 29fcdc9

Browse files
authored
Merge pull request #13267 from gmazelier/lakeformation_resource
Lake Formation Resource
2 parents 226e9a5 + 870c35d commit 29fcdc9

5 files changed

Lines changed: 452 additions & 0 deletions

aws/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ func Provider() *schema.Provider {
742742
"aws_kms_grant": resourceAwsKmsGrant(),
743743
"aws_kms_key": resourceAwsKmsKey(),
744744
"aws_kms_ciphertext": resourceAwsKmsCiphertext(),
745+
"aws_lakeformation_resource": resourceAwsLakeFormationResource(),
745746
"aws_lambda_alias": resourceAwsLambdaAlias(),
746747
"aws_lambda_code_signing_config": resourceAwsLambdaCodeSigningConfig(),
747748
"aws_lambda_event_source_mapping": resourceAwsLambdaEventSourceMapping(),
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"time"
7+
8+
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go/service/lakeformation"
10+
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
)
13+
14+
func resourceAwsLakeFormationResource() *schema.Resource {
15+
return &schema.Resource{
16+
Create: resourceAwsLakeFormationResourceCreate,
17+
Read: resourceAwsLakeFormationResourceRead,
18+
Delete: resourceAwsLakeFormationResourceDelete,
19+
20+
Schema: map[string]*schema.Schema{
21+
"last_modified": {
22+
Type: schema.TypeString,
23+
Computed: true,
24+
},
25+
"resource_arn": {
26+
Type: schema.TypeString,
27+
Required: true,
28+
ForceNew: true,
29+
ValidateFunc: validateArn,
30+
},
31+
"role_arn": {
32+
Type: schema.TypeString,
33+
Optional: true,
34+
Computed: true,
35+
ForceNew: true,
36+
ValidateFunc: validateArn,
37+
},
38+
},
39+
}
40+
}
41+
42+
func resourceAwsLakeFormationResourceCreate(d *schema.ResourceData, meta interface{}) error {
43+
conn := meta.(*AWSClient).lakeformationconn
44+
resourceArn := d.Get("resource_arn").(string)
45+
46+
input := &lakeformation.RegisterResourceInput{
47+
ResourceArn: aws.String(resourceArn),
48+
}
49+
50+
if v, ok := d.GetOk("role_arn"); ok {
51+
input.RoleArn = aws.String(v.(string))
52+
} else {
53+
input.UseServiceLinkedRole = aws.Bool(true)
54+
}
55+
56+
_, err := conn.RegisterResource(input)
57+
58+
if tfawserr.ErrCodeEquals(err, lakeformation.ErrCodeAlreadyExistsException) {
59+
log.Printf("[WARN] Lake Formation Resource (%s) already exists", resourceArn)
60+
} else if err != nil {
61+
return fmt.Errorf("error registering Lake Formation Resource (%s): %s", resourceArn, err)
62+
}
63+
64+
d.SetId(resourceArn)
65+
return resourceAwsLakeFormationResourceRead(d, meta)
66+
}
67+
68+
func resourceAwsLakeFormationResourceRead(d *schema.ResourceData, meta interface{}) error {
69+
conn := meta.(*AWSClient).lakeformationconn
70+
resourceArn := d.Get("resource_arn").(string)
71+
72+
input := &lakeformation.DescribeResourceInput{
73+
ResourceArn: aws.String(resourceArn),
74+
}
75+
76+
output, err := conn.DescribeResource(input)
77+
78+
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, lakeformation.ErrCodeEntityNotFoundException) {
79+
log.Printf("[WARN] Lake Formation Resource (%s) not found, removing from state", d.Id())
80+
d.SetId("")
81+
return nil
82+
}
83+
84+
if err != nil {
85+
return fmt.Errorf("error getting Lake Formation Resource (%s): %w", d.Id(), err)
86+
}
87+
88+
if output == nil || output.ResourceInfo == nil {
89+
return fmt.Errorf("error getting Lake Formation Resource (%s): empty response", d.Id())
90+
}
91+
92+
if err != nil {
93+
return fmt.Errorf("error reading Lake Formation Resource (%s): %w", d.Id(), err)
94+
}
95+
96+
// d.Set("resource_arn", output.ResourceInfo.ResourceArn) // output not including resource arn currently
97+
d.Set("role_arn", output.ResourceInfo.RoleArn)
98+
if output.ResourceInfo.LastModified != nil { // output not including last modified currently
99+
d.Set("last_modified", output.ResourceInfo.LastModified.Format(time.RFC3339))
100+
}
101+
102+
return nil
103+
}
104+
105+
func resourceAwsLakeFormationResourceDelete(d *schema.ResourceData, meta interface{}) error {
106+
conn := meta.(*AWSClient).lakeformationconn
107+
resourceArn := d.Get("resource_arn").(string)
108+
109+
input := &lakeformation.DeregisterResourceInput{
110+
ResourceArn: aws.String(resourceArn),
111+
}
112+
113+
_, err := conn.DeregisterResource(input)
114+
if err != nil {
115+
return fmt.Errorf("error deregistering Lake Formation Resource (%s): %w", d.Id(), err)
116+
}
117+
118+
return nil
119+
}

0 commit comments

Comments
 (0)