diff --git a/CHANGELOG.md b/CHANGELOG.md index 499b22c6..c43df698 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/go.mod b/go.mod index 282a954f..de37c215 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index 9f50c587..43731cc1 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/ns1/config.go b/ns1/config.go index a444b0d1..8af9fc46 100644 --- a/ns1/config.go +++ b/ns1/config.go @@ -19,7 +19,7 @@ import ( ) var ( - clientVersion = "2.1.0" + clientVersion = "2.2.0" providerUserAgent = "tf-ns1" + "/" + clientVersion defaultRetryMax = 3 ) diff --git a/ns1/data_source_monitoring_regions.go b/ns1/data_source_monitoring_regions.go new file mode 100644 index 00000000..04e668d5 --- /dev/null +++ b/ns1/data_source_monitoring_regions.go @@ -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) +} diff --git a/ns1/data_source_monitoring_regions_test.go b/ns1/data_source_monitoring_regions_test.go new file mode 100644 index 00000000..99475173 --- /dev/null +++ b/ns1/data_source_monitoring_regions_test.go @@ -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" { +} +` diff --git a/ns1/provider.go b/ns1/provider.go index f611472a..1df8177f 100644 --- a/ns1/provider.go +++ b/ns1/provider.go @@ -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(), diff --git a/website/docs/d/monitoring_regions.markdown b/website/docs/d/monitoring_regions.markdown new file mode 100644 index 00000000..df7e0d7f --- /dev/null +++ b/website/docs/d/monitoring_regions.markdown @@ -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. \ No newline at end of file