Skip to content

Commit 86f4aa0

Browse files
authored
Moved environment to DiffResult (#41)
1 parent 6bed78f commit 86f4aa0

File tree

8 files changed

+128
-14
lines changed

8 files changed

+128
-14
lines changed

.env.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ HANDLER_WAITING_TIME=1m
77

88
SWITCHER_API_URL=https://switcherapi.com/api
99
SWITCHER_API_JWT_SECRET=SecretSecretSecretSecretSecretSe
10+
SWITCHER_PATH_GRAPHQL=/gitops-graphql
11+
SWITCHER_PATH_PUSH=/gitops/v1/push
1012

1113
# Only for testing purposes. Values are loaded from accounts
1214
GIT_USER=
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"domain": {
3+
"group": [
4+
{
5+
"name": "Release 1",
6+
"description": "Showcase configuration",
7+
"activated": true,
8+
"config": [
9+
{
10+
"key": "MY_SWITCHER_1",
11+
"description": "My first switcher",
12+
"activated": true,
13+
"strategies": [
14+
{
15+
"strategy": "VALUE_VALIDATION",
16+
"activated": false,
17+
"operation": "EXIST",
18+
"values": [
19+
"user_1"
20+
]
21+
}
22+
],
23+
"components": [
24+
"switcher-playground"
25+
]
26+
},
27+
{
28+
"key": "MY_SWITCHER_2",
29+
"description": "",
30+
"activated": false,
31+
"strategies": [],
32+
"components": [
33+
"switcher-playground"
34+
]
35+
},
36+
{
37+
"key": "MY_SWITCHER_3",
38+
"description": "",
39+
"activated": true,
40+
"strategies": [],
41+
"components": [
42+
"benchmark"
43+
]
44+
},
45+
{
46+
"key": "MY_SWITCHER_4",
47+
"description": "",
48+
"activated": true,
49+
"strategies": [
50+
{
51+
"strategy": "VALUE_VALIDATION",
52+
"activated": false,
53+
"operation": "EXIST",
54+
"values": [
55+
"user_1"
56+
]
57+
}
58+
],
59+
"components": [
60+
"benchmark"
61+
]
62+
}
63+
]
64+
}
65+
]
66+
}
67+
}

src/core/api.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/golang-jwt/jwt"
12+
"github.com/switcherapi/switcher-gitops/src/config"
1213
"github.com/switcherapi/switcher-gitops/src/model"
1314
)
1415

@@ -24,7 +25,7 @@ type PushChangeResponse struct {
2425
type IAPIService interface {
2526
FetchSnapshotVersion(domainId string, environment string) (string, error)
2627
FetchSnapshot(domainId string, environment string) (string, error)
27-
PushChanges(domainId string, environment string, diff model.DiffResult) (PushChangeResponse, error)
28+
PushChanges(domainId string, diff model.DiffResult) (PushChangeResponse, error)
2829
NewDataFromJson(jsonData []byte) model.Data
2930
}
3031

@@ -68,9 +69,9 @@ func (a *ApiService) FetchSnapshot(domainId string, environment string) (string,
6869
return responseBody, nil
6970
}
7071

71-
func (a *ApiService) PushChanges(domainId string, environment string, diff model.DiffResult) (PushChangeResponse, error) {
72+
func (a *ApiService) PushChanges(domainId string, diff model.DiffResult) (PushChangeResponse, error) {
7273
reqBody, _ := json.Marshal(diff)
73-
responseBody, err := a.doPostRequest(a.apiUrl+"/gitops/push", domainId, reqBody)
74+
responseBody, err := a.doPostRequest(a.apiUrl+config.GetEnv("SWITCHER_PATH_PUSH"), domainId, reqBody)
7475

7576
if err != nil {
7677
return PushChangeResponse{}, err
@@ -87,7 +88,7 @@ func (a *ApiService) doGraphQLRequest(domainId string, query string) (string, er
8788

8889
// Create a new request
8990
reqBody, _ := json.Marshal(GraphQLRequest{Query: query})
90-
req, _ := http.NewRequest("POST", a.apiUrl+"/gitops-graphql", bytes.NewBuffer(reqBody))
91+
req, _ := http.NewRequest("POST", a.apiUrl+config.GetEnv("SWITCHER_PATH_GRAPHQL"), bytes.NewBuffer(reqBody))
9192

9293
// Set the request headers
9394
setHeaders(req, token)

src/core/api_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestFetchSnapshot(t *testing.T) {
116116
func TestPushChangesToAPI(t *testing.T) {
117117
t.Run("Should push changes to API", func(t *testing.T) {
118118
// Given
119-
diff := givenDiffResult()
119+
diff := givenDiffResult("default")
120120
fakeApiServer := givenApiResponse(http.StatusOK, `{
121121
"version": 2,
122122
"message": "Changes applied successfully"
@@ -126,7 +126,7 @@ func TestPushChangesToAPI(t *testing.T) {
126126
apiService := NewApiService(SWITCHER_API_JWT_SECRET, fakeApiServer.URL)
127127

128128
// Test
129-
response, _ := apiService.PushChanges("domainId", "default", diff)
129+
response, _ := apiService.PushChanges("domainId", diff)
130130

131131
// Assert
132132
assert.NotNil(t, response)
@@ -136,14 +136,14 @@ func TestPushChangesToAPI(t *testing.T) {
136136

137137
t.Run("Should return error - invalid API key", func(t *testing.T) {
138138
// Given
139-
diff := givenDiffResult()
139+
diff := givenDiffResult("default")
140140
fakeApiServer := givenApiResponse(http.StatusUnauthorized, `{ "message": "Invalid API token" }`)
141141
defer fakeApiServer.Close()
142142

143143
apiService := NewApiService("[INVALID_KEY]", fakeApiServer.URL)
144144

145145
// Test
146-
response, _ := apiService.PushChanges("domainId", "default", diff)
146+
response, _ := apiService.PushChanges("domainId", diff)
147147

148148
// Assert
149149
assert.NotNil(t, response)
@@ -152,11 +152,11 @@ func TestPushChangesToAPI(t *testing.T) {
152152

153153
t.Run("Should return error - API not accessible", func(t *testing.T) {
154154
// Given
155-
diff := givenDiffResult()
155+
diff := givenDiffResult("default")
156156
apiService := NewApiService("[SWITCHER_API_JWT_SECRET]", "http://localhost:8080")
157157

158158
// Test
159-
_, err := apiService.PushChanges("domainId", "default", diff)
159+
_, err := apiService.PushChanges("domainId", diff)
160160

161161
// Assert
162162
assert.NotNil(t, err)
@@ -165,8 +165,9 @@ func TestPushChangesToAPI(t *testing.T) {
165165

166166
// Helpers
167167

168-
func givenDiffResult() model.DiffResult {
168+
func givenDiffResult(environment string) model.DiffResult {
169169
diffResult := model.DiffResult{}
170+
diffResult.Environment = environment
170171
diffResult.Changes = append(diffResult.Changes, model.DiffDetails{
171172
Action: string(NEW),
172173
Diff: string(CONFIG),

src/core/comparator_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,47 @@ func TestCheckConfigSnapshot(t *testing.T) {
263263
]}`, utils.ToJsonFromObject(actual))
264264
})
265265

266+
t.Run("Should return new config with strategy", func(t *testing.T) {
267+
// Given
268+
jsonApi := utils.ReadJsonFromFile(DEFAULT_JSON)
269+
jsonRepo := utils.ReadJsonFromFile("../../resources/fixtures/comparator/new_config_strategy.json")
270+
fromApi := c.NewSnapshotFromJson([]byte(jsonApi))
271+
fromRepo := c.NewSnapshotFromJson([]byte(jsonRepo))
272+
273+
// Test Check/Merge changes
274+
diffNew := c.CheckSnapshotDiff(fromRepo, fromApi, NEW)
275+
actual := c.MergeResults([]model.DiffResult{diffNew})
276+
277+
assert.NotNil(t, actual)
278+
assert.JSONEq(t, `{
279+
"changes": [
280+
{
281+
"action": "NEW",
282+
"diff": "CONFIG",
283+
"path": [
284+
"Release 1"
285+
],
286+
"content": {
287+
"key": "MY_SWITCHER_4",
288+
"activated": true,
289+
"strategies": [
290+
{
291+
"strategy": "VALUE_VALIDATION",
292+
"activated": false,
293+
"operation": "EXIST",
294+
"values": [
295+
"user_1"
296+
]
297+
}
298+
],
299+
"components": [
300+
"benchmark"
301+
]
302+
}
303+
}
304+
]}`, utils.ToJsonFromObject(actual))
305+
})
306+
266307
t.Run("Should return deleted config", func(t *testing.T) {
267308
// Given
268309
jsonApi := utils.ReadJsonFromFile(DEFAULT_JSON)

src/core/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ func (c *CoreHandler) pushChangesToAPI(account model.Account,
204204
}
205205

206206
// Push changes to API
207-
apiResponse, err := c.apiService.PushChanges(account.Domain.ID, account.Environment, diff)
207+
diff.Environment = account.Environment
208+
apiResponse, err := c.apiService.PushChanges(account.Domain.ID, diff)
208209

209210
if err != nil {
210211
return account, err

src/core/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ func (f *FakeApiService) FetchSnapshot(domainId string, environment string) (str
581581
return f.responseSnapshot, nil
582582
}
583583

584-
func (f *FakeApiService) PushChanges(domainId string, environment string, diff model.DiffResult) (PushChangeResponse, error) {
584+
func (f *FakeApiService) PushChanges(domainId string, diff model.DiffResult) (PushChangeResponse, error) {
585585
if f.throwErrorPush {
586586
return PushChangeResponse{}, errors.New("something went wrong")
587587
}

src/model/diff.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package model
22

33
type DiffResult struct {
4-
Changes []DiffDetails `json:"changes"`
4+
Environment string `json:"environment,omitempty"`
5+
Changes []DiffDetails `json:"changes"`
56
}
67

78
type DiffDetails struct {

0 commit comments

Comments
 (0)