forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_aws_internet_gateway.go
98 lines (86 loc) · 2.69 KB
/
data_source_aws_internet_gateway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsInternetGateway() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsInternetGatewayRead,
Schema: map[string]*schema.Schema{
"internet_gateway_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"filter": ec2CustomFiltersSchema(),
"tags": tagsSchemaComputed(),
"attachments": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"state": {
Type: schema.TypeString,
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAwsInternetGatewayRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
req := &ec2.DescribeInternetGatewaysInput{}
internetGatewayId, internetGatewayIdOk := d.GetOk("internet_gateway_id")
tags, tagsOk := d.GetOk("tags")
filter, filterOk := d.GetOk("filter")
if !internetGatewayIdOk && !filterOk && !tagsOk {
return fmt.Errorf("One of internet_gateway_id or filter or tags must be assigned")
}
req.Filters = buildEC2AttributeFilterList(map[string]string{
"internet-gateway-id": internetGatewayId.(string),
})
req.Filters = append(req.Filters, buildEC2TagFilterList(
tagsFromMap(tags.(map[string]interface{})),
)...)
req.Filters = append(req.Filters, buildEC2CustomFilterList(
filter.(*schema.Set),
)...)
log.Printf("[DEBUG] Reading Internet Gateway: %s", req)
resp, err := conn.DescribeInternetGateways(req)
if err != nil {
return err
}
if resp == nil || len(resp.InternetGateways) == 0 {
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}
if len(resp.InternetGateways) > 1 {
return fmt.Errorf("Multiple Internet Gateways matched; use additional constraints to reduce matches to a single Internet Gateway")
}
igw := resp.InternetGateways[0]
d.SetId(aws.StringValue(igw.InternetGatewayId))
d.Set("tags", tagsToMap(igw.Tags))
d.Set("internet_gateway_id", igw.InternetGatewayId)
if err := d.Set("attachments", dataSourceAttachmentsRead(igw.Attachments)); err != nil {
return err
}
return nil
}
func dataSourceAttachmentsRead(igwAttachments []*ec2.InternetGatewayAttachment) []map[string]interface{} {
attachments := make([]map[string]interface{}, 0, len(igwAttachments))
for _, a := range igwAttachments {
m := make(map[string]interface{})
m["state"] = *a.State
m["vpc_id"] = *a.VpcId
attachments = append(attachments, m)
}
return attachments
}