From 241bd1a817584071ea80bd5ed4f30af8382aba90 Mon Sep 17 00:00:00 2001 From: Yichun Ma Date: Mon, 20 Mar 2023 13:04:52 +0800 Subject: [PATCH] `azurerm_application_insights` - re-add support for updating `workspace_id` --- .../application_insights_resource.go | 8 ++- .../application_insights_resource_test.go | 49 +++++++++++++++++-- .../docs/r/application_insights.html.markdown | 4 +- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/internal/services/applicationinsights/application_insights_resource.go b/internal/services/applicationinsights/application_insights_resource.go index 4737e759f754..64ad1456233b 100644 --- a/internal/services/applicationinsights/application_insights_resource.go +++ b/internal/services/applicationinsights/application_insights_resource.go @@ -78,7 +78,6 @@ func resourceApplicationInsights() *pluginsdk.Resource { "workspace_id": { Type: pluginsdk.TypeString, Optional: true, - ForceNew: true, ValidateFunc: workspaces.ValidateWorkspaceID, }, @@ -227,6 +226,13 @@ func resourceApplicationInsightsCreateUpdate(d *pluginsdk.ResourceData, meta int ForceCustomerStorageForProfiler: utils.Bool(forceCustomerStorageForProfiler), } + if !d.IsNewResource() { + oldWorkspaceId, newWorkspaceId := d.GetChange("workspace_id") + if oldWorkspaceId.(string) != "" && newWorkspaceId.(string) == "" { + return fmt.Errorf("`workspace_id` can not be removed after set") + } + } + if workspaceRaw, hasWorkspaceId := d.GetOk("workspace_id"); hasWorkspaceId { workspaceID, err := workspaces.ParseWorkspaceID(workspaceRaw.(string)) if err != nil { diff --git a/internal/services/applicationinsights/application_insights_resource_test.go b/internal/services/applicationinsights/application_insights_resource_test.go index ca9b2120e80f..b36faf4af993 100644 --- a/internal/services/applicationinsights/application_insights_resource_test.go +++ b/internal/services/applicationinsights/application_insights_resource_test.go @@ -152,10 +152,16 @@ func TestAccApplicationInsights_basicWorkspaceMode(t *testing.T) { data.ResourceTest(t, r, []acceptance.TestStep{ { - Config: r.basic_workspace_mode(data), + Config: r.basicWorkspaceMode(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.basicWorkspaceModeUpdated(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), - check.That(data.ResourceName).Key("workspace_id").Exists(), ), }, data.ImportStep(), @@ -279,7 +285,7 @@ resource "azurerm_application_insights" "test" { `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, applicationType) } -func (AppInsightsResource) basic_workspace_mode(data acceptance.TestData) string { +func (AppInsightsResource) basicWorkspaceMode(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { features {} @@ -308,6 +314,43 @@ resource "azurerm_application_insights" "test" { `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) } +func (AppInsightsResource) basicWorkspaceModeUpdated(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-appinsights-%[2]d" + location = "%[1]s" +} + +resource "azurerm_log_analytics_workspace" "test" { + name = "acctest-%[2]d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + sku = "PerGB2018" + retention_in_days = 30 +} + +resource "azurerm_log_analytics_workspace" "test2" { + name = "acctest2-%[2]d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + sku = "PerGB2018" + retention_in_days = 30 +} + +resource "azurerm_application_insights" "test" { + name = "acctestappinsights-%[2]d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + workspace_id = azurerm_log_analytics_workspace.test2.id + application_type = "web" +} +`, data.Locations.Primary, data.RandomInteger) +} + func (AppInsightsResource) requiresImport(data acceptance.TestData, applicationType string) string { template := AppInsightsResource{}.basic(data, applicationType) return fmt.Sprintf(` diff --git a/website/docs/r/application_insights.html.markdown b/website/docs/r/application_insights.html.markdown index 9455bfd2a3db..036cacd59c52 100644 --- a/website/docs/r/application_insights.html.markdown +++ b/website/docs/r/application_insights.html.markdown @@ -91,7 +91,9 @@ The following arguments are supported: * `tags` - (Optional) A mapping of tags to assign to the resource. -* `workspace_id` - (Optional) Specifies the id of a log analytics workspace resource. Changing this forces a new resource to be created. +* `workspace_id` - (Optional) Specifies the id of a log analytics workspace resource. + +~> **NOTE:** This can not be removed after set. More details can be found at [Migrate to workspace-based Application Insights resources](https://docs.microsoft.com/azure/azure-monitor/app/convert-classic-resource#migration-process) * `local_authentication_disabled` - (Optional) Disable Non-Azure AD based Auth. Defaults to `false`.