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_pool - support node_deallocation_method dynamic_vnet_assignment_scope source_port_ranges #18436

Merged
merged 12 commits into from
Oct 3, 2022
Merged
78 changes: 76 additions & 2 deletions internal/services/batch/batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func flattenBatchPoolAutoScaleSettings(settings *batch.AutoScaleSettings) []inte
}

// flattenBatchPoolFixedScaleSettings flattens the fixed scale settings for a Batch pool
func flattenBatchPoolFixedScaleSettings(settings *batch.FixedScaleSettings) []interface{} {
func flattenBatchPoolFixedScaleSettings(d *pluginsdk.ResourceData, settings *batch.FixedScaleSettings) []interface{} {
results := make([]interface{}, 0)

if settings == nil {
Expand All @@ -45,6 +45,11 @@ func flattenBatchPoolFixedScaleSettings(settings *batch.FixedScaleSettings) []in

result := make(map[string]interface{})

// for now, this is a writeOnly property, so we treat this as secret.
if v, ok := d.GetOk("fixed_scale.0.node_deallocation_method"); ok {
result["node_deallocation_method"] = v.(string)
}

if settings.TargetDedicatedNodes != nil {
result["target_dedicated_nodes"] = *settings.TargetDedicatedNodes
}
Expand Down Expand Up @@ -89,7 +94,7 @@ func flattenBatchPoolImageReference(image *batch.ImageReference) []interface{} {
}

// flattenBatchPoolStartTask flattens a Batch pool start task
func flattenBatchPoolStartTask(startTask *batch.StartTask) []interface{} {
func flattenBatchPoolStartTask(oldConfig *pluginsdk.ResourceData, startTask *batch.StartTask) []interface{} {
results := make([]interface{}, 0)

if startTask == nil {
Expand All @@ -104,6 +109,25 @@ func flattenBatchPoolStartTask(startTask *batch.StartTask) []interface{} {
}
result["command_line"] = commandLine

if startTask.ContainerSettings != nil {
containerSettings := make(map[string]interface{})
containerSettings["image_name"] = *startTask.ContainerSettings.ImageName
containerSettings["working_directory"] = string(startTask.ContainerSettings.WorkingDirectory)
if startTask.ContainerSettings.ContainerRunOptions != nil {
containerSettings["run_options"] = *startTask.ContainerSettings.ContainerRunOptions
}
if startTask.ContainerSettings.Registry != nil {
tmpReg := flattenBatchPoolContainerRegistry(oldConfig, startTask.ContainerSettings.Registry)
containerSettings["registry"] = []interface{}{
tmpReg,
}
}

result["container"] = []interface{}{
containerSettings,
}
}

waitForSuccess := false
if startTask.WaitForSuccess != nil {
waitForSuccess = *startTask.WaitForSuccess
Expand Down Expand Up @@ -676,6 +700,29 @@ func ExpandBatchPoolStartTask(list []interface{}) (*batch.StartTask, error) {
startTask.EnvironmentSettings = expandCommonEnvironmentProperties(v)
}

if startTaskValue["container"] != nil && len(startTaskValue["container"].([]interface{})) > 0 {
var containerSettings batch.TaskContainerSettings
containerSettingsList := startTaskValue["container"].([]interface{})

if len(containerSettingsList) > 0 && containerSettingsList[0] != nil {
settingMap := containerSettingsList[0].(map[string]interface{})
containerSettings.ImageName = utils.String(settingMap["image_name"].(string))
if containerRunOptions, ok := settingMap["run_options"]; ok {
containerSettings.ContainerRunOptions = utils.String(containerRunOptions.(string))
}
if settingMap["registry"].([]interface{})[0] != nil {
containerRegMap := settingMap["registry"].([]interface{})[0].(map[string]interface{})
if containerRegistryRef, err := expandBatchPoolContainerRegistry(containerRegMap); err == nil {
containerSettings.Registry = containerRegistryRef
}
}
if workingDir, ok := settingMap["working_directory"]; ok {
containerSettings.WorkingDirectory = batch.ContainerWorkingDirectory(workingDir.(string))
}
}
startTask.ContainerSettings = &containerSettings
}

return startTask, nil
}

Expand Down Expand Up @@ -1060,6 +1107,10 @@ func ExpandBatchPoolNetworkConfiguration(list []interface{}) (*batch.NetworkConf
networkConfigValue := list[0].(map[string]interface{})
networkConfiguration := &batch.NetworkConfiguration{}

if v, ok := networkConfigValue["dynamic_vnet_assignment_scope"]; ok {
networkConfiguration.DynamicVNetAssignmentScope = batch.DynamicVNetAssignmentScope(v.(string))
}

if v, ok := networkConfigValue["subnet_id"]; ok {
if value := v.(string); value != "" {
networkConfiguration.SubnetID = &value
Expand Down Expand Up @@ -1164,6 +1215,21 @@ func expandPoolNetworkSecurityGroupRule(list []interface{}) []batch.NetworkSecur
sourceAddressPrefix := groupRuleMap["source_address_prefix"].(string)
access := batch.NetworkSecurityGroupRuleAccess(groupRuleMap["access"].(string))

networkSecurityGroupRuleObject := batch.NetworkSecurityGroupRule{
Priority: &priority,
SourceAddressPrefix: &sourceAddressPrefix,
Access: access,
}

portRanges := groupRuleMap["source_port_ranges"].([]interface{})
if len(portRanges) > 0 {
portRangesResult := make([]string, 0)
for _, v := range portRanges {
portRangesResult = append(portRangesResult, v.(string))
}
networkSecurityGroupRuleObject.SourcePortRanges = &portRangesResult
}

networkSecurityGroupRule = append(networkSecurityGroupRule, batch.NetworkSecurityGroupRule{
Priority: &priority,
SourceAddressPrefix: &sourceAddressPrefix,
Expand Down Expand Up @@ -1220,10 +1286,17 @@ func flattenBatchPoolNetworkConfiguration(input *batch.NetworkConfiguration) []i
if networkSecurity.SourceAddressPrefix != nil {
sourceAddressPrefix = *networkSecurity.SourceAddressPrefix
}
sourcePortRanges := make([]interface{}, 0)
if networkSecurity.SourcePortRanges != nil {
for _, sourcePortRange := range *networkSecurity.SourcePortRanges {
sourcePortRanges = append(sourcePortRanges, sourcePortRange)
}
}
networkSecurities = append(networkSecurities, map[string]interface{}{
"access": string(networkSecurity.Access),
"priority": priority,
"source_address_prefix": sourceAddressPrefix,
"source_port_ranges": sourcePortRanges,
})
}
}
Expand All @@ -1240,6 +1313,7 @@ func flattenBatchPoolNetworkConfiguration(input *batch.NetworkConfiguration) []i

return []interface{}{
map[string]interface{}{
"dynamic_vnet_assignment_scope": string(input.DynamicVNetAssignmentScope),
"endpoint_configuration": endpointConfigs,
"public_address_provisioning_type": publicAddressProvisioningType,
"public_ips": pluginsdk.NewSet(pluginsdk.HashString, publicIPAddressIds),
Expand Down
86 changes: 65 additions & 21 deletions internal/services/batch/batch_pool_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/batch/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/batch/validate"
Expand Down Expand Up @@ -139,25 +140,7 @@ func dataSourceBatchPool() *pluginsdk.Resource {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"registry_server": {
Type: pluginsdk.TypeString,
Computed: true,
},
"user_assigned_identity_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"user_name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"password": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},
},
Schema: batchPoolDataContainerRegistry(),
},
},
},
Expand Down Expand Up @@ -484,6 +467,10 @@ func dataSourceBatchPool() *pluginsdk.Resource {
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"dynamic_vnet_assignment_scope": {
Type: pluginsdk.TypeString,
Computed: true,
},
"subnet_id": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -537,6 +524,13 @@ func dataSourceBatchPool() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Computed: true,
},
"source_port_ranges": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
Expand Down Expand Up @@ -581,6 +575,34 @@ func startTaskDSSchema() map[string]*pluginsdk.Schema {
Computed: true,
},

"container": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"run_options": {
Type: pluginsdk.TypeString,
Computed: true,
},
"image_name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"registry": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: batchPoolDataContainerRegistry(),
},
},
"working_directory": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"task_retry_maximum": {
Type: pluginsdk.TypeInt,
Computed: true,
Expand Down Expand Up @@ -669,6 +691,28 @@ func startTaskDSSchema() map[string]*pluginsdk.Schema {
return s
}

func batchPoolDataContainerRegistry() map[string]*schema.Schema {
return map[string]*pluginsdk.Schema{
"registry_server": {
Type: pluginsdk.TypeString,
Computed: true,
},
"user_assigned_identity_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"user_name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"password": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},
}
}

func dataSourceBatchPoolRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Batch.PoolClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
Expand Down Expand Up @@ -700,7 +744,7 @@ func dataSourceBatchPoolRead(d *pluginsdk.ResourceData, meta interface{}) error
if err := d.Set("auto_scale", flattenBatchPoolAutoScaleSettings(scaleSettings.AutoScale)); err != nil {
return fmt.Errorf("flattening `auto_scale`: %+v", err)
}
if err := d.Set("fixed_scale", flattenBatchPoolFixedScaleSettings(scaleSettings.FixedScale)); err != nil {
if err := d.Set("fixed_scale", flattenBatchPoolFixedScaleSettings(d, scaleSettings.FixedScale)); err != nil {
return fmt.Errorf("flattening `fixed_scale `: %+v", err)
}
}
Expand Down Expand Up @@ -816,7 +860,7 @@ func dataSourceBatchPoolRead(d *pluginsdk.ResourceData, meta interface{}) error
return fmt.Errorf("setting `certificate`: %v", err)
}

d.Set("start_task", flattenBatchPoolStartTask(props.StartTask))
d.Set("start_task", flattenBatchPoolStartTask(d, props.StartTask))
d.Set("metadata", FlattenBatchMetaData(props.Metadata))

if err := d.Set("network_configuration", flattenBatchPoolNetworkConfiguration(props.NetworkConfiguration)); err != nil {
Expand Down
Loading