Skip to content

Commit 12be689

Browse files
azurerm_container_registry: allowing the principal_id and tenant_id to be exported (#12378)
This is to help issue #9955 by adding support to expose the prinicpal_id and tenant_id from the container registry identity block with systemAssigned managed identities.
1 parent 137fe42 commit 12be689

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

azurerm/internal/services/containers/container_registry_resource.go

+10
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ func resourceContainerRegistry() *pluginsdk.Resource {
172172
Type: pluginsdk.TypeString,
173173
Computed: true,
174174
},
175+
"tenant_id": {
176+
Type: pluginsdk.TypeString,
177+
Computed: true,
178+
},
175179
"identity_ids": {
176180
Type: pluginsdk.TypeList,
177181
Optional: true,
@@ -1040,6 +1044,12 @@ func flattenIdentityProperties(identityProperties *containerregistry.IdentityPro
10401044
}
10411045
identity["identity_ids"] = identityIds
10421046
}
1047+
if identityProperties.PrincipalID != nil {
1048+
identity["principal_id"] = *identityProperties.PrincipalID
1049+
}
1050+
if identityProperties.TenantID != nil {
1051+
identity["tenant_id"] = *identityProperties.TenantID
1052+
}
10431053
return []interface{}{identity}, nil
10441054
}
10451055

azurerm/internal/services/containers/container_registry_resource_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
10+
validateHelper "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
1011
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
1112
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance/check"
1213
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
@@ -508,6 +509,27 @@ func TestAccContainerRegistry_identity(t *testing.T) {
508509
})
509510
}
510511

512+
func TestAccContainerRegistry_identitySystemAssigned(t *testing.T) {
513+
data := acceptance.BuildTestData(t, "azurerm_container_registry", "test")
514+
r := ContainerRegistryResource{}
515+
skuPremium := "Premium"
516+
userAssigned := "systemAssigned"
517+
data.ResourceTest(t, r, []acceptance.TestStep{
518+
// creates an ACR with encryption
519+
{
520+
Config: r.identitySystemAssigned(data),
521+
Check: acceptance.ComposeTestCheckFunc(
522+
check.That(data.ResourceName).ExistsInAzure(r),
523+
check.That(data.ResourceName).Key("sku").HasValue(skuPremium),
524+
check.That(data.ResourceName).Key("identity.0.type").HasValue(userAssigned),
525+
acceptance.TestMatchResourceAttr(data.ResourceName, "identity.0.principal_id", validateHelper.UUIDRegExp),
526+
acceptance.TestMatchResourceAttr(data.ResourceName, "identity.0.tenant_id", validateHelper.UUIDRegExp),
527+
),
528+
},
529+
data.ImportStep(),
530+
})
531+
}
532+
511533
func TestAccContainerRegistry_zoneRedundancy(t *testing.T) {
512534
data := acceptance.BuildTestData(t, "azurerm_container_registry", "test")
513535
r := ContainerRegistryResource{}
@@ -1066,6 +1088,31 @@ resource "azurerm_user_assigned_identity" "test" {
10661088
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
10671089
}
10681090

1091+
func (ContainerRegistryResource) identitySystemAssigned(data acceptance.TestData) string {
1092+
return fmt.Sprintf(`
1093+
provider "azurerm" {
1094+
features {}
1095+
}
1096+
1097+
resource "azurerm_resource_group" "test" {
1098+
name = "acctestRG-acr-%d"
1099+
location = "%s"
1100+
}
1101+
1102+
resource "azurerm_container_registry" "test" {
1103+
name = "testacccr%d"
1104+
resource_group_name = azurerm_resource_group.test.name
1105+
location = azurerm_resource_group.test.location
1106+
sku = "Premium"
1107+
identity {
1108+
type = "SystemAssigned"
1109+
}
1110+
}
1111+
1112+
1113+
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
1114+
}
1115+
10691116
func (ContainerRegistryResource) zoneRedundancy(data acceptance.TestData) string {
10701117
return fmt.Sprintf(`
10711118
provider "azurerm" {

website/docs/r/container_registry.html.markdown

+14
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ The following attributes are exported:
216216

217217
* `admin_password` - The Password associated with the Container Registry Admin account - if the admin account is enabled.
218218

219+
* `identity` - An `identity` block as defined below, which contains the Managed Service Identity information for this Container Registry.
220+
221+
---
222+
223+
A `identity` block exports the following:
224+
225+
* `principal_id` - The Principal ID for the Service Principal associated with the Managed Service Identity of this Container Registry.
226+
227+
* `tenant_id` - The Tenant ID for the Service Principal associated with the Managed Service Identity of this Container Registry.
228+
229+
-> You can access the Principal ID via `azurerm_container_registry.example.identity.0.principal_id` and the Tenant ID via `azurerm_container_registry.example.identity.0.tenant_id`
230+
231+
---
232+
219233
## Timeouts
220234

221235
The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

0 commit comments

Comments
 (0)