From 2639ffcbecc313e7cb36a9cdb47ff20bfd79279b Mon Sep 17 00:00:00 2001 From: Linda Xu Date: Tue, 31 Jan 2017 05:39:24 -0800 Subject: [PATCH] add Aurora create corss region read replica cluster function (#11428) --- .../providers/aws/resource_aws_rds_cluster.go | 64 ++++++++++++++++++- .../providers/aws/r/rds_cluster.html.markdown | 1 + 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_aws_rds_cluster.go b/builtin/providers/aws/resource_aws_rds_cluster.go index b81af34ca9..1d5c7e1bd1 100644 --- a/builtin/providers/aws/resource_aws_rds_cluster.go +++ b/builtin/providers/aws/resource_aws_rds_cluster.go @@ -201,6 +201,11 @@ func resourceAwsRDSCluster() *schema.Resource { ValidateFunc: validateArn, }, + "replication_source_identifier": { + Type: schema.TypeString, + Optional: true, + }, + "tags": tagsSchema(), }, } @@ -272,7 +277,7 @@ func resourceAwsRDSClusterCreate(d *schema.ResourceData, meta interface{}) error Pending: []string{"creating", "backing-up", "modifying"}, Target: []string{"available"}, Refresh: resourceAwsRDSClusterStateRefreshFunc(d, meta), - Timeout: 40 * time.Minute, + Timeout: 120 * time.Minute, MinTimeout: 3 * time.Second, Delay: 30 * time.Second, // Wait 30 secs before starting } @@ -288,6 +293,60 @@ func resourceAwsRDSClusterCreate(d *schema.ResourceData, meta interface{}) error return err } } + } else if _, ok := d.GetOk("replication_source_identifier"); ok { + createOpts := &rds.CreateDBClusterInput{ + DBClusterIdentifier: aws.String(d.Get("cluster_identifier").(string)), + Engine: aws.String("aurora"), + StorageEncrypted: aws.Bool(d.Get("storage_encrypted").(bool)), + ReplicationSourceIdentifier: aws.String(d.Get("replication_source_identifier").(string)), + Tags: tags, + } + + if attr, ok := d.GetOk("port"); ok { + createOpts.Port = aws.Int64(int64(attr.(int))) + } + + if attr, ok := d.GetOk("db_subnet_group_name"); ok { + createOpts.DBSubnetGroupName = aws.String(attr.(string)) + } + + if attr, ok := d.GetOk("db_cluster_parameter_group_name"); ok { + createOpts.DBClusterParameterGroupName = aws.String(attr.(string)) + } + + if attr := d.Get("vpc_security_group_ids").(*schema.Set); attr.Len() > 0 { + createOpts.VpcSecurityGroupIds = expandStringList(attr.List()) + } + + if attr := d.Get("availability_zones").(*schema.Set); attr.Len() > 0 { + createOpts.AvailabilityZones = expandStringList(attr.List()) + } + + if v, ok := d.GetOk("backup_retention_period"); ok { + createOpts.BackupRetentionPeriod = aws.Int64(int64(v.(int))) + } + + if v, ok := d.GetOk("preferred_backup_window"); ok { + createOpts.PreferredBackupWindow = aws.String(v.(string)) + } + + if v, ok := d.GetOk("preferred_maintenance_window"); ok { + createOpts.PreferredMaintenanceWindow = aws.String(v.(string)) + } + + if attr, ok := d.GetOk("kms_key_id"); ok { + createOpts.KmsKeyId = aws.String(attr.(string)) + } + + log.Printf("[DEBUG] Create RDS Cluster as read replica: %s", createOpts) + resp, err := conn.CreateDBCluster(createOpts) + if err != nil { + log.Printf("[ERROR] Error creating RDS Cluster: %s", err) + return err + } + + log.Printf("[DEBUG]: RDS Cluster create response: %s", resp) + } else { if _, ok := d.GetOk("master_password"); !ok { return fmt.Errorf(`provider.aws: aws_rds_cluster: %s: "master_password": required field is not set`, d.Get("name").(string)) @@ -367,7 +426,7 @@ func resourceAwsRDSClusterCreate(d *schema.ResourceData, meta interface{}) error Pending: []string{"creating", "backing-up", "modifying"}, Target: []string{"available"}, Refresh: resourceAwsRDSClusterStateRefreshFunc(d, meta), - Timeout: 40 * time.Minute, + Timeout: 120 * time.Minute, MinTimeout: 3 * time.Second, } @@ -437,6 +496,7 @@ func resourceAwsRDSClusterRead(d *schema.ResourceData, meta interface{}) error { d.Set("preferred_maintenance_window", dbc.PreferredMaintenanceWindow) d.Set("kms_key_id", dbc.KmsKeyId) d.Set("reader_endpoint", dbc.ReaderEndpoint) + d.Set("replication_source_identifier", dbc.ReplicationSourceIdentifier) var vpcg []string for _, g := range dbc.VpcSecurityGroups { diff --git a/website/source/docs/providers/aws/r/rds_cluster.html.markdown b/website/source/docs/providers/aws/r/rds_cluster.html.markdown index 6595e0131b..db24571fb0 100644 --- a/website/source/docs/providers/aws/r/rds_cluster.html.markdown +++ b/website/source/docs/providers/aws/r/rds_cluster.html.markdown @@ -105,6 +105,7 @@ load-balanced across replicas * `username` - The master username for the database * `storage_encrypted` - Specifies whether the DB cluster is encrypted * `preferred_backup_window` - The daily time range during which the backups happen +* `replication_source_identifier` - ARN of the source DB cluster if this DB cluster is created as a Read Replica. [1]: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.Replication.html