Skip to content

Commit

Permalink
data.azurerm_storage_account - add supports of identity (#17215)
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo authored Jun 23, 2022
1 parent bdd602d commit 7b06052
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/services/storage/storage_account_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func dataSourceStorageAccount() *pluginsdk.Resource {

"location": commonschema.LocationComputed(),

"identity": commonschema.SystemAssignedUserAssignedIdentityComputed(),

"account_kind": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -419,5 +421,13 @@ func dataSourceStorageAccountRead(d *pluginsdk.ResourceData, meta interface{}) e
d.Set("secondary_access_key", storageAccountKeys[1].Value)
}

identity, err := flattenAzureRmStorageAccountIdentity(resp.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
}
if err := d.Set("identity", identity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

return tags.FlattenAndSet(d, resp.Tags)
}
144 changes: 144 additions & 0 deletions internal/services/storage/storage_account_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,53 @@ func TestAccDataSourceStorageAccount_withInfrastructureEncryption(t *testing.T)
})
}

func TestAccDataSourceStorageAccount_systemAssignedIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_storage_account", "test")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: StorageAccountDataSource{}.systemAssignedIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("identity.0.type").HasValue("SystemAssigned"),
check.That(data.ResourceName).Key("identity.0.principal_id").IsUUID(),
check.That(data.ResourceName).Key("identity.0.tenant_id").IsUUID(),
),
},
})
}

func TestAccDataSourceStorageAccount_userAssignedIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_storage_account", "test")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: StorageAccountDataSource{}.userAssignedIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
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.identity_ids.0").IsSet(),
),
},
})
}

func TestAccDataSourceStorageAccount_systemAssignedUserAssignedIdentity(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_storage_account", "test")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: StorageAccountDataSource{}.systemAssignedUserAssignedIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
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.identity_ids.0").IsSet(),
check.That(data.ResourceName).Key("identity.0.principal_id").IsUUID(),
check.That(data.ResourceName).Key("identity.0.tenant_id").IsUUID(),
),
},
})
}

func (d StorageAccountDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down Expand Up @@ -211,3 +258,100 @@ data "azurerm_storage_account" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, t)
}

func (d StorageAccountDataSource) identityTemplate(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-storage-%d"
location = "%s"
}
resource "azurerm_user_assigned_identity" "test" {
name = "acctestUAI-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (d StorageAccountDataSource) systemAssignedIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
identity {
type = "SystemAssigned"
}
}
data "azurerm_storage_account" "test" {
name = azurerm_storage_account.test.name
resource_group_name = azurerm_storage_account.test.resource_group_name
}
`, d.identityTemplate(data), data.RandomString)
}

func (d StorageAccountDataSource) userAssignedIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
identity {
type = "UserAssigned"
identity_ids = [
azurerm_user_assigned_identity.test.id,
]
}
}
data "azurerm_storage_account" "test" {
name = azurerm_storage_account.test.name
resource_group_name = azurerm_storage_account.test.resource_group_name
}
`, d.identityTemplate(data), data.RandomString)
}

func (d StorageAccountDataSource) systemAssignedUserAssignedIdentity(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [
azurerm_user_assigned_identity.test.id,
]
}
}
data "azurerm_storage_account" "test" {
name = azurerm_storage_account.test.name
resource_group_name = azurerm_storage_account.test.resource_group_name
}
`, d.identityTemplate(data), data.RandomString)
}
14 changes: 14 additions & 0 deletions website/docs/d/storage_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ output "storage_account_tier" {

* `location` - The Azure location where the Storage Account exists

* `identity` - An `identity` block as documented below.

* `account_kind` - The Kind of account.

* `account_tier` - The Tier of this storage account.
Expand Down Expand Up @@ -136,6 +138,18 @@ output "storage_account_tier" {

* `name` - The Custom Domain Name used for the Storage Account.

---

`identity` supports the following:

* `type` - The type of Managed Service Identity that is configured on this Storage Account

* `identity_ids` - A list of User Assigned Managed Identity IDs assigned with the Identity of this Storage Account.

* `principal_id` - The Principal ID for the Service Principal associated with the Identity of this Storage Account.

* `tenant_id` - The Tenant ID for the Service Principal associated with the Identity of this Storage Account.

## Timeouts

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

0 comments on commit 7b06052

Please sign in to comment.