From 6409b44e910af5f46e336bc67983351484808768 Mon Sep 17 00:00:00 2001 From: pburrows-ns1 Date: Thu, 7 Mar 2024 15:12:45 +0000 Subject: [PATCH 1/3] add support for listing available monitoring regions --- CHANGELOG.md | 6 +++++ mockns1/monitor_regions.go | 18 +++++++++++++++ rest/client.go | 4 +++- rest/model/monitor/region.go | 8 +++++++ rest/monitor_regions.go | 29 +++++++++++++++++++++++ rest/monitor_regions_test.go | 45 ++++++++++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 mockns1/monitor_regions.go create mode 100644 rest/model/monitor/region.go create mode 100644 rest/monitor_regions.go create mode 100644 rest/monitor_regions_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index d0df05b..5c89e18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 2.9.0 (February 14th, 2024) + +FEATURES: + +* Adds support for listing available monitoring regions + ## 2.8.0 (February 14th, 2024) FEATURES: diff --git a/mockns1/monitor_regions.go b/mockns1/monitor_regions.go new file mode 100644 index 0000000..05d9de6 --- /dev/null +++ b/mockns1/monitor_regions.go @@ -0,0 +1,18 @@ +package mockns1 + +import ( + "net/http" + + "gopkg.in/ns1/ns1-go.v2/rest/model/monitor" +) + +// AddMonitorRegionsListTestCase sets up a test case for the api.Client.MonitorRegionsService.List() function. +func (s *Service) AddMonitorRegionsListTestCase( + requestHeaders, responseHeaders http.Header, + response []*monitor.Region, +) error { + return s.AddTestCase( + http.MethodGet, "/monitoring/regions", http.StatusOK, requestHeaders, + responseHeaders, "", response, + ) +} diff --git a/rest/client.go b/rest/client.go index e9c072c..8c9f7a3 100644 --- a/rest/client.go +++ b/rest/client.go @@ -14,7 +14,7 @@ import ( ) const ( - clientVersion = "2.8.0" + clientVersion = "2.9.0" defaultEndpoint = "https://api.nsone.net/v1/" defaultShouldFollowPagination = true @@ -66,6 +66,7 @@ type Client struct { DataFeeds *DataFeedsService DataSources *DataSourcesService Jobs *JobsService + MonitorRegions *MonitorRegionsService PulsarJobs *PulsarJobsService Notifications *NotificationsService Records *RecordsService @@ -113,6 +114,7 @@ func NewClient(httpClient Doer, options ...func(*Client)) *Client { c.DataFeeds = (*DataFeedsService)(&c.common) c.DataSources = (*DataSourcesService)(&c.common) c.Jobs = (*JobsService)(&c.common) + c.MonitorRegions = (*MonitorRegionsService)(&c.common) c.PulsarJobs = (*PulsarJobsService)(&c.common) c.Notifications = (*NotificationsService)(&c.common) c.Records = (*RecordsService)(&c.common) diff --git a/rest/model/monitor/region.go b/rest/model/monitor/region.go new file mode 100644 index 0000000..0156169 --- /dev/null +++ b/rest/model/monitor/region.go @@ -0,0 +1,8 @@ +package monitor + +// Region wraps an NS1 /monitoring/regions resource. +type Region struct { + Code string `json:"code"` + Name string `json:"name"` + Subnets []string `json:"subnets"` +} diff --git a/rest/monitor_regions.go b/rest/monitor_regions.go new file mode 100644 index 0000000..ffb2ed2 --- /dev/null +++ b/rest/monitor_regions.go @@ -0,0 +1,29 @@ +package rest + +import ( + "net/http" + + "gopkg.in/ns1/ns1-go.v2/rest/model/monitor" +) + +// MonitoringRegionsService handles 'monitoring/regions' endpoint. +type MonitorRegionsService service + +// List returns all available monitoring regions. +// +// API docs: https://developer.ibm.com/apis/catalog/ns1--ibm-ns1-connect-api/api/API--ns1--ibm-ns1-connect-api#listMonitoringRegions +func (s *MonitorRegionsService) List() ([]*monitor.Region, *http.Response, error) { + req, err := s.client.NewRequest("GET", "monitoring/regions", nil) + if err != nil { + return nil, nil, err + } + + regions := []*monitor.Region{} + + resp, err := s.client.Do(req, ®ions) + if err != nil { + return nil, resp, err + } + + return regions, resp, nil +} diff --git a/rest/monitor_regions_test.go b/rest/monitor_regions_test.go new file mode 100644 index 0000000..1999dc3 --- /dev/null +++ b/rest/monitor_regions_test.go @@ -0,0 +1,45 @@ +package rest_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/ns1/ns1-go.v2/mockns1" + api "gopkg.in/ns1/ns1-go.v2/rest" + "gopkg.in/ns1/ns1-go.v2/rest/model/monitor" +) + +func TestMonitorRegionsService(t *testing.T) { + mock, doer, err := mockns1.New(t) + require.Nil(t, err) + defer mock.Shutdown() + + client := api.NewClient(doer, api.SetEndpoint("https://"+mock.Address+"/v1/")) + + t.Run("List", func(t *testing.T) { + t.Run("Success", func(t *testing.T) { + defer mock.ClearTestCases() + + mockRegions := []*monitor.Region{ + { + Code: "lga", + Name: "New York", + Subnets: []string{"1.2.3.4/24"}, + }, + { + Code: "sjc", + Name: "San Jose", + Subnets: []string{"5.6.7.8/24"}, + }, + } + + require.Nil(t, mock.AddMonitorRegionsListTestCase(nil, nil, mockRegions)) + + regions, resp, err := client.MonitorRegions.List() + + require.Nil(t, err) + require.Equal(t, 200, resp.StatusCode) + require.Len(t, regions, len(mockRegions)) + }) + }) +} From 2aa525392238dec3c9275e0e3be2fbc588bfdc5f Mon Sep 17 00:00:00 2001 From: pburrows-ns1 Date: Thu, 7 Mar 2024 16:23:22 +0000 Subject: [PATCH 2/3] fix typo --- rest/monitor_regions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest/monitor_regions.go b/rest/monitor_regions.go index ffb2ed2..da0e7b9 100644 --- a/rest/monitor_regions.go +++ b/rest/monitor_regions.go @@ -6,7 +6,7 @@ import ( "gopkg.in/ns1/ns1-go.v2/rest/model/monitor" ) -// MonitoringRegionsService handles 'monitoring/regions' endpoint. +// MonitorRegionsService handles 'monitoring/regions' endpoint. type MonitorRegionsService service // List returns all available monitoring regions. From 96cd129b69697af4f2f9a396746d4b257ee8a633 Mon Sep 17 00:00:00 2001 From: pburrows-ns1 Date: Thu, 7 Mar 2024 16:25:00 +0000 Subject: [PATCH 3/3] fix changelog date --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c89e18..471d9ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 2.9.0 (February 14th, 2024) +## 2.9.0 (March 7th, 2024) FEATURES: