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_container_group - set storage_account_key in the update operation #26640

Merged
merged 9 commits into from
Aug 8, 2024
Merged
31 changes: 29 additions & 2 deletions internal/services/containers/container_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,14 +885,41 @@ func resourceContainerGroupUpdate(d *pluginsdk.ResourceData, meta interface{}) e
model.Identity = expandedIdentity
}

if d.HasChange("tags") {
model.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
// As API doesn't return the value of StorageAccountKey, so it has to get the value from tf config and set it to request payload. Otherwise, the Update API call would fail
addedEmptyDirs := map[string]bool{}
_, initContainerVolumes, err := expandContainerGroupInitContainers(d, addedEmptyDirs)
if err != nil {
return err
}
_, _, containerVolumes, err := expandContainerGroupContainers(d, addedEmptyDirs)
if err != nil {
return err
}
var containerGroupVolumes []containerinstance.Volume
if initContainerVolumes != nil {
containerGroupVolumes = initContainerVolumes
}
if containerGroupVolumes != nil {
containerGroupVolumes = append(containerGroupVolumes, containerVolumes...)
}
model.Properties.Volumes = pointer.To(containerGroupVolumes)

// 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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only the tags are updated then we would be making an unnecessary update call. I think this whole block should be moved into

if d.HasChange("identity") {
...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated


if d.HasChange("tags") {
updateParameters := containerinstance.Resource{
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

// As CreateOrUpdate API doesn't support to update tags, so it has to use Update API to update tags
if _, err := client.ContainerGroupsUpdate(ctx, *id, updateParameters); err != nil {
return fmt.Errorf("updating tags %s: %+v", *id, err)
}
}

return resourceContainerGroupRead(d, meta)
}

Expand Down
144 changes: 144 additions & 0 deletions internal/services/containers/container_group_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,28 @@ func TestAccContainerGroup_priority(t *testing.T) {
})
}

func TestAccContainerGroup_updateWithStorageAccount(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_container_group", "test")
r := ContainerGroupResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.storageAccount(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("container.0.volume.0.storage_account_key"),
{
Config: r.updateWithStorageAccount(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("container.0.volume.0.storage_account_key"),
})
}

func (ContainerGroupResource) SystemAssignedIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down Expand Up @@ -2756,3 +2778,125 @@ resource "azurerm_container_group" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, priority)
}

func (ContainerGroupResource) storageAccount(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-cg-%d"
location = "%s"
}

resource "azurerm_storage_account" "test" {
name = "accsa%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_share" "test" {
name = "acctestss-%d"
storage_account_name = azurerm_storage_account.test.name
quota = 1
}

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 = "1"
memory = "1"

ports {
port = 443
protocol = "TCP"
}

volume {
name = "testvolume"
mount_path = "/test"
share_name = azurerm_storage_share.test.name
storage_account_name = azurerm_storage_account.test.name
storage_account_key = azurerm_storage_account.test.primary_access_key
}
}

tags = {
environment = "Test1"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (ContainerGroupResource) updateWithStorageAccount(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-cg-%d"
location = "%s"
}

resource "azurerm_storage_account" "test" {
name = "accsa%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_storage_share" "test" {
name = "acctestss-%d"
storage_account_name = azurerm_storage_account.test.name
quota = 1
}

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 = "1"
memory = "1"

ports {
port = 443
protocol = "TCP"
}

volume {
name = "testvolume"
mount_path = "/test"
share_name = azurerm_storage_share.test.name
storage_account_name = azurerm_storage_account.test.name
storage_account_key = azurerm_storage_account.test.primary_access_key
}
}

identity {
type = "SystemAssigned"
}

tags = {
environment = "Test2"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}
Loading