Skip to content

Commit

Permalink
r/db_instance: deprecate 'name' and add 'db_name'
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Feb 1, 2022
1 parent 5af79cc commit 3d3be8b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
66 changes: 49 additions & 17 deletions internal/service/rds/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ func ResourceInstance() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"db_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name"},
},
"db_subnet_group_name": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -287,10 +294,11 @@ func ResourceInstance() *schema.Resource {
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"db_name"},
},
"nchar_character_set_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -668,26 +676,31 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error creating DB Instance: %s", err)
}
} else if v, ok := d.GetOk("s3_import"); ok {
dbName := d.Get("db_name").(string)
if dbName == "" {
dbName = d.Get("name").(string)
}

if _, ok := d.GetOk("allocated_storage"); !ok {
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "allocated_storage": required field is not set`, d.Get("name").(string))

return fmt.Errorf(`provider.aws: aws_db_instance: %s: "allocated_storage": required field is not set`, dbName)
}
if _, ok := d.GetOk("engine"); !ok {
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "engine": required field is not set`, d.Get("name").(string))
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "engine": required field is not set`, dbName)
}
if _, ok := d.GetOk("password"); !ok {
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "password": required field is not set`, d.Get("name").(string))
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "password": required field is not set`, dbName)
}
if _, ok := d.GetOk("username"); !ok {
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "username": required field is not set`, d.Get("name").(string))
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "username": required field is not set`, dbName)
}

s3_bucket := v.([]interface{})[0].(map[string]interface{})
opts := rds.RestoreDBInstanceFromS3Input{
AllocatedStorage: aws.Int64(int64(d.Get("allocated_storage").(int))),
AutoMinorVersionUpgrade: aws.Bool(d.Get("auto_minor_version_upgrade").(bool)),
CopyTagsToSnapshot: aws.Bool(d.Get("copy_tags_to_snapshot").(bool)),
DBName: aws.String(d.Get("name").(string)),
DBName: aws.String(dbName),
DBInstanceClass: aws.String(d.Get("instance_class").(string)),
DBInstanceIdentifier: aws.String(d.Get("identifier").(string)),
DeletionProtection: aws.Bool(d.Get("deletion_protection").(bool)),
Expand All @@ -710,10 +723,10 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
}

if _, ok := d.GetOk("character_set_name"); ok {
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "character_set_name" doesn't work with with restores"`, d.Get("name").(string))
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "character_set_name" doesn't work with with restores"`, dbName)
}
if _, ok := d.GetOk("timezone"); ok {
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "timezone" doesn't work with with restores"`, d.Get("name").(string))
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "timezone" doesn't work with with restores"`, dbName)
}

attr := d.Get("backup_retention_period")
Expand Down Expand Up @@ -858,7 +871,16 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
Tags: Tags(tags.IgnoreAWS()),
}

if attr, ok := d.GetOk("name"); ok {
if attr, ok := d.GetOk("db_name"); ok {
// "Note: This parameter [DBName] doesn't apply to the MySQL, PostgreSQL, or MariaDB engines."
// https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceFromDBSnapshot.html
switch strings.ToLower(d.Get("engine").(string)) {
case "mysql", "postgres", "mariadb":
// skip
default:
opts.DBName = aws.String(attr.(string))
}
} else if attr, ok := d.GetOk("name"); ok {
// "Note: This parameter [DBName] doesn't apply to the MySQL, PostgreSQL, or MariaDB engines."
// https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_RestoreDBInstanceFromDBSnapshot.html
switch strings.ToLower(d.Get("engine").(string)) {
Expand Down Expand Up @@ -1054,6 +1076,10 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
input.AvailabilityZone = aws.String(v.(string))
}

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

if v, ok := d.GetOk("domain"); ok {
input.Domain = aws.String(v.(string))
}
Expand Down Expand Up @@ -1134,22 +1160,27 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
}
}
} else {
dbName := d.Get("db_name").(string)
if dbName == "" {
dbName = d.Get("name").(string)
}

if _, ok := d.GetOk("allocated_storage"); !ok {
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "allocated_storage": required field is not set`, d.Get("name").(string))
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "allocated_storage": required field is not set`, dbName)
}
if _, ok := d.GetOk("engine"); !ok {
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "engine": required field is not set`, d.Get("name").(string))
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "engine": required field is not set`, dbName)
}
if _, ok := d.GetOk("password"); !ok {
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "password": required field is not set`, d.Get("name").(string))
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "password": required field is not set`, dbName)
}
if _, ok := d.GetOk("username"); !ok {
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "username": required field is not set`, d.Get("name").(string))
return fmt.Errorf(`provider.aws: aws_db_instance: %s: "username": required field is not set`, dbName)
}

opts := rds.CreateDBInstanceInput{
AllocatedStorage: aws.Int64(int64(d.Get("allocated_storage").(int))),
DBName: aws.String(d.Get("name").(string)),
DBName: aws.String(dbName),
DBInstanceClass: aws.String(d.Get("instance_class").(string)),
DBInstanceIdentifier: aws.String(d.Get("identifier").(string)),
DeletionProtection: aws.Bool(d.Get("deletion_protection").(bool)),
Expand Down Expand Up @@ -1379,6 +1410,7 @@ func resourceInstanceRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("error reading DB Instance (%s): %w", d.Id(), err)
}

d.Set("db_name", v.DBName)
d.Set("name", v.DBName)
d.Set("identifier", v.DBInstanceIdentifier)
d.Set("resource_id", v.DbiResourceId)
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/db_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ encoding in Oracle and Microsoft SQL instances (collation). This can't be change
Supported in Amazon RDS](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.OracleCharacterSets.html)
or [Server-Level Collation for Microsoft SQL Server](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.SQLServer.CommonDBATasks.Collation.html) for more information.
* `copy_tags_to_snapshot` – (Optional, boolean) Copy all Instance `tags` to snapshots. Default is `false`.
* `db_name` - (Optional) The name of the database to create when the DB instance is created. If this parameter is not specified, no database is created in the DB instance. Note that this does not apply for Oracle or SQL Server engines. See the [AWS documentation](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/rds/create-db-instance.html) for more details on what applies for those engines. If you are providing an Oracle db name, it needs to be in all upper case.
* `db_subnet_group_name` - (Optional) Name of [DB subnet group](/docs/providers/aws/r/db_subnet_group.html). DB instance will
be created in the VPC associated with the DB subnet group. If unspecified, will
be created in the `default` VPC, or in EC2 Classic, if available. When working
Expand Down Expand Up @@ -153,7 +154,7 @@ information on the [AWS
Documentation](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Monitoring.html)
what IAM permissions are needed to allow Enhanced Monitoring for RDS Instances.
* `multi_az` - (Optional) Specifies if the RDS instance is multi-AZ
* `name` - (Optional) The name of the database to create when the DB instance is created. If this parameter is not specified, no database is created in the DB instance. Note that this does not apply for Oracle or SQL Server engines. See the [AWS documentation](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/rds/create-db-instance.html) for more details on what applies for those engines. If you are providing an Oracle db name, it needs to be in all upper case.
* `name` - (Optional, **Deprecated** use `db_name` instead) The name of the database to create when the DB instance is created. If this parameter is not specified, no database is created in the DB instance. Note that this does not apply for Oracle or SQL Server engines. See the [AWS documentation](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/rds/create-db-instance.html) for more details on what applies for those engines. If you are providing an Oracle db name, it needs to be in all upper case.
* `nchar_character_set_name` - (Optional, Forces new resource) The national character set is used in the NCHAR, NVARCHAR2, and NCLOB data types for Oracle instances. This can't be changed. See [Oracle Character Sets
Supported in Amazon RDS](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Appendix.OracleCharacterSets.html).
* `option_group_name` - (Optional) Name of the DB option group to associate.
Expand Down Expand Up @@ -280,6 +281,7 @@ In addition to all arguments above, the following attributes are exported:
* `backup_window` - The backup window.
* `ca_cert_identifier` - Specifies the identifier of the CA certificate for the
DB instance.
* `db_name` - The database name.
* `domain` - The ID of the Directory Service Active Directory domain the instance is joined to
* `domain_iam_role_name` - The name of the IAM role to be used when making API calls to the Directory Service.
* `endpoint` - The connection endpoint in `address:port` format.
Expand Down

0 comments on commit 3d3be8b

Please sign in to comment.