github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/data_source_aws_alb.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/service/elbv2"
     8  	"github.com/hashicorp/errwrap"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func dataSourceAwsAlb() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsAlbRead,
    15  		Schema: map[string]*schema.Schema{
    16  			"arn": {
    17  				Type:     schema.TypeString,
    18  				Optional: true,
    19  				Computed: true,
    20  			},
    21  
    22  			"arn_suffix": {
    23  				Type:     schema.TypeString,
    24  				Computed: true,
    25  			},
    26  
    27  			"name": {
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  				Computed: true,
    31  			},
    32  
    33  			"internal": {
    34  				Type:     schema.TypeBool,
    35  				Computed: true,
    36  			},
    37  
    38  			"security_groups": {
    39  				Type:     schema.TypeSet,
    40  				Elem:     &schema.Schema{Type: schema.TypeString},
    41  				Computed: true,
    42  				Set:      schema.HashString,
    43  			},
    44  
    45  			"subnets": {
    46  				Type:     schema.TypeSet,
    47  				Elem:     &schema.Schema{Type: schema.TypeString},
    48  				Computed: true,
    49  				Set:      schema.HashString,
    50  			},
    51  
    52  			"access_logs": {
    53  				Type:     schema.TypeList,
    54  				Computed: true,
    55  				MaxItems: 1,
    56  				Elem: &schema.Resource{
    57  					Schema: map[string]*schema.Schema{
    58  						"bucket": {
    59  							Type:     schema.TypeString,
    60  							Computed: true,
    61  						},
    62  						"prefix": {
    63  							Type:     schema.TypeString,
    64  							Computed: true,
    65  						},
    66  						"enabled": {
    67  							Type:     schema.TypeBool,
    68  							Computed: true,
    69  						},
    70  					},
    71  				},
    72  			},
    73  
    74  			"enable_deletion_protection": {
    75  				Type:     schema.TypeBool,
    76  				Computed: true,
    77  			},
    78  
    79  			"idle_timeout": {
    80  				Type:     schema.TypeInt,
    81  				Computed: true,
    82  			},
    83  
    84  			"vpc_id": {
    85  				Type:     schema.TypeString,
    86  				Computed: true,
    87  			},
    88  
    89  			"zone_id": {
    90  				Type:     schema.TypeString,
    91  				Computed: true,
    92  			},
    93  
    94  			"dns_name": {
    95  				Type:     schema.TypeString,
    96  				Computed: true,
    97  			},
    98  
    99  			"tags": tagsSchemaComputed(),
   100  		},
   101  	}
   102  }
   103  
   104  func dataSourceAwsAlbRead(d *schema.ResourceData, meta interface{}) error {
   105  	elbconn := meta.(*AWSClient).elbv2conn
   106  	albArn := d.Get("arn").(string)
   107  	albName := d.Get("name").(string)
   108  
   109  	describeAlbOpts := &elbv2.DescribeLoadBalancersInput{}
   110  	switch {
   111  	case albArn != "":
   112  		describeAlbOpts.LoadBalancerArns = []*string{aws.String(albArn)}
   113  	case albName != "":
   114  		describeAlbOpts.Names = []*string{aws.String(albName)}
   115  	}
   116  
   117  	describeResp, err := elbconn.DescribeLoadBalancers(describeAlbOpts)
   118  	if err != nil {
   119  		return errwrap.Wrapf("Error retrieving ALB: {{err}}", err)
   120  	}
   121  	if len(describeResp.LoadBalancers) != 1 {
   122  		return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(describeResp.LoadBalancers))
   123  	}
   124  	d.SetId(*describeResp.LoadBalancers[0].LoadBalancerArn)
   125  
   126  	return flattenAwsAlbResource(d, meta, describeResp.LoadBalancers[0])
   127  }