Skip to content

Commit

Permalink
Merge pull request hashicorp#34850 from alexknez/feature/zonal_config
Browse files Browse the repository at this point in the history
Add `zonal_config` argument to `aws_codedeploy_deployment_config`
  • Loading branch information
ewbankkit authored Oct 23, 2024
2 parents fca5204 + b40e5ef commit 46bd33c
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 66 deletions.
3 changes: 3 additions & 0 deletions .changelog/34850.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_codedeploy_deployment_config: Add `zonal_config` argument
```
245 changes: 180 additions & 65 deletions internal/service/deploy/deployment_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,47 @@ func resourceDeploymentConfig() *schema.Resource {
},
},
},
"zonal_config": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"first_zone_monitor_duration_in_seconds": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"minimum_healthy_hosts_per_zone": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrType: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateDiagFunc: enum.Validate[types.MinimumHealthyHostsPerZoneType](),
},
names.AttrValue: {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
},
},
"monitor_duration_in_seconds": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
},
},
},
}
}
Expand All @@ -148,6 +189,7 @@ func resourceDeploymentConfigCreate(ctx context.Context, d *schema.ResourceData,
DeploymentConfigName: aws.String(name),
MinimumHealthyHosts: expandMinimumHealthyHosts(d),
TrafficRoutingConfig: expandTrafficRoutingConfig(d),
ZonalConfig: expandZonalConfig(d),
}

_, err := conn.CreateDeploymentConfig(ctx, input)
Expand Down Expand Up @@ -195,6 +237,9 @@ func resourceDeploymentConfigRead(ctx context.Context, d *schema.ResourceData, m
if err := d.Set("traffic_routing_config", flattenTrafficRoutingConfig(deploymentConfig.TrafficRoutingConfig)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting traffic_routing_config: %s", err)
}
if err := d.Set("zonal_config", flattenZonalConfig(deploymentConfig.ZonalConfig)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting zonal_config: %s", err)
}

return diags
}
Expand Down Expand Up @@ -241,116 +286,186 @@ func findDeploymentConfigByName(ctx context.Context, conn *codedeploy.Client, na
}

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

minimumHealthyHost := types.MinimumHealthyHosts{
Type: types.MinimumHealthyHostsType(host[names.AttrType].(string)),
Value: int32(host[names.AttrValue].(int)),
tfMap := v.([]interface{})[0].(map[string]interface{})

apiObject := &types.MinimumHealthyHosts{
Type: types.MinimumHealthyHostsType(tfMap[names.AttrType].(string)),
Value: int32(tfMap[names.AttrValue].(int)),
}

return &minimumHealthyHost
return apiObject
}

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

if trafficType, ok := config[names.AttrType]; ok {
trafficRoutingConfig.Type = types.TrafficRoutingType(trafficType.(string))
tfMap := v.([]interface{})[0].(map[string]interface{})
apiObject := &types.TrafficRoutingConfig{}

if v, ok := tfMap["time_based_canary"]; ok && len(v.([]interface{})) > 0 {
apiObject.TimeBasedCanary = expandTimeBasedCanary(v.([]interface{})[0].(map[string]interface{}))
}
if v, ok := tfMap["time_based_linear"]; ok && len(v.([]interface{})) > 0 {
apiObject.TimeBasedLinear = expandTimeBasedLinear(v.([]interface{})[0].(map[string]interface{}))
}
if v, ok := tfMap[names.AttrType]; ok {
apiObject.Type = types.TrafficRoutingType(v.(string))
}

return apiObject
}

func expandTimeBasedCanary(tfMap map[string]interface{}) *types.TimeBasedCanary {
apiObject := &types.TimeBasedCanary{}

if v, ok := tfMap[names.AttrInterval]; ok {
apiObject.CanaryInterval = int32(v.(int))
}
if v, ok := tfMap["percentage"]; ok {
apiObject.CanaryPercentage = int32(v.(int))
}
if canary, ok := config["time_based_canary"]; ok && len(canary.([]interface{})) > 0 {
canaryConfig := canary.([]interface{})[0].(map[string]interface{})
trafficRoutingConfig.TimeBasedCanary = expandTimeBasedCanary(canaryConfig)

return apiObject
}

func expandTimeBasedLinear(tfMap map[string]interface{}) *types.TimeBasedLinear {
apiObject := &types.TimeBasedLinear{}

if v, ok := tfMap[names.AttrInterval]; ok {
apiObject.LinearInterval = int32(v.(int))
}
if linear, ok := config["time_based_linear"]; ok && len(linear.([]interface{})) > 0 {
linearConfig := linear.([]interface{})[0].(map[string]interface{})
trafficRoutingConfig.TimeBasedLinear = expandTimeBasedLinear(linearConfig)
if v, ok := tfMap["percentage"]; ok {
apiObject.LinearPercentage = int32(v.(int))
}

return &trafficRoutingConfig
return apiObject
}

func expandTimeBasedCanary(config map[string]interface{}) *types.TimeBasedCanary {
canary := types.TimeBasedCanary{}
if interval, ok := config[names.AttrInterval]; ok {
canary.CanaryInterval = int32(interval.(int))
func expandZonalConfig(d *schema.ResourceData) *types.ZonalConfig {
v, ok := d.GetOk("zonal_config")
if !ok {
return nil
}

tfMap := v.([]interface{})[0].(map[string]interface{})
apiObject := &types.ZonalConfig{}

if v, ok := tfMap["first_zone_monitor_duration_in_seconds"].(int); ok {
apiObject.FirstZoneMonitorDurationInSeconds = aws.Int64(int64(v))
}
if percentage, ok := config["percentage"]; ok {
canary.CanaryPercentage = int32(percentage.(int))
if v, ok := tfMap["minimum_healthy_hosts_per_zone"]; ok && len(v.([]interface{})) > 0 {
apiObject.MinimumHealthyHostsPerZone = expandMinimumHealthyHostsPerZone(v.([]interface{})[0].(map[string]interface{}))
}
return &canary
if v, ok := tfMap["monitor_duration_in_seconds"].(int); ok {
apiObject.MonitorDurationInSeconds = aws.Int64(int64(v))
}

return apiObject
}

func expandTimeBasedLinear(config map[string]interface{}) *types.TimeBasedLinear {
linear := types.TimeBasedLinear{}
if interval, ok := config[names.AttrInterval]; ok {
linear.LinearInterval = int32(interval.(int))
func expandMinimumHealthyHostsPerZone(tfMap map[string]interface{}) *types.MinimumHealthyHostsPerZone {
if tfMap == nil {
return nil
}
if percentage, ok := config["percentage"]; ok {
linear.LinearPercentage = int32(percentage.(int))

apiObject := &types.MinimumHealthyHostsPerZone{
Type: types.MinimumHealthyHostsPerZoneType(tfMap[names.AttrType].(string)),
Value: int32(tfMap[names.AttrValue].(int)),
}
return &linear

return apiObject
}

func flattenMinimumHealthHosts(hosts *types.MinimumHealthyHosts) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
if hosts == nil {
return result
func flattenMinimumHealthHosts(apiObject *types.MinimumHealthyHosts) []interface{} {
tfList := make([]interface{}, 0)

if apiObject == nil {
return tfList
}

item := make(map[string]interface{})
tfMap := make(map[string]interface{})
tfMap[names.AttrType] = apiObject.Type
tfMap[names.AttrValue] = apiObject.Value

item[names.AttrType] = string(hosts.Type)
item[names.AttrValue] = hosts.Value
return append(tfList, tfMap)
}

return append(result, item)
func flattenTrafficRoutingConfig(apiObject *types.TrafficRoutingConfig) []interface{} {
tfList := make([]interface{}, 0)

if apiObject == nil {
return tfList
}

tfMap := make(map[string]interface{})
tfMap["time_based_canary"] = flattenTimeBasedCanary(apiObject.TimeBasedCanary)
tfMap["time_based_linear"] = flattenTimeBasedLinear(apiObject.TimeBasedLinear)
tfMap[names.AttrType] = apiObject.Type

return append(tfList, tfMap)
}

func flattenTrafficRoutingConfig(config *types.TrafficRoutingConfig) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
if config == nil {
return result
func flattenTimeBasedCanary(apiObject *types.TimeBasedCanary) []interface{} {
tfList := make([]interface{}, 0)

if apiObject == nil {
return tfList
}

item := make(map[string]interface{})
tfMap := make(map[string]interface{})
tfMap[names.AttrInterval] = apiObject.CanaryInterval
tfMap["percentage"] = apiObject.CanaryPercentage

item[names.AttrType] = string(config.Type)
item["time_based_canary"] = flattenTimeBasedCanary(config.TimeBasedCanary)
item["time_based_linear"] = flattenTimeBasedLinear(config.TimeBasedLinear)
return append(tfList, tfMap)
}

return append(result, item)
func flattenTimeBasedLinear(apiObject *types.TimeBasedLinear) []interface{} {
tfList := make([]interface{}, 0)

if apiObject == nil {
return tfList
}

tfMap := make(map[string]interface{})
tfMap[names.AttrInterval] = apiObject.LinearInterval
tfMap["percentage"] = apiObject.LinearPercentage

return append(tfList, tfMap)
}

func flattenTimeBasedCanary(canary *types.TimeBasedCanary) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
if canary == nil {
return result
func flattenZonalConfig(apiObject *types.ZonalConfig) []interface{} {
tfList := make([]interface{}, 0)

if apiObject == nil {
return nil
}

item := make(map[string]interface{})
item[names.AttrInterval] = canary.CanaryInterval
item["percentage"] = canary.CanaryPercentage
tfMap := make(map[string]interface{})
tfMap["first_zone_monitor_duration_in_seconds"] = aws.ToInt64(apiObject.FirstZoneMonitorDurationInSeconds)
tfMap["minimum_healthy_hosts_per_zone"] = flattenMinimumHealthHostsPerZone(apiObject.MinimumHealthyHostsPerZone)
tfMap["monitor_duration_in_seconds"] = aws.ToInt64(apiObject.MonitorDurationInSeconds)

return append(result, item)
return append(tfList, tfMap)
}

func flattenTimeBasedLinear(linear *types.TimeBasedLinear) []map[string]interface{} {
result := make([]map[string]interface{}, 0)
if linear == nil {
return result
func flattenMinimumHealthHostsPerZone(apiObject *types.MinimumHealthyHostsPerZone) []interface{} {
tfList := make([]interface{}, 0)

if apiObject == nil {
return nil
}

item := make(map[string]interface{})
item[names.AttrInterval] = linear.LinearInterval
item["percentage"] = linear.LinearPercentage
tfMap := make(map[string]interface{})
tfMap[names.AttrType] = apiObject.Type
tfMap[names.AttrValue] = apiObject.Value

return append(result, item)
return append(tfList, tfMap)
}
Loading

0 comments on commit 46bd33c

Please sign in to comment.