Skip to content

Commit 1d4f483

Browse files
authored
INBOX-2404 - Add support for listing available monitoring regions
2 parents 93b60f8 + 0d1002b commit 1d4f483

8 files changed

Lines changed: 163 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
## 2.2.0 (March 7, 2024)
2+
ENHANCEMENTS
3+
* Adds support for listing available monitoring regions
4+
15
## 2.1.0 (February 14, 2024)
26
ENHANCEMENTS
3-
* `Adds support for Datasets
7+
* Adds support for Datasets
48

59
## 2.0.10 (October 12, 2023)
610
BUGFIX

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/hashicorp/go-retryablehttp v0.7.2
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
99
github.com/stretchr/testify v1.8.1
10-
gopkg.in/ns1/ns1-go.v2 v2.8.0
10+
gopkg.in/ns1/ns1-go.v2 v2.9.0
1111
)
1212

1313
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
250250
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
251251
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
252252
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
253-
gopkg.in/ns1/ns1-go.v2 v2.8.0 h1:oX8QEHCCvnbTSqnSZRRHiGJsgke8eluTtQhNlPpFZBc=
254-
gopkg.in/ns1/ns1-go.v2 v2.8.0/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
253+
gopkg.in/ns1/ns1-go.v2 v2.9.0 h1:6sEUgb0bSNM4AqgUe44iIcHhwAAr1MakiqC7Ul5j8IM=
254+
gopkg.in/ns1/ns1-go.v2 v2.9.0/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
255255
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
256256
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
257257
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

ns1/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
var (
22-
clientVersion = "2.1.0"
22+
clientVersion = "2.2.0"
2323
providerUserAgent = "tf-ns1" + "/" + clientVersion
2424
defaultRetryMax = 3
2525
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package ns1
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5+
ns1 "gopkg.in/ns1/ns1-go.v2/rest"
6+
)
7+
8+
func dataSourceMonitoringRegions() *schema.Resource {
9+
return &schema.Resource{
10+
Schema: map[string]*schema.Schema{
11+
"regions": {
12+
Type: schema.TypeList,
13+
Optional: true,
14+
Elem: &schema.Resource{
15+
Schema: map[string]*schema.Schema{
16+
"code": {
17+
Type: schema.TypeString,
18+
Optional: true,
19+
},
20+
"name": {
21+
Type: schema.TypeString,
22+
Optional: true,
23+
},
24+
"subnets": {
25+
Type: schema.TypeList,
26+
Optional: true,
27+
Elem: &schema.Schema{
28+
Type: schema.TypeString,
29+
},
30+
},
31+
},
32+
},
33+
},
34+
},
35+
Read: MonitoringingRegionsRead,
36+
}
37+
}
38+
39+
// MonitoringRegionsRead reads the available Monitoring Regions from ns1.
40+
func MonitoringingRegionsRead(d *schema.ResourceData, meta any) error {
41+
client := meta.(*ns1.Client)
42+
43+
regions, resp, err := client.MonitorRegions.List()
44+
if err != nil {
45+
return ConvertToNs1Error(resp, err)
46+
}
47+
48+
out := []map[string]any{}
49+
50+
for _, region := range regions {
51+
out = append(out, map[string]any{
52+
"code": region.Code,
53+
"name": region.Name,
54+
"subnets": region.Subnets,
55+
})
56+
}
57+
58+
d.SetId("1")
59+
return d.Set("regions", out)
60+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ns1
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
)
10+
11+
func TestAccMonitoringRegions_basic(t *testing.T) {
12+
name := "foobar"
13+
resourceName := fmt.Sprintf("data.ns1_monitoring_regions.%s", name)
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
Providers: testAccProviders,
17+
CheckDestroy: testAccCheckTeamDestroy,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: fmt.Sprintf(testAccMonitoringRegionsBasic, name),
21+
Check: resource.ComposeTestCheckFunc(
22+
testAccCheckMonitoringRegionsLength(resourceName),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
func testAccCheckMonitoringRegionsLength(
30+
n string,
31+
) resource.TestCheckFunc {
32+
return func(s *terraform.State) error {
33+
regions, ok := s.RootModule().Resources[n]
34+
35+
if !ok {
36+
return fmt.Errorf("not found: %s", n)
37+
}
38+
39+
// make sure we get some monitoring regions
40+
if len(regions.Primary.Attributes["regions.#"]) == 0 {
41+
return fmt.Errorf("no monitoring regions found")
42+
}
43+
44+
return nil
45+
}
46+
}
47+
48+
const testAccMonitoringRegionsBasic = `
49+
data "ns1_monitoring_regions" "%s" {
50+
}
51+
`

ns1/provider.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ func Provider() *schema.Provider {
5656
},
5757
},
5858
DataSourcesMap: map[string]*schema.Resource{
59-
"ns1_zone": dataSourceZone(),
60-
"ns1_dnssec": dataSourceDNSSEC(),
61-
"ns1_record": dataSourceRecord(),
62-
"ns1_networks": dataSourceNetworks(),
59+
"ns1_zone": dataSourceZone(),
60+
"ns1_dnssec": dataSourceDNSSEC(),
61+
"ns1_record": dataSourceRecord(),
62+
"ns1_networks": dataSourceNetworks(),
63+
"ns1_monitoring_regions": dataSourceMonitoringRegions(),
6364
},
6465
ResourcesMap: map[string]*schema.Resource{
6566
"ns1_zone": resourceZone(),
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
layout: "ns1"
3+
page_title: "NS1: ns1_monitoring_regions"
4+
sidebar_current: "docs-ns1-datasource-monitoring-regions"
5+
description: |-
6+
Provides details of all available monitoring regions.
7+
---
8+
9+
# Data Source: ns1_monitoring_regions
10+
11+
Provides details of all available monitoring regions.
12+
13+
## Example Usage
14+
15+
```hcl
16+
# Get details of all available monitoring regions.
17+
data "ns1_monitoring_regions" "example" {
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
There are no required arguments.
24+
25+
## Attributes Reference
26+
27+
The following are attributes exported:
28+
29+
* `regions` - A set of the available monitoring regions. [Regions](#regions) is
30+
documented below.
31+
32+
#### Regions
33+
34+
A region has the following fields:
35+
36+
* `code` - 3-letter city code identifying the location of the monitor.
37+
* `name` - City name identifying the location of the monitor.
38+
* `subnets` - A list of IPv4 and IPv6 subnets the monitor sources requests from.

0 commit comments

Comments
 (0)