From bf132a538fe48e5ab2c1243682a445f335cb0fdb Mon Sep 17 00:00:00 2001 From: Yichun Ma Date: Fri, 25 Aug 2023 12:33:17 +0800 Subject: [PATCH] `azurerm_container_app_environment` - support `dapr_application_insights_connection_string` --- .../container_app_environment_resource.go | 34 ++++++++++++---- ...container_app_environment_resource_test.go | 40 +++++++++++++++++++ .../r/container_app_environment.html.markdown | 2 + 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/internal/services/containerapps/container_app_environment_resource.go b/internal/services/containerapps/container_app_environment_resource.go index a7254cfe1438..235e8abcffac 100644 --- a/internal/services/containerapps/container_app_environment_resource.go +++ b/internal/services/containerapps/container_app_environment_resource.go @@ -19,18 +19,20 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/services/containerapps/validate" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" ) type ContainerAppEnvironmentResource struct{} type ContainerAppEnvironmentModel struct { - Name string `tfschema:"name"` - ResourceGroup string `tfschema:"resource_group_name"` - Location string `tfschema:"location"` - LogAnalyticsWorkspaceId string `tfschema:"log_analytics_workspace_id"` - InfrastructureSubnetId string `tfschema:"infrastructure_subnet_id"` - InternalLoadBalancerEnabled bool `tfschema:"internal_load_balancer_enabled"` - Tags map[string]interface{} `tfschema:"tags"` + Name string `tfschema:"name"` + ResourceGroup string `tfschema:"resource_group_name"` + Location string `tfschema:"location"` + DaprApplicationInsightsConnectionString string `tfschema:"dapr_application_insights_connection_string"` + LogAnalyticsWorkspaceId string `tfschema:"log_analytics_workspace_id"` + InfrastructureSubnetId string `tfschema:"infrastructure_subnet_id"` + InternalLoadBalancerEnabled bool `tfschema:"internal_load_balancer_enabled"` + Tags map[string]interface{} `tfschema:"tags"` DefaultDomain string `tfschema:"default_domain"` DockerBridgeCidr string `tfschema:"docker_bridge_cidr"` @@ -67,6 +69,15 @@ func (r ContainerAppEnvironmentResource) Arguments() map[string]*pluginsdk.Schem "resource_group_name": commonschema.ResourceGroupName(), + "dapr_application_insights_connection_string": { + Type: pluginsdk.TypeString, + Optional: true, + ForceNew: true, + Sensitive: true, + ValidateFunc: validation.StringIsNotEmpty, + Description: "Application Insights connection string used by Dapr to export Service to Service communication telemetry.", + }, + "log_analytics_workspace_id": { Type: pluginsdk.TypeString, Optional: true, @@ -165,6 +176,10 @@ func (r ContainerAppEnvironmentResource) Create() sdk.ResourceFunc { Tags: tags.Expand(containerAppEnvironment.Tags), } + if containerAppEnvironment.DaprApplicationInsightsConnectionString != "" { + managedEnvironment.Properties.DaprAIConnectionString = pointer.To(containerAppEnvironment.DaprApplicationInsightsConnectionString) + } + if containerAppEnvironment.LogAnalyticsWorkspaceId != "" { logAnalyticsId, err := workspaces.ParseWorkspaceID(containerAppEnvironment.LogAnalyticsWorkspaceId) if err != nil { @@ -255,6 +270,11 @@ func (r ContainerAppEnvironmentResource) Read() sdk.ResourceFunc { } } + // `dapr_application_insights_connection_string` is sensitive and not returned by API + if v := metadata.ResourceData.Get("dapr_application_insights_connection_string").(string); v != "" { + state.DaprApplicationInsightsConnectionString = v + } + // Reading in log_analytics_workspace_id is not possible, so reading from config. Import will need to ignore_changes unfortunately if v := metadata.ResourceData.Get("log_analytics_workspace_id").(string); v != "" { state.LogAnalyticsWorkspaceId = v diff --git a/internal/services/containerapps/container_app_environment_resource_test.go b/internal/services/containerapps/container_app_environment_resource_test.go index c9729b562908..f2d39895c769 100644 --- a/internal/services/containerapps/container_app_environment_resource_test.go +++ b/internal/services/containerapps/container_app_environment_resource_test.go @@ -86,6 +86,21 @@ func TestAccContainerAppEnvironment_completeUpdate(t *testing.T) { }) } +func TestAccContainerAppEnvironment_daprApplicationInsightsConnectionString(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_container_app_environment", "test") + r := ContainerAppEnvironmentResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.daprApplicationInsightsConnectionString(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep("dapr_application_insights_connection_string"), + }) +} + func (r ContainerAppEnvironmentResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := managedenvironments.ParseManagedEnvironmentID(state.ID) if err != nil { @@ -184,6 +199,31 @@ resource "azurerm_container_app_environment" "test" { `, r.templateVNet(data), data.RandomInteger) } +func (r ContainerAppEnvironmentResource) daprApplicationInsightsConnectionString(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +%[1]s + +resource "azurerm_application_insights" "test" { + name = "acctestappinsights-%[2]d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + application_type = "web" +} + +resource "azurerm_container_app_environment" "test" { + name = "acctest-CAEnv%[2]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + + dapr_application_insights_connection_string = azurerm_application_insights.test.connection_string +} +`, r.template(data), data.RandomInteger) +} + func (r ContainerAppEnvironmentResource) template(data acceptance.TestData) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { diff --git a/website/docs/r/container_app_environment.html.markdown b/website/docs/r/container_app_environment.html.markdown index 265dffac789f..4968213de28f 100644 --- a/website/docs/r/container_app_environment.html.markdown +++ b/website/docs/r/container_app_environment.html.markdown @@ -46,6 +46,8 @@ The following arguments are supported: --- +* `dapr_application_insights_connection_string` - (Optional) Application Insights connection string used by Dapr to export Service to Service communication telemetry. + * `infrastructure_subnet_id` - (Optional) The existing Subnet to use for the Container Apps Control Plane. Changing this forces a new resource to be created. ~> **NOTE:** The Subnet must have a `/21` or larger address space.