Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 44fb93c

Browse files
Add Grafana cloud regions API (#72)
* Add Grafana cloud regions API Structs and functions for this AP: https://grafana.com/docs/grafana-cloud/reference/cloud-api/#list-regions * Fix function name :facepalm:
1 parent 0254e2c commit 44fb93c

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed

cloud_regions.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package gapi
2+
3+
import "fmt"
4+
5+
// CloudRegion represents a Grafana Cloud region.
6+
// https://grafana.com/docs/grafana-cloud/reference/cloud-api/#list-regions
7+
type CloudRegion struct {
8+
ID int `json:"id"`
9+
Status string `json:"status"`
10+
Slug string `json:"slug"`
11+
Name string `json:"name"`
12+
Description string `json:"description"`
13+
CreatedAt string `json:"createdAt"`
14+
UpdatedAt string `json:"updatedAt"`
15+
Visibility string `json:"visibility"`
16+
17+
// Service URLs for the region
18+
StackStateServiceURL string `json:"stackStateServiceUrl"`
19+
SyntheticMonitoringAPIURL string `json:"syntheticMonitoringApiUrl"`
20+
IntegrationsAPIURL string `json:"integrationsApiUrl"`
21+
HostedExportersAPIURL string `json:"hostedExportersApiUrl"`
22+
MachineLearningAPIURL string `json:"machineLearningApiUrl"`
23+
IncidentsAPIURL string `json:"incidentsApiUrl"`
24+
25+
// Hosted Grafana
26+
HGClusterID int `json:"hgClusterId"`
27+
HGClusterSlug string `json:"hgClusterSlug"`
28+
HGClusterName string `json:"hgClusterName"`
29+
HGClusterURL string `json:"hgClusterUrl"`
30+
31+
// Hosted Metrics: Prometheus
32+
HMPromClusterID int `json:"hmPromClusterId"`
33+
HMPromClusterSlug string `json:"hmPromClusterSlug"`
34+
HMPromClusterName string `json:"hmPromClusterName"`
35+
HMPromClusterURL string `json:"hmPromClusterUrl"`
36+
37+
// Hosted Metrics: Graphite
38+
HMGraphiteClusterID int `json:"hmGraphiteClusterId"`
39+
HMGraphiteClusterSlug string `json:"hmGraphiteClusterSlug"`
40+
HMGraphiteClusterName string `json:"hmGraphiteClusterName"`
41+
HMGraphiteClusterURL string `json:"hmGraphiteClusterUrl"`
42+
43+
// Hosted Logs
44+
HLClusterID int `json:"hlClusterId"`
45+
HLClusterSlug string `json:"hlClusterSlug"`
46+
HLClusterName string `json:"hlClusterName"`
47+
HLClusterURL string `json:"hlClusterUrl"`
48+
49+
// Alertmanager
50+
AMClusterID int `json:"amClusterId"`
51+
AMClusterSlug string `json:"amClusterSlug"`
52+
AMClusterName string `json:"amClusterName"`
53+
AMClusterURL string `json:"amClusterUrl"`
54+
55+
// Hosted Traces
56+
HTClusterID int `json:"htClusterId"`
57+
HTClusterSlug string `json:"htClusterSlug"`
58+
HTClusterName string `json:"htClusterName"`
59+
HTClusterURL string `json:"htClusterUrl"`
60+
}
61+
62+
// CloudRegionsResponse represents the response from the Grafana Cloud regions API.
63+
type CloudRegionsResponse struct {
64+
Items []CloudRegion `json:"items"`
65+
}
66+
67+
// GetCloudRegions fetches and returns all Grafana Cloud regions.
68+
func (c *Client) GetCloudRegions() (CloudRegionsResponse, error) {
69+
var regions CloudRegionsResponse
70+
err := c.request("GET", "/api/stack-regions", nil, nil, &regions)
71+
return regions, err
72+
}
73+
74+
// GetCloudRegionBySlug fetches and returns the cloud region which matches the given slug.
75+
// You can also provide a numeric region ID.
76+
func (c *Client) GetCloudRegionBySlug(slug string) (CloudRegion, error) {
77+
var region CloudRegion
78+
err := c.request("GET", fmt.Sprintf("/api/stack-regions/%s", slug), nil, nil, &region)
79+
return region, err
80+
}

cloud_regions_test.go

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package gapi
2+
3+
import "testing"
4+
5+
var (
6+
cloudRegionResponse = `{
7+
"id": 1,
8+
"status": "active",
9+
"slug": "us",
10+
"name": "United States",
11+
"description": "United States",
12+
"createdAt": "2021-08-20T20:00:27.000Z",
13+
"updatedAt": "2022-01-18T20:00:51.000Z",
14+
"visibility": "public",
15+
"stackStateServiceUrl": "http://apiserver.stackstate.svc.cluster.local",
16+
"syntheticMonitoringApiUrl": "https://synthetic-monitoring-api.grafana.net",
17+
"integrationsApiUrl": "https://integrations-api-us-central.grafana.net",
18+
"hostedExportersApiUrl": "https://hosted-exporters-api-us-central.grafana.net",
19+
"machineLearningApiUrl": "https://machine-learning-prod-us-central-0.grafana.net/machine-learning",
20+
"incidentApiUrl": null,
21+
"hgClusterId": 69,
22+
"hgClusterSlug": "prod-us-central-0",
23+
"hgClusterName": "prod-us-central-0",
24+
"hgClusterUrl": "https://hg-api-prod-us-central-0.grafana.net",
25+
"hmPromClusterId": 105,
26+
"hmPromClusterSlug": "prod-10-prod-us-central-0",
27+
"hmPromClusterName": "cortex-prod-10",
28+
"hmPromClusterUrl": "https://prometheus-prod-10-prod-us-central-0.grafana.net",
29+
"hmGraphiteClusterId": 105,
30+
"hmGraphiteClusterSlug": "prod-10-prod-us-central-0",
31+
"hmGraphiteClusterName": "cortex-prod-10",
32+
"hmGraphiteClusterUrl": "https://prometheus-prod-10-prod-us-central-0.grafana.net",
33+
"hlClusterId": 84,
34+
"hlClusterSlug": "loki-prod-us-central-0",
35+
"hlClusterName": "Hosted Logs Cluster (prod-us-central-0)",
36+
"hlClusterUrl": "https://logs-prod3.grafana.net",
37+
"amClusterId": 68,
38+
"amClusterSlug": "alertmanager-us-central1",
39+
"amClusterName": "alertmanager-us-central1",
40+
"amClusterUrl": "https://alertmanager-us-central1.grafana.net",
41+
"htClusterId": 78,
42+
"htClusterSlug": "tempo-prod-us-central1",
43+
"htClusterName": "tempo-prod-us-central1",
44+
"htClusterUrl": "https://tempo-us-central1.grafana.net"
45+
}
46+
`
47+
cloudRegionsResponse = `{
48+
"items": [
49+
` + cloudRegionResponse + `
50+
]
51+
}
52+
`
53+
expectedRegion = CloudRegion{ID: 1,
54+
Status: "active",
55+
Slug: "us",
56+
Name: "United States",
57+
Description: "United States",
58+
CreatedAt: "2021-08-20T20:00:27.000Z",
59+
UpdatedAt: "2022-01-18T20:00:51.000Z",
60+
Visibility: "public",
61+
StackStateServiceURL: "http://apiserver.stackstate.svc.cluster.local",
62+
SyntheticMonitoringAPIURL: "https://synthetic-monitoring-api.grafana.net",
63+
IntegrationsAPIURL: "https://integrations-api-us-central.grafana.net",
64+
HostedExportersAPIURL: "https://hosted-exporters-api-us-central.grafana.net",
65+
MachineLearningAPIURL: "https://machine-learning-prod-us-central-0.grafana.net/machine-learning",
66+
IncidentsAPIURL: "",
67+
HGClusterID: 69,
68+
HGClusterSlug: "prod-us-central-0",
69+
HGClusterName: "prod-us-central-0",
70+
HGClusterURL: "https://hg-api-prod-us-central-0.grafana.net",
71+
HMPromClusterID: 105,
72+
HMPromClusterSlug: "prod-10-prod-us-central-0",
73+
HMPromClusterName: "cortex-prod-10",
74+
HMPromClusterURL: "https://prometheus-prod-10-prod-us-central-0.grafana.net",
75+
HMGraphiteClusterID: 105,
76+
HMGraphiteClusterSlug: "prod-10-prod-us-central-0",
77+
HMGraphiteClusterName: "cortex-prod-10",
78+
HMGraphiteClusterURL: "https://prometheus-prod-10-prod-us-central-0.grafana.net",
79+
HLClusterID: 84,
80+
HLClusterSlug: "loki-prod-us-central-0",
81+
HLClusterName: "Hosted Logs Cluster (prod-us-central-0)",
82+
HLClusterURL: "https://logs-prod3.grafana.net",
83+
AMClusterID: 68,
84+
AMClusterSlug: "alertmanager-us-central1",
85+
AMClusterName: "alertmanager-us-central1",
86+
AMClusterURL: "https://alertmanager-us-central1.grafana.net",
87+
HTClusterID: 78,
88+
HTClusterSlug: "tempo-prod-us-central1",
89+
HTClusterName: "tempo-prod-us-central1",
90+
HTClusterURL: "https://tempo-us-central1.grafana.net"}
91+
)
92+
93+
func TestCloudRegions(t *testing.T) {
94+
server, client := gapiTestTools(t, 200, cloudRegionsResponse)
95+
defer server.Close()
96+
97+
regions, err := client.GetCloudRegions()
98+
99+
if err != nil {
100+
t.Fatalf("expected error to be nil; got: %s", err.Error())
101+
}
102+
103+
// check that the number of items is the same
104+
if got := len(regions.Items); got != 1 {
105+
t.Errorf("Length of returned regions - Actual regions count: %d, Expected regions count: %d", got, 1)
106+
}
107+
108+
if got := regions.Items[0]; got != expectedRegion {
109+
t.Errorf("Unexpected Region - Got:\n%#v\n, Expected:\n%#v\n", got, expectedRegion)
110+
}
111+
}
112+
113+
func TestCloudRegionBySlug(t *testing.T) {
114+
server, client := gapiTestTools(t, 200, cloudRegionResponse)
115+
defer server.Close()
116+
117+
resp, err := client.GetCloudRegionBySlug("us")
118+
if err != nil {
119+
t.Fatal(err)
120+
}
121+
122+
if resp != expectedRegion {
123+
t.Errorf("Unexpected Region - Got:\n%#v\n, Expected:\n%#v\n", resp, expectedRegion)
124+
}
125+
}

0 commit comments

Comments
 (0)