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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func dataSourceAwsSecurityGroup() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsSecurityGroupRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"vpc_id": {
    18  				Type:     schema.TypeString,
    19  				Optional: true,
    20  				Computed: true,
    21  			},
    22  			"name": {
    23  				Type:     schema.TypeString,
    24  				Optional: true,
    25  				Computed: true,
    26  			},
    27  			"filter": ec2CustomFiltersSchema(),
    28  
    29  			"id": {
    30  				Type:     schema.TypeString,
    31  				Optional: true,
    32  				Computed: true,
    33  			},
    34  
    35  			"arn": {
    36  				Type:     schema.TypeString,
    37  				Computed: true,
    38  			},
    39  
    40  			"tags": tagsSchemaComputed(),
    41  		},
    42  	}
    43  }
    44  
    45  func dataSourceAwsSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
    46  	conn := meta.(*AWSClient).ec2conn
    47  	req := &ec2.DescribeSecurityGroupsInput{}
    48  
    49  	if id, idExists := d.GetOk("id"); idExists {
    50  		req.GroupIds = []*string{aws.String(id.(string))}
    51  	}
    52  
    53  	req.Filters = buildEC2AttributeFilterList(
    54  		map[string]string{
    55  			"group-name": d.Get("name").(string),
    56  			"vpc-id":     d.Get("vpc_id").(string),
    57  		},
    58  	)
    59  	req.Filters = append(req.Filters, buildEC2TagFilterList(
    60  		tagsFromMap(d.Get("tags").(map[string]interface{})),
    61  	)...)
    62  	req.Filters = append(req.Filters, buildEC2CustomFilterList(
    63  		d.Get("filter").(*schema.Set),
    64  	)...)
    65  	if len(req.Filters) == 0 {
    66  		// Don't send an empty filters list; the EC2 API won't accept it.
    67  		req.Filters = nil
    68  	}
    69  
    70  	log.Printf("[DEBUG] Describe Security Groups %v\n", req)
    71  	resp, err := conn.DescribeSecurityGroups(req)
    72  	if err != nil {
    73  		return err
    74  	}
    75  	if resp == nil || len(resp.SecurityGroups) == 0 {
    76  		return fmt.Errorf("no matching SecurityGroup found")
    77  	}
    78  	if len(resp.SecurityGroups) > 1 {
    79  		return fmt.Errorf("multiple Security Groups matched; use additional constraints to reduce matches to a single Security Group")
    80  	}
    81  
    82  	sg := resp.SecurityGroups[0]
    83  
    84  	d.SetId(*sg.GroupId)
    85  	d.Set("id", sg.VpcId)
    86  	d.Set("name", sg.GroupName)
    87  	d.Set("description", sg.Description)
    88  	d.Set("vpc_id", sg.VpcId)
    89  	d.Set("tags", tagsToMap(sg.Tags))
    90  	d.Set("arn", fmt.Sprintf("arn:%s:ec2:%s:%s/security-group/%s",
    91  		meta.(*AWSClient).partition, meta.(*AWSClient).region, *sg.OwnerId, *sg.GroupId))
    92  
    93  	return nil
    94  }