github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_db_instance.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/rds"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func dataSourceAwsDbInstance() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsDbInstanceRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"db_instance_identifier": {
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  				ForceNew: true,
    21  			},
    22  
    23  			"address": {
    24  				Type:     schema.TypeString,
    25  				Computed: true,
    26  			},
    27  
    28  			"allocated_storage": {
    29  				Type:     schema.TypeInt,
    30  				Computed: true,
    31  			},
    32  
    33  			"auto_minor_version_upgrade": {
    34  				Type:     schema.TypeBool,
    35  				Computed: true,
    36  			},
    37  
    38  			"availability_zone": {
    39  				Type:     schema.TypeString,
    40  				Computed: true,
    41  			},
    42  
    43  			"backup_retention_period": {
    44  				Type:     schema.TypeInt,
    45  				Computed: true,
    46  			},
    47  
    48  			"db_cluster_identifier": {
    49  				Type:     schema.TypeString,
    50  				Computed: true,
    51  			},
    52  
    53  			"db_instance_arn": {
    54  				Type:     schema.TypeString,
    55  				Computed: true,
    56  			},
    57  
    58  			"db_instance_class": {
    59  				Type:     schema.TypeString,
    60  				Computed: true,
    61  			},
    62  
    63  			"db_name": {
    64  				Type:     schema.TypeString,
    65  				Computed: true,
    66  			},
    67  
    68  			"db_parameter_groups": {
    69  				Type:     schema.TypeList,
    70  				Computed: true,
    71  				Elem:     &schema.Schema{Type: schema.TypeString},
    72  			},
    73  
    74  			"db_security_groups": {
    75  				Type:     schema.TypeList,
    76  				Computed: true,
    77  				Elem:     &schema.Schema{Type: schema.TypeString},
    78  			},
    79  
    80  			"db_subnet_group": {
    81  				Type:     schema.TypeString,
    82  				Computed: true,
    83  			},
    84  
    85  			"db_instance_port": {
    86  				Type:     schema.TypeInt,
    87  				Computed: true,
    88  			},
    89  
    90  			"endpoint": {
    91  				Type:     schema.TypeString,
    92  				Computed: true,
    93  			},
    94  
    95  			"engine": {
    96  				Type:     schema.TypeString,
    97  				Computed: true,
    98  			},
    99  
   100  			"engine_version": {
   101  				Type:     schema.TypeString,
   102  				Computed: true,
   103  			},
   104  
   105  			"hosted_zone_id": {
   106  				Type:     schema.TypeString,
   107  				Computed: true,
   108  			},
   109  
   110  			"iops": {
   111  				Type:     schema.TypeInt,
   112  				Computed: true,
   113  			},
   114  
   115  			"kms_key_id": {
   116  				Type:     schema.TypeString,
   117  				Computed: true,
   118  			},
   119  
   120  			"license_model": {
   121  				Type:     schema.TypeString,
   122  				Computed: true,
   123  			},
   124  
   125  			"master_username": {
   126  				Type:     schema.TypeString,
   127  				Computed: true,
   128  			},
   129  
   130  			"monitoring_interval": {
   131  				Type:     schema.TypeInt,
   132  				Computed: true,
   133  			},
   134  
   135  			"monitoring_role_arn": {
   136  				Type:     schema.TypeString,
   137  				Computed: true,
   138  			},
   139  
   140  			"multi_az": {
   141  				Type:     schema.TypeBool,
   142  				Computed: true,
   143  			},
   144  
   145  			"option_group_memberships": {
   146  				Type:     schema.TypeList,
   147  				Computed: true,
   148  				Elem:     &schema.Schema{Type: schema.TypeString},
   149  			},
   150  
   151  			"port": {
   152  				Type:     schema.TypeInt,
   153  				Computed: true,
   154  			},
   155  
   156  			"preferred_backup_window": {
   157  				Type:     schema.TypeString,
   158  				Computed: true,
   159  			},
   160  
   161  			"preferred_maintenance_window": {
   162  				Type:     schema.TypeString,
   163  				Computed: true,
   164  			},
   165  
   166  			"publicly_accessible": {
   167  				Type:     schema.TypeBool,
   168  				Computed: true,
   169  			},
   170  
   171  			"storage_encrypted": {
   172  				Type:     schema.TypeBool,
   173  				Computed: true,
   174  			},
   175  
   176  			"storage_type": {
   177  				Type:     schema.TypeString,
   178  				Computed: true,
   179  			},
   180  
   181  			"timezone": {
   182  				Type:     schema.TypeString,
   183  				Computed: true,
   184  			},
   185  
   186  			"vpc_security_groups": {
   187  				Type:     schema.TypeList,
   188  				Computed: true,
   189  				Elem:     &schema.Schema{Type: schema.TypeString},
   190  			},
   191  
   192  			"replicate_source_db": {
   193  				Type:     schema.TypeString,
   194  				Computed: true,
   195  			},
   196  		},
   197  	}
   198  }
   199  
   200  func dataSourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
   201  	conn := meta.(*AWSClient).rdsconn
   202  
   203  	opts := rds.DescribeDBInstancesInput{
   204  		DBInstanceIdentifier: aws.String(d.Get("db_instance_identifier").(string)),
   205  	}
   206  
   207  	log.Printf("[DEBUG] DB Instance describe configuration: %#v", opts)
   208  
   209  	resp, err := conn.DescribeDBInstances(&opts)
   210  	if err != nil {
   211  		return err
   212  	}
   213  
   214  	if len(resp.DBInstances) < 1 {
   215  		return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
   216  	}
   217  	if len(resp.DBInstances) > 1 {
   218  		return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria.")
   219  	}
   220  
   221  	dbInstance := *resp.DBInstances[0]
   222  
   223  	d.SetId(d.Get("db_instance_identifier").(string))
   224  
   225  	d.Set("allocated_storage", dbInstance.AllocatedStorage)
   226  	d.Set("auto_minor_upgrade_enabled", dbInstance.AutoMinorVersionUpgrade)
   227  	d.Set("availability_zone", dbInstance.AvailabilityZone)
   228  	d.Set("backup_retention_period", dbInstance.BackupRetentionPeriod)
   229  	d.Set("db_cluster_identifier", dbInstance.DBClusterIdentifier)
   230  	d.Set("db_instance_arn", dbInstance.DBClusterIdentifier)
   231  	d.Set("db_instance_class", dbInstance.DBInstanceClass)
   232  	d.Set("db_name", dbInstance.DBName)
   233  
   234  	var parameterGroups []string
   235  	for _, v := range dbInstance.DBParameterGroups {
   236  		parameterGroups = append(parameterGroups, *v.DBParameterGroupName)
   237  	}
   238  	if err := d.Set("db_parameter_groups", parameterGroups); err != nil {
   239  		return fmt.Errorf("[DEBUG] Error setting db_parameter_groups attribute: %#v, error: %#v", parameterGroups, err)
   240  	}
   241  
   242  	var dbSecurityGroups []string
   243  	for _, v := range dbInstance.DBSecurityGroups {
   244  		dbSecurityGroups = append(dbSecurityGroups, *v.DBSecurityGroupName)
   245  	}
   246  	if err := d.Set("db_security_groups", dbSecurityGroups); err != nil {
   247  		return fmt.Errorf("[DEBUG] Error setting db_security_groups attribute: %#v, error: %#v", dbSecurityGroups, err)
   248  	}
   249  
   250  	d.Set("db_subnet_group", dbInstance.DBSubnetGroup)
   251  	d.Set("db_instance_port", dbInstance.DbInstancePort)
   252  	d.Set("engine", dbInstance.Engine)
   253  	d.Set("engine_version", dbInstance.EngineVersion)
   254  	d.Set("iops", dbInstance.Iops)
   255  	d.Set("kms_key_id", dbInstance.KmsKeyId)
   256  	d.Set("license_model", dbInstance.LicenseModel)
   257  	d.Set("master_username", dbInstance.MasterUsername)
   258  	d.Set("monitoring_interval", dbInstance.MonitoringInterval)
   259  	d.Set("monitoring_role_arn", dbInstance.MonitoringRoleArn)
   260  	d.Set("address", dbInstance.Endpoint.Address)
   261  	d.Set("port", dbInstance.Endpoint.Port)
   262  	d.Set("hosted_zone_id", dbInstance.Endpoint.HostedZoneId)
   263  	d.Set("endpoint", fmt.Sprintf("%s:%d", *dbInstance.Endpoint.Address, *dbInstance.Endpoint.Port))
   264  
   265  	var optionGroups []string
   266  	for _, v := range dbInstance.OptionGroupMemberships {
   267  		optionGroups = append(optionGroups, *v.OptionGroupName)
   268  	}
   269  	if err := d.Set("option_group_memberships", optionGroups); err != nil {
   270  		return fmt.Errorf("[DEBUG] Error setting option_group_memberships attribute: %#v, error: %#v", optionGroups, err)
   271  	}
   272  
   273  	d.Set("preferred_backup_window", dbInstance.PreferredBackupWindow)
   274  	d.Set("preferred_maintenance_window", dbInstance.PreferredMaintenanceWindow)
   275  	d.Set("publicly_accessible", dbInstance.PubliclyAccessible)
   276  	d.Set("storage_encrypted", dbInstance.StorageEncrypted)
   277  	d.Set("storage_type", dbInstance.StorageType)
   278  	d.Set("timezone", dbInstance.Timezone)
   279  	d.Set("replicate_source_db", dbInstance.ReadReplicaSourceDBInstanceIdentifier)
   280  
   281  	var vpcSecurityGroups []string
   282  	for _, v := range dbInstance.VpcSecurityGroups {
   283  		vpcSecurityGroups = append(vpcSecurityGroups, *v.VpcSecurityGroupId)
   284  	}
   285  	if err := d.Set("vpc_security_groups", vpcSecurityGroups); err != nil {
   286  		return fmt.Errorf("[DEBUG] Error setting vpc_security_groups attribute: %#v, error: %#v", vpcSecurityGroups, err)
   287  	}
   288  
   289  	return nil
   290  }