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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/service/ec2"
     7  	"github.com/hashicorp/terraform/helper/hashcode"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func dataSourceAwsEbsSnapshotIds() *schema.Resource {
    12  	return &schema.Resource{
    13  		Read: dataSourceAwsEbsSnapshotIdsRead,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"filter": dataSourceFiltersSchema(),
    17  			"owners": {
    18  				Type:     schema.TypeList,
    19  				Optional: true,
    20  				ForceNew: true,
    21  				Elem:     &schema.Schema{Type: schema.TypeString},
    22  			},
    23  			"restorable_by_user_ids": {
    24  				Type:     schema.TypeList,
    25  				Optional: true,
    26  				ForceNew: true,
    27  				Elem:     &schema.Schema{Type: schema.TypeString},
    28  			},
    29  			"tags": dataSourceTagsSchema(),
    30  			"ids": &schema.Schema{
    31  				Type:     schema.TypeList,
    32  				Computed: true,
    33  				Elem:     &schema.Schema{Type: schema.TypeString},
    34  			},
    35  		},
    36  	}
    37  }
    38  
    39  func dataSourceAwsEbsSnapshotIdsRead(d *schema.ResourceData, meta interface{}) error {
    40  	conn := meta.(*AWSClient).ec2conn
    41  
    42  	restorableUsers, restorableUsersOk := d.GetOk("restorable_by_user_ids")
    43  	filters, filtersOk := d.GetOk("filter")
    44  	owners, ownersOk := d.GetOk("owners")
    45  
    46  	if restorableUsers == false && filtersOk == false && ownersOk == false {
    47  		return fmt.Errorf("One of filters, restorable_by_user_ids, or owners must be assigned")
    48  	}
    49  
    50  	params := &ec2.DescribeSnapshotsInput{}
    51  
    52  	if restorableUsersOk {
    53  		params.RestorableByUserIds = expandStringList(restorableUsers.([]interface{}))
    54  	}
    55  	if filtersOk {
    56  		params.Filters = buildAwsDataSourceFilters(filters.(*schema.Set))
    57  	}
    58  	if ownersOk {
    59  		params.OwnerIds = expandStringList(owners.([]interface{}))
    60  	}
    61  
    62  	resp, err := conn.DescribeSnapshots(params)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	snapshotIds := make([]string, 0)
    68  
    69  	for _, snapshot := range sortSnapshots(resp.Snapshots) {
    70  		snapshotIds = append(snapshotIds, *snapshot.SnapshotId)
    71  	}
    72  
    73  	d.SetId(fmt.Sprintf("%d", hashcode.String(params.String())))
    74  	d.Set("ids", snapshotIds)
    75  
    76  	return nil
    77  }