Skip to content

Commit

Permalink
Merge pull request #31 from natrontech/momentum-model
Browse files Browse the repository at this point in the history
add momentum model
  • Loading branch information
Joel-Haeberli authored Jun 20, 2023
2 parents bc65afa + d3c8c92 commit 59ed320
Show file tree
Hide file tree
Showing 15 changed files with 938 additions and 380 deletions.
51 changes: 0 additions & 51 deletions momentum-backend/momentum-core/momentum-config/db-consts.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
gitclient "momentum/git-client"
kustomizeclient "momentum/kustomize-client"
config "momentum/momentum-core/momentum-config"
model "momentum/momentum-core/momentum-model"
services "momentum/momentum-core/momentum-services"
tree "momentum/momentum-core/momentum-tree"
utils "momentum/momentum-core/momentum-utils"
Expand All @@ -23,22 +24,22 @@ type RepositoryAddedEvent struct {
}

type RepositoryController struct {
repositorySyncService *services.RepositorySyncService
repositoryService *services.RepositoryService
deploymentService *services.DeploymentService
repositoryAddedEventChannel chan *RepositoryAddedEvent
kustomizeValidation *kustomizeclient.KustomizationValidationService
}

func NewRepositoryController(
repoSyncService *services.RepositorySyncService,
repoService *services.RepositoryService,
deploymentService *services.DeploymentService,
repositoryAddedEventChannel chan *RepositoryAddedEvent,
kustomizeValidator *kustomizeclient.KustomizationValidationService) *RepositoryController {

repoController := new(RepositoryController)

repoController.repositorySyncService = repoSyncService
repoController.repositoryService = repoService
repoController.deploymentService = deploymentService
repoController.repositoryAddedEventChannel = repositoryAddedEventChannel
repoController.kustomizeValidation = kustomizeValidator

Expand All @@ -47,8 +48,8 @@ func NewRepositoryController(

func (rc *RepositoryController) AddRepository(record *models.Record, conf *config.MomentumConfig) error {

repoName := record.GetString(config.TABLE_REPOSITORIES_FIELD_NAME)
repoUrl := record.GetString(config.TABLE_REPOSITORIES_FIELD_URL)
repoName := record.GetString(model.TABLE_REPOSITORIES_FIELD_NAME)
repoUrl := record.GetString(model.TABLE_REPOSITORIES_FIELD_URL)
path := utils.BuildPath(conf.DataDir(), strings.ReplaceAll(repoName, " ", ""))

fmt.Println("adding repo", repoName, ", located at", repoUrl, "and to be written to", path)
Expand Down Expand Up @@ -77,7 +78,7 @@ func (rc *RepositoryController) AddRepository(record *models.Record, conf *confi
return err
}

_, apps, deployments, err := rc.repositoryService.SyncRepositoryFromDisk(repo, record)
_, apps, deployments, err := rc.repositorySyncService.SyncRepositoryFromDisk(repo, record)
if err != nil {
fmt.Println("ERROR:", err.Error())
utils.DirDelete(path)
Expand Down
30 changes: 16 additions & 14 deletions momentum-backend/momentum-core/momentum-dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
kustomizeclient "momentum/kustomize-client"
conf "momentum/momentum-core/momentum-config"
controllers "momentum/momentum-core/momentum-controllers"
model "momentum/momentum-core/momentum-model"
services "momentum/momentum-core/momentum-services"

"github.com/pocketbase/pocketbase"
Expand Down Expand Up @@ -48,10 +49,11 @@ func NewDispatcher(config *conf.MomentumConfig, pb *pocketbase.PocketBase) *Mome
stageService := services.NewStageService(pb.Dao(), deploymentService, keyValueService)
appService := services.NewApplicationService(pb.Dao(), stageService)
repoService := services.NewRepositoryService(pb.Dao(), appService)
repoSyncService := services.NewRepositorySyncService(pb.Dao(), appService, stageService, deploymentService, keyValueService)

dispatcher.kustomizeValidator = kustomizeclient.NewKustomizationValidationService(dispatcher.Config, repoService)

dispatcher.RepositoryController = controllers.NewRepositoryController(repoService, deploymentService, REPOSITORY_ADDED_EVENT_CHANNEL, dispatcher.kustomizeValidator)
dispatcher.RepositoryController = controllers.NewRepositoryController(repoSyncService, repoService, REPOSITORY_ADDED_EVENT_CHANNEL, dispatcher.kustomizeValidator)
dispatcher.ApplicationsController = controllers.NewApplicationController(appService, repoService)
dispatcher.StagesController = controllers.NewStageController(stageService)
dispatcher.DeploymentController = controllers.NewDeploymentController(deploymentService, repoService)
Expand Down Expand Up @@ -109,34 +111,34 @@ func (d *MomentumDispatcher) DispatchDelete(recordEvent *core.RecordDeleteEvent)

func (d *MomentumDispatcher) setupCreateRules() []*MomentumDispatcherRule {
return []*MomentumDispatcherRule{
{conf.TABLE_REPOSITORIES_NAME, d.RepositoryController.AddRepository},
{conf.TABLE_APPLICATIONS_NAME, d.ApplicationsController.AddApplication},
{conf.TABLE_STAGES_NAME, d.StagesController.AddStage},
{conf.TABLE_DEPLOYMENTS_NAME, d.DeploymentController.AddDeployment},
{model.TABLE_REPOSITORIES_NAME, d.RepositoryController.AddRepository},
{model.TABLE_APPLICATIONS_NAME, d.ApplicationsController.AddApplication},
{model.TABLE_STAGES_NAME, d.StagesController.AddStage},
{model.TABLE_DEPLOYMENTS_NAME, d.DeploymentController.AddDeployment},
}
}

func (d *MomentumDispatcher) setupUpdateRules() []*MomentumDispatcherRule {
return []*MomentumDispatcherRule{
{conf.TABLE_REPOSITORIES_NAME, d.RepositoryController.UpdateRepository},
{conf.TABLE_APPLICATIONS_NAME, d.ApplicationsController.UpdateApplication},
{conf.TABLE_STAGES_NAME, d.StagesController.UpdateStage},
{conf.TABLE_DEPLOYMENTS_NAME, d.DeploymentController.UpdateDeployment},
{model.TABLE_REPOSITORIES_NAME, d.RepositoryController.UpdateRepository},
{model.TABLE_APPLICATIONS_NAME, d.ApplicationsController.UpdateApplication},
{model.TABLE_STAGES_NAME, d.StagesController.UpdateStage},
{model.TABLE_DEPLOYMENTS_NAME, d.DeploymentController.UpdateDeployment},
}
}

func (d *MomentumDispatcher) setupDeleteRules() []*MomentumDispatcherRule {
return []*MomentumDispatcherRule{
{conf.TABLE_REPOSITORIES_NAME, d.RepositoryController.DeleteRepository},
{conf.TABLE_APPLICATIONS_NAME, d.ApplicationsController.DeleteApplication},
{conf.TABLE_STAGES_NAME, d.StagesController.DeleteStage},
{conf.TABLE_DEPLOYMENTS_NAME, d.DeploymentController.DeleteDeployment},
{model.TABLE_REPOSITORIES_NAME, d.RepositoryController.DeleteRepository},
{model.TABLE_APPLICATIONS_NAME, d.ApplicationsController.DeleteApplication},
{model.TABLE_STAGES_NAME, d.StagesController.DeleteStage},
{model.TABLE_DEPLOYMENTS_NAME, d.DeploymentController.DeleteDeployment},
}
}

func (d *MomentumDispatcher) setupRepositoryAddedEventChannelObserver() {

d.Pocketbase.OnRecordAfterCreateRequest(conf.TABLE_REPOSITORIES_NAME).Add(func(e *core.RecordCreateEvent) error {
d.Pocketbase.OnRecordAfterCreateRequest(model.TABLE_REPOSITORIES_NAME).Add(func(e *core.RecordCreateEvent) error {

event := <-REPOSITORY_ADDED_EVENT_CHANNEL

Expand Down
101 changes: 101 additions & 0 deletions momentum-backend/momentum-core/momentum-model/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package momentummodel

import (
"errors"

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

const TABLE_APPLICATIONS_NAME = "applications"
const TABLE_APPLICATIONS_FIELD_ID = "id"
const TABLE_APPLICATIONS_FIELD_NAME = "name"
const TABLE_APPLICATIONS_FIELD_STAGES = "stages"
const TABLE_APPLICATIONS_FIELD_HELMREPO = "helmRepository"
const TABLE_APPLICATIONS_FIELD_PARENTREPOSITORY = "parentRepository"

type IApplication interface {
IModel

Name() string
SetName(string)

ParentRepositoryId() string
SetParentRepositoryId(string)

StageIds() []string
SetStageIds([]string)

HelmRepositoryUrl() string
SetHelmRepositoryUrl(string)
}

type Application struct {
IApplication

name string
parentRepositoryId string
stageIds []string
helmRepositoryUrl string
}

func ToApplication(record *models.Record) (IApplication, error) {

if record.TableName() != TABLE_APPLICATIONS_NAME {
return nil, errors.New("unallowed record type")
}

app := new(Application)
app.SetId(record.Id)
app.name = record.GetString(TABLE_APPLICATIONS_FIELD_NAME)
app.stageIds = record.Get(TABLE_APPLICATIONS_FIELD_STAGES).([]string)
app.helmRepositoryUrl = record.GetString(TABLE_APPLICATIONS_FIELD_HELMREPO)
app.parentRepositoryId = record.GetString(TABLE_APPLICATIONS_FIELD_PARENTREPOSITORY)

return app, nil
}

func ToApplicationRecord(app IApplication, recordInstance *models.Record) (*models.Record, error) {

if recordInstance.TableName() != TABLE_APPLICATIONS_NAME {
return nil, errors.New("unallowed record type")
}

recordInstance.Set(TABLE_APPLICATIONS_FIELD_NAME, app.Name())
recordInstance.Set(TABLE_APPLICATIONS_FIELD_STAGES, app.StageIds())
recordInstance.Set(TABLE_APPLICATIONS_FIELD_HELMREPO, app.HelmRepositoryUrl())
recordInstance.Set(TABLE_APPLICATIONS_FIELD_PARENTREPOSITORY, app.ParentRepositoryId())

return recordInstance, nil
}

func (a *Application) Name() string {
return a.name
}

func (a *Application) SetName(name string) {
a.name = name
}

func (a *Application) ParentRepositoryId() string {
return a.parentRepositoryId
}

func (a *Application) SetParentRepositoryId(parentRepositoryId string) {
a.parentRepositoryId = parentRepositoryId
}

func (a *Application) StageIds() []string {
return a.stageIds
}

func (a *Application) SetStageIds(stageIds []string) {
a.stageIds = stageIds
}

func (a *Application) HelmRepositoryUrl() string {
return a.helmRepositoryUrl
}

func (a *Application) SetHelmRepositoryUrl(helmRepositoryUrl string) {
a.helmRepositoryUrl = helmRepositoryUrl
}
116 changes: 116 additions & 0 deletions momentum-backend/momentum-core/momentum-model/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package momentummodel

import (
"errors"

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

const TABLE_DEPLOYMENTS_NAME = "deployments"
const TABLE_DEPLOYMENTS_FIELD_ID = "id"
const TABLE_DEPLOYMENTS_FIELD_NAME = "name"
const TABLE_DEPLOYMENTS_FIELD_DESCRIPTION = "description"
const TABLE_DEPLOYMENTS_FIELD_REPOSITORIES = "repositories"
const TABLE_DEPLOYMENTS_FIELD_KEYVALUES = "keyValues"
const TABLE_DEPLOYMENTS_FIELD_PARENTSTAGE = "parentStage"

type IDeployment interface {
IModel

Name() string
SetName(string)

Description() string
SetDescription(string)

ParentStageId() string
SetParentStageId(string)

KeyValueIds() []string
SetKeyValueIds([]string)

RepositoryIds() []string
SetRepositoryIds([]string)
}

type Deployment struct {
IDeployment

name string
description string
parentStageId string
keyValueIds []string
repositoryIds []string
}

func ToDeployment(record *models.Record) (IDeployment, error) {

if record.TableName() != TABLE_DEPLOYMENTS_NAME {
return nil, errors.New("unallowed record type")
}

dep := new(Deployment)
dep.SetId(record.Id)
dep.name = record.GetString(TABLE_APPLICATIONS_FIELD_NAME)
dep.description = record.GetString(TABLE_DEPLOYMENTS_FIELD_DESCRIPTION)
dep.parentStageId = record.GetString(TABLE_DEPLOYMENTS_FIELD_PARENTSTAGE)
dep.keyValueIds = record.Get(TABLE_DEPLOYMENTS_FIELD_KEYVALUES).([]string)
dep.repositoryIds = record.Get(TABLE_DEPLOYMENTS_FIELD_REPOSITORIES).([]string)

return dep, nil
}

func ToDeploymentRecord(dep IDeployment, recordInstance *models.Record) (*models.Record, error) {

if recordInstance.TableName() != TABLE_DEPLOYMENTS_NAME {
return nil, errors.New("unallowed record type")
}

recordInstance.Set(TABLE_DEPLOYMENTS_FIELD_NAME, dep.Name())
recordInstance.Set(TABLE_DEPLOYMENTS_FIELD_DESCRIPTION, dep.Description())
recordInstance.Set(TABLE_DEPLOYMENTS_FIELD_REPOSITORIES, dep.RepositoryIds())
recordInstance.Set(TABLE_DEPLOYMENTS_FIELD_KEYVALUES, dep.KeyValueIds())
recordInstance.Set(TABLE_DEPLOYMENTS_FIELD_PARENTSTAGE, dep.ParentStageId())

return recordInstance, nil
}

func (d *Deployment) Name() string {
return d.name
}

func (d *Deployment) SetName(name string) {
d.name = name
}

func (d *Deployment) Description() string {
return d.description
}

func (d *Deployment) SetDescription(description string) {
d.description = description
}

func (d *Deployment) ParentStageId() string {
return d.parentStageId
}

func (d *Deployment) SetParentStageId(parentStageId string) {
d.parentStageId = parentStageId
}

func (d *Deployment) KeyValueIds() []string {
return d.keyValueIds
}

func (d *Deployment) SetKeyValueIds(keyValueIds []string) {
d.keyValueIds = keyValueIds
}

func (d *Deployment) RepositoryIds() []string {
return d.repositoryIds
}

func (d *Deployment) SetRepositoryIds(repositoryIds []string) {
d.repositoryIds = repositoryIds
}
Loading

0 comments on commit 59ed320

Please sign in to comment.