Skip to content

Commit

Permalink
Day 100/100 commits -🎖️⭐️ THE LAST DAY OF CHALLENGE🎖️⭐️ store informa…
Browse files Browse the repository at this point in the history
…tion about apply
  • Loading branch information
PawelHaracz committed Jun 8, 2024
1 parent 7ec8646 commit ae9e443
Show file tree
Hide file tree
Showing 30 changed files with 545 additions and 118 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Upps here should be demo video - no worry, I will be soon! - Now check the API o
- [X] Add and remove variables
- [X] Fix handling returning changes from plan
- [X] Add unit testing of aggregates
- [ ] ~~Bug fixing e2e testing~~
- [X] ~~Bug fixing e2e testing~~ (manually)
- [X] Path for parameters in git folder
- [X] Create handlers cmd
- [X] Time Lease for the state
Expand All @@ -61,10 +61,12 @@ Upps here should be demo video - no worry, I will be soon! - Now check the API o
- [ ] Access Token for Backend http
- [ ] Clean solution to be more DDD
- [ ] Create Plan changes during run, what was happened during the time
- [ ] Apply Mechanism to handle the state
- [X] Apply Mechanism to handle the state
- [X] Apply based on the Plan
- [ ] Save outputs as deployment, handle errors
- [ ] Backup before apply using ApplyOptions.Backup
- [X] Save outputs as deployment, handle errors
- [ ] Backup before apply using ApplyOptions.Backup
- [ ] Implement retries on apply
- [ ] Correlate Project, Deployment, Plans
- [ ] Integrate with the Git
- [ ] Handle other version than version 4.0 of tf
- [ ] Handle multiple version of tf and tofu
Expand Down
1 change: 1 addition & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func main() {
repositories.WithIaCRepositoryDbRepository(db),
repositories.WithTerraformStateDbRepository(db),
repositories.WithIacPlanRepositoryDbRepository(db),
repositories.WithIacDeploymentRepositoryDbRepository(db),
)
if err != nil {
log.Panic().Err(err)
Expand Down
1 change: 1 addition & 0 deletions cmd/handlers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func main() {
repositories.WithIaCRepositoryDbRepository(db),
repositories.WithTerraformStateDbRepository(db),
repositories.WithIacPlanRepositoryDbRepository(db),
repositories.WithIacDeploymentRepositoryDbRepository(db),
)
if err != nil {
cancel()
Expand Down
1 change: 1 addition & 0 deletions cmd/monolith/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func main() {
repositories.WithIaCRepositoryDbRepository(db),
repositories.WithTerraformStateDbRepository(db),
repositories.WithIacPlanRepositoryDbRepository(db),
repositories.WithIacDeploymentRepositoryDbRepository(db),
)
if err != nil {
log.Panic().Err(err)
Expand Down
96 changes: 96 additions & 0 deletions internal/aggregates/iac_deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package aggregates

import (
"github.com/google/uuid"
"labraboard/internal/entities"
"labraboard/internal/valueobjects/iac"
"time"
)

// todo design -> it should be combined somehow with IacPlan

type IacDeployment struct {
id uuid.UUID
planId uuid.UUID
projectId uuid.UUID
deploymentType IaCDeploymentType
startedTime time.Time
deployedTime time.Time
changes []iac.ChangesIac
changeSummary iac.ChangeSummaryIac
outputs []iac.Output
}

func NewIacDeployment(id uuid.UUID, planId uuid.UUID, projectId uuid.UUID, deploymentType IaCDeploymentType) *IacDeployment {
return &IacDeployment{
id: id,
planId: planId,
projectId: projectId,
deploymentType: deploymentType,
startedTime: time.Now().UTC(),
changes: make([]iac.ChangesIac, 0),
outputs: make([]iac.Output, 0),
}
}

func NewIacDeploymentExplicit(deploymentId uuid.UUID, planId uuid.UUID, projectId uuid.UUID, startedTime time.Time, deployedTime time.Time, deployedType string, changes []iac.ChangesIac, summary iac.ChangeSummaryIac, outputs []iac.Output) *IacDeployment {
return &IacDeployment{
id: deploymentId,
planId: planId,
projectId: projectId,
deploymentType: IaCDeploymentType(deployedType),
startedTime: startedTime,
deployedTime: deployedTime,
changes: changes,
changeSummary: summary,
outputs: outputs,
}
}

// GetID returns the Iac root entity ID
func (deployment *IacDeployment) GetID() uuid.UUID {
return deployment.id
}

func (deployment *IacDeployment) FinishDeployment(plans ...entities.IacTerraformOutputJson) {
var changes []iac.ChangesIac
deployment.deployedTime = time.Now().UTC()
for _, p := range plans {
if p.Type == entities.Version {
continue
}

if p.Change == emptyPlanChange {

if p.SummaryChanges == emptySummaryChange {
continue
}
deployment.changeSummary = iac.ChangeSummaryIac{
Add: p.SummaryChanges.Add,
Change: p.SummaryChanges.Change,
Remove: p.SummaryChanges.Remove,
}

} else if p.Outputs != nil && len(p.Outputs) != 0 {
for key, value := range p.Outputs {
deployment.outputs = append(deployment.outputs, iac.Output{
Name: key,
Sensitive: value.Sensitive,
Type: value.Type,
Value: value.Value,
})
}
} else {
planner := newChangeIacPlanner(p.Change.Resource.ResourceType, p.Change.Resource.ResourceName, p.Change.Resource.Provider, iac.PlanTypeAction(p.Change.Action))
deployment.changes = append(changes, *planner)
}
}
}

func (deployment *IacDeployment) Composite() ([]iac.ChangesIac, iac.ChangeSummaryIac, []iac.Output) {
return deployment.changes, deployment.changeSummary, deployment.outputs
}

func (deployment *IacDeployment) GetMetadata() (deploymentId uuid.UUID, planId uuid.UUID, projectId uuid.UUID, startedTime time.Time, deployedTime time.Time, deployedType string) {
return deployment.id, deployment.planId, deployment.projectId, deployment.startedTime, deployment.deployedTime, string(deployment.deploymentType)
}
42 changes: 21 additions & 21 deletions internal/aggregates/iac_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@ package aggregates
import (
"github.com/google/uuid"
"labraboard/internal/entities"
"labraboard/internal/valueobjects/iacPlans"
"labraboard/internal/valueobjects/iac"
)

type IaCPlanType string
type IaCDeploymentType string

var (
Terraform IaCPlanType = "terraform"
Tofu IaCPlanType = "tofu"
Terraform IaCDeploymentType = "terraform"
Tofu IaCDeploymentType = "tofu"
)

var (
emptyPlanChange = entities.IacTerraformPlanChangeJson{}
emptySummaryChange = entities.IacTerraformPlanSummaryChangesJson{}
emptyPlanChange = entities.IacTerraformChangeJson{}
emptySummaryChange = entities.IacTerraformSummaryChangesJson{}
)

type IacPlan struct {
id uuid.UUID
HistoryConfig *iacPlans.HistoryProjectConfig
changeSummary *iacPlans.ChangeSummaryIacPlan
changes []iacPlans.ChangesIacPlan
planType IaCPlanType
HistoryConfig *iac.HistoryProjectConfig
changeSummary *iac.ChangeSummaryIac
changes []iac.ChangesIac
planType IaCDeploymentType
planJson []byte
planRaw []byte
}

func NewIacPlan(id uuid.UUID, planType IaCPlanType, historyConfig *iacPlans.HistoryProjectConfig) (*IacPlan, error) {
func NewIacPlan(id uuid.UUID, planType IaCDeploymentType, historyConfig *iac.HistoryProjectConfig) (*IacPlan, error) {
return &IacPlan{
id: id,
planType: planType,
HistoryConfig: historyConfig,
}, nil
}

func NewIacPlanExplicit(id uuid.UUID, planType IaCPlanType, config *iacPlans.HistoryProjectConfig, summary *iacPlans.ChangeSummaryIacPlan, changes []iacPlans.ChangesIacPlan, planJson []byte, planRaw []byte) (*IacPlan, error) {
func NewIacPlanExplicit(id uuid.UUID, planType IaCDeploymentType, config *iac.HistoryProjectConfig, summary *iac.ChangeSummaryIac, changes []iac.ChangesIac, planJson []byte, planRaw []byte) (*IacPlan, error) {
return &IacPlan{
id: id,
planType: planType,
Expand All @@ -53,17 +53,17 @@ func (p *IacPlan) AddPlan(plan []byte, planRaw []byte) {
p.planRaw = planRaw
}

func newChangeIacPlanner(resourceType string, resourceName string, provider string, action iacPlans.PlanTypeAction) *iacPlans.ChangesIacPlan {
return &iacPlans.ChangesIacPlan{
func newChangeIacPlanner(resourceType string, resourceName string, provider string, action iac.PlanTypeAction) *iac.ChangesIac {
return &iac.ChangesIac{
ResourceType: resourceType,
ResourceName: resourceName,
Provider: provider,
Action: action,
}
}

func newChangeSummaryIacPlan(added int, changed int, removed int) *iacPlans.ChangeSummaryIacPlan {
return &iacPlans.ChangeSummaryIacPlan{
func newChangeSummaryIacPlan(added int, changed int, removed int) *iac.ChangeSummaryIac {
return &iac.ChangeSummaryIac{
Add: added,
Change: changed,
Remove: removed,
Expand All @@ -75,8 +75,8 @@ func (plan *IacPlan) GetID() uuid.UUID {
return plan.id
}

func (plan *IacPlan) AddChanges(plans ...entities.IacTerraformPlanJson) {
var changes []iacPlans.ChangesIacPlan
func (plan *IacPlan) AddChanges(plans ...entities.IacTerraformOutputJson) {
var changes []iac.ChangesIac

for _, p := range plans {
if p.Type == entities.Version {
Expand All @@ -92,7 +92,7 @@ func (plan *IacPlan) AddChanges(plans ...entities.IacTerraformPlanJson) {
plan.changeSummary = summary

} else {
planner := newChangeIacPlanner(p.Change.Resource.ResourceType, p.Change.Resource.ResourceName, p.Change.Resource.Provider, iacPlans.PlanTypeAction(p.Change.Action))
planner := newChangeIacPlanner(p.Change.Resource.ResourceType, p.Change.Resource.ResourceName, p.Change.Resource.Provider, iac.PlanTypeAction(p.Change.Action))
changes = append(changes, *planner)
}
}
Expand All @@ -108,9 +108,9 @@ func (plan *IacPlan) GetPlanJson() string {
return string(plan.planJson)
}

func (plan *IacPlan) Composite() (planJson []byte, planType IaCPlanType, changes []iacPlans.ChangesIacPlan, summary iacPlans.ChangeSummaryIacPlan, planRaw []byte) {
func (plan *IacPlan) Composite() (planJson []byte, planType IaCDeploymentType, changes []iac.ChangesIac, summary iac.ChangeSummaryIac, planRaw []byte) {
if plan.changeSummary == nil {
return plan.planJson, plan.planType, plan.changes, iacPlans.ChangeSummaryIacPlan{Add: 0, Change: 0, Remove: 0}, plan.planRaw
return plan.planJson, plan.planType, plan.changes, iac.ChangeSummaryIac{Add: 0, Change: 0, Remove: 0}, plan.planRaw
}
return plan.planJson, plan.planType, plan.changes, *plan.changeSummary, plan.planRaw
}
Expand Down
4 changes: 2 additions & 2 deletions internal/aggregates/iac_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestIaCPlanAddPlan(t *testing.T) {

func TestIaCPlan_ChangesBasedOnPlan_ValidChangeCount(t *testing.T) {
//arrange
serializer := helpers.NewSerializer[entities.IacTerraformPlanJson]()
serializer := helpers.NewSerializer[entities.IacTerraformOutputJson]()
aggregate, _ := NewIacPlan(uuid.New(), Terraform, nil)
buf := bytes.NewBuffer(planJson)
plan, err := serializer.DeserializeJsonl(buf)
Expand All @@ -55,7 +55,7 @@ func TestIaCPlan_ChangesBasedOnPlan_ValidChangeCount(t *testing.T) {

func TestIaCPlan_ChangesBasedOnPlan_ValidResourceChanges(t *testing.T) {
//arrange
serializer := helpers.NewSerializer[entities.IacTerraformPlanJson]()
serializer := helpers.NewSerializer[entities.IacTerraformOutputJson]()
aggregate, _ := NewIacPlan(uuid.New(), Terraform, nil)
buf := bytes.NewBuffer(planJson)
plan, err := serializer.DeserializeJsonl(buf)
Expand Down
44 changes: 0 additions & 44 deletions internal/entities/iacTerraformPlanJson.go

This file was deleted.

51 changes: 51 additions & 0 deletions internal/entities/iac_terraform_output_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package entities

import (
"time"
)

type IacPlatformType string

var (
Version IacPlatformType
PlannedChange IacPlatformType
ChangeSummary IacPlatformType
)

type IacTerraformOutputJson struct {
Message string `json:"@message"`
Module string `json:"@module"`
Timestamp time.Time `json:"@timestamp"`
Type IacPlatformType `json:"type"`
Change IacTerraformChangeJson `json:"change"`
SummaryChanges IacTerraformSummaryChangesJson `json:"changes"`
Outputs map[string]IacTerraformOutputValue `json:"outputs"`
}

type IacTerraformChangeJson struct {
Resource IacTerraformChangeResourceJson `json:"resource"`
Action string `json:"action"`
}

type IacTerraformChangeResourceJson struct {
Addr string `json:"addr"`
Module string `json:"module"`
Resource string `json:"resource"`
Provider string `json:"implied_provider"`
ResourceType string `json:"resource_type"`
ResourceName string `json:"resource_name"`
ResourceKey string `json:"resource_key"`
}

type IacTerraformSummaryChangesJson struct {
Add int `json:"add"`
Change int `json:"change"`
Remove int `json:"remove"`
Operation string `json:"operation"`
}

type IacTerraformOutputValue struct {
Sensitive bool `json:"sensitive"`
Type string `json:"type"`
Value string `json:"value"`
}
2 changes: 1 addition & 1 deletion internal/eventbus/events/apply_iac.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type IacApplyScheduled struct {
ChangeId uuid.UUID
ProjectId uuid.UUID
PlanId uuid.UUID
IacType aggregates.IaCPlanType
IacType aggregates.IaCDeploymentType
Owner string
}

Expand Down
Loading

0 comments on commit ae9e443

Please sign in to comment.