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

Commit f65e351

Browse files
authored
Add Stacks API (#53)
* Added stacks api * Fixed linting issues * Fixed PR review comments * Updated code to use better practices * Added slug to the stack update api * Corrected comment on UpdateStack api function * Converted create and update params to structs for the Stacks api
1 parent 4f6d294 commit f65e351

File tree

2 files changed

+412
-0
lines changed

2 files changed

+412
-0
lines changed

stack_test.go

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
package gapi
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
)
7+
8+
const (
9+
getStackJSON = `{"id": 1,"slug": "mystack"}`
10+
createStackJSON = `{"id": 1,"slug": "mystack"}`
11+
getStacksJSON = `
12+
{
13+
"items": [
14+
{
15+
"id": 1,
16+
"orgId": 1,
17+
"orgSlug": "myorg",
18+
"orgName": "MyOrg",
19+
"name": "mystack.grafana.net",
20+
"url": "https://mystack.grafana.net",
21+
"slug": "mystack",
22+
"version": "stable",
23+
"description": "My amazing stack",
24+
"status": "active",
25+
"gateway": "istio",
26+
"createdAt": "2021-12-22T14:02:46.000Z",
27+
"createdBy": "xyz",
28+
"updatedAt": null,
29+
"updatedBy": "",
30+
"trial": 0,
31+
"trialExpiresAt": null,
32+
"clusterId": 666,
33+
"clusterSlug": "prod-eu-west-0",
34+
"clusterName": "prod-eu-west-0",
35+
"plan": "gcloud",
36+
"planName": "Grafana Cloud",
37+
"billingStartDate": "2021-12-22T14:02:46.000Z",
38+
"billingEndDate": null,
39+
"billingActiveUsers": 0,
40+
"currentActiveUsers": 1,
41+
"currentActiveAdminUsers": 1,
42+
"currentActiveEditorUsers": 0,
43+
"currentActiveViewerUsers": 0,
44+
"dailyUserCnt": 0,
45+
"dailyAdminCnt": 0,
46+
"dailyEditorCnt": 0,
47+
"dailyViewerCnt": 0,
48+
"billableUserCnt": 1,
49+
"dashboardCnt": 6,
50+
"datasourceCnts": {},
51+
"userQuota": 10,
52+
"dashboardQuota": -1,
53+
"alertQuota": -1,
54+
"ssl": true,
55+
"customAuth": true,
56+
"customDomain": true,
57+
"support": true,
58+
"runningVersion": "8.3.3 (commit: 30bb7a93c, branch: HEAD)",
59+
"machineLearning": 0,
60+
"hmInstancePromId": 112233,
61+
"hmInstancePromUrl": "https://prometheus-prod-01-eu-west-0.grafana.net",
62+
"hmInstancePromName": "mystack-prom",
63+
"hmInstancePromStatus": "active",
64+
"hmInstancePromCurrentUsage": 11111,
65+
"hmInstancePromCurrentActiveSeries": 222222,
66+
"hmInstanceGraphiteId": 111111,
67+
"hmInstanceGraphiteUrl": "https://graphite-prod-01-eu-west-0.grafana.net",
68+
"hmInstanceGraphiteName": "mystack-graphite",
69+
"hmInstanceGraphiteType": "graphite-v5",
70+
"hmInstanceGraphiteStatus": "active",
71+
"hmInstanceGraphiteCurrentUsage": 0,
72+
"hlInstanceId": 121416,
73+
"hlInstanceUrl": "https://logs-prod-eu-west-0.grafana.net",
74+
"hlInstanceName": "mystack-logs",
75+
"hlInstanceStatus": "active",
76+
"hlInstanceCurrentUsage": 0,
77+
"amInstanceId": 654321,
78+
"amInstanceName": "mystack-alerts",
79+
"amInstanceUrl": "https://alertmanager-eu-west-0.grafana.net",
80+
"amInstanceStatus": "active",
81+
"amInstanceGeneratorUrl": "https://mystack.grafana.net",
82+
"htInstanceId": 123456,
83+
"htInstanceUrl": "https://tempo-eu-west-0.grafana.net",
84+
"htInstanceName": "mystack-traces",
85+
"htInstanceStatus": "active",
86+
"regionId": 3,
87+
"regionSlug": "eu"
88+
}
89+
]
90+
}
91+
`
92+
)
93+
94+
func TestStacks(t *testing.T) {
95+
server, client := gapiTestTools(t, 200, getStacksJSON)
96+
defer server.Close()
97+
98+
stacks, err := client.Stacks()
99+
100+
if err != nil {
101+
t.Fatalf("expected error to be nil; got: %s", err.Error())
102+
}
103+
104+
var expectedStacks StackItems
105+
106+
err = UnmarshalJSONToStruct(getStacksJSON, &expectedStacks)
107+
if err != nil {
108+
t.Fatal(err)
109+
}
110+
actualItemCount := len(stacks.Items)
111+
expectedItemCount := len(expectedStacks.Items)
112+
113+
// check that the number of items is the same
114+
if actualItemCount != expectedItemCount {
115+
t.Errorf("Length of returned stacks - Actual Stacks Count: %d, Expected Stacks Count: %d", actualItemCount, expectedItemCount)
116+
}
117+
118+
// Check ID of the returned Stack is as expected
119+
actualStackID := stacks.Items[0].ID
120+
expectedStackID := expectedStacks.Items[0].ID
121+
122+
if actualStackID != expectedStackID {
123+
t.Errorf("Unexpected Stack ID - Actual Stack ID: %d, Expected Stack ID: %d", actualStackID, expectedStackID)
124+
}
125+
126+
// Check the slug of the returned stack as expected
127+
actualSlug := stacks.Items[0].Slug
128+
expectedSlug := "mystack"
129+
if actualSlug != expectedSlug {
130+
t.Errorf("Unexpected Stack Slug - Actual Slug: %d, Expected Slug: %d", actualStackID, expectedStackID)
131+
}
132+
}
133+
134+
func TestCreateStack(t *testing.T) {
135+
server, client := gapiTestTools(t, 200, createStackJSON)
136+
defer server.Close()
137+
138+
stack := &CreateStackInput{
139+
Name: "mystack",
140+
Slug: "mystack",
141+
Region: "eu",
142+
URL: "",
143+
}
144+
145+
actualStackID, err := client.NewStack(stack)
146+
147+
if err != nil {
148+
t.Fatal(err)
149+
}
150+
151+
var expectedStack Stack
152+
err = UnmarshalJSONToStruct(createStackJSON, &expectedStack)
153+
if err != nil {
154+
t.Fatal(err)
155+
}
156+
157+
if actualStackID != expectedStack.ID {
158+
t.Errorf("Unexpected Stack ID - Actual: %d, Expected: %d", actualStackID, expectedStack.ID)
159+
}
160+
}
161+
162+
func TestStackBySlug(t *testing.T) {
163+
server, client := gapiTestTools(t, 200, getStackJSON)
164+
defer server.Close()
165+
166+
expectedStackSlug := "mystack"
167+
resp, err := client.StackBySlug(expectedStackSlug)
168+
if err != nil {
169+
t.Fatal(err)
170+
}
171+
172+
actualStackSlug := resp.Slug
173+
174+
if actualStackSlug != expectedStackSlug {
175+
t.Errorf("Unexpected Stack Slug - Actual: %s, Expected: %s", actualStackSlug, expectedStackSlug)
176+
}
177+
}
178+
179+
func TestStackByID(t *testing.T) {
180+
server, client := gapiTestTools(t, 200, getStackJSON)
181+
defer server.Close()
182+
183+
expectedStackID := int64(1)
184+
resp, err := client.StackByID(expectedStackID)
185+
186+
if err != nil {
187+
t.Fatal(err)
188+
}
189+
190+
actualStackID := resp.ID
191+
192+
if actualStackID != expectedStackID {
193+
t.Errorf("Unexpected Stack ID - Actual: %d, Expected: %d", actualStackID, expectedStackID)
194+
}
195+
}
196+
197+
func TestUpdateStack(t *testing.T) {
198+
server, client := gapiTestTools(t, 200, getStacksJSON)
199+
defer server.Close()
200+
201+
stack := &UpdateStackInput{
202+
Name: "mystack2",
203+
Slug: "mystack2",
204+
Description: "Stack update",
205+
}
206+
207+
err := client.UpdateStack(1, stack)
208+
if err != nil {
209+
t.Error(err)
210+
}
211+
}
212+
213+
func TestDeleteStack(t *testing.T) {
214+
server, client := gapiTestTools(t, 200, getStacksJSON)
215+
defer server.Close()
216+
217+
err := client.DeleteStack("mystack")
218+
219+
// The DELETE api returns an error so check if there is an error
220+
if err != nil {
221+
t.Errorf("Unexpected error - Actual: %s, Expected: nil", err.Error())
222+
}
223+
}
224+
225+
func UnmarshalJSONToStruct(jsonString string, target interface{}) error {
226+
err := json.Unmarshal([]byte(jsonString), &target)
227+
if err != nil {
228+
return err
229+
}
230+
231+
return nil
232+
}

0 commit comments

Comments
 (0)