Skip to content

Commit 55b2e08

Browse files
authored
(feature) add static ips data source (#299)
* (feature) add static ips data source * bump go version to 1.22 * bump go in ci * bump checkly-go-sdk to v1.8.0 * update docs * update deps
1 parent cbd7e6f commit 55b2e08

18 files changed

+413
-126
lines changed

.github/workflows/test.yml

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Set up Go
2323
uses: actions/setup-go@v4
2424
with:
25-
go-version: '1.18'
25+
go-version: '1.22'
2626
id: go
2727
- name: Check out code into the Go module directory
2828
uses: actions/checkout@v3
@@ -39,7 +39,7 @@ jobs:
3939
steps:
4040
- uses: actions/setup-go@v4
4141
with:
42-
go-version: '1.18'
42+
go-version: '1.22'
4343
- uses: actions/checkout@v3
4444
- run: go generate ./...
4545
- name: git diff
@@ -68,7 +68,7 @@ jobs:
6868
- name: Set up Go
6969
uses: actions/setup-go@v4
7070
with:
71-
go-version: '1.18'
71+
go-version: '1.22'
7272
id: go
7373

7474
- uses: hashicorp/setup-terraform@v2
@@ -93,5 +93,3 @@ jobs:
9393
CHECKLY_ACCOUNT_ID: ${{ secrets.CHECKLY_ACCOUNT_ID }}
9494
CHECKLY_API_URL: ${{ secrets.CHECKLY_API_URL }}
9595
CHECKLY_API_KEY: ${{ secrets.CHECKLY_API_KEY }}
96-
97-

checkly/data_source_static_ips.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package checkly
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"slices"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
10+
checkly "github.com/checkly/checkly-go-sdk"
11+
)
12+
13+
func dataSourceStaticIPs() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceStaticIPsRead,
16+
Schema: map[string]*schema.Schema{
17+
"id": {
18+
Type: schema.TypeString,
19+
Computed: true,
20+
Description: "ID of the static IPs data source.",
21+
},
22+
"addresses": {
23+
Type: schema.TypeSet,
24+
Elem: &schema.Schema{
25+
Type: schema.TypeString,
26+
},
27+
Computed: true,
28+
Description: "Static IP addresses for Checkly's runner infrastructure.",
29+
},
30+
"locations": {
31+
Type: schema.TypeSet,
32+
Elem: &schema.Schema{
33+
Type: schema.TypeString,
34+
},
35+
Optional: true,
36+
Description: "Specify the locations you want to get static IPs for.",
37+
},
38+
"ip_family": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
Description: "Specify the IP families you want to get static IPs for. Only `IPv6` or `IPv4` are valid options.",
42+
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
43+
value := val.(string)
44+
if !slices.Contains([]string{"IPv6", "IPv4"}, value) {
45+
errs = append(errs, fmt.Errorf("%q must be either \"IPv6\" or \"IPv4\"", key))
46+
}
47+
return
48+
},
49+
},
50+
},
51+
}
52+
}
53+
54+
func dataSourceStaticIPsRead(d *schema.ResourceData, client interface{}) error {
55+
ctx, cancel := context.WithTimeout(context.Background(), apiCallTimeout())
56+
staticIPs, err := client.(checkly.Client).GetStaticIPs(ctx)
57+
defer cancel()
58+
if err != nil {
59+
return fmt.Errorf("dateSourceStaticIPsRead: API error: %w", err)
60+
}
61+
return dataSourceFromStaticIPs(staticIPs, d)
62+
}
63+
64+
func dataSourceFromStaticIPs(s []checkly.StaticIP, d *schema.ResourceData) error {
65+
var staticIPs []checkly.StaticIP
66+
var addresses []string
67+
68+
locations := stringsFromSet(d.Get("locations").(*schema.Set))
69+
ip_family := d.Get("ip_family").(string)
70+
71+
// only locations is set for filtering
72+
if len(locations) > 0 && ip_family == "" {
73+
for _, ip := range s {
74+
if slices.Contains(locations, ip.Region) {
75+
staticIPs = append(staticIPs, ip)
76+
}
77+
}
78+
// only ip_family is set for filtering
79+
} else if ip_family != "" && len(locations) == 0 {
80+
for _, ip := range s {
81+
if ip_family == "IPv4" && ip.Address.Addr().Is4() {
82+
staticIPs = append(staticIPs, ip)
83+
} else if ip_family == "IPv6" && ip.Address.Addr().Is6() {
84+
staticIPs = append(staticIPs, ip)
85+
}
86+
}
87+
// both region and ip_family are set for filtering
88+
} else if len(locations) > 0 && ip_family != "" {
89+
for _, ip := range s {
90+
if ip_family == "IPv4" && ip.Address.Addr().Is4() && slices.Contains(locations, ip.Region) {
91+
staticIPs = append(staticIPs, ip)
92+
} else if ip_family == "IPv6" && ip.Address.Addr().Is6() && slices.Contains(locations, ip.Region) {
93+
staticIPs = append(staticIPs, ip)
94+
}
95+
}
96+
// no region nor ip_family filters set
97+
} else {
98+
staticIPs = s
99+
}
100+
101+
for _, ip := range staticIPs {
102+
addresses = append(addresses, ip.Address.String())
103+
}
104+
105+
d.Set("addresses", addresses)
106+
d.SetId("checkly_static_ips_data_source_id")
107+
108+
return nil
109+
}
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package checkly
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccStaticIPsAll(t *testing.T) {
11+
config := `data "checkly_static_ips" "test" {}`
12+
13+
accTestCase(t, []resource.TestStep{
14+
{
15+
Config: config,
16+
Check: resource.ComposeTestCheckFunc(
17+
resource.TestCheckResourceAttr(
18+
"data.checkly_static_ips.test",
19+
"addresses.#",
20+
"162",
21+
),
22+
),
23+
},
24+
})
25+
}
26+
27+
func TestAccStaticIPsTwoRegionsOnly(t *testing.T) {
28+
config := `data "checkly_static_ips" "test" {
29+
locations = ["us-east-1","ap-southeast-1"]
30+
}`
31+
32+
accTestCase(t, []resource.TestStep{
33+
{
34+
Config: config,
35+
Check: resource.ComposeTestCheckFunc(
36+
resource.TestCheckResourceAttr(
37+
"data.checkly_static_ips.test",
38+
"addresses.#",
39+
"20",
40+
),
41+
),
42+
},
43+
})
44+
}
45+
46+
func TestAccStaticIPsIPv6Only(t *testing.T) {
47+
config := `data "checkly_static_ips" "test" {
48+
ip_family = "IPv6"
49+
}`
50+
51+
accTestCase(t, []resource.TestStep{
52+
{
53+
Config: config,
54+
Check: resource.ComposeTestCheckFunc(
55+
resource.TestCheckResourceAttr(
56+
"data.checkly_static_ips.test",
57+
"addresses.#",
58+
"22",
59+
),
60+
),
61+
},
62+
})
63+
}
64+
65+
func TestAccStaticIPsIPv4Only(t *testing.T) {
66+
config := `data "checkly_static_ips" "test" {
67+
ip_family = "IPv4"
68+
}`
69+
70+
accTestCase(t, []resource.TestStep{
71+
{
72+
Config: config,
73+
Check: resource.ComposeTestCheckFunc(
74+
resource.TestCheckResourceAttr(
75+
"data.checkly_static_ips.test",
76+
"addresses.#",
77+
"140",
78+
),
79+
),
80+
},
81+
})
82+
}
83+
84+
func TestAccStaticIPsIPv6AndOneRegionOnly(t *testing.T) {
85+
config := `data "checkly_static_ips" "test" {
86+
ip_family = "IPv6"
87+
locations = ["us-east-1"]
88+
}`
89+
90+
accTestCase(t, []resource.TestStep{
91+
{
92+
Config: config,
93+
Check: resource.ComposeTestCheckFunc(
94+
resource.TestCheckResourceAttr(
95+
"data.checkly_static_ips.test",
96+
"addresses.#",
97+
"1",
98+
),
99+
),
100+
},
101+
})
102+
}
103+
104+
func TestAccStaticIPsIPv4AndOneRegionOnly(t *testing.T) {
105+
config := `data "checkly_static_ips" "test" {
106+
ip_family = "IPv4"
107+
locations = ["us-east-1"]
108+
}`
109+
110+
accTestCase(t, []resource.TestStep{
111+
{
112+
Config: config,
113+
Check: resource.ComposeTestCheckFunc(
114+
resource.TestCheckResourceAttr(
115+
"data.checkly_static_ips.test",
116+
"addresses.#",
117+
"12",
118+
),
119+
),
120+
},
121+
})
122+
}
123+
124+
func TestAccStaticIPsInvalidIPFamily(t *testing.T) {
125+
config := `data "checkly_static_ips" "test" {
126+
ip_family = "invalid"
127+
}`
128+
129+
accTestCase(t, []resource.TestStep{
130+
{
131+
Config: config,
132+
ExpectError: regexp.MustCompile(`"ip_family" must be either "IPv6" or "IPv4"`),
133+
},
134+
})
135+
}

checkly/provider.go

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func Provider() *schema.Provider {
4343
"checkly_environment_variable": resourceEnvironmentVariable(),
4444
"checkly_private_location": resourcePrivateLocation(),
4545
},
46+
DataSourcesMap: map[string]*schema.Resource{
47+
"checkly_static_ips": dataSourceStaticIPs(),
48+
},
4649
ConfigureFunc: func(r *schema.ResourceData) (interface{}, error) {
4750
debugLog := os.Getenv("CHECKLY_DEBUG_LOG")
4851
var debugOutput io.Writer

docs/data-sources/static_ips.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "checkly_static_ips Data Source - terraform-provider-checkly"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# checkly_static_ips (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Optional
19+
20+
- `ip_family` (String) Specify the IP families you want to get static IPs for. Only `IPv6` or `IPv4` are valid options.
21+
- `locations` (Set of String) Specify the locations you want to get static IPs for.
22+
23+
### Read-Only
24+
25+
- `addresses` (Set of String) Static IP addresses for Checkly's runner infrastructure.
26+
- `id` (String) ID of the static IPs data source.

docs/resources/alert_channel.md

-2
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,3 @@ Optional:
207207
- `template` (String)
208208
- `webhook_secret` (String)
209209
- `webhook_type` (String) Type of the webhook. Possible values are 'WEBHOOK_DISCORD', 'WEBHOOK_FIREHYDRANT', 'WEBHOOK_GITLAB_ALERT', 'WEBHOOK_SPIKESH', 'WEBHOOK_SPLUNK', 'WEBHOOK_MSTEAMS' and 'WEBHOOK_TELEGRAM'.
210-
211-

docs/resources/check.md

-2
Original file line numberDiff line numberDiff line change
@@ -363,5 +363,3 @@ Optional:
363363
- `max_duration_seconds` (Number) The total amount of time to continue retrying the check (maximum 600 seconds).
364364
- `max_retries` (Number) The maximum number of times to retry the check. Value must be between 1 and 10.
365365
- `same_region` (Boolean) Whether retries should be run in the same region as the initial check run.
366-
367-

docs/resources/check_group.md

-2
Original file line numberDiff line numberDiff line change
@@ -297,5 +297,3 @@ Optional:
297297
- `max_duration_seconds` (Number) The total amount of time to continue retrying the check (maximum 600 seconds).
298298
- `max_retries` (Number) The maximum number of times to retry the check. Value must be between 1 and 10.
299299
- `same_region` (Boolean) Whether retries should be run in the same region as the initial check run.
300-
301-

docs/resources/dashboard.md

-2
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,3 @@ resource "checkly_dashboard" "dashboard_1" {
5858

5959
- `id` (String) The ID of this resource.
6060
- `key` (String, Sensitive) The access key when the dashboard is private.
61-
62-

docs/resources/environment_variable.md

-2
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,3 @@ resource "checkly_environment_variable" "variable_2" {
4141
### Read-Only
4242

4343
- `id` (String) The ID of this resource.
44-
45-

docs/resources/heartbeat.md

-2
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,3 @@ Optional:
124124
Optional:
125125

126126
- `minutes_failing_threshold` (Number) After how many minutes after a check starts failing an alert should be sent. Possible values are `5`, `10`, `15`, and `30`. (Default `5`).
127-
128-

docs/resources/maintenance_windows.md

-2
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,3 @@ resource "checkly_maintenance_windows" "maintenance-1" {
4545
### Read-Only
4646

4747
- `id` (String) The ID of this resource.
48-
49-

docs/resources/private_location.md

-2
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,3 @@ resource "checkly_private_location" "location" {
3535

3636
- `id` (String) The ID of this resource.
3737
- `keys` (Set of String, Sensitive) Private location API keys.
38-
39-

docs/resources/snippet.md

-2
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,3 @@ EOT
4949
### Read-Only
5050

5151
- `id` (String) The ID of this resource.
52-
53-

docs/resources/trigger_check.md

-2
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,3 @@ output "test_trigger_check_url" {
3737
### Read-Only
3838

3939
- `id` (String) The ID of this resource.
40-
41-

docs/resources/trigger_group.md

-2
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,3 @@ output "test_trigger_group_url" {
3737
### Read-Only
3838

3939
- `id` (String) The ID of this resource.
40-
41-

0 commit comments

Comments
 (0)