forked from DataDog/terraform-provider-datadog
/
data_source_datadog_ip_ranges.go
200 lines (183 loc) · 7.28 KB
/
data_source_datadog_ip_ranges.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
package datadog
import (
"context"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/Telemetron/terraform-provider-datadog/v3/datadog/internal/utils"
)
func dataSourceDatadogIPRanges() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to retrieve information about Datadog's IP addresses.",
ReadContext: dataSourceDatadogIPRangesRead,
// IP ranges are divided between ipv4 and ipv6
Schema: map[string]*schema.Schema{
// v4
"agents_ipv4": {
Description: "An Array of IPv4 addresses in CIDR format specifying the A records for the Agent endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"api_ipv4": {
Description: "An Array of IPv4 addresses in CIDR format specifying the A records for the API endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"apm_ipv4": {
Description: "An Array of IPv4 addresses in CIDR format specifying the A records for the APM endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"logs_ipv4": {
Description: "An Array of IPv4 addresses in CIDR format specifying the A records for the Logs endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"process_ipv4": {
Description: "An Array of IPv4 addresses in CIDR format specifying the A records for the Process endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"synthetics_ipv4": {
Description: "An Array of IPv4 addresses in CIDR format specifying the A records for the Synthetics endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"synthetics_ipv4_by_location": {
Description: "A map of IPv4 prefixes (string of concatenated IPs, delimited by ',') by location.",
Type: schema.TypeMap,
Computed: true,
},
"webhooks_ipv4": {
Description: "An Array of IPv4 addresses in CIDR format specifying the A records for the Webhooks endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
// v6
"agents_ipv6": {
Description: "An Array of IPv6 addresses in CIDR format specifying the A records for the Agent endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"api_ipv6": {
Description: "An Array of IPv6 addresses in CIDR format specifying the A records for the API endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"apm_ipv6": {
Description: "An Array of IPv6 addresses in CIDR format specifying the A records for the APM endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"logs_ipv6": {
Description: "An Array of IPv6 addresses in CIDR format specifying the A records for the Logs endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"process_ipv6": {
Description: "An Array of IPv6 addresses in CIDR format specifying the A records for the Process endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"synthetics_ipv6": {
Description: "An Array of IPv6 addresses in CIDR format specifying the A records for the Synthetics endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"synthetics_ipv6_by_location": {
Description: "A map of IPv6 prefixes (string of concatenated IPs, delimited by ',') by location.",
Type: schema.TypeMap,
Computed: true,
},
"webhooks_ipv6": {
Description: "An Array of IPv6 addresses in CIDR format specifying the A records for the Webhooks endpoint.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceDatadogIPRangesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1
ipAddresses, _, err := datadogClientV1.IPRangesApi.GetIPRanges(authV1)
if err != nil {
return diag.FromErr(err)
}
if err := utils.CheckForUnparsed(ipAddresses); err != nil {
return diag.FromErr(err)
}
// v4 and v6
ipAddressesPtr := &ipAddresses
agents := ipAddressesPtr.GetAgents()
api := ipAddressesPtr.GetApi()
apm := ipAddressesPtr.GetApm()
logs := ipAddressesPtr.GetLogs()
process := ipAddressesPtr.GetProcess()
synthetics := ipAddressesPtr.GetSynthetics()
webhook := ipAddressesPtr.GetWebhooks()
if len(agents.GetPrefixesIpv4())+len(api.GetPrefixesIpv4())+
len(apm.GetPrefixesIpv4())+len(logs.GetPrefixesIpv4())+
len(process.GetPrefixesIpv4())+len(synthetics.GetPrefixesIpv4())+
len(webhook.GetPrefixesIpv4())+len(agents.GetPrefixesIpv6())+
len(api.GetPrefixesIpv6())+len(apm.GetPrefixesIpv6())+
len(logs.GetPrefixesIpv6())+len(process.GetPrefixesIpv6())+
len(synthetics.GetPrefixesIpv6())+len(webhook.GetPrefixesIpv6())+
len(synthetics.GetPrefixesIpv4ByLocation())+len(synthetics.GetPrefixesIpv6ByLocation()) > 0 {
d.SetId("datadog-ip-ranges")
}
// Set ranges when the list is not empty
// v4
d.Set("agents_ipv4", agents.GetPrefixesIpv4())
d.Set("api_ipv4", api.GetPrefixesIpv4())
d.Set("apm_ipv4", apm.GetPrefixesIpv4())
d.Set("logs_ipv4", logs.GetPrefixesIpv4())
d.Set("process_ipv4", process.GetPrefixesIpv4())
d.Set("synthetics_ipv4", synthetics.GetPrefixesIpv4())
d.Set("webhooks_ipv4", webhook.GetPrefixesIpv4())
// v6
d.Set("agents_ipv6", agents.GetPrefixesIpv6())
d.Set("api_ipv6", api.GetPrefixesIpv6())
d.Set("apm_ipv6", apm.GetPrefixesIpv6())
d.Set("logs_ipv6", logs.GetPrefixesIpv6())
d.Set("process_ipv6", process.GetPrefixesIpv6())
d.Set("synthetics_ipv6", synthetics.GetPrefixesIpv6())
d.Set("webhooks_ipv6", webhook.GetPrefixesIpv6())
ipv4PrefixesByLocationMap := make(map[string]string)
ipv6PrefixesByLocationMap := make(map[string]string)
ipv4PrefixesByLocation := synthetics.GetPrefixesIpv4ByLocation()
ipv6PrefixesByLocation := synthetics.GetPrefixesIpv6ByLocation()
for key, value := range ipv4PrefixesByLocation {
ipv4PrefixesByLocationMap[key] = strings.Join(value, ",")
}
for key, value := range ipv6PrefixesByLocation {
ipv6PrefixesByLocationMap[key] = strings.Join(value, ",")
}
err = d.Set("synthetics_ipv4_by_location", ipv4PrefixesByLocationMap)
if err != nil {
log.Printf("[DEBUG] Error setting IPv4 prefixes by location: %s", err)
return diag.FromErr(err)
}
err = d.Set("synthetics_ipv6_by_location", ipv6PrefixesByLocationMap)
if err != nil {
log.Printf("[DEBUG] Error setting IPv6 prefixes by location: %s", err)
return diag.FromErr(err)
}
return nil
}