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