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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/hashcode"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func dataSourceAwsAmiIds() *schema.Resource {
    14  	return &schema.Resource{
    15  		Read: dataSourceAwsAmiIdsRead,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"filter": dataSourceFiltersSchema(),
    19  			"executable_users": {
    20  				Type:     schema.TypeList,
    21  				Optional: true,
    22  				ForceNew: true,
    23  				Elem:     &schema.Schema{Type: schema.TypeString},
    24  			},
    25  			"name_regex": {
    26  				Type:         schema.TypeString,
    27  				Optional:     true,
    28  				ForceNew:     true,
    29  				ValidateFunc: validateNameRegex,
    30  			},
    31  			"owners": {
    32  				Type:     schema.TypeList,
    33  				Optional: true,
    34  				ForceNew: true,
    35  				Elem:     &schema.Schema{Type: schema.TypeString},
    36  			},
    37  			"tags": dataSourceTagsSchema(),
    38  			"ids": &schema.Schema{
    39  				Type:     schema.TypeList,
    40  				Computed: true,
    41  				Elem:     &schema.Schema{Type: schema.TypeString},
    42  			},
    43  		},
    44  	}
    45  }
    46  
    47  func dataSourceAwsAmiIdsRead(d *schema.ResourceData, meta interface{}) error {
    48  	conn := meta.(*AWSClient).ec2conn
    49  
    50  	executableUsers, executableUsersOk := d.GetOk("executable_users")
    51  	filters, filtersOk := d.GetOk("filter")
    52  	nameRegex, nameRegexOk := d.GetOk("name_regex")
    53  	owners, ownersOk := d.GetOk("owners")
    54  
    55  	if executableUsersOk == false && filtersOk == false && nameRegexOk == false && ownersOk == false {
    56  		return fmt.Errorf("One of executable_users, filters, name_regex, or owners must be assigned")
    57  	}
    58  
    59  	params := &ec2.DescribeImagesInput{}
    60  
    61  	if executableUsersOk {
    62  		params.ExecutableUsers = expandStringList(executableUsers.([]interface{}))
    63  	}
    64  	if filtersOk {
    65  		params.Filters = buildAwsDataSourceFilters(filters.(*schema.Set))
    66  	}
    67  	if ownersOk {
    68  		o := expandStringList(owners.([]interface{}))
    69  
    70  		if len(o) > 0 {
    71  			params.Owners = o
    72  		}
    73  	}
    74  
    75  	resp, err := conn.DescribeImages(params)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	var filteredImages []*ec2.Image
    81  	imageIds := make([]string, 0)
    82  
    83  	if nameRegexOk {
    84  		r := regexp.MustCompile(nameRegex.(string))
    85  		for _, image := range resp.Images {
    86  			// Check for a very rare case where the response would include no
    87  			// image name. No name means nothing to attempt a match against,
    88  			// therefore we are skipping such image.
    89  			if image.Name == nil || *image.Name == "" {
    90  				log.Printf("[WARN] Unable to find AMI name to match against "+
    91  					"for image ID %q owned by %q, nothing to do.",
    92  					*image.ImageId, *image.OwnerId)
    93  				continue
    94  			}
    95  			if r.MatchString(*image.Name) {
    96  				filteredImages = append(filteredImages, image)
    97  			}
    98  		}
    99  	} else {
   100  		filteredImages = resp.Images[:]
   101  	}
   102  
   103  	for _, image := range sortImages(filteredImages) {
   104  		imageIds = append(imageIds, *image.ImageId)
   105  	}
   106  
   107  	d.SetId(fmt.Sprintf("%d", hashcode.String(params.String())))
   108  	d.Set("ids", imageIds)
   109  
   110  	return nil
   111  }