github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/data_source_aws_ebs_volume.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/davecgh/go-spew/spew"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func dataSourceAwsEbsVolume() *schema.Resource {
    14  	return &schema.Resource{
    15  		Read: dataSourceAwsEbsVolumeRead,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"filter": dataSourceFiltersSchema(),
    19  			"most_recent": {
    20  				Type:     schema.TypeBool,
    21  				Optional: true,
    22  				Default:  false,
    23  				ForceNew: true,
    24  			},
    25  			"availability_zone": {
    26  				Type:     schema.TypeString,
    27  				Computed: true,
    28  			},
    29  			"encrypted": {
    30  				Type:     schema.TypeBool,
    31  				Computed: true,
    32  			},
    33  			"iops": {
    34  				Type:     schema.TypeInt,
    35  				Computed: true,
    36  			},
    37  			"volume_type": {
    38  				Type:     schema.TypeString,
    39  				Computed: true,
    40  			},
    41  			"size": {
    42  				Type:     schema.TypeInt,
    43  				Computed: true,
    44  			},
    45  			"snapshot_id": {
    46  				Type:     schema.TypeString,
    47  				Computed: true,
    48  			},
    49  			"kms_key_id": {
    50  				Type:     schema.TypeString,
    51  				Computed: true,
    52  			},
    53  			"volume_id": {
    54  				Type:     schema.TypeString,
    55  				Computed: true,
    56  			},
    57  			"tags": dataSourceTagsSchema(),
    58  		},
    59  	}
    60  }
    61  
    62  func dataSourceAwsEbsVolumeRead(d *schema.ResourceData, meta interface{}) error {
    63  	conn := meta.(*AWSClient).ec2conn
    64  
    65  	filters, filtersOk := d.GetOk("filter")
    66  
    67  	params := &ec2.DescribeVolumesInput{}
    68  	if filtersOk {
    69  		params.Filters = buildAwsDataSourceFilters(filters.(*schema.Set))
    70  	}
    71  
    72  	resp, err := conn.DescribeVolumes(params)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	log.Printf("Found These Volumes %s", spew.Sdump(resp.Volumes))
    78  
    79  	filteredVolumes := resp.Volumes[:]
    80  
    81  	var volume *ec2.Volume
    82  	if len(filteredVolumes) < 1 {
    83  		return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
    84  	}
    85  
    86  	if len(filteredVolumes) > 1 {
    87  		recent := d.Get("most_recent").(bool)
    88  		log.Printf("[DEBUG] aws_ebs_volume - multiple results found and `most_recent` is set to: %t", recent)
    89  		if recent {
    90  			volume = mostRecentVolume(filteredVolumes)
    91  		} else {
    92  			return fmt.Errorf("Your query returned more than one result. Please try a more " +
    93  				"specific search criteria, or set `most_recent` attribute to true.")
    94  		}
    95  	} else {
    96  		// Query returned single result.
    97  		volume = filteredVolumes[0]
    98  	}
    99  
   100  	log.Printf("[DEBUG] aws_ebs_volume - Single Volume found: %s", *volume.VolumeId)
   101  	return volumeDescriptionAttributes(d, volume)
   102  }
   103  
   104  type volumeSort []*ec2.Volume
   105  
   106  func (a volumeSort) Len() int      { return len(a) }
   107  func (a volumeSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
   108  func (a volumeSort) Less(i, j int) bool {
   109  	itime := *a[i].CreateTime
   110  	jtime := *a[j].CreateTime
   111  	return itime.Unix() < jtime.Unix()
   112  }
   113  
   114  func mostRecentVolume(volumes []*ec2.Volume) *ec2.Volume {
   115  	sortedVolumes := volumes
   116  	sort.Sort(volumeSort(sortedVolumes))
   117  	return sortedVolumes[len(sortedVolumes)-1]
   118  }
   119  
   120  func volumeDescriptionAttributes(d *schema.ResourceData, volume *ec2.Volume) error {
   121  	d.SetId(*volume.VolumeId)
   122  	d.Set("volume_id", volume.VolumeId)
   123  	d.Set("availability_zone", volume.AvailabilityZone)
   124  	d.Set("encrypted", volume.Encrypted)
   125  	d.Set("iops", volume.Iops)
   126  	d.Set("kms_key_id", volume.KmsKeyId)
   127  	d.Set("size", volume.Size)
   128  	d.Set("snapshot_id", volume.SnapshotId)
   129  	d.Set("volume_type", volume.VolumeType)
   130  
   131  	if err := d.Set("tags", dataSourceTags(volume.Tags)); err != nil {
   132  		return err
   133  	}
   134  
   135  	return nil
   136  }