Skip to content

Added APIService::ApplyChangesToAPI #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ jobs:
go-version: '1.22.4'

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.10.0
uses: supercharge/mongodb-github-action@1.11.0
with:
mongodb-version: 6.0
mongodb-version: 7.0

- name: Build
run: go build -v ./...
Expand All @@ -45,7 +45,7 @@ jobs:
GIT_BRANCH: ${{ secrets.GIT_BRANCH }}

- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@v2.0.2
uses: sonarsource/sonarcloud-github-action@v3.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GitOps Domain Snapshot Orchestrator for Switcher API

[![Master CI](https://github.com/switcherapi/switcher-gitops/actions/workflows/master.yml/badge.svg?branch=master)](https://github.com/switcherapi/switcher-gitops/actions/workflows/master.yml)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=switcherapi_switcher-gitops&metric=alert_status)](https://sonarcloud.io/dashboard?id=switcherapi_switcher-gitops)
[![Known Vulnerabilities](https://snyk.io/test/github/switcherapi/switcher-gitops/badge.svg)](https://snyk.io/test/github/switcherapi/switcher-gitops)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Slack: Switcher-HQ](https://img.shields.io/badge/slack-@switcher/hq-blue.svg?logo=slack)](https://switcher-hq.slack.com/)

Expand Down
6 changes: 0 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,20 @@ require (
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/src-d/gcfg v1.4.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.22.0 // indirect
gopkg.in/src-d/go-billy.v4 v4.3.2 // indirect
gopkg.in/src-d/go-git.v4 v4.13.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

Expand Down
100 changes: 24 additions & 76 deletions go.sum

Large diffs are not rendered by default.

42 changes: 39 additions & 3 deletions src/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ type GraphQLRequest struct {
Query string `json:"query"`
}

type ApplyChangeResponse struct {
Message string `json:"message"`
Version string `json:"version"`
}

type IAPIService interface {
FetchSnapshot(domainId string, environment string) (string, error)
ApplyChangesToAPI(domainId string, environment string, diff model.DiffResult) (ApplyChangeResponse, error)
NewDataFromJson(jsonData []byte) model.Data
}

Expand Down Expand Up @@ -48,11 +54,10 @@ func (a *ApiService) FetchSnapshot(domainId string, environment string) (string,

// Create a new request
reqBody, _ := json.Marshal(GraphQLRequest{Query: query})
req, _ := http.NewRequest("POST", a.ApiUrl, bytes.NewBuffer(reqBody))
req, _ := http.NewRequest("POST", a.ApiUrl+"/gitops-graphql", bytes.NewBuffer(reqBody))

// Set the request headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
setHeaders(req, token)

// Send the request
client := &http.Client{}
Expand All @@ -67,6 +72,32 @@ func (a *ApiService) FetchSnapshot(domainId string, environment string) (string,
return string(body), nil
}

func (a *ApiService) ApplyChangesToAPI(domainId string, environment string, diff model.DiffResult) (ApplyChangeResponse, error) {
// Generate a bearer token
token := generateBearerToken(a.ApiKey, domainId)

// Create a new request
reqBody, _ := json.Marshal(diff)
req, _ := http.NewRequest("POST", a.ApiUrl+"/gitops/apply", bytes.NewBuffer(reqBody))

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

// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return ApplyChangeResponse{}, err
}
defer resp.Body.Close()

// Read and print the response
body, _ := io.ReadAll(resp.Body)
var response ApplyChangeResponse
json.Unmarshal(body, &response)
return response, nil
}

func generateBearerToken(apiKey string, subject string) string {
// Define the claims for the JWT token
claims := jwt.MapClaims{
Expand All @@ -85,6 +116,11 @@ func generateBearerToken(apiKey string, subject string) string {
return signedToken
}

func setHeaders(req *http.Request, token string) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
}

func createQuery(domainId string, environment string) string {
return fmt.Sprintf(`
{
Expand Down
74 changes: 74 additions & 0 deletions src/core/api_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package core

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/switcherapi/switcher-gitops/src/config"
"github.com/switcherapi/switcher-gitops/src/model"
)

func TestFetchSnapshot(t *testing.T) {
Expand Down Expand Up @@ -53,3 +56,74 @@ func TestFetchSnapshot(t *testing.T) {
AssertNotNil(t, err)
})
}

func TestApplyChangesToAPI(t *testing.T) {
t.Run("Should apply changes to API", func(t *testing.T) {
// Given
diff := givenDiffResult()
fakeApiServer := givenApiResponse(http.StatusOK, `{
"version": "2",
"message": "Changes applied successfully"
}`)
defer fakeApiServer.Close()

apiService := NewApiService("[SWITCHER_API_JWT_SECRET]", fakeApiServer.URL)

// Test
response, _ := apiService.ApplyChangesToAPI("domainId", "default", diff)

// Assert
assert.NotNil(t, response)
assert.Equal(t, "2", response.Version)
assert.Equal(t, "Changes applied successfully", response.Message)
})

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

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

// Test
response, _ := apiService.ApplyChangesToAPI("domainId", "default", diff)

// Assert
assert.NotNil(t, response)
assert.Contains(t, response.Message, "Invalid API token")
})

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

// Test
_, err := apiService.ApplyChangesToAPI("domainId", "default", diff)

// Assert
AssertNotNil(t, err)
})
}

// Helpers

func givenDiffResult() model.DiffResult {
diffResult := model.DiffResult{}
diffResult.Changes = append(diffResult.Changes, model.DiffDetails{
Action: string(NEW),
Diff: string(CONFIG),
Path: []string{"Release 1", "MY_SWITCHER"},
Content: map[string]interface{}{"activated": true},
})
return diffResult
}

func givenApiResponse(status int, response string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(status)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(response))
}))
}
4 changes: 4 additions & 0 deletions src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ func (f *FakeApiService) FetchSnapshot(domainId string, environment string) (str
return f.response, nil
}

func (f *FakeApiService) ApplyChangesToAPI(domainId string, environment string, diff model.DiffResult) (ApplyChangeResponse, error) {
return ApplyChangeResponse{}, nil
}

func (f *FakeApiService) NewDataFromJson(jsonData []byte) model.Data {
var data model.Data
json.Unmarshal(jsonData, &data)
Expand Down