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_application_insights - re-add support for updating workspace_id #21029

Merged
merged 1 commit into from
Mar 23, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func resourceApplicationInsights() *pluginsdk.Resource {
"workspace_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: workspaces.ValidateWorkspaceID,
},

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -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(`
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/application_insights.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down