Skip to content

Commit 3ab35d1

Browse files
authored
azurerm_batch_pool: Support for identity (#13779)
Fixes #13758
1 parent d1e9b1d commit 3ab35d1

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed

internal/services/batch/batch_pool_resource.go

+70
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ import (
1313
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
1414
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1515
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
16+
"github.com/hashicorp/terraform-provider-azurerm/internal/identity"
1617
"github.com/hashicorp/terraform-provider-azurerm/internal/services/batch/parse"
1718
"github.com/hashicorp/terraform-provider-azurerm/internal/services/batch/validate"
19+
msiparse "github.com/hashicorp/terraform-provider-azurerm/internal/services/msi/parse"
1820
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
1921
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress"
2022
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
2123
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
2224
"github.com/hashicorp/terraform-provider-azurerm/utils"
2325
)
2426

27+
type batchPoolIdentity = identity.UserAssigned
28+
2529
func resourceBatchPool() *pluginsdk.Resource {
2630
return &pluginsdk.Resource{
2731
Create: resourceBatchPoolCreate,
@@ -282,6 +286,9 @@ func resourceBatchPool() *pluginsdk.Resource {
282286
},
283287
},
284288
},
289+
290+
"identity": batchPoolIdentity{}.Schema(),
291+
285292
"start_task": {
286293
Type: pluginsdk.TypeList,
287294
Optional: true,
@@ -545,6 +552,12 @@ func resourceBatchPoolCreate(d *pluginsdk.ResourceData, meta interface{}) error
545552
},
546553
}
547554

555+
identity, err := expandBatchPoolIdentity(d.Get("identity").([]interface{}))
556+
if err != nil {
557+
return fmt.Errorf(`expanding "identity": %v`, err)
558+
}
559+
parameters.Identity = identity
560+
548561
scaleSettings, err := expandBatchPoolScaleSettings(d)
549562
if err != nil {
550563
return fmt.Errorf("expanding scale settings: %+v", err)
@@ -684,6 +697,12 @@ func resourceBatchPoolUpdate(d *pluginsdk.ResourceData, meta interface{}) error
684697
PoolProperties: &batch.PoolProperties{},
685698
}
686699

700+
identity, err := expandBatchPoolIdentity(d.Get("identity").([]interface{}))
701+
if err != nil {
702+
return fmt.Errorf(`expanding "identity": %v`, err)
703+
}
704+
parameters.Identity = identity
705+
687706
scaleSettings, err := expandBatchPoolScaleSettings(d)
688707
if err != nil {
689708
return fmt.Errorf("expanding scale settings: %+v", err)
@@ -762,6 +781,14 @@ func resourceBatchPoolRead(d *pluginsdk.ResourceData, meta interface{}) error {
762781
d.Set("account_name", id.BatchAccountName)
763782
d.Set("resource_group_name", id.ResourceGroup)
764783

784+
identity, err := flattenBatchPoolIdentity(resp.Identity)
785+
if err != nil {
786+
return err
787+
}
788+
if err := d.Set("identity", identity); err != nil {
789+
return fmt.Errorf("setting `identity`: %+v", err)
790+
}
791+
765792
if props := resp.PoolProperties; props != nil {
766793
d.Set("display_name", props.DisplayName)
767794
d.Set("vm_size", props.VMSize)
@@ -956,3 +983,46 @@ func validateBatchPoolCrossFieldRules(pool *batch.Pool) error {
956983

957984
return nil
958985
}
986+
987+
func expandBatchPoolIdentity(input []interface{}) (*batch.PoolIdentity, error) {
988+
config, err := batchPoolIdentity{}.Expand(input)
989+
if err != nil {
990+
return nil, err
991+
}
992+
993+
var identityMaps map[string]*batch.UserAssignedIdentities
994+
if len(config.UserAssignedIdentityIds) != 0 {
995+
identityMaps = make(map[string]*batch.UserAssignedIdentities, len(config.UserAssignedIdentityIds))
996+
for _, id := range config.UserAssignedIdentityIds {
997+
identityMaps[id] = &batch.UserAssignedIdentities{}
998+
}
999+
}
1000+
1001+
return &batch.PoolIdentity{
1002+
Type: batch.PoolIdentityType(config.Type),
1003+
UserAssignedIdentities: identityMaps,
1004+
}, nil
1005+
}
1006+
1007+
func flattenBatchPoolIdentity(input *batch.PoolIdentity) ([]interface{}, error) {
1008+
var config *identity.ExpandedConfig
1009+
1010+
if input == nil {
1011+
return []interface{}{}, nil
1012+
}
1013+
1014+
var identityIds []string
1015+
for id := range input.UserAssignedIdentities {
1016+
parsedId, err := msiparse.UserAssignedIdentityIDInsensitively(id)
1017+
if err != nil {
1018+
return nil, err
1019+
}
1020+
identityIds = append(identityIds, parsedId.ID())
1021+
}
1022+
1023+
config = &identity.ExpandedConfig{
1024+
Type: identity.Type(string(input.Type)),
1025+
UserAssignedIdentityIds: identityIds,
1026+
}
1027+
return batchPoolIdentity{}.Flatten(config), nil
1028+
}

internal/services/batch/batch_pool_resource_test.go

+110
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,68 @@ func TestAccBatchPool_basic(t *testing.T) {
4242
})
4343
}
4444

45+
func TestAccBatchPool_identity(t *testing.T) {
46+
data := acceptance.BuildTestData(t, "azurerm_batch_pool", "test")
47+
r := BatchPoolResource{}
48+
49+
data.ResourceTest(t, r, []acceptance.TestStep{
50+
{
51+
Config: r.identity(data),
52+
Check: acceptance.ComposeTestCheckFunc(
53+
check.That(data.ResourceName).ExistsInAzure(r),
54+
),
55+
},
56+
data.ImportStep("stop_pending_resize_operation"),
57+
})
58+
}
59+
60+
func TestAccBatchPool_identityUpdate(t *testing.T) {
61+
data := acceptance.BuildTestData(t, "azurerm_batch_pool", "test")
62+
r := BatchPoolResource{}
63+
64+
data.ResourceTest(t, r, []acceptance.TestStep{
65+
{
66+
Config: r.basic(data),
67+
Check: acceptance.ComposeTestCheckFunc(
68+
check.That(data.ResourceName).ExistsInAzure(r),
69+
check.That(data.ResourceName).Key("vm_size").HasValue("STANDARD_A1"),
70+
check.That(data.ResourceName).Key("node_agent_sku_id").HasValue("batch.node.ubuntu 16.04"),
71+
check.That(data.ResourceName).Key("storage_image_reference.#").HasValue("1"),
72+
check.That(data.ResourceName).Key("storage_image_reference.0.publisher").HasValue("Canonical"),
73+
check.That(data.ResourceName).Key("storage_image_reference.0.sku").HasValue("16.04.0-LTS"),
74+
check.That(data.ResourceName).Key("storage_image_reference.0.offer").HasValue("UbuntuServer"),
75+
check.That(data.ResourceName).Key("fixed_scale.#").HasValue("1"),
76+
check.That(data.ResourceName).Key("fixed_scale.0.target_dedicated_nodes").HasValue("1"),
77+
check.That(data.ResourceName).Key("start_task.#").HasValue("0"),
78+
),
79+
},
80+
data.ImportStep("stop_pending_resize_operation"),
81+
{
82+
Config: r.identity(data),
83+
Check: acceptance.ComposeTestCheckFunc(
84+
check.That(data.ResourceName).ExistsInAzure(r),
85+
),
86+
},
87+
data.ImportStep("stop_pending_resize_operation"),
88+
{
89+
Config: r.basic(data),
90+
Check: acceptance.ComposeTestCheckFunc(
91+
check.That(data.ResourceName).ExistsInAzure(r),
92+
check.That(data.ResourceName).Key("vm_size").HasValue("STANDARD_A1"),
93+
check.That(data.ResourceName).Key("node_agent_sku_id").HasValue("batch.node.ubuntu 16.04"),
94+
check.That(data.ResourceName).Key("storage_image_reference.#").HasValue("1"),
95+
check.That(data.ResourceName).Key("storage_image_reference.0.publisher").HasValue("Canonical"),
96+
check.That(data.ResourceName).Key("storage_image_reference.0.sku").HasValue("16.04.0-LTS"),
97+
check.That(data.ResourceName).Key("storage_image_reference.0.offer").HasValue("UbuntuServer"),
98+
check.That(data.ResourceName).Key("fixed_scale.#").HasValue("1"),
99+
check.That(data.ResourceName).Key("fixed_scale.0.target_dedicated_nodes").HasValue("1"),
100+
check.That(data.ResourceName).Key("start_task.#").HasValue("0"),
101+
),
102+
},
103+
data.ImportStep("stop_pending_resize_operation"),
104+
})
105+
}
106+
45107
func TestAccBatchPool_requiresImport(t *testing.T) {
46108
data := acceptance.BuildTestData(t, "azurerm_batch_pool", "test")
47109
r := BatchPoolResource{}
@@ -608,6 +670,54 @@ resource "azurerm_batch_pool" "test" {
608670
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString)
609671
}
610672

673+
func (BatchPoolResource) identity(data acceptance.TestData) string {
674+
return fmt.Sprintf(`
675+
provider "azurerm" {
676+
features {}
677+
}
678+
679+
resource "azurerm_resource_group" "test" {
680+
name = "testaccRG-batch-%d"
681+
location = "%s"
682+
}
683+
684+
resource "azurerm_user_assigned_identity" "test" {
685+
name = "acctest%s"
686+
resource_group_name = azurerm_resource_group.test.name
687+
location = azurerm_resource_group.test.location
688+
}
689+
690+
resource "azurerm_batch_account" "test" {
691+
name = "testaccbatch%s"
692+
resource_group_name = azurerm_resource_group.test.name
693+
location = azurerm_resource_group.test.location
694+
}
695+
696+
resource "azurerm_batch_pool" "test" {
697+
name = "testaccpool%s"
698+
resource_group_name = azurerm_resource_group.test.name
699+
account_name = azurerm_batch_account.test.name
700+
node_agent_sku_id = "batch.node.ubuntu 16.04"
701+
vm_size = "Standard_A1"
702+
identity {
703+
type = "UserAssigned"
704+
identity_ids = [azurerm_user_assigned_identity.test.id]
705+
}
706+
707+
fixed_scale {
708+
target_dedicated_nodes = 1
709+
}
710+
711+
storage_image_reference {
712+
publisher = "Canonical"
713+
offer = "UbuntuServer"
714+
sku = "16.04.0-LTS"
715+
version = "latest"
716+
}
717+
}
718+
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomString, data.RandomString)
719+
}
720+
611721
func (BatchPoolResource) requiresImport(data acceptance.TestData) string {
612722
return fmt.Sprintf(`
613723
%s

website/docs/r/batch_pool.html.markdown

+11
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ The following arguments are supported:
129129

130130
* `display_name` - (Optional) Specifies the display name of the Batch pool.
131131

132+
* `identity` - (Optional) An `identity` block as defined below.
133+
132134
* `max_tasks_per_node` - (Optional) Specifies the maximum number of tasks that can run concurrently on a single compute node in the pool. Defaults to `1`. Changing this forces a new resource to be created.
133135

134136
* `fixed_scale` - (Optional) A `fixed_scale` block that describes the scale settings when using fixed scale.
@@ -150,6 +152,15 @@ The following arguments are supported:
150152
~> **Please Note:** `fixed_scale` and `auto_scale` blocks cannot be used both at the same time.
151153

152154
---
155+
156+
An `identity` block supports the following:
157+
158+
* `type` - (Required) The identity type of the Batch Account. Only possible values is `UserAssigned`.
159+
160+
* `identity_ids` - (Required) Specifies a list of user assigned identity ids.
161+
162+
---
163+
153164
A `storage_image_reference` block supports the following:
154165

155166
This block provisions virtual machines in the Batch Pool from one of two sources: an Azure Platform Image (e.g. Ubuntu/Windows Server) or a Custom Image.

0 commit comments

Comments
 (0)