Skip to content

Commit

Permalink
azurerm_application_insights - re-add support for updating `workspa…
Browse files Browse the repository at this point in the history
…ce_id`
  • Loading branch information
myc2h6o committed Mar 20, 2023
1 parent 74c3872 commit 241bd1a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
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

0 comments on commit 241bd1a

Please sign in to comment.