Skip to content

Commit

Permalink
sync key-value pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel-Haeberli committed Jun 13, 2023
1 parent 67172a8 commit 27a9b8a
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 15 deletions.
7 changes: 6 additions & 1 deletion momentum-backend/momentum-core/momentum-config/db-consts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package momentumconfig

const GENERIC_FIELD_KEYVALUES = "keyValues"

const TABLE_REPOSITORIES_NAME = "repositories"
const TABLE_REPOSITORIES_FIELD_ID = "id"
const TABLE_REPOSITORIES_FIELD_NAME = "name"
Expand Down Expand Up @@ -34,6 +36,9 @@ const TABLE_HELMREPOSITORYCREDENTIALS_NAME = "helmRepositoryCredentials"

const TABLE_HOOKS_NAME = "hooks"

const TABLE_KEYVALUE_NAME = "keyValue"
const TABLE_KEYVALUE_NAME = "keyValues"
const TABLE_KEYVALUE_FIELD_ID = "id"
const TABLE_KEYVALUE_FIELD_KEY = "key"
const TABLE_KEYVALUE_FIELD_VALUE = "value"

const TABLE_SECRETKEYVALUE_NAME = "secretKeyValues"
5 changes: 3 additions & 2 deletions momentum-backend/momentum-core/momentum-dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func NewDispatcher(config *conf.MomentumConfig, pb *pocketbase.PocketBase) *Mome
dispatcher := new(MomentumDispatcher)
dispatcher.Config = config

deploymentService := services.NewDeploymentService(pb.Dao())
stageService := services.NewStageService(pb.Dao(), deploymentService)
keyValueService := services.NewKeyValueService(pb.Dao())
deploymentService := services.NewDeploymentService(pb.Dao(), keyValueService)
stageService := services.NewStageService(pb.Dao(), deploymentService, keyValueService)
appService := services.NewApplicationService(pb.Dao(), stageService)
repoService := services.NewRepositoryService(pb.Dao(), appService)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (as *ApplicationService) SyncApplicationsFromDisk(n *tree.Node, record *mod
return nil, err
}

rec, err := as.createWithoutEvent(app.Path, stages)
rec, err := as.createWithoutEvent(app.NormalizedPath(), stages)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
)

type DeploymentService struct {
dao *daos.Dao
dao *daos.Dao
keyValueService *KeyValueService
}

func NewDeploymentService(dao *daos.Dao) *DeploymentService {
func NewDeploymentService(dao *daos.Dao, keyValueService *KeyValueService) *DeploymentService {

if dao == nil {
panic("cannot initialize service with nil dao")
Expand All @@ -23,6 +24,7 @@ func NewDeploymentService(dao *daos.Dao) *DeploymentService {
deplyomentService := new(DeploymentService)

deplyomentService.dao = dao
deplyomentService.keyValueService = keyValueService

return deplyomentService
}
Expand All @@ -34,11 +36,18 @@ func (ds *DeploymentService) SyncDeploymentsFromDisk(n *tree.Node) ([]string, er
deploymentIds := make([]string, 0)
for _, deployment := range deployments {

deploymentId, err := ds.createWithoutEvent(deployment.Path)
deploymentId, deploymentRecord, err := ds.createWithoutEvent(deployment.NormalizedPath())
if err != nil {
return nil, err
}

if deployment.Kind == tree.File {
err = ds.keyValueService.SyncFile(deployment, deploymentRecord)
if err != nil {
return nil, err
}
}

deploymentIds = append(deploymentIds, deploymentId)
}

Expand Down Expand Up @@ -75,17 +84,17 @@ func (ds *DeploymentService) GetDeploymentsCollection() (*models.Collection, err
return coll, nil
}

func (ds *DeploymentService) createWithoutEvent(name string) (string, error) {
func (ds *DeploymentService) createWithoutEvent(name string) (string, *models.Record, error) {

deploymentCollection, err := ds.GetDeploymentsCollection()
if err != nil {
return "", err
return "", nil, err
}

deploymentRecord := models.NewRecord(deploymentCollection)
deploymentRecord.Set(consts.TABLE_DEPLOYMENTS_FIELD_NAME, name)

err = ds.dao.Clone().SaveRecord(deploymentRecord)

return deploymentRecord.Id, nil
return deploymentRecord.Id, deploymentRecord, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package momentumservices

import (
"errors"
"fmt"
consts "momentum/momentum-core/momentum-config"
tree "momentum/momentum-core/momentum-tree"
"strings"

"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
)

type KeyValueService struct {
dao *daos.Dao
}

func NewKeyValueService(dao *daos.Dao) *KeyValueService {

if dao == nil {
panic("cannot initialize service with nil dao")
}

keyValueService := new(KeyValueService)

keyValueService.dao = dao

return keyValueService
}

func (kvs *KeyValueService) SyncFile(n *tree.Node, parentArtifact *models.Record) error {

if n.Kind != tree.File {
return errors.New("can only sync nodes of type file")
}

kvs.syncChildren(n.Children, parentArtifact, n.NormalizedPath())

return nil
}

func (kvs *KeyValueService) GetKeyValueCollection() (*models.Collection, error) {

coll, err := kvs.dao.FindCollectionByNameOrId(consts.TABLE_KEYVALUE_NAME)
if err != nil {
return nil, err
}

return coll, nil
}

func (kvs *KeyValueService) syncChildren(children []*tree.Node, parentArtifact *models.Record, filename string) error {

var err error = nil

for _, child := range children {

if len(child.Children) > 0 {

kvs.syncChildren(child.Children, parentArtifact, filename)
} else {

if child.Value == "" {
fmt.Println("empty leaf at:", child.FullPath())
break
}

kvColl, err := kvs.GetKeyValueCollection()
if err != nil {
break
}

propertyPath := strings.Split(child.FullPath(), filename)[1]

childRecord := models.NewRecord(kvColl)
childRecord.Set(consts.TABLE_KEYVALUE_FIELD_KEY, propertyPath)
childRecord.Set(consts.TABLE_KEYVALUE_FIELD_VALUE, child.Value)

err = kvs.dao.Clone().SaveRecord(childRecord)
if err != nil {
break
}

currentKeyValues, ok := parentArtifact.Get(consts.GENERIC_FIELD_KEYVALUES).([]string)
if ok {
parentArtifact.Set(consts.GENERIC_FIELD_KEYVALUES, append(currentKeyValues, childRecord.Id))
} else {
parentArtifact.Set(consts.GENERIC_FIELD_KEYVALUES, childRecord.Id)
}
err = kvs.dao.Clone().SaveRecord(parentArtifact)
if err != nil {
break
}
}
}

return err
}
22 changes: 17 additions & 5 deletions momentum-backend/momentum-core/momentum-services/stage-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
type StageService struct {
dao *daos.Dao
deploymentService *DeploymentService
keyValueService *KeyValueService
}

func NewStageService(dao *daos.Dao, deploymentService *DeploymentService) *StageService {
func NewStageService(dao *daos.Dao, deploymentService *DeploymentService, keyValueService *KeyValueService) *StageService {

if dao == nil {
panic("cannot initialize service with nil dao")
Expand All @@ -22,6 +23,7 @@ func NewStageService(dao *daos.Dao, deploymentService *DeploymentService) *Stage
stageService := new(StageService)
stageService.deploymentService = deploymentService
stageService.dao = dao
stageService.keyValueService = keyValueService

return stageService
}
Expand All @@ -37,11 +39,21 @@ func (ss *StageService) SyncStagesFromDisk(n *tree.Node) ([]string, error) {
return nil, err
}

stageId, err := ss.createWithoutEvent(stage.Path, deploymentIds)
stageId, stageRecord, err := ss.createWithoutEvent(stage.NormalizedPath(), deploymentIds)
if err != nil {
return nil, err
}

if stage.Kind == tree.Directory {
stageFiles := stage.Files()
for _, f := range stageFiles {
err = ss.keyValueService.SyncFile(f, stageRecord)
if err != nil {
return nil, err
}
}
}

stageIds = append(stageIds, stageId)
}

Expand All @@ -53,11 +65,11 @@ func (ss *StageService) GetStagesCollection() (*models.Collection, error) {
return ss.dao.FindCollectionByNameOrId(consts.TABLE_STAGES_NAME)
}

func (ss *StageService) createWithoutEvent(name string, deploymentIds []string) (string, error) {
func (ss *StageService) createWithoutEvent(name string, deploymentIds []string) (string, *models.Record, error) {

stageCollection, err := ss.GetStagesCollection()
if err != nil {
return "", err
return "", nil, err
}

stageRecord := models.NewRecord(stageCollection)
Expand All @@ -66,5 +78,5 @@ func (ss *StageService) createWithoutEvent(name string, deploymentIds []string)

err = ss.dao.Clone().SaveRecord(stageRecord)

return stageRecord.Id, err
return stageRecord.Id, stageRecord, err
}

0 comments on commit 27a9b8a

Please sign in to comment.