From 4218e75ab69f8b5dd5e3c3656ae070db85628d1e Mon Sep 17 00:00:00 2001 From: magodo Date: Sat, 14 Sep 2024 15:18:08 +0800 Subject: [PATCH 1/4] `azurerm_container_app` - Relax validation on the ingress traffic --- .../containerapps/container_app_resource.go | 36 +++---------------- .../container_app_resource_test.go | 36 +++---------------- 2 files changed, 9 insertions(+), 63 deletions(-) diff --git a/internal/services/containerapps/container_app_resource.go b/internal/services/containerapps/container_app_resource.go index 2bfc598948f3..0164964bd20b 100644 --- a/internal/services/containerapps/container_app_resource.go +++ b/internal/services/containerapps/container_app_resource.go @@ -437,39 +437,13 @@ func (r ContainerAppResource) CustomizeDiff() sdk.ResourceFunc { if err := metadata.DecodeDiff(&app); err != nil { return err } - // Ingress traffic weight validations + //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) } } } diff --git a/internal/services/containerapps/container_app_resource_test.go b/internal/services/containerapps/container_app_resource_test.go index fbc03d535314..c1049e0783bf 100644 --- a/internal/services/containerapps/container_app_resource_test.go +++ b/internal/services/containerapps/container_app_resource_test.go @@ -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(fmt.Sprintf("`either ingress.0.traffic_weight.0.revision_suffix` or `ingress.0.traffic_weight.0.latest_revision` should be specified")), }, }) } @@ -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" -} -` -} From 5efe2795d70fba68ac8dbbc3a08d4d61cd8db95c Mon Sep 17 00:00:00 2001 From: magodo Date: Sat, 14 Sep 2024 15:34:11 +0800 Subject: [PATCH 2/4] Update doc --- website/docs/r/container_app.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/container_app.html.markdown b/website/docs/r/container_app.html.markdown index 6d71cb9be01e..589b63acca2c 100644 --- a/website/docs/r/container_app.html.markdown +++ b/website/docs/r/container_app.html.markdown @@ -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. From 5706bfb76c0c679bbe06795c747acc1f5b64737f Mon Sep 17 00:00:00 2001 From: magodo Date: Sat, 14 Sep 2024 15:38:27 +0800 Subject: [PATCH 3/4] pass golint --- internal/services/containerapps/container_app_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/containerapps/container_app_resource.go b/internal/services/containerapps/container_app_resource.go index 0164964bd20b..d83027d24eea 100644 --- a/internal/services/containerapps/container_app_resource.go +++ b/internal/services/containerapps/container_app_resource.go @@ -437,7 +437,7 @@ func (r ContainerAppResource) CustomizeDiff() sdk.ResourceFunc { if err := metadata.DecodeDiff(&app); err != nil { return err } - //Ingress traffic weight validations + // Ingress traffic weight validations if len(app.Ingress) != 0 { ingress := app.Ingress[0] From cc14a5845911ccb62b5f24052f3cbd42839883e0 Mon Sep 17 00:00:00 2001 From: magodo Date: Sat, 14 Sep 2024 16:29:16 +0800 Subject: [PATCH 4/4] pass golint --- internal/services/containerapps/container_app_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/containerapps/container_app_resource_test.go b/internal/services/containerapps/container_app_resource_test.go index c1049e0783bf..b98919252059 100644 --- a/internal/services/containerapps/container_app_resource_test.go +++ b/internal/services/containerapps/container_app_resource_test.go @@ -571,7 +571,7 @@ func TestAccContainerAppResource_ingressTrafficValidation(t *testing.T) { data.ResourceTest(t, r, []acceptance.TestStep{ { Config: r.ingressTrafficValidation(data, r.latestRevisionFalseRevisionSuffixEmpty()), - ExpectError: regexp.MustCompile(fmt.Sprintf("`either ingress.0.traffic_weight.0.revision_suffix` or `ingress.0.traffic_weight.0.latest_revision` should be specified")), + ExpectError: regexp.MustCompile("`either ingress.0.traffic_weight.0.revision_suffix` or `ingress.0.traffic_weight.0.latest_revision` should be specified"), }, }) }