/
data_source_dns_view.go
257 lines (229 loc) · 8.51 KB
/
data_source_dns_view.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
package solidserver
import (
"context"
"encoding/json"
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"net/url"
"regexp"
"strconv"
"strings"
)
func dataSourcednsview() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcednsviewRead,
Description: heredoc.Doc(`
DNS view data-source allows to retrieve information about DNS views.
View(s) are virutal containers mostly used to implement DNS split horizon
providing different answers depending on matching criterias.
`),
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of the DNS view.",
Required: true,
},
"dnsserver": {
Type: schema.TypeString,
Description: "The name of DNS server or DNS SMART hosting the DNS view to create.",
Required: true,
},
"order": {
Type: schema.TypeString,
Description: "The level of the DNS view, where 0 represents the highest level in the views hierarchy.",
Computed: true,
},
"recursion": {
Type: schema.TypeBool,
Description: "The recursion status of the DNS view.",
Computed: true,
},
"forward": {
Type: schema.TypeString,
Description: "The forwarding mode of the DNS view (disabled if empty).",
Computed: true,
},
"forwarders": {
Type: schema.TypeList,
Description: "The IP address list of the forwarder(s) configured on the DNS view.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"allow_transfer": {
Type: schema.TypeList,
Description: "A list of network prefixes allowed to query the DNS view for zone transfert (named ACL(s) are not supported using this provider).",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"allow_query": {
Type: schema.TypeList,
Description: "A list of network prefixes allowed to query the DNS view (named ACL(s) are not supported using this provider).",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"allow_recursion": {
Type: schema.TypeList,
Description: "A list of network prefixes allowed to query the DNS view for recursion (named ACL(s) are not supported using this provider).",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"match_clients": {
Type: schema.TypeList,
Description: "A list of network prefixes used to match the clients of the view (named ACL(s) are not supported using this provider).",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"match_to": {
Type: schema.TypeList,
Description: "A list of network prefixes used to match the traffic to the view (named ACL(s) are not supported using this provider).",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"class": {
Type: schema.TypeString,
Description: "The class associated to the DNS view.",
Computed: true,
},
"class_parameters": {
Type: schema.TypeMap,
Description: "The class parameters associated to the DNS view.",
Computed: true,
},
},
}
}
func dataSourcednsviewRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
s := meta.(*SOLIDserver)
d.SetId("")
// Building parameters
parameters := url.Values{}
parameters.Add("WHERE", "dns_name='"+d.Get("dnsserver").(string)+"' AND dnsview_name='"+d.Get("name").(string)+"'")
// Sending the read request
resp, body, err := s.Request("get", "rest/dns_view_list", ¶meters)
if err == nil {
var buf [](map[string]interface{})
json.Unmarshal([]byte(body), &buf)
// Checking the answer
if resp.StatusCode == 200 && len(buf) > 0 {
d.SetId(buf[0]["dnsview_id"].(string))
d.Set("name", buf[0]["dnsview_name"].(string))
d.Set("dnsserver", buf[0]["dns_name"].(string))
viewOrder, _ := strconv.Atoi(buf[0]["dnsview_order"].(string))
d.Set("order", viewOrder)
// Updating recursion mode
if buf[0]["dnsview_recursion"].(string) == "yes" {
d.Set("recursion", true)
} else {
d.Set("recursion", false)
}
// Updating forward mode
forward, forwardErr := dnsparamget(buf[0]["dns_name"].(string), d.Id(), "forward", meta)
if forwardErr == nil {
if forward == "" {
d.Set("forward", "none")
} else {
d.Set("forward", strings.ToLower(forward))
}
} else {
tflog.Debug(ctx, fmt.Sprintf("Unable to DNS view's forward mode (oid): %s\n", d.Id()))
d.Set("forward", "none")
}
// Updating forwarder information
forwarders, forwardersErr := dnsparamget(buf[0]["dns_name"].(string), d.Id(), "forwarders", meta)
if forwardersErr == nil {
if forwarders != "" {
d.Set("forwarders", toStringArrayInterface(strings.Split(strings.TrimSuffix(forwarders, ";"), ";")))
}
} else {
tflog.Debug(ctx, fmt.Sprintf("Unable to DNS view's forwarders list (oid): %s\n", d.Id()))
d.Set("forwarders", make([]string, 0))
}
// Only look for network prefixes, acl(s) names will be ignored during the sync process with SOLIDserver
// Building allow_transfer ACL
if buf[0]["dnsview_allow_transfer"].(string) != "" {
allowTransfers := []string{}
for _, allowTransfer := range toStringArrayInterface(strings.Split(strings.TrimSuffix(buf[0]["dnsview_allow_transfer"].(string), ";"), ";")) {
if match, _ := regexp.MatchString(regexpNetworkAcl, allowTransfer.(string)); match == true {
allowTransfers = append(allowTransfers, allowTransfer.(string))
}
}
d.Set("allow_transfer", allowTransfers)
}
// Building allow_query ACL
if buf[0]["dnsview_allow_query"].(string) != "" {
allowQueries := []string{}
for _, allowQuery := range toStringArrayInterface(strings.Split(strings.TrimSuffix(buf[0]["dnsview_allow_query"].(string), ";"), ";")) {
if match, _ := regexp.MatchString(regexpNetworkAcl, allowQuery.(string)); match == true {
allowQueries = append(allowQueries, allowQuery.(string))
}
}
d.Set("allow_query", allowQueries)
}
// Building allow_recursion ACL
if buf[0]["dnsview_allow_recursion"].(string) != "" {
allowRecursions := []string{}
for _, allowRecursion := range toStringArrayInterface(strings.Split(strings.TrimSuffix(buf[0]["dnsview_allow_recursion"].(string), ";"), ";")) {
if match, _ := regexp.MatchString(regexpNetworkAcl, allowRecursion.(string)); match == true {
allowRecursions = append(allowRecursions, allowRecursion.(string))
}
}
d.Set("allow_recursion", allowRecursions)
}
// Updating ACL information
if buf[0]["dnsview_match_clients"].(string) != "" {
matchClients := []string{}
for _, matchClient := range toStringArrayInterface(strings.Split(strings.TrimSuffix(buf[0]["dnsview_match_clients"].(string), ";"), ";")) {
if match, _ := regexp.MatchString(regexpNetworkAcl, matchClient.(string)); match == true {
matchClients = append(matchClients, matchClient.(string))
}
}
d.Set("match_clients", matchClients)
}
if buf[0]["dnsview_match_to"].(string) != "" {
matchTos := []string{}
for _, matchTo := range toStringArrayInterface(strings.Split(strings.TrimSuffix(buf[0]["dnsview_match_to"].(string), ";"), ";")) {
if match, _ := regexp.MatchString(regexpNetworkAcl, matchTo.(string)); match == true {
matchTos = append(matchTos, matchTo.(string))
}
}
d.Set("match_to", matchTos)
}
d.Set("class", buf[0]["dnsview_class_name"].(string))
// Setting local class_parameters
retrievedClassParameters, _ := url.ParseQuery(buf[0]["dnsview_class_parameters"].(string))
computedClassParameters := map[string]string{}
for ck := range retrievedClassParameters {
computedClassParameters[ck] = retrievedClassParameters[ck][0]
}
d.Set("class_parameters", computedClassParameters)
return nil
}
if len(buf) > 0 {
if errMsg, errExist := buf[0]["errmsg"].(string); errExist {
tflog.Debug(ctx, fmt.Sprintf("Unable read information from DNS view: %s (%s)\n", d.Get("name"), errMsg))
}
} else {
// Log the error
tflog.Debug(ctx, fmt.Sprintf("Unable to read information from DNS view: %s\n", d.Get("name")))
}
// Reporting a failure
return diag.Errorf("Unable to find DNS view: %s\n", d.Get("name"))
}
// Reporting a failure
return diag.FromErr(err)
}