Skip to content

Commit

Permalink
r/codedeploy_config: add support for lambda and traffic_routing
Browse files Browse the repository at this point in the history
fixes #5393, refs #4839
- adds traffic_routing_config sub-resource to
codedeploy_deployment_config resource
- add computed_platform optional element
- make minimum_healthy_hosts optional as per aws api spec
  • Loading branch information
acburdine committed Nov 30, 2018
1 parent 6477cfc commit d6b4d8f
Show file tree
Hide file tree
Showing 3 changed files with 382 additions and 9 deletions.
192 changes: 184 additions & 8 deletions aws/resource_aws_codedeploy_deployment_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,28 @@ func resourceAwsCodeDeployDeploymentConfig() *schema.Resource {
ForceNew: true,
},

"compute_platform": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
codedeploy.ComputePlatformServer,
codedeploy.ComputePlatformLambda,
codedeploy.ComputePlatformEcs,
}, false),
Default: codedeploy.ComputePlatformServer,
},

"minimum_healthy_hosts": {
Type: schema.TypeList,
Required: true,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
codedeploy.MinimumHealthyHostsTypeHostCount,
Expand All @@ -52,6 +64,72 @@ func resourceAwsCodeDeployDeploymentConfig() *schema.Resource {
},
},

"traffic_routing_config": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
codedeploy.TrafficRoutingTypeAllAtOnce,
codedeploy.TrafficRoutingTypeTimeBasedCanary,
codedeploy.TrafficRoutingTypeTimeBasedLinear,
}, false),
Default: codedeploy.TrafficRoutingTypeAllAtOnce,
},

"time_based_canary": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"traffic_routing_config.0.time_based_linear"},
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"interval": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"percentage": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
},
},

"time_based_linear": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"traffic_routing_config.0.time_based_canary"},
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"interval": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"percentage": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
},
},
},
},
},

"deployment_config_id": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -65,7 +143,9 @@ func resourceAwsCodeDeployDeploymentConfigCreate(d *schema.ResourceData, meta in

input := &codedeploy.CreateDeploymentConfigInput{
DeploymentConfigName: aws.String(d.Get("deployment_config_name").(string)),
ComputePlatform: aws.String(d.Get("compute_platform").(string)),
MinimumHealthyHosts: expandAwsCodeDeployConfigMinimumHealthHosts(d),
TrafficRoutingConfig: expandAwsCodeDeployTrafficRoutingConfig(d),
}

_, err := conn.CreateDeploymentConfig(input)
Expand Down Expand Up @@ -104,8 +184,14 @@ func resourceAwsCodeDeployDeploymentConfigRead(d *schema.ResourceData, meta inte
if err := d.Set("minimum_healthy_hosts", flattenAwsCodeDeployConfigMinimumHealthHosts(resp.DeploymentConfigInfo.MinimumHealthyHosts)); err != nil {
return err
}

if err := d.Set("traffic_routing_config", flattenAwsCodeDeployTrafficRoutingConfig(resp.DeploymentConfigInfo.TrafficRoutingConfig)); err != nil {
return err
}

d.Set("deployment_config_id", resp.DeploymentConfigInfo.DeploymentConfigId)
d.Set("deployment_config_name", resp.DeploymentConfigInfo.DeploymentConfigName)
d.Set("compute_platform", resp.DeploymentConfigInfo.ComputePlatform)

return nil
}
Expand All @@ -126,8 +212,11 @@ func resourceAwsCodeDeployDeploymentConfigDelete(d *schema.ResourceData, meta in
}

func expandAwsCodeDeployConfigMinimumHealthHosts(d *schema.ResourceData) *codedeploy.MinimumHealthyHosts {
hosts := d.Get("minimum_healthy_hosts").([]interface{})
host := hosts[0].(map[string]interface{})
hosts, ok := d.GetOk("minimum_healthy_hosts")
if !ok {
return nil
}
host := hosts.([]interface{})[0].(map[string]interface{})

minimumHealthyHost := codedeploy.MinimumHealthyHosts{
Type: aws.String(host["type"].(string)),
Expand All @@ -137,15 +226,102 @@ func expandAwsCodeDeployConfigMinimumHealthHosts(d *schema.ResourceData) *codede
return &minimumHealthyHost
}

func expandAwsCodeDeployTrafficRoutingConfig(d *schema.ResourceData) *codedeploy.TrafficRoutingConfig {
block, ok := d.GetOk("traffic_routing_config")
if !ok {
return nil
}
config := block.([]interface{})[0].(map[string]interface{})
trafficRoutingConfig := codedeploy.TrafficRoutingConfig{}

if trafficType, ok := config["type"]; ok {
trafficRoutingConfig.Type = aws.String(trafficType.(string))
}
if canary, ok := config["time_based_canary"]; ok && len(canary.([]interface{})) > 0 {
canaryConfig := canary.([]interface{})[0].(map[string]interface{})
trafficRoutingConfig.TimeBasedCanary = expandAwsCodeDeployTrafficTimeBasedCanaryConfig(canaryConfig)
}
if linear, ok := config["time_based_linear"]; ok && len(linear.([]interface{})) > 0 {
linearConfig := linear.([]interface{})[0].(map[string]interface{})
trafficRoutingConfig.TimeBasedLinear = expandAwsCodeDeployTrafficTimeBasedLinearConfig(linearConfig)
}

return &trafficRoutingConfig
}

func expandAwsCodeDeployTrafficTimeBasedCanaryConfig(config map[string]interface{}) *codedeploy.TimeBasedCanary {
canary := codedeploy.TimeBasedCanary{}
if interval, ok := config["interval"]; ok {
canary.CanaryInterval = aws.Int64(int64(interval.(int)))
}
if percentage, ok := config["percentage"]; ok {
canary.CanaryPercentage = aws.Int64(int64(percentage.(int)))
}
return &canary
}

func expandAwsCodeDeployTrafficTimeBasedLinearConfig(config map[string]interface{}) *codedeploy.TimeBasedLinear {
linear := codedeploy.TimeBasedLinear{}
if interval, ok := config["interval"]; ok {
linear.LinearInterval = aws.Int64(int64(interval.(int)))
}
if percentage, ok := config["percentage"]; ok {
linear.LinearPercentage = aws.Int64(int64(percentage.(int)))
}
return &linear
}

func flattenAwsCodeDeployConfigMinimumHealthHosts(hosts *codedeploy.MinimumHealthyHosts) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
if hosts == nil {
return result
}

item := make(map[string]interface{})

item["type"] = aws.StringValue(hosts.Type)
item["value"] = aws.Int64Value(hosts.Value)

return append(result, item)
}

func flattenAwsCodeDeployTrafficRoutingConfig(config *codedeploy.TrafficRoutingConfig) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
if config == nil {
return result
}

item := make(map[string]interface{})

item["type"] = *hosts.Type
item["value"] = *hosts.Value
item["type"] = aws.StringValue(config.Type)
item["time_based_canary"] = flattenAwsCodeDeployTrafficRoutingCanaryConfig(config.TimeBasedCanary)
item["time_based_linear"] = flattenAwsCodeDeployTrafficRoutingLinearConfig(config.TimeBasedLinear)

return append(result, item)
}

func flattenAwsCodeDeployTrafficRoutingCanaryConfig(canary *codedeploy.TimeBasedCanary) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
if canary == nil {
return result
}

result = append(result, item)
item := make(map[string]interface{})
item["interval"] = aws.Int64Value(canary.CanaryInterval)
item["percentage"] = aws.Int64Value(canary.CanaryPercentage)

return append(result, item)
}

func flattenAwsCodeDeployTrafficRoutingLinearConfig(linear *codedeploy.TimeBasedLinear) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
if linear == nil {
return result
}

item := make(map[string]interface{})
item["interval"] = aws.Int64Value(linear.LinearInterval)
item["percentage"] = aws.Int64Value(linear.LinearPercentage)

return result
return append(result, item)
}
Loading

0 comments on commit d6b4d8f

Please sign in to comment.