Skip to content

Commit

Permalink
azurerm_container_app - Relax validation on the ingress traffic (#2…
Browse files Browse the repository at this point in the history
…7396)

* `azurerm_container_app` - Relax validation on the ingress traffic

* Update doc

* pass golint

* pass golint
  • Loading branch information
magodo committed Sep 20, 2024
1 parent eccb825 commit 5bff5bf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 63 deletions.
34 changes: 4 additions & 30 deletions internal/services/containerapps/container_app_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,36 +440,10 @@ func (r ContainerAppResource) CustomizeDiff() sdk.ResourceFunc {
// 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`")

for i, tw := range ingress.TrafficWeights {
if !tw.LatestRevision && tw.RevisionSuffix == "" {
return fmt.Errorf("`either ingress.0.traffic_weight.%[1]d.revision_suffix` or `ingress.0.traffic_weight.%[1]d.latest_revision` should be specified", i)
}
}
}
Expand Down
36 changes: 4 additions & 32 deletions internal/services/containerapps/container_app_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,16 +570,8 @@ func TestAccContainerAppResource_ingressTrafficValidation(t *testing.T) {

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.ingressTrafficValidation(data, r.trafficBlockMoreThanOne()),
ExpectError: regexp.MustCompile(fmt.Sprintf(`at most one %s can be specified during creation`, "`ingress.0.traffic_weight`")),
},
{
Config: r.ingressTrafficValidation(data, r.trafficBlockLatestRevisionNotSet()),
ExpectError: regexp.MustCompile(fmt.Sprintf(`%s must be set to true during creation`, "`ingress.0.traffic_weight.0.latest_revision`")),
},
{
Config: r.ingressTrafficValidation(data, r.trafficBlockRevisionSuffixSet()),
ExpectError: regexp.MustCompile(fmt.Sprintf(`%s must not be set during creation`, "`ingress.0.traffic_weight.0.revision_suffix`")),
Config: r.ingressTrafficValidation(data, r.latestRevisionFalseRevisionSuffixEmpty()),
ExpectError: regexp.MustCompile("`either ingress.0.traffic_weight.0.revision_suffix` or `ingress.0.traffic_weight.0.latest_revision` should be specified"),
},
})
}
Expand Down Expand Up @@ -2832,31 +2824,11 @@ resource "azurerm_container_app" "test" {
`, r.template(data), data.RandomInteger)
}

func (r ContainerAppResource) trafficBlockMoreThanOne() string {
return `
traffic_weight {
percentage = 50
}
traffic_weight {
percentage = 50
}
`
}

func (r ContainerAppResource) trafficBlockLatestRevisionNotSet() string {
func (r ContainerAppResource) latestRevisionFalseRevisionSuffixEmpty() string {
return `
traffic_weight {
latest_revision = false
percentage = 100
}
`
}

func (r ContainerAppResource) trafficBlockRevisionSuffixSet() string {
return `
traffic_weight {
percentage = 100
latest_revision = true
revision_suffix = "foo"
}
`
}
2 changes: 1 addition & 1 deletion website/docs/r/container_app.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ A `traffic_weight` block supports the following:

* `revision_suffix` - (Optional) The suffix string to which this `traffic_weight` applies.

~> **Note:** `latest_revision` conflicts with `revision_suffix`, which means you shall either set `latest_revision` to `true` or specify `revision_suffix`. Especially for creation, there shall only be one `traffic_weight`, with the `latest_revision` set to `true`, and leave the `revision_suffix` empty.
~> **Note:** If `latest_revision` is `false`, the `revision_suffix` shall be specified.

* `percentage` - (Required) The percentage of traffic which should be sent this revision.

Expand Down

0 comments on commit 5bff5bf

Please sign in to comment.