-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper funcs for converting rootcfgs to terraform roots (#744)
- Loading branch information
Showing
3 changed files
with
282 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
server/neptune/adhoc/adhocexecutionhelpers/root_helpers.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package adhoc | ||
|
||
import ( | ||
"github.com/runatlantis/atlantis/server/config/valid" | ||
"github.com/runatlantis/atlantis/server/neptune/gateway/deploy" | ||
"github.com/runatlantis/atlantis/server/neptune/workflows" | ||
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/execute" | ||
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/terraform" | ||
) | ||
|
||
func getRootsFromMergedProjectCfgs(rootCfgs []*valid.MergedProjectCfg) []terraform.Root { | ||
roots := make([]terraform.Root, 0, len(rootCfgs)) | ||
for _, rootCfg := range rootCfgs { | ||
root := convertMergedProjectCfgToRoot(rootCfg) | ||
terraformRoot := convertToTerraformRoot(root) | ||
roots = append(roots, terraformRoot) | ||
} | ||
return roots | ||
} | ||
|
||
func convertMergedProjectCfgToRoot(rootCfg *valid.MergedProjectCfg) workflows.Root { | ||
var tfVersion string | ||
if rootCfg.TerraformVersion != nil { | ||
tfVersion = rootCfg.TerraformVersion.String() | ||
} | ||
|
||
return workflows.Root{ | ||
Name: rootCfg.Name, | ||
Plan: workflows.Job{ | ||
Steps: prependPlanEnvSteps(rootCfg), | ||
}, | ||
Apply: workflows.Job{ | ||
Steps: generateSteps(rootCfg.DeploymentWorkflow.Apply.Steps), | ||
}, | ||
RepoRelPath: rootCfg.RepoRelDir, | ||
TrackedFiles: rootCfg.WhenModified, | ||
TfVersion: tfVersion, | ||
// note we don't set TriggerInfo or PlanMode | ||
} | ||
} | ||
|
||
func convertToTerraformRoot(root workflows.Root) terraform.Root { | ||
return terraform.Root{ | ||
Name: root.Name, | ||
Apply: execute.Job{ | ||
Steps: steps(root.Apply.Steps), | ||
}, | ||
Plan: terraform.PlanJob{ | ||
Job: execute.Job{ | ||
Steps: steps(root.Plan.Steps)}, | ||
}, | ||
// Note we don't have mode, nor PlanApproval | ||
Path: root.RepoRelPath, | ||
TfVersion: root.TfVersion, | ||
} | ||
} | ||
|
||
// These are copied here so that we don't have to use a workflowsignaler | ||
func prependPlanEnvSteps(cfg *valid.MergedProjectCfg) []workflows.Step { | ||
var steps []workflows.Step | ||
if t, ok := cfg.Tags[deploy.Manifest]; ok { | ||
//this is a Lyft specific env var | ||
steps = append(steps, workflows.Step{ | ||
StepName: deploy.EnvStep, | ||
EnvVarName: "MANIFEST_FILEPATH", | ||
EnvVarValue: t, | ||
}) | ||
} | ||
steps = append(steps, generateSteps(cfg.DeploymentWorkflow.Plan.Steps)...) | ||
return steps | ||
} | ||
|
||
func generateSteps(steps []valid.Step) []workflows.Step { | ||
var workflowSteps []workflows.Step | ||
for _, step := range steps { | ||
workflowSteps = append(workflowSteps, workflows.Step{ | ||
StepName: step.StepName, | ||
ExtraArgs: step.ExtraArgs, | ||
RunCommand: step.RunCommand, | ||
EnvVarName: step.EnvVarName, | ||
EnvVarValue: step.EnvVarValue, | ||
}) | ||
} | ||
return workflowSteps | ||
} | ||
|
||
func steps(requestSteps []workflows.Step) []execute.Step { | ||
var terraformSteps []execute.Step | ||
for _, step := range requestSteps { | ||
terraformSteps = append(terraformSteps, execute.Step{ | ||
StepName: step.StepName, | ||
ExtraArgs: step.ExtraArgs, | ||
RunCommand: step.RunCommand, | ||
EnvVarName: step.EnvVarName, | ||
EnvVarValue: step.EnvVarValue, | ||
}) | ||
} | ||
return terraformSteps | ||
} |
162 changes: 162 additions & 0 deletions
162
server/neptune/adhoc/adhocexecutionhelpers/root_helpers_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package adhoc | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/runatlantis/atlantis/server/config/valid" | ||
v "github.com/runatlantis/atlantis/server/config/valid" | ||
"github.com/runatlantis/atlantis/server/neptune/workflows" | ||
"github.com/runatlantis/atlantis/server/neptune/workflows/activities/execute" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPrependPlanEnvSteps(t *testing.T) { | ||
tests := []struct { | ||
cfg *valid.MergedProjectCfg | ||
expectedSteps []workflows.Step | ||
}{ | ||
{ | ||
cfg: &valid.MergedProjectCfg{ | ||
Tags: map[string]string{"manifest_path": "manifest_path"}, | ||
DeploymentWorkflow: v.Workflow{ | ||
Plan: v.Stage{ | ||
Steps: []v.Step{ | ||
{ | ||
StepName: "step1", | ||
ExtraArgs: []string{"arg1", "arg2"}, | ||
RunCommand: "run", | ||
EnvVarName: "env1", | ||
EnvVarValue: "value1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedSteps: []workflows.Step{ | ||
{ | ||
StepName: "env", | ||
EnvVarName: "MANIFEST_FILEPATH", | ||
EnvVarValue: "manifest_path", | ||
}, | ||
{ | ||
StepName: "step1", | ||
ExtraArgs: []string{"arg1", "arg2"}, | ||
RunCommand: "run", | ||
EnvVarName: "env1", | ||
EnvVarValue: "value1", | ||
}, | ||
}, | ||
}, | ||
{ | ||
cfg: &valid.MergedProjectCfg{ | ||
Tags: map[string]string{"foo": "foo"}, | ||
DeploymentWorkflow: v.Workflow{ | ||
Plan: v.Stage{ | ||
Steps: []v.Step{ | ||
{ | ||
StepName: "step1", | ||
ExtraArgs: []string{"arg1", "arg2"}, | ||
RunCommand: "run", | ||
EnvVarName: "env1", | ||
EnvVarValue: "value1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedSteps: []workflows.Step{ | ||
{ | ||
StepName: "step1", | ||
ExtraArgs: []string{"arg1", "arg2"}, | ||
RunCommand: "run", | ||
EnvVarName: "env1", | ||
EnvVarValue: "value1", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
res := prependPlanEnvSteps(tt.cfg) | ||
assert.True(t, compareSteps(res, tt.expectedSteps)) | ||
} | ||
} | ||
|
||
func compareSteps(a []workflows.Step, b []workflows.Step) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
|
||
for i, step := range a { | ||
if step.StepName != b[i].StepName { | ||
return false | ||
} | ||
if step.RunCommand != b[i].RunCommand { | ||
return false | ||
} | ||
if step.EnvVarName != b[i].EnvVarName { | ||
return false | ||
} | ||
if step.EnvVarValue != b[i].EnvVarValue { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func compareExecuteSteps(a []execute.Step, b []execute.Step) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
|
||
for i, step := range a { | ||
if step.StepName != b[i].StepName { | ||
return false | ||
} | ||
if step.RunCommand != b[i].RunCommand { | ||
return false | ||
} | ||
if step.EnvVarName != b[i].EnvVarName { | ||
return false | ||
} | ||
if step.EnvVarValue != b[i].EnvVarValue { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func TestSteps(t *testing.T) { | ||
tests := []struct { | ||
requestSteps []workflows.Step | ||
expectedSteps []execute.Step | ||
}{ | ||
{ | ||
requestSteps: []workflows.Step{ | ||
{ | ||
StepName: "step1", | ||
ExtraArgs: []string{"arg1", "arg2"}, | ||
RunCommand: "run", | ||
EnvVarName: "env1", | ||
EnvVarValue: "value1", | ||
}, | ||
}, | ||
expectedSteps: []execute.Step{ | ||
{ | ||
StepName: "step1", | ||
ExtraArgs: []string{"arg1", "arg2"}, | ||
RunCommand: "run", | ||
EnvVarName: "env1", | ||
EnvVarValue: "value1", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
res := steps(tt.requestSteps) | ||
assert.True(t, compareExecuteSteps(res, tt.expectedSteps)) | ||
} | ||
} |