Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate IP Range resource with Official Go SDK #491

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 43 additions & 25 deletions datadog/cassettes/TestAccDatadogIpRangesDatasource_existing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ interactions:
body: ""
form: {}
headers:
Accept:
- application/json
Dd-Operation-Id:
- GetIPRanges
User-Agent:
- Datadog/dev/terraform (go1.13)
url: https://ip-ranges.datadoghq.com
- datadog-api-client-go/1.0.0-beta.1+dev.1 (go go1.13; os darwin; arch amd64)
url: https://ip-ranges.datadoghq.com/
method: GET
response:
body: |
Expand Down Expand Up @@ -896,31 +900,35 @@ interactions:
Content-Type:
- application/json
Date:
- Mon, 16 Mar 2020 13:06:39 GMT
- Wed, 29 Apr 2020 14:48:20 GMT
Last-Modified:
- Wed, 13 Nov 2019 16:02:25 GMT
Server:
- AmazonS3
Vary:
- Accept-Encoding
Via:
- 1.1 4ba0e9deb9465045a3261b8712935964.cloudfront.net (CloudFront)
- 1.1 ca58d8de88ffcf8b392312b62a4bdee8.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- QCBZTZhjgjW_j1-W3Z806f3lvOozeahgiwgOnLJwQFtXCQ29Vi743w==
- VSv6DCx272yN05pN1T4_8EDQ84cjNRoNaol0Kfo-uyK0c8DrmVk0ag==
X-Amz-Cf-Pop:
- FRA53
- LHR62-C1
X-Cache:
- Miss from cloudfront
- RefreshHit from cloudfront
status: 200 OK
code: 200
duration: ""
- request:
body: ""
form: {}
headers:
Accept:
- application/json
Dd-Operation-Id:
- GetIPRanges
User-Agent:
- Datadog/dev/terraform (go1.13)
url: https://ip-ranges.datadoghq.com
- datadog-api-client-go/1.0.0-beta.1+dev.1 (go go1.13; os darwin; arch amd64)
url: https://ip-ranges.datadoghq.com/
method: GET
response:
body: |
Expand Down Expand Up @@ -1809,19 +1817,19 @@ interactions:
Content-Type:
- application/json
Date:
- Mon, 16 Mar 2020 13:06:39 GMT
- Wed, 29 Apr 2020 14:48:20 GMT
Last-Modified:
- Wed, 13 Nov 2019 16:02:25 GMT
Server:
- AmazonS3
Vary:
- Accept-Encoding
Via:
- 1.1 4ba0e9deb9465045a3261b8712935964.cloudfront.net (CloudFront)
- 1.1 ca58d8de88ffcf8b392312b62a4bdee8.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- fgQILALEBi6ihw8b2gcl8_pBQz-ZB4DbYe5g6PFk55OJfoiyxAu-bA==
- wgnyosYRyRtHxuF1_O0RWZYGZ0ZUO6JHWSd0gEx6qpc_V99oFjyBOA==
X-Amz-Cf-Pop:
- FRA53
- LHR62-C1
X-Cache:
- Miss from cloudfront
status: 200 OK
Expand All @@ -1831,9 +1839,13 @@ interactions:
body: ""
form: {}
headers:
Accept:
- application/json
Dd-Operation-Id:
- GetIPRanges
User-Agent:
- Datadog/dev/terraform (go1.13)
url: https://ip-ranges.datadoghq.com
- datadog-api-client-go/1.0.0-beta.1+dev.1 (go go1.13; os darwin; arch amd64)
url: https://ip-ranges.datadoghq.com/
method: GET
response:
body: |
Expand Down Expand Up @@ -2722,19 +2734,19 @@ interactions:
Content-Type:
- application/json
Date:
- Mon, 16 Mar 2020 13:06:39 GMT
- Wed, 29 Apr 2020 14:48:20 GMT
Last-Modified:
- Wed, 13 Nov 2019 16:02:25 GMT
Server:
- AmazonS3
Vary:
- Accept-Encoding
Via:
- 1.1 4ba0e9deb9465045a3261b8712935964.cloudfront.net (CloudFront)
- 1.1 ca58d8de88ffcf8b392312b62a4bdee8.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- 7lYx9adeiE6jQFQVdHr6b5vYDm5GZr94W7QJzNuW-b3KndllfWGphg==
- 1WEoqO7t6NmPiZSQg1blZhNlLjrRbfebr3hPLzYiViIAem_KNgaN6w==
X-Amz-Cf-Pop:
- FRA53
- LHR62-C1
X-Cache:
- Hit from cloudfront
status: 200 OK
Expand All @@ -2744,9 +2756,13 @@ interactions:
body: ""
form: {}
headers:
Accept:
- application/json
Dd-Operation-Id:
- GetIPRanges
User-Agent:
- Datadog/dev/terraform (go1.13)
url: https://ip-ranges.datadoghq.com
- datadog-api-client-go/1.0.0-beta.1+dev.1 (go go1.13; os darwin; arch amd64)
url: https://ip-ranges.datadoghq.com/
method: GET
response:
body: |
Expand Down Expand Up @@ -3630,24 +3646,26 @@ interactions:
}
}
headers:
Age:
- "1"
Connection:
- keep-alive
Content-Type:
- application/json
Date:
- Mon, 16 Mar 2020 13:06:39 GMT
- Wed, 29 Apr 2020 14:48:20 GMT
Last-Modified:
- Wed, 13 Nov 2019 16:02:25 GMT
Server:
- AmazonS3
Vary:
- Accept-Encoding
Via:
- 1.1 4ba0e9deb9465045a3261b8712935964.cloudfront.net (CloudFront)
- 1.1 ca58d8de88ffcf8b392312b62a4bdee8.cloudfront.net (CloudFront)
X-Amz-Cf-Id:
- EkPmn1ya1Fe33hvopmAb4tx85OsUU7i--oC4ToUyJ6lWWkoFQDg-QQ==
- 6HKYazpDbjrJywXDef7ooAzHYBt9DATXyx9ebsKauDgYVace07-peQ==
X-Amz-Cf-Pop:
- FRA53
- LHR62-C1
X-Cache:
- Hit from cloudfront
status: 200 OK
Expand Down
130 changes: 36 additions & 94 deletions datadog/data_source_datadog_ip_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func dataSourceDatadogIpRanges() *schema.Resource {
return &schema.Resource{
Read: dataSourceDatadogIpRangesRead,
Read: dataSourceDatadogIPRangesRead,

// IP ranges are divided between ipv4 and ipv6
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -86,112 +86,54 @@ func dataSourceDatadogIpRanges() *schema.Resource {
}
}

func dataSourceDatadogIpRangesRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceDatadogIPRangesRead(d *schema.ResourceData, meta interface{}) error {
providerConf := meta.(*ProviderConfiguration)
client := providerConf.CommunityClient
client := providerConf.DatadogClientV1
auth := providerConf.AuthV1

ipAddresses, err := client.GetIPRanges()
ipAddresses, _, err := client.IPRangesApi.GetIPRanges(auth).Execute()
if err != nil {
return translateClientError(err, "error getting ip range")
return err
}

// v4 and v6
if len(ipAddresses.Agents["prefixes_ipv4"])+len(ipAddresses.API["prefixes_ipv4"])+
len(ipAddresses.Apm["prefixes_ipv4"])+len(ipAddresses.Logs["prefixes_ipv4"])+
len(ipAddresses.Process["prefixes_ipv4"])+len(ipAddresses.Synthetics["prefixes_ipv4"])+
len(ipAddresses.Webhooks["prefixes_ipv4"])+len(ipAddresses.Agents["prefixes_ipv6"])+
len(ipAddresses.API["prefixes_ipv6"])+len(ipAddresses.Apm["prefixes_ipv6"])+
len(ipAddresses.Logs["prefixes_ipv6"])+len(ipAddresses.Process["prefixes_ipv6"])+
len(ipAddresses.Synthetics["prefixes_ipv6"])+len(ipAddresses.Webhooks["prefixes_ipv6"]) > 0 {
ipAddressesPtr := &ipAddresses
agents := ipAddressesPtr.GetAgents()
api := ipAddressesPtr.GetAgents()
apm := ipAddressesPtr.GetAgents()
logs := ipAddressesPtr.GetAgents()
process := ipAddressesPtr.GetAgents()
synthetics := ipAddressesPtr.GetAgents()
webhook := ipAddressesPtr.GetAgents()

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()) > 0 {
d.SetId("datadog-ip-ranges")
}

// Set ranges when the list is not empty
// v4
if len(ipAddresses.Agents["prefixes_ipv4"]) > 0 {
d.Set("agents_ipv4", ipAddresses.Agents["prefixes_ipv4"])
} else {
d.Set("agents_ipv4", []string{})
}

if len(ipAddresses.API["prefixes_ipv4"]) > 0 {
d.Set("api_ipv4", ipAddresses.API["prefixes_ipv4"])
} else {
d.Set("api_ipv4", []string{})
}

if len(ipAddresses.Apm["prefixes_ipv4"]) > 0 {
d.Set("apm_ipv4", ipAddresses.Apm["prefixes_ipv4"])
} else {
d.Set("apm_ipv4", []string{})
}

if len(ipAddresses.Logs["prefixes_ipv4"]) > 0 {
d.Set("logs_ipv4", ipAddresses.Logs["prefixes_ipv4"])
} else {
d.Set("logs_ipv4", []string{})
}

if len(ipAddresses.Process["prefixes_ipv4"]) > 0 {
d.Set("process_ipv4", ipAddresses.Process["prefixes_ipv4"])
} else {
d.Set("process_ipv4", []string{})
}

if len(ipAddresses.Synthetics["prefixes_ipv4"]) > 0 {
d.Set("synthetics_ipv4", ipAddresses.Synthetics["prefixes_ipv4"])
} else {
d.Set("synthetics_ipv4", []string{})
}

if len(ipAddresses.Webhooks["prefixes_ipv4"]) > 0 {
d.Set("webhooks_ipv4", ipAddresses.Webhooks["prefixes_ipv4"])
} else {
d.Set("webhooks_ipv4", []string{})
}
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
if len(ipAddresses.Agents["prefixes_ipv6"]) > 0 {
d.Set("agents_ipv6", ipAddresses.Agents["prefixes_ipv6"])
} else {
d.Set("agents_ipv6", []string{})
}

if len(ipAddresses.API["prefixes_ipv6"]) > 0 {
d.Set("api_ipv6", ipAddresses.API["prefixes_ipv6"])
} else {
d.Set("api_ipv6", []string{})
}

if len(ipAddresses.Apm["prefixes_ipv6"]) > 0 {
d.Set("apm_ipv6", ipAddresses.Apm["prefixes_ipv6"])
} else {
d.Set("apm_ipv6", []string{})
}

if len(ipAddresses.Logs["prefixes_ipv6"]) > 0 {
d.Set("logs_ipv6", ipAddresses.Logs["prefixes_ipv6"])
} else {
d.Set("logs_ipv6", []string{})
}

if len(ipAddresses.Process["prefixes_ipv6"]) > 0 {
d.Set("process_ipv6", ipAddresses.Process["prefixes_ipv6"])
} else {
d.Set("process_ipv6", []string{})
}

if len(ipAddresses.Synthetics["prefixes_ipv6"]) > 0 {
d.Set("synthetics_ipv6", ipAddresses.Synthetics["prefixes_ipv6"])
} else {
d.Set("synthetics_ipv6", []string{})
}

if len(ipAddresses.Webhooks["prefixes_ipv6"]) > 0 {
d.Set("webhooks_ipv6", ipAddresses.Webhooks["prefixes_ipv6"])
} else {
d.Set("webhooks_ipv6", []string{})
}
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())

return nil
}