diff --git a/internal/services/containerapps/container_app_environment_resource.go b/internal/services/containerapps/container_app_environment_resource.go index c915e7bed6a2..a7254cfe1438 100644 --- a/internal/services/containerapps/container_app_environment_resource.go +++ b/internal/services/containerapps/container_app_environment_resource.go @@ -69,7 +69,7 @@ func (r ContainerAppEnvironmentResource) Arguments() map[string]*pluginsdk.Schem "log_analytics_workspace_id": { Type: pluginsdk.TypeString, - Required: true, + Optional: true, ForceNew: true, ValidateFunc: workspaces.ValidateWorkspaceID, Description: "The ID for the Log Analytics Workspace to link this Container Apps Managed Environment to.", @@ -156,49 +156,50 @@ func (r ContainerAppEnvironmentResource) Create() sdk.ResourceFunc { return metadata.ResourceRequiresImport(r.ResourceType(), id) } - logAnalyticsId, err := workspaces.ParseWorkspaceID(containerAppEnvironment.LogAnalyticsWorkspaceId) - if err != nil { - return err - } - - workspace, err := logAnalyticsClient.Get(ctx, *logAnalyticsId) - if err != nil { - return fmt.Errorf("retrieving %s for %s: %+v", logAnalyticsId, id, err) - } - - if workspace.Model == nil || workspace.Model.Properties == nil { - return fmt.Errorf("reading customer ID from %s", logAnalyticsId) - } - - if workspace.Model.Properties.CustomerId == nil { - return fmt.Errorf("reading customer ID from %s, `customer_id` is nil", logAnalyticsId) - } - - keys, err := logAnalyticsClient.SharedKeysGetSharedKeys(ctx, *logAnalyticsId) - if err != nil { - return fmt.Errorf("retrieving access keys to %s for %s: %+v", logAnalyticsId, id, err) - } - - if keys.Model.PrimarySharedKey == nil { - return fmt.Errorf("reading shared key for %s in %s", logAnalyticsId, id) - } - managedEnvironment := managedenvironments.ManagedEnvironment{ Location: containerAppEnvironment.Location, Name: pointer.To(containerAppEnvironment.Name), Properties: &managedenvironments.ManagedEnvironmentProperties{ - AppLogsConfiguration: &managedenvironments.AppLogsConfiguration{ - Destination: pointer.To("log-analytics"), - LogAnalyticsConfiguration: &managedenvironments.LogAnalyticsConfiguration{ - CustomerId: workspace.Model.Properties.CustomerId, - SharedKey: keys.Model.PrimarySharedKey, - }, - }, VnetConfiguration: &managedenvironments.VnetConfiguration{}, }, Tags: tags.Expand(containerAppEnvironment.Tags), } + if containerAppEnvironment.LogAnalyticsWorkspaceId != "" { + logAnalyticsId, err := workspaces.ParseWorkspaceID(containerAppEnvironment.LogAnalyticsWorkspaceId) + if err != nil { + return err + } + + workspace, err := logAnalyticsClient.Get(ctx, *logAnalyticsId) + if err != nil { + return fmt.Errorf("retrieving %s for %s: %+v", logAnalyticsId, id, err) + } + + if workspace.Model == nil || workspace.Model.Properties == nil { + return fmt.Errorf("reading customer ID from %s", logAnalyticsId) + } + + if workspace.Model.Properties.CustomerId == nil { + return fmt.Errorf("reading customer ID from %s, `customer_id` is nil", logAnalyticsId) + } + + keys, err := logAnalyticsClient.SharedKeysGetSharedKeys(ctx, *logAnalyticsId) + if err != nil { + return fmt.Errorf("retrieving access keys to %s for %s: %+v", logAnalyticsId, id, err) + } + if keys.Model.PrimarySharedKey == nil { + return fmt.Errorf("reading shared key for %s in %s", logAnalyticsId, id) + } + managedEnvironment.Properties.AppLogsConfiguration = &managedenvironments.AppLogsConfiguration{ + Destination: pointer.To("log-analytics"), + LogAnalyticsConfiguration: &managedenvironments.LogAnalyticsConfiguration{ + CustomerId: workspace.Model.Properties.CustomerId, + SharedKey: keys.Model.PrimarySharedKey, + }, + } + } + if containerAppEnvironment.InfrastructureSubnetId != "" { managedEnvironment.Properties.VnetConfiguration.InfrastructureSubnetId = pointer.To(containerAppEnvironment.InfrastructureSubnetId) managedEnvironment.Properties.VnetConfiguration.Internal = pointer.To(containerAppEnvironment.InternalLoadBalancerEnabled) diff --git a/internal/services/containerapps/container_app_environment_resource_test.go b/internal/services/containerapps/container_app_environment_resource_test.go index 15a0990e373d..c9729b562908 100644 --- a/internal/services/containerapps/container_app_environment_resource_test.go +++ b/internal/services/containerapps/container_app_environment_resource_test.go @@ -30,7 +30,7 @@ func TestAccContainerAppEnvironment_basic(t *testing.T) { check.That(data.ResourceName).ExistsInAzure(r), ), }, - data.ImportStep("log_analytics_workspace_id"), + data.ImportStep(), }) } @@ -112,10 +112,9 @@ provider "azurerm" { %[1]s 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 - log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id + name = "acctest-CAEnv%[2]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location } `, r.template(data), data.RandomInteger) } @@ -123,13 +122,13 @@ resource "azurerm_container_app_environment" "test" { func (r ContainerAppEnvironmentResource) requiresImport(data acceptance.TestData) string { return fmt.Sprintf(` + %[1]s resource "azurerm_container_app_environment" "import" { - name = azurerm_container_app_environment.test.name - resource_group_name = azurerm_container_app_environment.test.resource_group_name - location = azurerm_container_app_environment.test.location - log_analytics_workspace_id = azurerm_container_app_environment.test.log_analytics_workspace_id + name = azurerm_container_app_environment.test.name + resource_group_name = azurerm_container_app_environment.test.resource_group_name + location = azurerm_container_app_environment.test.location } `, r.basic(data), data.RandomInteger) } diff --git a/website/docs/r/container_app_environment.html.markdown b/website/docs/r/container_app_environment.html.markdown index fc6ff34a8f3d..265dffac789f 100644 --- a/website/docs/r/container_app_environment.html.markdown +++ b/website/docs/r/container_app_environment.html.markdown @@ -44,8 +44,6 @@ The following arguments are supported: * `location` - (Required) Specifies the supported Azure location where the Container App Environment is to exist. Changing this forces a new resource to be created. -* `log_analytics_workspace_id` - (Required) The ID for the Log Analytics Workspace to link this Container Apps Managed Environment to. Changing this forces a new resource to be created. - --- * `infrastructure_subnet_id` - (Optional) The existing Subnet to use for the Container Apps Control Plane. Changing this forces a new resource to be created. @@ -56,6 +54,8 @@ The following arguments are supported: ~> **Note:** can only be set to `true` if `infrastructure_subnet_id` is specified. +* `log_analytics_workspace_id` - (Optional) The ID for the Log Analytics Workspace to link this Container Apps Managed Environment to. Changing this forces a new resource to be created. + * `tags` - (Optional) A mapping of tags to assign to the resource. ## Attributes Reference