-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_dns_zones.go
274 lines (251 loc) · 7.4 KB
/
data_source_huaweicloud_dns_zones.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product DNS
// ---------------------------------------------------------------
package dns
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/pagination"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API DNS GET /v2/zones
func DataSourceZones() *schema.Resource {
return &schema.Resource{
ReadContext: resourceZonesRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"zone_type": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the zone type. The value can be **public** or **private**.`,
},
"tags": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the resource tag.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the zone name.`,
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the zone status.`,
},
"search_mode": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the query criteria search mode.`,
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the enterprise project ID which the zone associated.`,
},
"zones": {
Type: schema.TypeList,
Elem: zoneSchema(),
Computed: true,
Description: `The list of zones.`,
},
},
}
}
func zoneSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The zone ID.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The zone name.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: `The zone description.`,
},
"email": {
Type: schema.TypeString,
Computed: true,
Description: `The email address of the administrator managing the zone.`,
},
"zone_type": {
Type: schema.TypeString,
Computed: true,
Description: `The zone type.`,
},
"ttl": {
Type: schema.TypeInt,
Computed: true,
Description: `The time to live (TTL) of the zone.`,
},
"enterprise_project_id": {
Type: schema.TypeString,
Computed: true,
Description: `The enterprise project ID.`,
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: `The zone status.`,
},
"record_num": {
Type: schema.TypeInt,
Computed: true,
Description: `The number of record sets in the zone.`,
},
"masters": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: `The master DNS servers, from which the slave servers get DNS information.`,
},
"routers": {
Type: schema.TypeList,
Elem: zoneRouterSchema(),
Computed: true,
Description: `The list of VPCs associated with the zone.`,
},
"tags": common.TagsComputedSchema(),
},
}
return &sc
}
func zoneRouterSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"router_id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the VPC associated with the zone.`,
},
"router_region": {
Type: schema.TypeString,
Computed: true,
Description: `The region of the VPC.`,
},
},
}
return &sc
}
func resourceZonesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
mErr *multierror.Error
listHttpUrl = "v2/zones"
dnsProduct = "dns_region"
)
client, err := cfg.NewServiceClient(dnsProduct, region)
if err != nil {
return diag.Errorf("error creating DNS client: %s", err)
}
listPath := client.Endpoint + listHttpUrl
listPath += buildListZonesQueryParams(d)
listResp, err := pagination.ListAllItems(
client,
"offset",
listPath,
&pagination.QueryOpts{MarkerField: ""})
if err != nil {
return diag.Errorf("error retrieving DNS zones, %s", err)
}
listRespJson, err := json.Marshal(listResp)
if err != nil {
return diag.FromErr(err)
}
var listRespBody interface{}
err = json.Unmarshal(listRespJson, &listRespBody)
if err != nil {
return diag.FromErr(err)
}
generateUUID, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(generateUUID)
mErr = multierror.Append(
mErr,
d.Set("region", region),
d.Set("zones", flattenListZones(listRespBody)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func flattenListZones(resp interface{}) []interface{} {
if resp == nil {
return nil
}
curJson := utils.PathSearch("zones", resp, make([]interface{}, 0))
curArray := curJson.([]interface{})
rst := make([]interface{}, len(curArray))
for i, v := range curArray {
rst[i] = map[string]interface{}{
"id": utils.PathSearch("id", v, nil),
"name": utils.PathSearch("name", v, nil),
"description": utils.PathSearch("description", v, nil),
"email": utils.PathSearch("email", v, nil),
"zone_type": utils.PathSearch("zone_type", v, nil),
"ttl": utils.PathSearch("ttl", v, nil),
"enterprise_project_id": utils.PathSearch("enterprise_project_id", v, nil),
"status": utils.PathSearch("status", v, nil),
"record_num": utils.PathSearch("record_num", v, nil),
"masters": utils.PathSearch("masters", v, nil),
"routers": flattenZonesRouters(v),
"tags": utils.FlattenTagsToMap(utils.PathSearch("tags", v, nil)),
}
}
return rst
}
func flattenZonesRouters(resp interface{}) []interface{} {
if resp == nil {
return nil
}
curJson := utils.PathSearch("routers", resp, make([]interface{}, 0))
curArray := curJson.([]interface{})
rst := make([]interface{}, len(curArray))
for i, v := range curArray {
rst[i] = map[string]interface{}{
"router_id": utils.PathSearch("router_id", v, nil),
"router_region": utils.PathSearch("router_region", v, nil),
}
}
return rst
}
func buildListZonesQueryParams(d *schema.ResourceData) string {
queryParam := fmt.Sprintf("?type=%v", d.Get("zone_type"))
if v, ok := d.GetOk("tags"); ok {
queryParam = fmt.Sprintf("%s&tags=%v", queryParam, v)
}
if v, ok := d.GetOk("name"); ok {
queryParam = fmt.Sprintf("%s&name=%v", queryParam, v)
}
if v, ok := d.GetOk("status"); ok {
queryParam = fmt.Sprintf("%s&status=%v", queryParam, v)
}
if v, ok := d.GetOk("search_mode"); ok {
queryParam = fmt.Sprintf("%s&search_mode=%v", queryParam, v)
}
if v, ok := d.GetOk("enterprise_project_id"); ok {
queryParam = fmt.Sprintf("%s&enterprise_project_id=%v", queryParam, v)
}
return queryParam
}