github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/aws/data_source_aws_elastic_beanstalk_solution_stack.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  
     8  	"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func dataSourceAwsElasticBeanstalkSolutionStack() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsElasticBeanstalkSolutionStackRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"name_regex": {
    18  				Type:         schema.TypeString,
    19  				Required:     true,
    20  				ForceNew:     true,
    21  				ValidateFunc: validateSolutionStackNameRegex,
    22  			},
    23  			"most_recent": {
    24  				Type:     schema.TypeBool,
    25  				Optional: true,
    26  				Default:  false,
    27  				ForceNew: true,
    28  			},
    29  			// Computed values.
    30  			"name": {
    31  				Type:     schema.TypeString,
    32  				Computed: true,
    33  			},
    34  		},
    35  	}
    36  }
    37  
    38  // dataSourceAwsElasticBeanstalkSolutionStackRead performs the API lookup.
    39  func dataSourceAwsElasticBeanstalkSolutionStackRead(d *schema.ResourceData, meta interface{}) error {
    40  	conn := meta.(*AWSClient).elasticbeanstalkconn
    41  
    42  	nameRegex := d.Get("name_regex")
    43  
    44  	var params *elasticbeanstalk.ListAvailableSolutionStacksInput
    45  
    46  	resp, err := conn.ListAvailableSolutionStacks(params)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	var filteredSolutionStacks []*string
    52  
    53  	r := regexp.MustCompile(nameRegex.(string))
    54  	for _, solutionStack := range resp.SolutionStacks {
    55  		if r.MatchString(*solutionStack) {
    56  			filteredSolutionStacks = append(filteredSolutionStacks, solutionStack)
    57  		}
    58  	}
    59  
    60  	var solutionStack *string
    61  	if len(filteredSolutionStacks) < 1 {
    62  		return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
    63  	}
    64  
    65  	if len(filteredSolutionStacks) == 1 {
    66  		// Query returned single result.
    67  		solutionStack = filteredSolutionStacks[0]
    68  	} else {
    69  		recent := d.Get("most_recent").(bool)
    70  		log.Printf("[DEBUG] aws_elastic_beanstalk_solution_stack - multiple results found and `most_recent` is set to: %t", recent)
    71  		if recent {
    72  			solutionStack = mostRecentSolutionStack(filteredSolutionStacks)
    73  		} else {
    74  			return fmt.Errorf("Your query returned more than one result. Please try a more " +
    75  				"specific search criteria, or set `most_recent` attribute to true.")
    76  		}
    77  	}
    78  
    79  	log.Printf("[DEBUG] aws_elastic_beanstalk_solution_stack - Single solution stack found: %s", *solutionStack)
    80  	return solutionStackDescriptionAttributes(d, solutionStack)
    81  }
    82  
    83  // Returns the most recent solution stack out of a slice of stacks.
    84  func mostRecentSolutionStack(solutionStacks []*string) *string {
    85  	return solutionStacks[0]
    86  }
    87  
    88  // populate the numerous fields that the image description returns.
    89  func solutionStackDescriptionAttributes(d *schema.ResourceData, solutionStack *string) error {
    90  	// Simple attributes first
    91  	d.SetId(*solutionStack)
    92  	d.Set("name", solutionStack)
    93  	return nil
    94  }
    95  
    96  func validateSolutionStackNameRegex(v interface{}, k string) (ws []string, errors []error) {
    97  	value := v.(string)
    98  
    99  	if _, err := regexp.Compile(value); err != nil {
   100  		errors = append(errors, fmt.Errorf(
   101  			"%q contains an invalid regular expression: %s",
   102  			k, err))
   103  	}
   104  	return
   105  }