forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_aws_route53_zone.go
176 lines (159 loc) · 4.64 KB
/
data_source_aws_route53_zone.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package aws
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsRoute53Zone() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsRoute53ZoneRead,
Schema: map[string]*schema.Schema{
"zone_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"private_zone": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"comment": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"caller_reference": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tags": tagsSchemaComputed(),
"resource_record_set_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
},
}
}
func dataSourceAwsRoute53ZoneRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).r53conn
name, nameExists := d.GetOk("name")
name = hostedZoneName(name.(string))
id, idExists := d.GetOk("zone_id")
vpcId, vpcIdExists := d.GetOk("vpc_id")
tags := tagsFromMap(d.Get("tags").(map[string]interface{}))
if nameExists && idExists {
return fmt.Errorf("zone_id and name arguments can't be used together")
} else if !nameExists && !idExists {
return fmt.Errorf("Either name or zone_id must be set")
}
var nextMarker *string
var hostedZoneFound *route53.HostedZone
// We loop through all hostedzone
for allHostedZoneListed := false; !allHostedZoneListed; {
req := &route53.ListHostedZonesInput{}
if nextMarker != nil {
req.Marker = nextMarker
}
resp, err := conn.ListHostedZones(req)
if err != nil {
return fmt.Errorf("Error finding Route 53 Hosted Zone: %v", err)
}
for _, hostedZone := range resp.HostedZones {
hostedZoneId := cleanZoneID(*hostedZone.Id)
if idExists && hostedZoneId == id.(string) {
hostedZoneFound = hostedZone
break
// we check if the name is the same as requested and if private zone field is the same as requested or if there is a vpc_id
} else if *hostedZone.Name == name && (*hostedZone.Config.PrivateZone == d.Get("private_zone").(bool) || (*hostedZone.Config.PrivateZone == true && vpcIdExists)) {
matchingVPC := false
if vpcIdExists {
reqHostedZone := &route53.GetHostedZoneInput{}
reqHostedZone.Id = aws.String(hostedZoneId)
respHostedZone, errHostedZone := conn.GetHostedZone(reqHostedZone)
if errHostedZone != nil {
return fmt.Errorf("Error finding Route 53 Hosted Zone: %v", errHostedZone)
}
// we go through all VPCs
for _, vpc := range respHostedZone.VPCs {
if *vpc.VPCId == vpcId.(string) {
matchingVPC = true
break
}
}
} else {
matchingVPC = true
}
// we check if tags match
matchingTags := true
if len(tags) > 0 {
reqListTags := &route53.ListTagsForResourceInput{}
reqListTags.ResourceId = aws.String(hostedZoneId)
reqListTags.ResourceType = aws.String("hostedzone")
respListTags, errListTags := conn.ListTagsForResource(reqListTags)
if errListTags != nil {
return fmt.Errorf("Error finding Route 53 Hosted Zone: %v", errListTags)
}
for _, tag := range tags {
found := false
for _, tagRequested := range respListTags.ResourceTagSet.Tags {
if *tag.Key == *tagRequested.Key && *tag.Value == *tagRequested.Value {
found = true
}
}
if !found {
matchingTags = false
break
}
}
}
if matchingTags && matchingVPC {
if hostedZoneFound != nil {
return fmt.Errorf("multiple Route53Zone found please use vpc_id option to filter")
} else {
hostedZoneFound = hostedZone
}
}
}
}
if *resp.IsTruncated {
nextMarker = resp.NextMarker
} else {
allHostedZoneListed = true
}
}
if hostedZoneFound == nil {
return fmt.Errorf("no matching Route53Zone found")
}
idHostedZone := cleanZoneID(*hostedZoneFound.Id)
d.SetId(idHostedZone)
d.Set("zone_id", idHostedZone)
d.Set("name", hostedZoneFound.Name)
d.Set("comment", hostedZoneFound.Config.Comment)
d.Set("private_zone", hostedZoneFound.Config.PrivateZone)
d.Set("caller_reference", hostedZoneFound.CallerReference)
d.Set("resource_record_set_count", hostedZoneFound.ResourceRecordSetCount)
return nil
}
// used to manage trailing .
func hostedZoneName(name string) string {
if strings.HasSuffix(name, ".") {
return name
} else {
return name + "."
}
}