github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/data_source_aws_ebs_snapshot.go (about)

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