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

INBOX-2404 - Add support for listing available monitoring regions #309

Merged
merged 4 commits into from
Mar 7, 2024
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## 2.2.0 (March 7, 2024)
ENHANCEMENTS
* Adds support for listing available monitoring regions

## 2.1.0 (February 14, 2024)
ENHANCEMENTS
* `Adds support for Datasets
* Adds support for Datasets

## 2.0.10 (October 12, 2023)
BUGFIX
Expand Down
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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ns1/ns1-go.v2 v2.8.0 h1:oX8QEHCCvnbTSqnSZRRHiGJsgke8eluTtQhNlPpFZBc=
gopkg.in/ns1/ns1-go.v2 v2.8.0/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
gopkg.in/ns1/ns1-go.v2 v2.9.0 h1:6sEUgb0bSNM4AqgUe44iIcHhwAAr1MakiqC7Ul5j8IM=
gopkg.in/ns1/ns1-go.v2 v2.9.0/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
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 details 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.
Loading