-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12370 from thehunt33r/Add-db_cluster_role_associa…
…tion_resource r/aws_rds_cluster_role_association - New resource
- Loading branch information
Showing
12 changed files
with
549 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```release-note:new-resource | ||
aws_rds_cluster_role_association | ||
``` | ||
|
||
```release-note:enhancement | ||
aws_rds_cluster: Set `iam_roles` as Computed to prevent drift when the `aws_rds_cluster_role_association` resource is used | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package rds | ||
|
||
const ( | ||
DBClusterRoleStatusActive = "ACTIVE" | ||
DBClusterRoleStatusDeleted = "DELETED" | ||
DBClusterRoleStatusPending = "PENDING" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/rds" | ||
"github.com/hashicorp/aws-sdk-go-base/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
tfrds "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/rds" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/rds/finder" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/rds/waiter" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" | ||
) | ||
|
||
func resourceAwsRDSClusterRoleAssociation() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsRDSClusterRoleAssociationCreate, | ||
Read: resourceAwsRDSClusterRoleAssociationRead, | ||
Delete: resourceAwsRDSClusterRoleAssociationDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"db_cluster_identifier": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"feature_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"role_arn": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateArn, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsRDSClusterRoleAssociationCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).rdsconn | ||
|
||
dbClusterID := d.Get("db_cluster_identifier").(string) | ||
roleARN := d.Get("role_arn").(string) | ||
input := &rds.AddRoleToDBClusterInput{ | ||
DBClusterIdentifier: aws.String(dbClusterID), | ||
FeatureName: aws.String(d.Get("feature_name").(string)), | ||
RoleArn: aws.String(roleARN), | ||
} | ||
|
||
log.Printf("[DEBUG] Creating RDS DB Cluster IAM Role Association: %s", input) | ||
_, err := conn.AddRoleToDBCluster(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error creating RDS DB Cluster (%s) IAM Role (%s) Association: %w", dbClusterID, roleARN, err) | ||
} | ||
|
||
d.SetId(tfrds.ClusterRoleAssociationCreateResourceID(dbClusterID, roleARN)) | ||
|
||
_, err = waiter.DBClusterRoleAssociationCreated(conn, dbClusterID, roleARN) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error waiting for RDS DB Cluster (%s) IAM Role (%s) Association to create: %w", dbClusterID, roleARN, err) | ||
} | ||
|
||
return resourceAwsRDSClusterRoleAssociationRead(d, meta) | ||
} | ||
|
||
func resourceAwsRDSClusterRoleAssociationRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).rdsconn | ||
|
||
dbClusterID, roleARN, err := tfrds.ClusterRoleAssociationParseResourceID(d.Id()) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error parsing RDS DB Cluster IAM Role Association ID: %s", err) | ||
} | ||
|
||
output, err := finder.DBClusterRoleByDBClusterIDAndRoleARN(conn, dbClusterID, roleARN) | ||
|
||
if !d.IsNewResource() && tfresource.NotFound(err) { | ||
log.Printf("[WARN] RDS DB Cluster (%s) IAM Role (%s) Association not found, removing from state", dbClusterID, roleARN) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading RDS DB Cluster (%s) IAM Role (%s) Association: %w", dbClusterID, roleARN, err) | ||
} | ||
|
||
d.Set("db_cluster_identifier", dbClusterID) | ||
d.Set("feature_name", output.FeatureName) | ||
d.Set("role_arn", output.RoleArn) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsRDSClusterRoleAssociationDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).rdsconn | ||
|
||
dbClusterID, roleARN, err := tfrds.ClusterRoleAssociationParseResourceID(d.Id()) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error parsing RDS DB Cluster IAM Role Association ID: %s", err) | ||
} | ||
|
||
input := &rds.RemoveRoleFromDBClusterInput{ | ||
DBClusterIdentifier: aws.String(dbClusterID), | ||
FeatureName: aws.String(d.Get("feature_name").(string)), | ||
RoleArn: aws.String(roleARN), | ||
} | ||
|
||
log.Printf("[DEBUG] Deleting RDS DB Cluster IAM Role Association: %s", d.Id()) | ||
_, err = conn.RemoveRoleFromDBCluster(input) | ||
|
||
if tfawserr.ErrCodeEquals(err, rds.ErrCodeDBClusterNotFoundFault) || tfawserr.ErrCodeEquals(err, rds.ErrCodeDBClusterRoleNotFoundFault) { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error deleting RDS DB Cluster (%s) IAM Role (%s) Association: %w", dbClusterID, roleARN, err) | ||
} | ||
|
||
_, err = waiter.DBClusterRoleAssociationDeleted(conn, dbClusterID, roleARN) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error waiting for RDS DB Cluster (%s) IAM Role (%s) Association to delete: %w", dbClusterID, roleARN, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.