Skip to content

Commit

Permalink
azurerm_container_group - Support updating managed identity (#25543)
Browse files Browse the repository at this point in the history
* implement update method

* update identity

* add acceptance test for managed identity update

* fix error handling as per comment
  • Loading branch information
ning-kang authored Apr 15, 2024
1 parent 7d09399 commit e54098d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
24 changes: 20 additions & 4 deletions internal/services/containers/container_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,13 +840,29 @@ func resourceContainerGroupUpdate(d *pluginsdk.ResourceData, meta interface{}) e
return err
}

t := d.Get("tags").(map[string]interface{})
existing, err := client.ContainerGroupsGet(ctx, *id)
if err != nil {
return fmt.Errorf("reading %s: %v", id, err)
}
if existing.Model == nil {
return fmt.Errorf("reading %s: `model` was nil", id)
}

model := *existing.Model

if d.HasChange("identity") {
expandedIdentity, err := identity.ExpandSystemAndUserAssignedMap(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}
model.Identity = expandedIdentity
}

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

if _, err := client.ContainerGroupsUpdate(ctx, *id, parameters); err != nil {
if err := client.ContainerGroupsCreateOrUpdateThenPoll(ctx, *id, model); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

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

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

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.SystemAssignedIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.0.type").HasValue("SystemAssigned"),
check.That(data.ResourceName).Key("identity.0.identity_ids.#").HasValue("0"),
check.That(data.ResourceName).Key("identity.0.principal_id").IsUUID(),
),
},
{
Config: r.UserAssignedIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.0.type").HasValue("UserAssigned"),
check.That(data.ResourceName).Key("identity.0.identity_ids.#").HasValue("1"),
check.That(data.ResourceName).Key("identity.0.principal_id").HasValue(""),
),
},
{
Config: r.MultipleAssignedIdentities(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("identity.0.type").HasValue("SystemAssigned, UserAssigned"),
check.That(data.ResourceName).Key("identity.0.identity_ids.#").HasValue("1"),
check.That(data.ResourceName).Key("identity.0.principal_id").IsUUID(),
),
},
})
}

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

0 comments on commit e54098d

Please sign in to comment.