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

r/redshift_cluster - add aqua_configuration_status argument #24856

Merged
merged 9 commits into from
May 23, 2022
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
11 changes: 11 additions & 0 deletions .changelog/24856.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:enhancement
resource/aws_redshift_cluster: Add plan-time validation for `iam_roles`, `owner_account`, and `port`.
```

```release-note:enhancement
resource/aws_redshift_cluster: Add `aqua_configuration_status` and `apply_immediately` arguments.
```

```release-note:enhancement
data-source/aws_redshift_cluster: Add `aqua_configuration_status` attribute.
```
273 changes: 151 additions & 122 deletions internal/service/redshift/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ func ResourceCluster() *schema.Resource {
Optional: true,
Default: true,
},
"apply_immediately": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"aqua_configuration_status": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice(redshift.AquaConfigurationStatus_Values(), false),
},
"arn": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -115,7 +126,6 @@ func ResourceCluster() *schema.Resource {
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"cluster_subnet_group_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -180,8 +190,10 @@ func ResourceCluster() *schema.Resource {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: verify.ValidARN,
},
},
"kms_key_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -245,13 +257,15 @@ func ResourceCluster() *schema.Resource {
Default: 1,
},
"owner_account": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidAccountID,
},
"port": {
Type: schema.TypeInt,
Optional: true,
Default: 5439,
Type: schema.TypeInt,
Optional: true,
Default: 5439,
ValidateFunc: validation.IntBetween(1115, 65535),
},
"preferred_maintenance_window": {
Type: schema.TypeString,
Expand Down Expand Up @@ -314,7 +328,6 @@ func ResourceCluster() *schema.Resource {
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},

Expand Down Expand Up @@ -348,76 +361,108 @@ func resourceClusterCreate(d *schema.ResourceData, meta interface{}) error {
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))

if v, ok := d.GetOk("snapshot_identifier"); ok {
clusterID := d.Get("cluster_identifier").(string)
input := &redshift.RestoreFromClusterSnapshotInput{
AllowVersionUpgrade: aws.Bool(d.Get("allow_version_upgrade").(bool)),
AutomatedSnapshotRetentionPeriod: aws.Int64(int64(d.Get("automated_snapshot_retention_period").(int))),
ClusterIdentifier: aws.String(clusterID),
Port: aws.Int64(int64(d.Get("port").(int))),
NodeType: aws.String(d.Get("node_type").(string)),
PubliclyAccessible: aws.Bool(d.Get("publicly_accessible").(bool)),
SnapshotIdentifier: aws.String(v.(string)),
}
clusterID := d.Get("cluster_identifier").(string)
backupInput := &redshift.RestoreFromClusterSnapshotInput{
AllowVersionUpgrade: aws.Bool(d.Get("allow_version_upgrade").(bool)),
AutomatedSnapshotRetentionPeriod: aws.Int64(int64(d.Get("automated_snapshot_retention_period").(int))),
ClusterIdentifier: aws.String(clusterID),
Port: aws.Int64(int64(d.Get("port").(int))),
NodeType: aws.String(d.Get("node_type").(string)),
PubliclyAccessible: aws.Bool(d.Get("publicly_accessible").(bool)),
}

if v, ok := d.GetOk("availability_zone"); ok {
input.AvailabilityZone = aws.String(v.(string))
}
input := &redshift.CreateClusterInput{
AllowVersionUpgrade: aws.Bool(d.Get("allow_version_upgrade").(bool)),
AutomatedSnapshotRetentionPeriod: aws.Int64(int64(d.Get("automated_snapshot_retention_period").(int))),
ClusterIdentifier: aws.String(clusterID),
ClusterVersion: aws.String(d.Get("cluster_version").(string)),
DBName: aws.String(d.Get("database_name").(string)),
MasterUsername: aws.String(d.Get("master_username").(string)),
MasterUserPassword: aws.String(d.Get("master_password").(string)),
NodeType: aws.String(d.Get("node_type").(string)),
Port: aws.Int64(int64(d.Get("port").(int))),
PubliclyAccessible: aws.Bool(d.Get("publicly_accessible").(bool)),
Tags: Tags(tags.IgnoreAWS()),
}

if v, ok := d.GetOk("availability_zone_relocation_enabled"); ok {
input.AvailabilityZoneRelocation = aws.Bool(v.(bool))
}
if v, ok := d.GetOk("aqua_configuration_status"); ok {
backupInput.AquaConfigurationStatus = aws.String(v.(string))
input.AquaConfigurationStatus = aws.String(v.(string))
}

if v, ok := d.GetOk("cluster_subnet_group_name"); ok {
input.ClusterSubnetGroupName = aws.String(v.(string))
}
if v, ok := d.GetOk("availability_zone"); ok {
backupInput.AvailabilityZone = aws.String(v.(string))
input.AvailabilityZone = aws.String(v.(string))
}

if v, ok := d.GetOk("cluster_parameter_group_name"); ok {
input.ClusterParameterGroupName = aws.String(v.(string))
}
if v, ok := d.GetOk("availability_zone_relocation_enabled"); ok {
backupInput.AvailabilityZoneRelocation = aws.Bool(v.(bool))
input.AvailabilityZoneRelocation = aws.Bool(v.(bool))
}

if v := d.Get("cluster_security_groups").(*schema.Set); v.Len() > 0 {
input.ClusterSecurityGroups = flex.ExpandStringSet(v)
}
if v, ok := d.GetOk("cluster_parameter_group_name"); ok {
backupInput.ClusterParameterGroupName = aws.String(v.(string))
input.ClusterParameterGroupName = aws.String(v.(string))
}

if v, ok := d.GetOk("elastic_ip"); ok {
input.ElasticIp = aws.String(v.(string))
}
if v := d.Get("cluster_security_groups").(*schema.Set); v.Len() > 0 {
backupInput.ClusterSecurityGroups = flex.ExpandStringSet(v)
input.ClusterSecurityGroups = flex.ExpandStringSet(v)
}

if v, ok := d.GetOk("enhanced_vpc_routing"); ok {
input.EnhancedVpcRouting = aws.Bool(v.(bool))
}
if v, ok := d.GetOk("cluster_subnet_group_name"); ok {
backupInput.ClusterSubnetGroupName = aws.String(v.(string))
input.ClusterSubnetGroupName = aws.String(v.(string))
}

if v, ok := d.GetOk("iam_roles"); ok {
input.IamRoles = flex.ExpandStringSet(v.(*schema.Set))
}
if v, ok := d.GetOk("elastic_ip"); ok {
backupInput.ElasticIp = aws.String(v.(string))
input.ElasticIp = aws.String(v.(string))
}

if v, ok := d.GetOk("kms_key_id"); ok {
input.KmsKeyId = aws.String(v.(string))
}
if v, ok := d.GetOk("enhanced_vpc_routing"); ok {
backupInput.EnhancedVpcRouting = aws.Bool(v.(bool))
input.EnhancedVpcRouting = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("number_of_nodes"); ok {
input.NumberOfNodes = aws.Int64(int64(v.(int)))
}
if v, ok := d.GetOk("iam_roles"); ok {
backupInput.IamRoles = flex.ExpandStringSet(v.(*schema.Set))
input.IamRoles = flex.ExpandStringSet(v.(*schema.Set))
}

if v, ok := d.GetOk("owner_account"); ok {
input.OwnerAccount = aws.String(v.(string))
}
if v, ok := d.GetOk("kms_key_id"); ok {
backupInput.KmsKeyId = aws.String(v.(string))
input.KmsKeyId = aws.String(v.(string))
}

if v, ok := d.GetOk("preferred_maintenance_window"); ok {
input.PreferredMaintenanceWindow = aws.String(v.(string))
}
if v, ok := d.GetOk("number_of_nodes"); ok {
backupInput.NumberOfNodes = aws.Int64(int64(v.(int)))
// NumberOfNodes set below for CreateCluster.
}

if v, ok := d.GetOk("snapshot_cluster_identifier"); ok {
input.SnapshotClusterIdentifier = aws.String(v.(string))
if v, ok := d.GetOk("preferred_maintenance_window"); ok {
backupInput.PreferredMaintenanceWindow = aws.String(v.(string))
input.PreferredMaintenanceWindow = aws.String(v.(string))
}

if v := d.Get("vpc_security_group_ids").(*schema.Set); v.Len() > 0 {
backupInput.VpcSecurityGroupIds = flex.ExpandStringSet(v)
input.VpcSecurityGroupIds = flex.ExpandStringSet(v)
}

if v, ok := d.GetOk("snapshot_identifier"); ok {
backupInput.SnapshotIdentifier = aws.String(v.(string))

if v, ok := d.GetOk("owner_account"); ok {
backupInput.OwnerAccount = aws.String(v.(string))
}

if v := d.Get("vpc_security_group_ids").(*schema.Set); v.Len() > 0 {
input.VpcSecurityGroupIds = flex.ExpandStringSet(v)
if v, ok := d.GetOk("snapshot_cluster_identifier"); ok {
backupInput.SnapshotClusterIdentifier = aws.String(v.(string))
}

log.Printf("[DEBUG] Restoring Redshift Cluster: %s", input)
output, err := conn.RestoreFromClusterSnapshot(input)
log.Printf("[DEBUG] Restoring Redshift Cluster: %s", backupInput)
output, err := conn.RestoreFromClusterSnapshot(backupInput)

if err != nil {
return fmt.Errorf("error restoring Redshift Cluster (%s) from snapshot: %w", clusterID, err)
Expand All @@ -433,76 +478,17 @@ func resourceClusterCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf(`provider.aws: aws_redshift_cluster: %s: "master_username": required field is not set`, d.Get("cluster_identifier").(string))
}

clusterID := d.Get("cluster_identifier").(string)
input := &redshift.CreateClusterInput{
AllowVersionUpgrade: aws.Bool(d.Get("allow_version_upgrade").(bool)),
AutomatedSnapshotRetentionPeriod: aws.Int64(int64(d.Get("automated_snapshot_retention_period").(int))),
ClusterIdentifier: aws.String(clusterID),
ClusterVersion: aws.String(d.Get("cluster_version").(string)),
DBName: aws.String(d.Get("database_name").(string)),
MasterUsername: aws.String(d.Get("master_username").(string)),
MasterUserPassword: aws.String(d.Get("master_password").(string)),
NodeType: aws.String(d.Get("node_type").(string)),
Port: aws.Int64(int64(d.Get("port").(int))),
PubliclyAccessible: aws.Bool(d.Get("publicly_accessible").(bool)),
Tags: Tags(tags.IgnoreAWS()),
}

if v, ok := d.GetOk("availability_zone"); ok {
input.AvailabilityZone = aws.String(v.(string))
}

if v, ok := d.GetOk("availability_zone_relocation_enabled"); ok {
input.AvailabilityZoneRelocation = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("cluster_parameter_group_name"); ok {
input.ClusterParameterGroupName = aws.String(v.(string))
}

if v := d.Get("cluster_security_groups").(*schema.Set); v.Len() > 0 {
input.ClusterSecurityGroups = flex.ExpandStringSet(v)
}

if v, ok := d.GetOk("cluster_subnet_group_name"); ok {
input.ClusterSubnetGroupName = aws.String(v.(string))
}

if v, ok := d.GetOk("elastic_ip"); ok {
input.ElasticIp = aws.String(v.(string))
}

if v, ok := d.GetOk("encrypted"); ok {
input.Encrypted = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("enhanced_vpc_routing"); ok {
input.EnhancedVpcRouting = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("iam_roles"); ok {
input.IamRoles = flex.ExpandStringSet(v.(*schema.Set))
}

if v, ok := d.GetOk("kms_key_id"); ok {
input.KmsKeyId = aws.String(v.(string))
}

if v := d.Get("number_of_nodes").(int); v > 1 {
input.ClusterType = aws.String(clusterTypeMultiNode)
input.NumberOfNodes = aws.Int64(int64(d.Get("number_of_nodes").(int)))
} else {
input.ClusterType = aws.String(clusterTypeSingleNode)
}

if v, ok := d.GetOk("preferred_maintenance_window"); ok {
input.PreferredMaintenanceWindow = aws.String(v.(string))
}

if v := d.Get("vpc_security_group_ids").(*schema.Set); v.Len() > 0 {
input.VpcSecurityGroupIds = flex.ExpandStringSet(v)
}

log.Printf("[DEBUG] Creating Redshift Cluster: %s", input)
output, err := conn.CreateCluster(input)

Expand Down Expand Up @@ -576,6 +562,9 @@ func resourceClusterRead(d *schema.ResourceData, meta interface{}) error {
Resource: fmt.Sprintf("cluster:%s", d.Id()),
}.String()
d.Set("arn", arn)
if rsc.AquaConfiguration != nil {
d.Set("aqua_configuration_status", rsc.AquaConfiguration.AquaConfigurationStatus)
}
d.Set("automated_snapshot_retention_period", rsc.AutomatedSnapshotRetentionPeriod)
d.Set("availability_zone", rsc.AvailabilityZone)
azr, err := clusterAvailabilityZoneRelocationStatus(rsc)
Expand Down Expand Up @@ -666,7 +655,7 @@ func resourceClusterRead(d *schema.ResourceData, meta interface{}) error {
func resourceClusterUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).RedshiftConn

if d.HasChangesExcept("availability_zone", "iam_roles", "logging", "snapshot_copy", "tags", "tags_all") {
if d.HasChangesExcept("aqua_configuration_status", "availability_zone", "iam_roles", "logging", "snapshot_copy", "tags", "tags_all") {
input := &redshift.ModifyClusterInput{
ClusterIdentifier: aws.String(d.Id()),
}
Expand Down Expand Up @@ -784,6 +773,46 @@ func resourceClusterUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

if d.HasChange("aqua_configuration_status") {
input := &redshift.ModifyAquaConfigurationInput{
AquaConfigurationStatus: aws.String(d.Get("aqua_configuration_status").(string)),
ClusterIdentifier: aws.String(d.Id()),
}

log.Printf("[DEBUG] Modifying Redshift Cluster Aqua Configuration: %s", input)
_, err := conn.ModifyAquaConfiguration(input)

if err != nil {
return fmt.Errorf("error modifying Redshift Cluster (%s) Aqua Configuration: %w", d.Id(), err)
}

if d.Get("apply_immediately").(bool) {
rebootInput := &redshift.RebootClusterInput{
ClusterIdentifier: aws.String(d.Id()),
}

_, err := tfresource.RetryWhenAWSErrCodeEquals(
clusterInvalidClusterStateFaultTimeout,
func() (interface{}, error) {
return conn.RebootCluster(rebootInput)
},
redshift.ErrCodeInvalidClusterStateFault,
)

if err != nil {
return fmt.Errorf("error rebooting Redshift Cluster (%s): %w", d.Id(), err)
}

if _, err := waitClusterRebooted(conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("error waiting for Redshift Cluster (%s) Rebooted: %w", d.Id(), err)
}

if _, err := waitClusterAquaApplied(conn, d.Id(), d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("error waiting for Redshift Cluster (%s) Aqua Configuration update: %w", d.Id(), err)
}
}
}

// Availability Zone cannot be changed at the same time as other settings
if d.HasChange("availability_zone") {
input := &redshift.ModifyClusterInput{
Expand Down
Loading