Skip to content

Fixed snapshot version/content check before push to repo #33

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 3 commits into from
Sep 11, 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
7 changes: 7 additions & 0 deletions resources/fixtures/api/default_snapshot_version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"data": {
"domain": {
"version": 1
}
}
}
67 changes: 52 additions & 15 deletions src/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ApplyChangeResponse struct {
}

type IAPIService interface {
FetchSnapshotVersion(domainId string, environment string) (string, error)
FetchSnapshot(domainId string, environment string) (string, error)
ApplyChangesToAPI(domainId string, environment string, diff model.DiffResult) (ApplyChangeResponse, error)
NewDataFromJson(jsonData []byte) model.Data
Expand All @@ -45,13 +46,45 @@ func (c *ApiService) NewDataFromJson(jsonData []byte) model.Data {
return data
}

func (a *ApiService) FetchSnapshotVersion(domainId string, environment string) (string, error) {
query := createQuerySnapshotVersion(domainId)
responseBody, err := a.doGraphQLRequest(domainId, query)

if err != nil {
return "", err
}

return responseBody, nil
}

func (a *ApiService) FetchSnapshot(domainId string, environment string) (string, error) {
query := createQuery(domainId, environment)
responseBody, err := a.doGraphQLRequest(domainId, query)

if err != nil {
return "", err
}

return responseBody, nil
}

func (a *ApiService) ApplyChangesToAPI(domainId string, environment string, diff model.DiffResult) (ApplyChangeResponse, error) {
reqBody, _ := json.Marshal(diff)
responseBody, err := a.doPostRequest(a.ApiUrl+"/gitops/apply", domainId, reqBody)

if err != nil {
return ApplyChangeResponse{}, err
}

var response ApplyChangeResponse
json.Unmarshal([]byte(responseBody), &response)
return response, nil
}

func (a *ApiService) doGraphQLRequest(domainId string, query string) (string, error) {
// Generate a bearer token
token := generateBearerToken(a.ApiKey, domainId)

// Define the GraphQL query
query := createQuery(domainId, environment)

// Create a new request
reqBody, _ := json.Marshal(GraphQLRequest{Query: query})
req, _ := http.NewRequest("POST", a.ApiUrl+"/gitops-graphql", bytes.NewBuffer(reqBody))
Expand All @@ -67,18 +100,16 @@ func (a *ApiService) FetchSnapshot(domainId string, environment string) (string,
}
defer resp.Body.Close()

// Read and print the response
body, _ := io.ReadAll(resp.Body)
return string(body), nil
responseBody, _ := io.ReadAll(resp.Body)
return string(responseBody), nil
}

func (a *ApiService) ApplyChangesToAPI(domainId string, environment string, diff model.DiffResult) (ApplyChangeResponse, error) {
func (a *ApiService) doPostRequest(url string, domainId string, body []byte) (string, 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))
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body))

// Set the request headers
setHeaders(req, token)
Expand All @@ -87,15 +118,12 @@ func (a *ApiService) ApplyChangesToAPI(domainId string, environment string, diff
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return ApplyChangeResponse{}, err
return "", 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
responseBody, _ := io.ReadAll(resp.Body)
return string(responseBody), nil
}

func generateBearerToken(apiKey string, subject string) string {
Expand All @@ -121,6 +149,15 @@ func setHeaders(req *http.Request, token string) {
req.Header.Set("Authorization", "Bearer "+token)
}

func createQuerySnapshotVersion(domainId string) string {
return fmt.Sprintf(`
{
domain(_id: "%s") {
version
}
}`, domainId)
}

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

const SWITCHER_API_JWT_SECRET = "SWITCHER_API_JWT_SECRET"

func TestFetchSnapshotVersion(t *testing.T) {
t.Run("Should return snapshot version", func(t *testing.T) {
responsePayload := utils.ReadJsonFromFile("../../resources/fixtures/api/default_snapshot_version.json")
fakeApiServer := givenApiResponse(http.StatusOK, responsePayload)
defer fakeApiServer.Close()

apiService := NewApiService(SWITCHER_API_JWT_SECRET, fakeApiServer.URL)
version, _ := apiService.FetchSnapshotVersion("domainId", "default")

assert.Contains(t, version, "version", "Missing version in response")
assert.Contains(t, version, "domain", "Missing domain in response")
})

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

apiService := NewApiService("INVALID_KEY", fakeApiServer.URL)
version, _ := apiService.FetchSnapshotVersion("domainId", "default")

assert.Contains(t, version, "Invalid API token")
})

t.Run("Should return error - invalid domain", func(t *testing.T) {
responsePayload := utils.ReadJsonFromFile("../../resources/fixtures/api/error_invalid_domain.json")
fakeApiServer := givenApiResponse(http.StatusUnauthorized, responsePayload)
defer fakeApiServer.Close()

apiService := NewApiService(SWITCHER_API_JWT_SECRET, fakeApiServer.URL)
version, _ := apiService.FetchSnapshotVersion("INVALID_DOMAIN", "default")

assert.Contains(t, version, "errors")
})

t.Run("Should return error - invalid API URL", func(t *testing.T) {
apiService := NewApiService(config.GetEnv(SWITCHER_API_JWT_SECRET), "http://localhost:8080")
_, err := apiService.FetchSnapshotVersion("domainId", "default")

assert.NotNil(t, err)
})
}

func TestFetchSnapshot(t *testing.T) {
t.Run("Should return snapshot", func(t *testing.T) {
responsePayload := utils.ReadJsonFromFile("../../resources/fixtures/api/default_snapshot.json")
Expand Down
53 changes: 35 additions & 18 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,18 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
continue
}

// Print account domain version
utils.Log(utils.LogLevelDebug, "[%s] Repository data: %s", accountId, utils.FormatJSON(repositoryData.Content))
// Fetch snapshot version from API
snapshotVersionPayload, err := c.ApiService.FetchSnapshotVersion(account.Domain.ID, account.Environment)

if err != nil {
utils.Log(utils.LogLevelError, "[%s] Failed to fetch snapshot version - %s", accountId, err.Error())
c.updateDomainStatus(*account, model.StatusError, "Failed to fetch snapshot version - "+err.Error())
time.Sleep(1 * time.Minute)
continue
}

// Check if repository is out of sync
if isRepositoryOutSync(*account, repositoryData.CommitHash) {
if c.isRepositoryOutSync(*account, repositoryData.CommitHash, snapshotVersionPayload) {
c.syncUp(*account, repositoryData, gitService)
}

Expand All @@ -104,7 +111,7 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi
}

func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.RepositoryData, gitService IGitService) {
utils.Log(utils.LogLevelInfo, "[%s] Syncing up", account.ID.Hex())
utils.Log(utils.LogLevelInfo, "[%s - %s] Syncing up", account.ID.Hex(), account.Domain.Name)

// Update account status: Out of sync
account.Domain.LastCommit = repositoryData.CommitHash
Expand All @@ -119,21 +126,27 @@ func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.Reposi
return
}

utils.Log(utils.LogLevelDebug, "[%s] SnapshotAPI version: %s - SnapshotRepo version: %s",
account.ID.Hex(), fmt.Sprint(snapshotApi.Domain.Version), fmt.Sprint(account.Domain.Version))
utils.Log(utils.LogLevelDebug, "[%s - %s] SnapshotAPI version: %s - SnapshotRepo version: %s",
account.ID.Hex(), account.Domain.Name, fmt.Sprint(snapshotApi.Domain.Version), fmt.Sprint(account.Domain.Version))

// Apply changes
changeSource := ""
if snapshotApi.Domain.Version > account.Domain.Version {
changeSource = "Repository"
account, err = c.applyChangesToRepository(account, snapshotApi, gitService)
if len(diff.Changes) > 0 {
account, err = c.applyChangesToRepository(account, snapshotApi, gitService)
} else {
utils.Log(utils.LogLevelInfo, "[%s - %s] Repository is up to date", account.ID.Hex(), account.Domain.Name)
account.Domain.Version = snapshotApi.Domain.Version
account.Domain.LastCommit = repositoryData.CommitHash
}
} else if len(diff.Changes) > 0 {
changeSource = "API"
account = c.applyChangesToAPI(account, repositoryData)
}

if err != nil {
utils.Log(utils.LogLevelError, "[%s] Failed to apply changes [%s] - %s", account.ID.Hex(), changeSource, err.Error())
utils.Log(utils.LogLevelError, "[%s - %s] Failed to apply changes [%s] - %s", account.ID.Hex(), account.Domain.Name, changeSource, err.Error())
c.updateDomainStatus(account, model.StatusError, "Failed to apply changes ["+changeSource+"] - "+err.Error())
return
}
Expand Down Expand Up @@ -171,7 +184,7 @@ func (c *CoreHandler) checkForChanges(account model.Account, content string) (mo
}

func (c *CoreHandler) applyChangesToAPI(account model.Account, repositoryData *model.RepositoryData) model.Account {
utils.Log(utils.LogLevelInfo, "[%s] Pushing changes to API", account.ID.Hex())
utils.Log(utils.LogLevelInfo, "[%s - %s] Pushing changes to API", account.ID.Hex(), account.Domain.Name)

// Push changes to API

Expand All @@ -183,7 +196,7 @@ func (c *CoreHandler) applyChangesToAPI(account model.Account, repositoryData *m
}

func (c *CoreHandler) applyChangesToRepository(account model.Account, snapshot model.Snapshot, gitService IGitService) (model.Account, error) {
utils.Log(utils.LogLevelInfo, "[%s] Pushing changes to repository", account.ID.Hex())
utils.Log(utils.LogLevelInfo, "[%s - %s] Pushing changes to repository", account.ID.Hex(), account.Domain.Name)

// Remove version from domain
snapshotContent := snapshot
Expand All @@ -198,16 +211,15 @@ func (c *CoreHandler) applyChangesToRepository(account model.Account, snapshot m
return account, err
}

func isRepositoryOutSync(account model.Account, lastCommit string) bool {
utils.Log(utils.LogLevelDebug, "[%s] Checking account - Last commit: %s - Domain Version: %d",
account.ID.Hex(), account.Domain.LastCommit, account.Domain.Version)
func (c *CoreHandler) isRepositoryOutSync(account model.Account, lastCommit string, snapshotVersionPayload string) bool {
snapshotVersion := c.ApiService.NewDataFromJson([]byte(snapshotVersionPayload)).Snapshot.Domain.Version

return account.Domain.LastCommit == "" || account.Domain.LastCommit != lastCommit
}
utils.Log(utils.LogLevelDebug, "[%s - %s] Checking account - Last commit: %s - Domain Version: %d - Snapshot Version: %d",
account.ID.Hex(), account.Domain.Name, account.Domain.LastCommit, account.Domain.Version, snapshotVersion)

func getTimeWindow(window string) (int, time.Duration) {
duration, _ := time.ParseDuration(window)
return 1, duration
return account.Domain.LastCommit == "" || // First sync
account.Domain.LastCommit != lastCommit || // Repository out of sync
account.Domain.Version != snapshotVersion // API out of sync
}

func (c *CoreHandler) updateDomainStatus(account model.Account, status string, message string) {
Expand All @@ -217,3 +229,8 @@ func (c *CoreHandler) updateDomainStatus(account model.Account, status string, m
account.Domain.LastDate = time.Now().Format(time.ANSIC)
c.AccountRepository.Update(&account)
}

func getTimeWindow(window string) (int, time.Duration) {
duration, _ := time.ParseDuration(window)
return 1, duration
}
Loading