Skip to content

Integrated APIServcice to handler #20

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
Aug 10, 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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.testFlags": ["-v"],
}
4 changes: 2 additions & 2 deletions src/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var accountV1 = model.Account{
Name: "Switcher GitOps",
Version: "123",
LastCommit: "123",
Status: "active",
Status: model.StatusSynced,
Message: "Synced successfully",
},
Settings: model.Settings{
Expand All @@ -83,7 +83,7 @@ var accountV2 = model.Account{
Name: "Switcher GitOps",
Version: "123",
LastCommit: "123",
Status: "active",
Status: model.StatusSynced,
},
Settings: model.Settings{
Active: false,
Expand Down
8 changes: 8 additions & 0 deletions src/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/golang-jwt/jwt"
"github.com/switcherapi/switcher-gitops/src/model"
)

type GraphQLRequest struct {
Expand All @@ -17,6 +18,7 @@ type GraphQLRequest struct {

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

type ApiService struct {
Expand All @@ -31,6 +33,12 @@ func NewApiService(apiKey string, apiUrl string) *ApiService {
}
}

func (c *ApiService) NewDataFromJson(jsonData []byte) model.Data {
var data model.Data
json.Unmarshal(jsonData, &data)
return data
}

func (a *ApiService) FetchSnapshot(domainId string, environment string) (string, error) {
// Generate a bearer token
token := generateBearerToken(a.ApiKey, domainId)
Expand Down
10 changes: 10 additions & 0 deletions src/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ func TestFetchSnapshot(t *testing.T) {
assert.Contains(t, snapshot, "config", "Missing config in snapshot")
})

t.Run("Should return data from snapshot", func(t *testing.T) {
apiService := NewApiService(config.GetEnv("SWITCHER_API_JWT_SECRET"), config.GetEnv("SWITCHER_API_URL"))
snapshot, _ := apiService.FetchSnapshot(config.GetEnv("API_DOMAIN_ID"), "default")
data := apiService.NewDataFromJson([]byte(snapshot))

assert.NotNil(t, data.Snapshot.Domain, "domain", "Missing domain in data")
assert.NotNil(t, data.Snapshot.Domain.Group, "group", "Missing groups in data")
assert.NotNil(t, data.Snapshot.Domain.Group[0].Config, "config", "Missing config in data")
})

t.Run("Should return error - invalid API key", func(t *testing.T) {
apiService := NewApiService("INVALID_KEY", config.GetEnv("SWITCHER_API_URL"))
snapshot, _ := apiService.FetchSnapshot(config.GetEnv("API_DOMAIN_ID"), "default")
Expand Down
5 changes: 3 additions & 2 deletions src/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func setup() {

accountRepository := repository.NewAccountRepositoryMongo(mongoDb)
gitService := NewGitService("repoURL", "token", "main")
apiService := NewApiService("apiKey", "")
comparatorService := NewComparatorService()
coreHandler = NewCoreHandler(accountRepository, gitService, comparatorService)
coreHandler = NewCoreHandler(accountRepository, gitService, apiService, comparatorService)
}

func shutdown() {
Expand Down Expand Up @@ -73,7 +74,7 @@ func givenAccount() model.Account {
Version: "",
LastCommit: "",
LastDate: "",
Status: "",
Status: model.StatusOutSync,
Message: "",
},
Settings: model.Settings{
Expand Down
27 changes: 21 additions & 6 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (
type CoreHandler struct {
AccountRepository repository.AccountRepository
GitService IGitService
ApiService IAPIService
ComparatorService IComparatorService
status int
}

func NewCoreHandler(repo repository.AccountRepository, gitService IGitService, comparatorService IComparatorService) *CoreHandler {
func NewCoreHandler(repo repository.AccountRepository, gitService IGitService, apiService IAPIService, comparatorService IComparatorService) *CoreHandler {
return &CoreHandler{
AccountRepository: repo,
GitService: gitService,
ApiService: apiService,
ComparatorService: comparatorService,
}
}
Expand Down Expand Up @@ -73,7 +75,15 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
c.AccountRepository.Update(&account)

// Check for changes
diff := c.checkForChanges(repositoryData.Content)
diff, err := c.checkForChanges(account.Domain.ID, account.Environment, repositoryData.Content)

if err != nil {
// Update account status: Error
account.Domain.Status = model.StatusError
account.Domain.Message = "Error syncing up"
c.AccountRepository.Update(&account)
return
}

// Apply changes
c.applyChanges(account, diff)
Expand All @@ -84,12 +94,17 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
c.AccountRepository.Update(&account)
}

func (c *CoreHandler) checkForChanges(content string) model.DiffResult {
func (c *CoreHandler) checkForChanges(domainId string, environment string, content string) (model.DiffResult, error) {
// Get Snapshot from API
snapshotJsonFromApi, err := c.ApiService.FetchSnapshot(domainId, environment)

if err != nil {
return model.DiffResult{}, err
}

// Convert API JSON to model.Snapshot
jsonLeft := []byte(content)
left := c.ComparatorService.NewSnapshotFromJson(jsonLeft)
snapshotApi := c.ApiService.NewDataFromJson([]byte(snapshotJsonFromApi))
left := snapshotApi.Snapshot

// Convert content to model.Snapshot
jsonRight := []byte(content)
Expand All @@ -100,7 +115,7 @@ func (c *CoreHandler) checkForChanges(content string) model.DiffResult {
diffChanged := c.ComparatorService.CheckSnapshotDiff(left, right, CHANGED)
diffDeleted := c.ComparatorService.CheckSnapshotDiff(left, right, DELETED)

return c.ComparatorService.MergeResults([]model.DiffResult{diffNew, diffChanged, diffDeleted})
return c.ComparatorService.MergeResults([]model.DiffResult{diffNew, diffChanged, diffDeleted}), nil
}

func (c *CoreHandler) applyChanges(account model.Account, diff model.DiffResult) {
Expand Down
Loading