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_batch_account - add supports for identity #13742

Merged
merged 4 commits into from
Oct 21, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 85 additions & 2 deletions internal/services/batch/batch_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/identity"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/batch/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/batch/validate"
msiparse "github.com/hashicorp/terraform-provider-azurerm/internal/services/msi/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type batchAccountIdentity = identity.SystemAssignedUserAssigned

func resourceBatchAccount() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceBatchAccountCreate,
Expand Down Expand Up @@ -96,6 +100,8 @@ func resourceBatchAccount() *pluginsdk.Resource {
},
},

"identity": batchAccountIdentity{}.Schema(),

"primary_access_key": {
Type: pluginsdk.TypeString,
Sensitive: true,
Expand Down Expand Up @@ -144,13 +150,19 @@ func resourceBatchAccountCreate(d *pluginsdk.ResourceData, meta interface{}) err
}
}

identity, err := expandBatchAccountIdentity(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf(`expanding "identity": %v`, err)
}

parameters := batch.AccountCreateParameters{
Location: &location,
AccountCreateProperties: &batch.AccountCreateProperties{
PoolAllocationMode: batch.PoolAllocationMode(poolAllocationMode),
PublicNetworkAccess: batch.PublicNetworkAccessTypeEnabled,
},
Tags: tags.Expand(t),
Identity: identity,
Tags: tags.Expand(t),
}

if enabled := d.Get("public_network_access_enabled").(bool); !enabled {
Expand Down Expand Up @@ -239,6 +251,14 @@ func resourceBatchAccountRead(d *pluginsdk.ResourceData, meta interface{}) error
}

d.Set("pool_allocation_mode", props.PoolAllocationMode)

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

if d.Get("pool_allocation_mode").(string) == string(batch.PoolAllocationModeBatchService) {
Expand Down Expand Up @@ -269,13 +289,19 @@ func resourceBatchAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) err
storageAccountId := d.Get("storage_account_id").(string)
t := d.Get("tags").(map[string]interface{})

identity, err := expandBatchAccountIdentity(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf(`expanding "identity": %v`, err)
}

parameters := batch.AccountUpdateParameters{
AccountUpdateProperties: &batch.AccountUpdateProperties{
AutoStorage: &batch.AutoStorageBaseProperties{
StorageAccountID: &storageAccountId,
},
},
Tags: tags.Expand(t),
Identity: identity,
Tags: tags.Expand(t),
}

if _, err = client.Update(ctx, id.ResourceGroup, id.BatchAccountName, parameters); err != nil {
Expand Down Expand Up @@ -319,3 +345,60 @@ func resourceBatchAccountDelete(d *pluginsdk.ResourceData, meta interface{}) err

return nil
}

func expandBatchAccountIdentity(input []interface{}) (*batch.AccountIdentity, error) {
config, err := batchAccountIdentity{}.Expand(input)
if err != nil {
return nil, err
}

var identityMaps map[string]*batch.UserAssignedIdentities
if len(config.UserAssignedIdentityIds) != 0 {
identityMaps = make(map[string]*batch.UserAssignedIdentities, len(config.UserAssignedIdentityIds))
for _, id := range config.UserAssignedIdentityIds {
identityMaps[id] = &batch.UserAssignedIdentities{}
}
}

return &batch.AccountIdentity{
Type: batch.ResourceIdentityType(config.Type),
TenantID: &config.TenantId,
PrincipalID: &config.PrincipalId,
UserAssignedIdentities: identityMaps,
}, nil
}

func flattenBatchAccountIdentity(input *batch.AccountIdentity) ([]interface{}, error) {
var config *identity.ExpandedConfig

if input == nil {
return []interface{}{}, nil
}

var identityIds []string
for id := range input.UserAssignedIdentities {
parsedId, err := msiparse.UserAssignedIdentityIDInsensitively(id)
if err != nil {
return nil, err
}
identityIds = append(identityIds, parsedId.ID())
}

principalId := ""
if input.PrincipalID != nil {
principalId = *input.PrincipalID
}

tenantId := ""
if input.TenantID != nil {
tenantId = *input.TenantID
}

config = &identity.ExpandedConfig{
Type: identity.Type(string(input.Type)),
PrincipalId: principalId,
TenantId: tenantId,
UserAssignedIdentityIds: identityIds,
}
return batchAccountIdentity{}.Flatten(config), nil
}
84 changes: 84 additions & 0 deletions internal/services/batch/batch_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ func TestAccBatchAccount_userSubscription(t *testing.T) {
})
}

func TestAccBatchAccount_identity(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_batch_account", "test")
r := BatchAccountResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.systemAssignedIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.userAssignedIdentity(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (t BatchAccountResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.AccountID(state.ID)
if err != nil {
Expand Down Expand Up @@ -295,3 +317,65 @@ resource "azurerm_batch_account" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, tenantID, tenantID, data.RandomString)
}

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

resource "azurerm_resource_group" "test" {
name = "testaccRG-batch-%d"
location = "%s"
}

resource "azurerm_batch_account" "test" {
name = "testaccbatch%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
pool_allocation_mode = "BatchService"
identity {
type = "SystemAssigned"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

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

resource "azurerm_resource_group" "test" {
name = "testaccRG-batch-%d"
location = "%s"
}

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

resource "azurerm_user_assigned_identity" "test" {
name = "acctest%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

resource "azurerm_batch_account" "test" {
name = "testaccbatch%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
pool_allocation_mode = "BatchService"
storage_account_id = azurerm_storage_account.test.id
identity {
type = "UserAssigned"
identity_ids = [azurerm_user_assigned_identity.test.id]
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomString)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading