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