provider/aws: data_aws_db_instance resource (#11717)

This commit is contained in:
Paul Stack 2017-02-10 14:58:31 +00:00 committed by GitHub
parent a306a6d780
commit 06aaa44a80
5 changed files with 375 additions and 0 deletions

View File

@ -0,0 +1,260 @@
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsDbInstance() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDbInstanceRead,
Schema: map[string]*schema.Schema{
"db_instance_identifier": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"allocated_storage": {
Type: schema.TypeInt,
Computed: true,
},
"auto_minor_version_upgrade": {
Type: schema.TypeBool,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
},
"backup_retention_period": {
Type: schema.TypeInt,
Computed: true,
},
"db_cluster_identifier": {
Type: schema.TypeString,
Computed: true,
},
"db_instance_arn": {
Type: schema.TypeString,
Computed: true,
},
"db_instance_class": {
Type: schema.TypeString,
Computed: true,
},
"db_name": {
Type: schema.TypeString,
Computed: true,
},
"db_parameter_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"db_security_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"db_subnet_group": {
Type: schema.TypeString,
Computed: true,
},
"db_instance_port": {
Type: schema.TypeInt,
Computed: true,
},
"engine": {
Type: schema.TypeString,
Computed: true,
},
"engine_version": {
Type: schema.TypeString,
Computed: true,
},
"iops": {
Type: schema.TypeInt,
Computed: true,
},
"kms_key_id": {
Type: schema.TypeString,
Computed: true,
},
"license_model": {
Type: schema.TypeString,
Computed: true,
},
"master_username": {
Type: schema.TypeString,
Computed: true,
},
"monitoring_interval": {
Type: schema.TypeInt,
Computed: true,
},
"monitoring_role_arn": {
Type: schema.TypeString,
Computed: true,
},
"multi_az": {
Type: schema.TypeBool,
Computed: true,
},
"option_group_memberships": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"preferred_backup_window": {
Type: schema.TypeString,
Computed: true,
},
"preferred_maintenance_window": {
Type: schema.TypeString,
Computed: true,
},
"publicly_accessible": {
Type: schema.TypeBool,
Computed: true,
},
"storage_encrypted": {
Type: schema.TypeBool,
Computed: true,
},
"storage_type": {
Type: schema.TypeString,
Computed: true,
},
"timezone": {
Type: schema.TypeString,
Computed: true,
},
"vpc_security_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).rdsconn
opts := rds.DescribeDBInstancesInput{
DBInstanceIdentifier: aws.String(d.Get("db_instance_identifier").(string)),
}
log.Printf("[DEBUG] DB Instance describe configuration: %#v", opts)
resp, err := conn.DescribeDBInstances(&opts)
if err != nil {
return err
}
if len(resp.DBInstances) < 1 {
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}
if len(resp.DBInstances) > 1 {
return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria.")
}
dbInstance := *resp.DBInstances[0]
d.SetId(d.Get("db_instance_identifier").(string))
d.Set("allocated_storage", dbInstance.AllocatedStorage)
d.Set("auto_minor_upgrade_enabled", dbInstance.AutoMinorVersionUpgrade)
d.Set("availability_zone", dbInstance.AvailabilityZone)
d.Set("backup_retention_period", dbInstance.BackupRetentionPeriod)
d.Set("db_cluster_identifier", dbInstance.DBClusterIdentifier)
d.Set("db_instance_arn", dbInstance.DBClusterIdentifier)
d.Set("db_instance_class", dbInstance.DBInstanceClass)
d.Set("db_name", dbInstance.DBName)
var parameterGroups []string
for _, v := range dbInstance.DBParameterGroups {
parameterGroups = append(parameterGroups, *v.DBParameterGroupName)
}
if err := d.Set("db_parameter_groups", parameterGroups); err != nil {
return fmt.Errorf("[DEBUG] Error setting db_parameter_groups attribute: %#v, error: %#v", parameterGroups, err)
}
var dbSecurityGroups []string
for _, v := range dbInstance.DBSecurityGroups {
dbSecurityGroups = append(dbSecurityGroups, *v.DBSecurityGroupName)
}
if err := d.Set("db_security_groups", dbSecurityGroups); err != nil {
return fmt.Errorf("[DEBUG] Error setting db_security_groups attribute: %#v, error: %#v", dbSecurityGroups, err)
}
d.Set("db_subnet_group", dbInstance.DBSubnetGroup)
d.Set("db_instance_port", dbInstance.DbInstancePort)
d.Set("engine", dbInstance.Engine)
d.Set("engine_version", dbInstance.EngineVersion)
d.Set("iops", dbInstance.Iops)
d.Set("kms_key_id", dbInstance.KmsKeyId)
d.Set("license_model", dbInstance.LicenseModel)
d.Set("master_username", dbInstance.MasterUsername)
d.Set("monitoring_interval", dbInstance.MonitoringInterval)
d.Set("monitoring_role_arn", dbInstance.MonitoringRoleArn)
var optionGroups []string
for _, v := range dbInstance.OptionGroupMemberships {
optionGroups = append(optionGroups, *v.OptionGroupName)
}
if err := d.Set("option_group_memberships", optionGroups); err != nil {
return fmt.Errorf("[DEBUG] Error setting option_group_memberships attribute: %#v, error: %#v", optionGroups, err)
}
d.Set("preferred_backup_window", dbInstance.PreferredBackupWindow)
d.Set("preferred_maintenance_window", dbInstance.PreferredMaintenanceWindow)
d.Set("publicly_accessible", dbInstance.PubliclyAccessible)
d.Set("storage_encrypted", dbInstance.StorageEncrypted)
d.Set("storage_type", dbInstance.StorageType)
d.Set("timezone", dbInstance.Timezone)
var vpcSecurityGroups []string
for _, v := range dbInstance.VpcSecurityGroups {
vpcSecurityGroups = append(vpcSecurityGroups, *v.VpcSecurityGroupId)
}
if err := d.Set("vpc_security_groups", vpcSecurityGroups); err != nil {
return fmt.Errorf("[DEBUG] Error setting vpc_security_groups attribute: %#v, error: %#v", vpcSecurityGroups, err)
}
return nil
}

View File

@ -0,0 +1,52 @@
package aws
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccAWSDataDbInstance_basic(t *testing.T) {
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstanceConfigWithDataSource(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.aws_db_instance.bar", "allocated_storage"),
resource.TestCheckResourceAttrSet("data.aws_db_instance.bar", "engine"),
resource.TestCheckResourceAttrSet("data.aws_db_instance.bar", "db_instance_class"),
resource.TestCheckResourceAttrSet("data.aws_db_instance.bar", "db_name"),
resource.TestCheckResourceAttrSet("data.aws_db_instance.bar", "master_username"),
),
},
},
})
}
func testAccAWSDBInstanceConfigWithDataSource(rInt int) string {
return fmt.Sprintf(`
resource "aws_db_instance" "bar" {
identifier = "datasource-test-terraform-%d"
allocated_storage = 10
engine = "MySQL"
instance_class = "db.m1.small"
name = "baz"
password = "barbarbarbar"
username = "foo"
backup_retention_period = 0
skip_final_snapshot = true
}
data "aws_db_instance" "bar" {
db_instance_identifier = "${aws_db_instance.bar.identifier}"
}
`, rInt)
}

View File

@ -163,6 +163,7 @@ func Provider() terraform.ResourceProvider {
"aws_caller_identity": dataSourceAwsCallerIdentity(),
"aws_canonical_user_id": dataSourceAwsCanonicalUserId(),
"aws_cloudformation_stack": dataSourceAwsCloudFormationStack(),
"aws_db_instance": dataSourceAwsDbInstance(),
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
"aws_ebs_volume": dataSourceAwsEbsVolume(),
"aws_ecs_cluster": dataSourceAwsEcsCluster(),

View File

@ -0,0 +1,59 @@
---
layout: "aws"
page_title: "AWS: aws_db_instance"
sidebar_current: "docs-aws-datasource-db-instance"
description: |-
Get information on an RDS Database Instance.
---
# aws\_ebs\_snapshot
Use this data source to get information about an EBS Snapshot for use when provisioning EBS Volumes
## Example Usage
```
data "aws_db_instance" "database" {
db_instance_identifier = "my-test-database"
}
```
## Argument Reference
The following arguments are supported:
* `db_instance_identifier` - (Required) The name of the RDS instance
## Attributes Reference
The following attributes are exported:
`allocated_storage` - Specifies the allocated storage size specified in gigabytes.
`auto_minor_version_upgrade` - Indicates that minor version patches are applied automatically.
`availability_zone` - Specifies the name of the Availability Zone the DB instance is located in.
`backup_retention_period` - Specifies the number of days for which automatic DB snapshots are retained.
`db_cluster_identifier` - If the DB instance is a member of a DB cluster, contains the name of the DB cluster that the DB instance is a member of.
`db_instance_arn` - The Amazon Resource Name (ARN) for the DB instance.
`db_instance_class` - Contains the name of the compute and memory capacity class of the DB instance.
`db_name` - Contains the name of the initial database of this instance that was provided at create time, if one was specified when the DB instance was created. This same name is returned for the life of the DB instance.
`db_parameter_groups` - Provides the list of DB parameter groups applied to this DB instance.
`db_security_groups` - Provides List of DB security groups associated to this DB instance.
`db_subnet_group` - Specifies the name of the subnet group associated with the DB instance.
`db_instance_port` - Specifies the port that the DB instance listens on.
`engine` - Provides the name of the database engine to be used for this DB instance.
`engine_version` - Indicates the database engine version.
`iops` - Specifies the Provisioned IOPS (I/O operations per second) value.
`kms_key_id` - If StorageEncrypted is true, the KMS key identifier for the encrypted DB instance.
`license_model` - License model information for this DB instance.
`master_username` - Contains the master username for the DB instance.
`monitoring_interval` - The interval, in seconds, between points when Enhanced Monitoring metrics are collected for the DB instance.
`monitoring_role_arn` - The ARN for the IAM role that permits RDS to send Enhanced Monitoring metrics to CloudWatch Logs.
`multi_az` - Specifies if the DB instance is a Multi-AZ deployment.
`option_group_memberships` - Provides the list of option group memberships for this DB instance.
`preferred_backup_window` - Specifies the daily time range during which automated backups are created.
`preferred_maintenance_window` - Specifies the weekly time range during which system maintenance can occur in UTC.
`publicly_accessible` - Specifies the accessibility options for the DB instance.
`storage_encrypted` - Specifies whether the DB instance is encrypted.
`storage_type` - Specifies the storage type associated with DB instance.
`timezone` - The time zone of the DB instance.
`vpc_security_groups` - Provides a list of VPC security group elements that the DB instance belongs to.

View File

@ -44,6 +44,9 @@
<li<%= sidebar_current("docs-aws-datasource-cloudformation-stack") %>>
<a href="/docs/providers/aws/d/cloudformation_stack.html">aws_cloudformation_stack</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-db-instance") %>>
<a href="/docs/providers/aws/d/db_instance.html">aws_db_instance</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-ebs-snapshot") %>>
<a href="/docs/providers/aws/d/ebs_snapshot.html">aws_ebs_snapshot</a>
</li>