Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_container_app - Relax validation on the ingress traffic #27396

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading