Skip to content

Commit

Permalink
Merge pull request #24042 from magodo/container_app_ingress_latest
Browse files Browse the repository at this point in the history
`azurerm_container_app` - Add validations for the `ingress.0.traffic_weight`
  • Loading branch information
jackofallops authored Jan 8, 2024
2 parents ed481a4 + 9b90a4c commit bb71f3e
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 138 deletions.
46 changes: 45 additions & 1 deletion internal/services/containerapps/container_app_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,51 @@ func (r ContainerAppResource) Update() sdk.ResourceFunc {
func (r ContainerAppResource) CustomizeDiff() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
if metadata.ResourceDiff != nil && metadata.ResourceDiff.HasChange("secret") {
if metadata.ResourceDiff == nil {
return nil
}
var app ContainerAppModel
if err := metadata.DecodeDiff(&app); err != nil {
return err
}
// Ingress traffic weight validations
if len(app.Ingress) != 0 {
ingress := app.Ingress[0]
if metadata.ResourceDiff.HasChange("name") {
// Validation for create time
// (Above is a trick to tell whether this is for a new create apply, as the "name" is a force new property)
if len(ingress.TrafficWeights) != 0 {
if len(ingress.TrafficWeights) > 1 {
return fmt.Errorf("at most one `ingress.0.traffic_weight` can be specified during creation")
}
tw := ingress.TrafficWeights[0]
if !tw.LatestRevision {
return fmt.Errorf("`ingress.0.traffic_weight.0.latest_revision` must be set to true during creation")
}
if tw.RevisionSuffix != "" {
return fmt.Errorf("`ingress.0.traffic_weight.0.revision_suffix` must not be set during creation")
}
}
} else {
// Validation for update time
var latestRevCount int
for i, tw := range ingress.TrafficWeights {
if tw.LatestRevision {
latestRevCount++
if tw.RevisionSuffix != "" {
return fmt.Errorf("`ingress.0.traffic_weight.%[1]d.revision_suffix` conflicts with `ingress.0.traffic_weight.%[1]d.latest_revision`", i)
}
} else if tw.RevisionSuffix == "" {
return fmt.Errorf("`ingress.0.traffic_weight.%[1]d.revision_suffix` is not specified", i)
}
}
if latestRevCount > 1 {
return fmt.Errorf("more than one `ingress.0.traffic_weight` has `latest_revision` set to `true`")
}
}
}

if metadata.ResourceDiff.HasChange("secret") {
stateSecretsRaw, configSecretsRaw := metadata.ResourceDiff.GetChange("secret")
stateSecrets := stateSecretsRaw.(*schema.Set).List()
configSecrets := configSecretsRaw.(*schema.Set).List()
Expand Down
Loading

0 comments on commit bb71f3e

Please sign in to comment.