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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

data.azurerm_storage_account - add supports of identity #17215

Merged
merged 1 commit into from Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/services/storage/storage_account_data_source.go
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
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
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