Skip to content

Commit

Permalink
TER-259: Handle safe_deployment parameters (expander, flattener, tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Caussat committed Apr 6, 2018
1 parent 0a09339 commit 0b5b1ed
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 14 deletions.
84 changes: 71 additions & 13 deletions ghost/resource_ghost_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,24 @@ func resourceGhostApp() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ha_backend": {
Type: schema.TypeString,
Optional: true,
},
"load_balancer_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"elb", "alb", "haproxy"}, false),
Default: "elb",
},
"app_tag_value": {
Type: schema.TypeString,
Optional: true,
},
"api_port": {
Type: schema.TypeInt,
Optional: true,
},
"wait_before_deploy": {
Type: schema.TypeInt,
Expand All @@ -459,19 +472,6 @@ func resourceGhostApp() *schema.Resource {
ValidateFunc: validation.IntAtLeast(0),
Default: 10,
},
"app_tag_value": {
Type: schema.TypeString,
Optional: true,
},
"api_port": {
Type: schema.TypeInt,
Optional: true,
Default: 5001,
},
"ha_backend": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -579,6 +579,7 @@ func expandGhostApp(d *schema.ResourceData) ghost.App {
LifecycleHooks: expandGhostAppLifecycleHooks(d.Get("lifecycle_hooks").([]interface{})),
LogNotifications: expandGhostAppStringList(d.Get("log_notifications").([]interface{})),
EnvironmentVariables: expandGhostAppEnvironmentVariables(d.Get("environment_variables").([]interface{})),
SafeDeployment: expandGhostAppSafeDeployment(d.Get("safe_deployment").([]interface{})),
}

return app
Expand All @@ -602,6 +603,7 @@ func flattenGhostApp(d *schema.ResourceData, app ghost.App) error {
d.Set("lifecycle_hooks", flattenGhostAppLifecycleHooks(app.LifecycleHooks))
d.Set("log_notifications", flattenGhostAppStringList(app.LogNotifications))
d.Set("environment_variables", flattenGhostAppEnvironmentVariables(app.EnvironmentVariables))
d.Set("safe_deployment", flattenGhostAppSafeDeployment(app.SafeDeployment))

return nil
}
Expand Down Expand Up @@ -1026,3 +1028,59 @@ func flattenGhostAppStringList(strings []string) []interface{} {

return stringList
}

func expandGhostAppSafeDeployment(d []interface{}) *ghost.SafeDeployment {
if len(d) == 0 {
return nil
}

data := d[0].(map[string]interface{})

safeDeployment := &ghost.SafeDeployment{
WaitBeforeDeploy: data["wait_before_deploy"].(int),
WaitAfterDeploy: data["wait_after_deploy"].(int),
LoadBalancerType: data["load_balancer_type"].(string),
}

if data["app_tag_value"] != nil {
safeDeployment.AppTagValue = data["app_tag_value"].(string)
}
if data["ha_backend"] != nil {
safeDeployment.HaBackend = data["ha_backend"].(string)
}
if data["api_port"] != nil {
safeDeployment.ApiPort = data["api_port"].(int)
}

return safeDeployment
}

func flattenGhostAppSafeDeployment(safeDeployment *ghost.SafeDeployment) []interface{} {
values := []interface{}{}

if safeDeployment == nil {
return nil
}

value := map[string]interface{}{
"wait_before_deploy": safeDeployment.WaitBeforeDeploy,
"wait_after_deploy": safeDeployment.WaitAfterDeploy,
"load_balancer_type": safeDeployment.LoadBalancerType,
}

if safeDeployment.AppTagValue != "" {
value["app_tag_value"] = safeDeployment.AppTagValue
}

if safeDeployment.HaBackend != "" {
value["ha_backend"] = safeDeployment.HaBackend
}

if safeDeployment.ApiPort != 0 {
value["api_port"] = safeDeployment.ApiPort
}

values = append(values, value)

return values
}
105 changes: 105 additions & 0 deletions ghost/resource_ghost_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ var (
Key: "env_var_key",
Value: "env_var_value",
}},
SafeDeployment: &ghost.SafeDeployment{
LoadBalancerType: "elb",
WaitBeforeDeploy: 10,
WaitAfterDeploy: 10,
},
}
)

Expand Down Expand Up @@ -825,6 +830,56 @@ func TestExpandGhostAppModules(t *testing.T) {
}
}

func TestExpandGhostSafeDeployment(t *testing.T) {
cases := []struct {
Input []interface{}
ExpectedOutput *ghost.SafeDeployment
}{
{
[]interface{}{
map[string]interface{}{
"wait_before_deploy": 10,
"wait_after_deploy": 10,
"load_balancer_type": "elb",
},
},
app.SafeDeployment,
},
{
[]interface{}{
map[string]interface{}{
"wait_before_deploy": 10,
"wait_after_deploy": 10,
"load_balancer_type": "elb",
"api_port": 5001,
"ha_backend": "test",
"app_tag_value": "test",
},
},
&ghost.SafeDeployment{
ApiPort: 5001,
AppTagValue: "test",
HaBackend: "test",
WaitBeforeDeploy: 10,
WaitAfterDeploy: 10,
LoadBalancerType: "elb",
},
},
{
nil,
nil,
},
}

for _, tc := range cases {
output := expandGhostAppSafeDeployment(tc.Input)
if !reflect.DeepEqual(output, tc.ExpectedOutput) {
t.Fatalf("Unexpected output from expander.\nExpected: %#v\nGiven: %#v",
tc.ExpectedOutput, output)
}
}
}

// Flatteners Unit Tests
func TestFlattenGhostAppStringList(t *testing.T) {
cases := []struct {
Expand Down Expand Up @@ -1193,3 +1248,53 @@ func TestFlattenGhostAppModules(t *testing.T) {
}
}
}

func TestFlattenGhostSafeDeployment(t *testing.T) {
cases := []struct {
Input *ghost.SafeDeployment
ExpectedOutput []interface{}
}{
{
app.SafeDeployment,
[]interface{}{
map[string]interface{}{
"wait_before_deploy": 10,
"wait_after_deploy": 10,
"load_balancer_type": "elb",
},
},
},
{
&ghost.SafeDeployment{
ApiPort: 5001,
AppTagValue: "test",
HaBackend: "test",
WaitBeforeDeploy: 10,
WaitAfterDeploy: 10,
LoadBalancerType: "elb",
},
[]interface{}{
map[string]interface{}{
"wait_before_deploy": 10,
"wait_after_deploy": 10,
"load_balancer_type": "elb",
"api_port": 5001,
"ha_backend": "test",
"app_tag_value": "test",
},
},
},
{
nil,
nil,
},
}

for _, tc := range cases {
output := flattenGhostAppSafeDeployment(tc.Input)
if !reflect.DeepEqual(output, tc.ExpectedOutput) {
t.Fatalf("Unexpected output from flattener.\nExpected: %#v\nGiven: %#v",
tc.ExpectedOutput, output)
}
}
}
2 changes: 1 addition & 1 deletion vendor/cloud-deploy.io/go-st/spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b5b1ed

Please sign in to comment.