From a56bda9ad7a5cef7a6d759bb58929648a3323493 Mon Sep 17 00:00:00 2001 From: magodo Date: Fri, 15 Nov 2024 12:06:59 +1100 Subject: [PATCH] `azurerm_container_group` - Fix update error with `diagnostics.log_analytics` defined --- .../containers/container_group_resource.go | 5 + .../container_group_resource_test.go | 219 ++++++++++++++---- 2 files changed, 179 insertions(+), 45 deletions(-) diff --git a/internal/services/containers/container_group_resource.go b/internal/services/containers/container_group_resource.go index 47f93e34f5a6..f078f2f902aa 100644 --- a/internal/services/containers/container_group_resource.go +++ b/internal/services/containers/container_group_resource.go @@ -875,6 +875,11 @@ func resourceContainerGroupUpdate(d *pluginsdk.ResourceData, meta interface{}) e } model.Properties.Volumes = pointer.To(containerGroupVolumes) + // As API doesn't return the value of WorkspaceKey, so it has to get the value from tf config and set it to request payload. Otherwise, the Update API call would fail + if diagnostics := expandContainerGroupDiagnostics(d.Get("diagnostics").([]interface{})); diagnostics != nil && diagnostics.LogAnalytics != nil { + model.Properties.Diagnostics.LogAnalytics.WorkspaceKey = diagnostics.LogAnalytics.WorkspaceKey + } + // As Update API doesn't support to update identity, so it has to use CreateOrUpdate API to update identity if err := client.ContainerGroupsCreateOrUpdateThenPoll(ctx, *id, model); err != nil { return fmt.Errorf("updating %s: %+v", *id, err) diff --git a/internal/services/containers/container_group_resource_test.go b/internal/services/containers/container_group_resource_test.go index 4861db6ecfcb..4794439a1c7b 100644 --- a/internal/services/containers/container_group_resource_test.go +++ b/internal/services/containers/container_group_resource_test.go @@ -266,6 +266,26 @@ func TestAccContainerGroup_logTypeUnset(t *testing.T) { }) } +func TestAccContainerGroup_AssignedIdentityUpdateWithLogWorkspace(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_container_group", "test") + r := ContainerGroupResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.SystemAssignedIdentityWithLogWorkspace(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + { + Config: r.UserAssignedIdentityWithLogWorkspace(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + }) +} + func TestAccContainerGroup_linuxBasic(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_container_group", "test") r := ContainerGroupResource{} @@ -326,20 +346,13 @@ func TestAccContainerGroup_requiresImport(t *testing.T) { }) } -func TestAccContainerGroup_linuxBasicUpdate(t *testing.T) { +func TestAccContainerGroup_linuxBasicMultipleContainers(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_container_group", "test") r := ContainerGroupResource{} data.ResourceTest(t, r, []acceptance.TestStep{ { - Config: r.linuxBasic(data), - Check: acceptance.ComposeTestCheckFunc( - check.That(data.ResourceName).ExistsInAzure(r), - check.That(data.ResourceName).Key("container.#").HasValue("1"), - ), - }, - { - Config: r.linuxBasicUpdated(data), + Config: r.linuxBasicMultipleContainers(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), check.That(data.ResourceName).Key("container.#").HasValue("2"), @@ -813,7 +826,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -853,7 +866,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -904,7 +917,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" liveness_probe { @@ -947,7 +960,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" } @@ -983,7 +996,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" } @@ -1028,7 +1041,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1098,7 +1111,7 @@ resource "azurerm_container_group" "test" { os_type = "Linux" container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1148,7 +1161,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1191,7 +1204,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1232,7 +1245,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1272,7 +1285,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1302,7 +1315,7 @@ resource "azurerm_container_group" "import" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1338,7 +1351,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1361,7 +1374,7 @@ resource "azurerm_container_group" "test" { container { name = "sidecar" - image = "mcr.microsoft.com/azuredocs/aci-tutorial-sidecar" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" } @@ -1393,7 +1406,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" @@ -1410,7 +1423,7 @@ resource "azurerm_container_group" "test" { container { name = "sidecar" - image = "mcr.microsoft.com/azuredocs/aci-tutorial-sidecar" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" } @@ -1449,7 +1462,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1471,7 +1484,7 @@ resource "azurerm_container_group" "test" { `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) } -func (ContainerGroupResource) linuxBasicUpdated(data acceptance.TestData) string { +func (ContainerGroupResource) linuxBasicMultipleContainers(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { features {} @@ -1491,7 +1504,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" @@ -1507,7 +1520,7 @@ resource "azurerm_container_group" "test" { container { name = "sidecar" - image = "mcr.microsoft.com/azuredocs/aci-tutorial-sidecar" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" } @@ -1562,7 +1575,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1630,7 +1643,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1684,7 +1697,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -1932,7 +1945,7 @@ resource "azurerm_container_group" "test" { container { name = "hf" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "1" memory = "1.5" @@ -2183,7 +2196,7 @@ resource "azurerm_container_group" "test" { init_container { name = "init" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" commands = ["touch", "/sharedempty/file.txt"] volume { @@ -2196,7 +2209,7 @@ resource "azurerm_container_group" "test" { container { name = "reader" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "1" memory = "1.5" @@ -2234,7 +2247,7 @@ resource "azurerm_container_group" "test" { container { name = "writer" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "1" memory = "1.5" commands = ["touch", "/sharedempty/file.txt"] @@ -2249,7 +2262,7 @@ resource "azurerm_container_group" "test" { container { name = "reader" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "1" memory = "1.5" @@ -2287,7 +2300,7 @@ resource "azurerm_container_group" "test" { init_container { name = "init" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" commands = ["echo", "hello from init"] secure_environment_variables = { PASSWORD = "something_very_secure_for_init" @@ -2296,7 +2309,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "1" memory = "1.5" @@ -2329,7 +2342,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -2394,7 +2407,7 @@ resource "azurerm_container_group" "test" { container { name = "hello-world" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "1.5" @@ -2503,7 +2516,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -2604,7 +2617,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -2644,7 +2657,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -2679,7 +2692,7 @@ resource "azurerm_container_group" "test" { container { name = "hw" - image = "mcr.microsoft.com/quantum/linux-selfcontained:latest" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" cpu = "0.5" memory = "0.5" ports { @@ -2797,3 +2810,119 @@ resource "azurerm_container_group" "test" { } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger) } + +func (ContainerGroupResource) SystemAssignedIdentityWithLogWorkspace(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_log_analytics_workspace" "test" { + name = "acctestLAW-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + sku = "PerGB2018" +} + +resource "azurerm_container_group" "test" { + name = "acctestcontainergroup-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + ip_address_type = "Public" + os_type = "Linux" + + container { + name = "hw" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" + cpu = "0.5" + memory = "0.5" + ports { + port = 80 + protocol = "TCP" + } + } + + diagnostics { + log_analytics { + workspace_id = azurerm_log_analytics_workspace.test.workspace_id + workspace_key = azurerm_log_analytics_workspace.test.primary_shared_key + } + } + + identity { + type = "SystemAssigned" + } + + tags = { + environment = "Testing" + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) +} + +func (ContainerGroupResource) UserAssignedIdentityWithLogWorkspace(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_log_analytics_workspace" "test" { + name = "acctestLAW-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + sku = "PerGB2018" +} + +resource "azurerm_user_assigned_identity" "test" { + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + + name = "acctest%s" +} + +resource "azurerm_container_group" "test" { + name = "acctestcontainergroup-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + ip_address_type = "Public" + os_type = "Linux" + + container { + name = "hw" + image = "mcr.microsoft.com/azuredocs/aci-helloworld:latest" + cpu = "0.5" + memory = "0.5" + ports { + port = 80 + protocol = "TCP" + } + } + + diagnostics { + log_analytics { + workspace_id = azurerm_log_analytics_workspace.test.workspace_id + workspace_key = azurerm_log_analytics_workspace.test.primary_shared_key + } + } + + identity { + type = "UserAssigned" + identity_ids = [azurerm_user_assigned_identity.test.id] + } + + tags = { + environment = "Testing" + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomString, data.RandomInteger) +}