Skip to content

Commit

Permalink
add support for listing available monitoring regions
Browse files Browse the repository at this point in the history
  • Loading branch information
pburrows-ns1 committed Mar 7, 2024
1 parent 93b60f8 commit 2b66555
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.2
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/stretchr/testify v1.8.1
gopkg.in/ns1/ns1-go.v2 v2.8.0
gopkg.in/ns1/ns1-go.v2 v2.9.0
)

require (
Expand Down
2 changes: 1 addition & 1 deletion ns1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

var (
clientVersion = "2.1.0"
clientVersion = "2.2.0"
providerUserAgent = "tf-ns1" + "/" + clientVersion
defaultRetryMax = 3
)
Expand Down
60 changes: 60 additions & 0 deletions ns1/data_source_monitoring_regions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ns1

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ns1 "gopkg.in/ns1/ns1-go.v2/rest"
)

func dataSourceMonitoringRegions() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"regions": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"subnets": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
Read: MonitoringingRegionsRead,
}
}

// MonitoringRegionsRead reads the available Monitoring Regions from ns1.
func MonitoringingRegionsRead(d *schema.ResourceData, meta any) error {
client := meta.(*ns1.Client)

regions, resp, err := client.MonitorRegions.List()
if err != nil {
return ConvertToNs1Error(resp, err)
}

out := []map[string]any{}

for _, region := range regions {
out = append(out, map[string]any{
"code": region.Code,
"name": region.Name,
"subnets": region.Subnets,
})
}

d.SetId("1")
return d.Set("regions", out)
}
51 changes: 51 additions & 0 deletions ns1/data_source_monitoring_regions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ns1

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccMonitoringRegions_basic(t *testing.T) {
name := "foobar"
resourceName := fmt.Sprintf("data.ns1_monitoring_regions.%s", name)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTeamDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccMonitoringRegionsBasic, name),
Check: resource.ComposeTestCheckFunc(
testAccCheckMonitoringRegionsLength(resourceName),
),
},
},
})
}

func testAccCheckMonitoringRegionsLength(
n string,
) resource.TestCheckFunc {
return func(s *terraform.State) error {
regions, ok := s.RootModule().Resources[n]

if !ok {
return fmt.Errorf("not found: %s", n)
}

// make sure we get some monitoring regions
if len(regions.Primary.Attributes["regions.#"]) == 0 {
return fmt.Errorf("no monitoring regions found")
}

return nil
}
}

const testAccMonitoringRegionsBasic = `
data "ns1_monitoring_regions" "%s" {
}
`
9 changes: 5 additions & 4 deletions ns1/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ func Provider() *schema.Provider {
},
},
DataSourcesMap: map[string]*schema.Resource{
"ns1_zone": dataSourceZone(),
"ns1_dnssec": dataSourceDNSSEC(),
"ns1_record": dataSourceRecord(),
"ns1_networks": dataSourceNetworks(),
"ns1_zone": dataSourceZone(),
"ns1_dnssec": dataSourceDNSSEC(),
"ns1_record": dataSourceRecord(),
"ns1_networks": dataSourceNetworks(),
"ns1_monitoring_regions": dataSourceMonitoringRegions(),
},
ResourcesMap: map[string]*schema.Resource{
"ns1_zone": resourceZone(),
Expand Down
38 changes: 38 additions & 0 deletions website/docs/d/monitoring_regions.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
layout: "ns1"
page_title: "NS1: ns1_monitoring_regions"
sidebar_current: "docs-ns1-datasource-monitoring-regions"
description: |-
Provides detials of all available monitoring regions.
---

# Data Source: ns1_monitoring_regions

Provides details of all available monitoring regions.

## Example Usage

```hcl
# Get details of all available monitoring regions.
data "ns1_monitoring_regions" "example" {
}
```

## Argument Reference

There are no required arguments.

## Attributes Reference

The following are attributes exported:

* `regions` - A set of the available monitoring regions. [Regions](#regions) is
documented below.

#### Regions

A region has the following fields:

* `code` - 3-letter city code identifying the location of the monitor.
* `name` - City name identifying the location of the monitor.
* `subnets` - A list of IPv4 and IPv6 subnets the monitor sources requests from.

0 comments on commit 2b66555

Please sign in to comment.