This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
data_source_openstack_dns_zone_v2.go
204 lines (165 loc) · 4.07 KB
/
data_source_openstack_dns_zone_v2.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package openstack
import (
"fmt"
"log"
"time"
"github.com/gophercloud/gophercloud/openstack/dns/v2/zones"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceDNSZoneV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceDNSZoneV2Read,
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"pool_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"project_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"email": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"status": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"ttl": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"version": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"serial": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"updated_at": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"transferred_at": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"attributes": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Computed: true,
},
"masters": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceDNSZoneV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
dnsClient, err := config.dnsV2Client(GetRegion(d, config))
if err != nil {
return err
}
listOpts := zones.ListOpts{}
if v, ok := d.GetOk("name"); ok {
listOpts.Name = v.(string)
}
if v, ok := d.GetOk("description"); ok {
listOpts.Description = v.(string)
}
if v, ok := d.GetOk("email"); ok {
listOpts.Email = v.(string)
}
if v, ok := d.GetOk("status"); ok {
listOpts.Status = v.(string)
}
if v, ok := d.GetOk("ttl"); ok {
listOpts.TTL = v.(int)
}
if v, ok := d.GetOk("type"); ok {
listOpts.Type = v.(string)
}
pages, err := zones.List(dnsClient, listOpts).AllPages()
if err != nil {
return fmt.Errorf("Unable to retrieve zones: %s", err)
}
allZones, err := zones.ExtractZones(pages)
if err != nil {
return fmt.Errorf("Unable to extract zones: %s", err)
}
if len(allZones) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(allZones) > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}
zone := allZones[0]
log.Printf("[DEBUG] Retrieved DNS Zone %s: %+v", zone.ID, zone)
d.SetId(zone.ID)
// strings
d.Set("name", zone.Name)
d.Set("pool_id", zone.PoolID)
d.Set("project_id", zone.ProjectID)
d.Set("email", zone.Email)
d.Set("description", zone.Description)
d.Set("status", zone.Status)
d.Set("type", zone.Type)
d.Set("region", GetRegion(d, config))
// ints
d.Set("ttl", zone.TTL)
d.Set("version", zone.Version)
d.Set("serial", zone.Serial)
// time.Times
d.Set("created_at", zone.CreatedAt.Format(time.RFC3339))
d.Set("updated_at", zone.UpdatedAt.Format(time.RFC3339))
d.Set("transferred_at", zone.TransferredAt.Format(time.RFC3339))
// maps
err = d.Set("attributes", zone.Attributes)
if err != nil {
log.Printf("[DEBUG] Unable to set attributes: %s", err)
return err
}
// slices
err = d.Set("masters", zone.Masters)
if err != nil {
log.Printf("[DEBUG] Unable to set masters: %s", err)
return err
}
return nil
}