github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/data_source_aws_ebs_snapshot.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"sort"
     7  
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func dataSourceAwsEbsSnapshot() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsEbsSnapshotRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			//selection criteria
    18  			"filter": dataSourceFiltersSchema(),
    19  			"most_recent": {
    20  				Type:     schema.TypeBool,
    21  				Optional: true,
    22  				Default:  false,
    23  				ForceNew: true,
    24  			},
    25  			"owners": {
    26  				Type:     schema.TypeList,
    27  				Optional: true,
    28  				ForceNew: true,
    29  				Elem:     &schema.Schema{Type: schema.TypeString},
    30  			},
    31  			"snapshot_ids": {
    32  				Type:     schema.TypeList,
    33  				Optional: true,
    34  				ForceNew: true,
    35  				Elem:     &schema.Schema{Type: schema.TypeString},
    36  			},
    37  			"restorable_by_user_ids": {
    38  				Type:     schema.TypeList,
    39  				Optional: true,
    40  				ForceNew: true,
    41  				Elem:     &schema.Schema{Type: schema.TypeString},
    42  			},
    43  			//Computed values returned
    44  			"snapshot_id": {
    45  				Type:     schema.TypeString,
    46  				Computed: true,
    47  			},
    48  			"volume_id": {
    49  				Type:     schema.TypeString,
    50  				Computed: true,
    51  			},
    52  			"state": {
    53  				Type:     schema.TypeString,
    54  				Computed: true,
    55  			},
    56  			"owner_id": {
    57  				Type:     schema.TypeString,
    58  				Computed: true,
    59  			},
    60  			"owner_alias": {
    61  				Type:     schema.TypeString,
    62  				Computed: true,
    63  			},
    64  			"encrypted": {
    65  				Type:     schema.TypeBool,
    66  				Computed: true,
    67  			},
    68  			"description": {
    69  				Type:     schema.TypeString,
    70  				Computed: true,
    71  			},
    72  			"volume_size": {
    73  				Type:     schema.TypeInt,
    74  				Computed: true,
    75  			},
    76  			"kms_key_id": {
    77  				Type:     schema.TypeString,
    78  				Computed: true,
    79  			},
    80  			"data_encryption_key_id": {
    81  				Type:     schema.TypeString,
    82  				Computed: true,
    83  			},
    84  			"tags": dataSourceTagsSchema(),
    85  		},
    86  	}
    87  }
    88  
    89  func dataSourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) error {
    90  	conn := meta.(*AWSClient).ec2conn
    91  
    92  	restorableUsers, restorableUsersOk := d.GetOk("restorable_by_user_ids")
    93  	filters, filtersOk := d.GetOk("filter")
    94  	snapshotIds, snapshotIdsOk := d.GetOk("snapshot_ids")
    95  	owners, ownersOk := d.GetOk("owners")
    96  
    97  	if restorableUsers == false && filtersOk == false && snapshotIds == false && ownersOk == false {
    98  		return fmt.Errorf("One of snapshot_ids, filters, restorable_by_user_ids, or owners must be assigned")
    99  	}
   100  
   101  	params := &ec2.DescribeSnapshotsInput{}
   102  	if restorableUsersOk {
   103  		params.RestorableByUserIds = expandStringList(restorableUsers.([]interface{}))
   104  	}
   105  	if filtersOk {
   106  		params.Filters = buildAwsDataSourceFilters(filters.(*schema.Set))
   107  	}
   108  	if ownersOk {
   109  		params.OwnerIds = expandStringList(owners.([]interface{}))
   110  	}
   111  	if snapshotIdsOk {
   112  		params.SnapshotIds = expandStringList(snapshotIds.([]interface{}))
   113  	}
   114  
   115  	resp, err := conn.DescribeSnapshots(params)
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	var snapshot *ec2.Snapshot
   121  	if len(resp.Snapshots) < 1 {
   122  		return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
   123  	}
   124  
   125  	if len(resp.Snapshots) > 1 {
   126  		recent := d.Get("most_recent").(bool)
   127  		log.Printf("[DEBUG] aws_ebs_snapshot - multiple results found and `most_recent` is set to: %t", recent)
   128  		if recent {
   129  			snapshot = mostRecentSnapshot(resp.Snapshots)
   130  		} else {
   131  			return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria.")
   132  		}
   133  	} else {
   134  		snapshot = resp.Snapshots[0]
   135  	}
   136  
   137  	//Single Snapshot found so set to state
   138  	return snapshotDescriptionAttributes(d, snapshot)
   139  }
   140  
   141  type snapshotSort []*ec2.Snapshot
   142  
   143  func (a snapshotSort) Len() int      { return len(a) }
   144  func (a snapshotSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   145  func (a snapshotSort) Less(i, j int) bool {
   146  	itime := *a[i].StartTime
   147  	jtime := *a[j].StartTime
   148  	return itime.Unix() < jtime.Unix()
   149  }
   150  
   151  func mostRecentSnapshot(snapshots []*ec2.Snapshot) *ec2.Snapshot {
   152  	sortedSnapshots := snapshots
   153  	sort.Sort(snapshotSort(sortedSnapshots))
   154  	return sortedSnapshots[len(sortedSnapshots)-1]
   155  }
   156  
   157  func snapshotDescriptionAttributes(d *schema.ResourceData, snapshot *ec2.Snapshot) error {
   158  	d.SetId(*snapshot.SnapshotId)
   159  	d.Set("snapshot_id", snapshot.SnapshotId)
   160  	d.Set("volume_id", snapshot.VolumeId)
   161  	d.Set("data_encryption_key_id", snapshot.DataEncryptionKeyId)
   162  	d.Set("description", snapshot.Description)
   163  	d.Set("encrypted", snapshot.Encrypted)
   164  	d.Set("kms_key_id", snapshot.KmsKeyId)
   165  	d.Set("volume_size", snapshot.VolumeSize)
   166  	d.Set("state", snapshot.State)
   167  	d.Set("owner_id", snapshot.OwnerId)
   168  	d.Set("owner_alias", snapshot.OwnerAlias)
   169  
   170  	if err := d.Set("tags", dataSourceTags(snapshot.Tags)); err != nil {
   171  		return err
   172  	}
   173  
   174  	return nil
   175  }