github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/aws/data_source_aws_db_snapshot.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"sort"
     7  	"time"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/service/rds"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func dataSourceAwsDbSnapshot() *schema.Resource {
    15  	return &schema.Resource{
    16  		Read: dataSourceAwsDbSnapshotRead,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			//selection criteria
    20  			"db_instance_identifier": {
    21  				Type:     schema.TypeString,
    22  				Optional: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"db_snapshot_identifier": {
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"snapshot_type": {
    33  				Type:     schema.TypeString,
    34  				Optional: true,
    35  				ForceNew: true,
    36  			},
    37  
    38  			"include_shared": {
    39  				Type:     schema.TypeBool,
    40  				Optional: true,
    41  				ForceNew: true,
    42  				Default:  false,
    43  			},
    44  
    45  			"include_public": {
    46  				Type:     schema.TypeBool,
    47  				Optional: true,
    48  				ForceNew: true,
    49  				Default:  false,
    50  			},
    51  			"most_recent": {
    52  				Type:     schema.TypeBool,
    53  				Optional: true,
    54  				Default:  false,
    55  				ForceNew: true,
    56  			},
    57  
    58  			//Computed values returned
    59  			"allocated_storage": {
    60  				Type:     schema.TypeInt,
    61  				Computed: true,
    62  			},
    63  			"availability_zone": {
    64  				Type:     schema.TypeString,
    65  				Computed: true,
    66  			},
    67  			"db_snapshot_arn": {
    68  				Type:     schema.TypeString,
    69  				Computed: true,
    70  			},
    71  			"encrypted": {
    72  				Type:     schema.TypeBool,
    73  				Computed: true,
    74  			},
    75  			"engine": {
    76  				Type:     schema.TypeString,
    77  				Computed: true,
    78  			},
    79  			"engine_version": {
    80  				Type:     schema.TypeString,
    81  				Computed: true,
    82  			},
    83  			"iops": {
    84  				Type:     schema.TypeInt,
    85  				Computed: true,
    86  			},
    87  			"kms_key_id": {
    88  				Type:     schema.TypeString,
    89  				Computed: true,
    90  			},
    91  			"license_model": {
    92  				Type:     schema.TypeString,
    93  				Computed: true,
    94  			},
    95  			"option_group_name": {
    96  				Type:     schema.TypeString,
    97  				Computed: true,
    98  			},
    99  			"port": {
   100  				Type:     schema.TypeInt,
   101  				Computed: true,
   102  			},
   103  			"source_db_snapshot_identifier": {
   104  				Type:     schema.TypeString,
   105  				Computed: true,
   106  			},
   107  			"source_region": {
   108  				Type:     schema.TypeString,
   109  				Computed: true,
   110  			},
   111  			"snapshot_create_time": {
   112  				Type:     schema.TypeString,
   113  				Computed: true,
   114  			},
   115  			"status": {
   116  				Type:     schema.TypeString,
   117  				Computed: true,
   118  			},
   119  			"storage_type": {
   120  				Type:     schema.TypeString,
   121  				Computed: true,
   122  			},
   123  			"vpc_id": {
   124  				Type:     schema.TypeString,
   125  				Computed: true,
   126  			},
   127  		},
   128  	}
   129  }
   130  
   131  func dataSourceAwsDbSnapshotRead(d *schema.ResourceData, meta interface{}) error {
   132  	conn := meta.(*AWSClient).rdsconn
   133  
   134  	instanceIdentifier, instanceIdentifierOk := d.GetOk("db_instance_identifier")
   135  	snapshotIdentifier, snapshotIdentifierOk := d.GetOk("db_snapshot_identifier")
   136  
   137  	if !instanceIdentifierOk && !snapshotIdentifierOk {
   138  		return fmt.Errorf("One of db_snapshot_indentifier or db_instance_identifier must be assigned")
   139  	}
   140  
   141  	params := &rds.DescribeDBSnapshotsInput{
   142  		IncludePublic: aws.Bool(d.Get("include_public").(bool)),
   143  		IncludeShared: aws.Bool(d.Get("include_shared").(bool)),
   144  	}
   145  	if v, ok := d.GetOk("snapshot_type"); ok {
   146  		params.SnapshotType = aws.String(v.(string))
   147  	}
   148  	if instanceIdentifierOk {
   149  		params.DBInstanceIdentifier = aws.String(instanceIdentifier.(string))
   150  	}
   151  	if snapshotIdentifierOk {
   152  		params.DBSnapshotIdentifier = aws.String(snapshotIdentifier.(string))
   153  	}
   154  
   155  	resp, err := conn.DescribeDBSnapshots(params)
   156  	if err != nil {
   157  		return err
   158  	}
   159  
   160  	if len(resp.DBSnapshots) < 1 {
   161  		return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
   162  	}
   163  
   164  	var snapshot *rds.DBSnapshot
   165  	if len(resp.DBSnapshots) > 1 {
   166  		recent := d.Get("most_recent").(bool)
   167  		log.Printf("[DEBUG] aws_db_snapshot - multiple results found and `most_recent` is set to: %t", recent)
   168  		if recent {
   169  			snapshot = mostRecentDbSnapshot(resp.DBSnapshots)
   170  		} else {
   171  			return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria.")
   172  		}
   173  	} else {
   174  		snapshot = resp.DBSnapshots[0]
   175  	}
   176  
   177  	return dbSnapshotDescriptionAttributes(d, snapshot)
   178  }
   179  
   180  type rdsSnapshotSort []*rds.DBSnapshot
   181  
   182  func (a rdsSnapshotSort) Len() int      { return len(a) }
   183  func (a rdsSnapshotSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   184  func (a rdsSnapshotSort) Less(i, j int) bool {
   185  	return (*a[i].SnapshotCreateTime).Before(*a[j].SnapshotCreateTime)
   186  }
   187  
   188  func mostRecentDbSnapshot(snapshots []*rds.DBSnapshot) *rds.DBSnapshot {
   189  	sortedSnapshots := snapshots
   190  	sort.Sort(rdsSnapshotSort(sortedSnapshots))
   191  	return sortedSnapshots[len(sortedSnapshots)-1]
   192  }
   193  
   194  func dbSnapshotDescriptionAttributes(d *schema.ResourceData, snapshot *rds.DBSnapshot) error {
   195  	d.SetId(*snapshot.DBInstanceIdentifier)
   196  	d.Set("db_instance_identifier", snapshot.DBInstanceIdentifier)
   197  	d.Set("db_snapshot_identifier", snapshot.DBSnapshotIdentifier)
   198  	d.Set("snapshot_type", snapshot.SnapshotType)
   199  	d.Set("allocated_storage", snapshot.AllocatedStorage)
   200  	d.Set("availability_zone", snapshot.AvailabilityZone)
   201  	d.Set("db_snapshot_arn", snapshot.DBSnapshotArn)
   202  	d.Set("encrypted", snapshot.Encrypted)
   203  	d.Set("engine", snapshot.Engine)
   204  	d.Set("engine_version", snapshot.EngineVersion)
   205  	d.Set("iops", snapshot.Iops)
   206  	d.Set("kms_key_id", snapshot.KmsKeyId)
   207  	d.Set("license_model", snapshot.LicenseModel)
   208  	d.Set("option_group_name", snapshot.OptionGroupName)
   209  	d.Set("port", snapshot.Port)
   210  	d.Set("source_db_snapshot_identifier", snapshot.SourceDBSnapshotIdentifier)
   211  	d.Set("source_region", snapshot.SourceRegion)
   212  	d.Set("status", snapshot.Status)
   213  	d.Set("vpc_id", snapshot.VpcId)
   214  	d.Set("snapshot_create_time", snapshot.SnapshotCreateTime.Format(time.RFC3339))
   215  
   216  	return nil
   217  }