|
| 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