Skip to content

Commit

Permalink
Merge pull request #3192 from stuartleeks/batch-resource-files
Browse files Browse the repository at this point in the history
Add resource_file to batch pool start_task
  • Loading branch information
tombuildsstuff authored Apr 16, 2019
2 parents 5dae1c2 + 190a8c5 commit adb0f1e
Show file tree
Hide file tree
Showing 23 changed files with 1,336 additions and 234 deletions.
2 changes: 1 addition & 1 deletion azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/apimanagement"
appinsights "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation"
"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2017-09-01/batch"
"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2018-12-01/batch"
"github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2017-10-12/cdn"
"github.com/Azure/azure-sdk-for-go/services/cognitiveservices/mgmt/2017-04-18/cognitiveservices"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"
Expand Down
2 changes: 1 addition & 1 deletion azurerm/data_source_batch_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package azurerm
import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2017-09-01/batch"
"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2018-12-01/batch"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down
33 changes: 33 additions & 0 deletions azurerm/data_source_batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,39 @@ func dataSourceArmBatchPool() *schema.Resource {
},
},
},

"resource_file": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auto_storage_container_name": {
Type: schema.TypeString,
Computed: true,
},
"blob_prefix": {
Type: schema.TypeString,
Computed: true,
},
"file_mode": {
Type: schema.TypeString,
Computed: true,
},
"file_path": {
Type: schema.TypeString,
Computed: true,
},
"http_url": {
Type: schema.TypeString,
Computed: true,
},
"storage_container_url": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
Expand Down
74 changes: 73 additions & 1 deletion azurerm/helpers/azure/batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"regexp"

"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2017-09-01/batch"
"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2018-12-01/batch"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -124,6 +124,32 @@ func FlattenBatchPoolStartTask(startTask *batch.StartTask) []interface{} {
result["user_identity"] = []interface{}{userIdentity}
}

resourceFiles := make([]interface{}, 0)
if startTask.ResourceFiles != nil {
for _, armResourceFile := range *startTask.ResourceFiles {
resourceFile := make(map[string]interface{})
if armResourceFile.AutoStorageContainerName != nil {
resourceFile["auto_storage_container_name"] = *armResourceFile.AutoStorageContainerName
}
if armResourceFile.StorageContainerURL != nil {
resourceFile["storage_container_url"] = *armResourceFile.StorageContainerURL
}
if armResourceFile.HTTPURL != nil {
resourceFile["http_url"] = *armResourceFile.HTTPURL
}
if armResourceFile.BlobPrefix != nil {
resourceFile["blob_prefix"] = *armResourceFile.BlobPrefix
}
if armResourceFile.FilePath != nil {
resourceFile["file_path"] = *armResourceFile.FilePath
}
if armResourceFile.FileMode != nil {
resourceFile["file_mode"] = *armResourceFile.FileMode
}
resourceFiles = append(resourceFiles, resourceFile)
}
}

if startTask.EnvironmentSettings != nil {
environment := make(map[string]interface{})
for _, envSetting := range *startTask.EnvironmentSettings {
Expand All @@ -132,6 +158,7 @@ func FlattenBatchPoolStartTask(startTask *batch.StartTask) []interface{} {

result["environment"] = environment
}
result["resource_file"] = resourceFiles

return append(results, result)
}
Expand Down Expand Up @@ -264,11 +291,56 @@ func ExpandBatchPoolStartTask(list []interface{}) (*batch.StartTask, error) {
return nil, fmt.Errorf("Error: either auto_user or user_name should be speicfied for Batch pool start task")
}

resourceFileList := startTaskValue["resource_file"].([]interface{})
resourceFiles := make([]batch.ResourceFile, 0)
for _, resourceFileValueTemp := range resourceFileList {
resourceFileValue := resourceFileValueTemp.(map[string]interface{})
resourceFile := batch.ResourceFile{}
if v, ok := resourceFileValue["auto_storage_container_name"]; ok {
autoStorageContainerName := v.(string)
if autoStorageContainerName != "" {
resourceFile.AutoStorageContainerName = &autoStorageContainerName
}
}
if v, ok := resourceFileValue["storage_container_url"]; ok {
storageContainerURL := v.(string)
if storageContainerURL != "" {
resourceFile.StorageContainerURL = &storageContainerURL
}
}
if v, ok := resourceFileValue["http_url"]; ok {
httpURL := v.(string)
if httpURL != "" {
resourceFile.HTTPURL = &httpURL
}
}
if v, ok := resourceFileValue["blob_prefix"]; ok {
blobPrefix := v.(string)
if blobPrefix != "" {
resourceFile.BlobPrefix = &blobPrefix
}
}
if v, ok := resourceFileValue["file_path"]; ok {
filePath := v.(string)
if filePath != "" {
resourceFile.FilePath = &filePath
}
}
if v, ok := resourceFileValue["file_mode"]; ok {
fileMode := v.(string)
if fileMode != "" {
resourceFile.FileMode = &fileMode
}
}
resourceFiles = append(resourceFiles, resourceFile)
}

startTask := &batch.StartTask{
CommandLine: &startTaskCmdLine,
MaxTaskRetryCount: &maxTaskRetryCount,
WaitForSuccess: &waitForSuccess,
UserIdentity: &userIdentity,
ResourceFiles: &resourceFiles,
}

// populate environment settings, if defined
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_batch_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"regexp"

"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2017-09-01/batch"
"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2018-12-01/batch"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_batch_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2017-09-01/batch"
"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2018-12-01/batch"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
Expand Down
83 changes: 82 additions & 1 deletion azurerm/resource_arm_batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"

"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2017-09-01/batch"
"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2018-12-01/batch"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down Expand Up @@ -275,6 +275,39 @@ func resourceArmBatchPool() *schema.Resource {
},
},
},

"resource_file": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auto_storage_container_name": {
Type: schema.TypeString,
Optional: true,
},
"blob_prefix": {
Type: schema.TypeString,
Optional: true,
},
"file_mode": {
Type: schema.TypeString,
Optional: true,
},
"file_path": {
Type: schema.TypeString,
Optional: true,
},
"http_url": {
Type: schema.TypeString,
Optional: true,
},
"storage_container_url": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -362,6 +395,10 @@ func resourceArmBatchPoolCreate(d *schema.ResourceData, meta interface{}) error
}
parameters.PoolProperties.Certificates = certificateReferences

if err := validateBatchPoolCrossFieldRules(&parameters); err != nil {
return err
}

future, err := client.Create(ctx, resourceGroup, accountName, poolName, parameters, "", "")
if err != nil {
return fmt.Errorf("Error creating Batch pool %q (Resource Group %q): %+v", poolName, resourceGroup, err)
Expand Down Expand Up @@ -461,6 +498,10 @@ func resourceArmBatchPoolUpdate(d *schema.ResourceData, meta interface{}) error
}
parameters.PoolProperties.Certificates = certificateReferences

if err := validateBatchPoolCrossFieldRules(&parameters); err != nil {
return err
}

result, err := client.Update(ctx, resourceGroup, accountName, poolName, parameters, "")
if err != nil {
return fmt.Errorf("Error updating Batch pool %q (Resource Group %q): %+v", poolName, resourceGroup, err)
Expand Down Expand Up @@ -643,3 +684,43 @@ func validateUserIdentity(userIdentity *batch.UserIdentity) error {

return nil
}

func validateBatchPoolCrossFieldRules(pool *batch.Pool) error {
// Perform validation across multiple fields as per https://docs.microsoft.com/en-us/rest/api/batchmanagement/pool/create#resourcefile

if pool.StartTask != nil {
startTask := *pool.StartTask
if startTask.ResourceFiles != nil {
for _, referenceFile := range *startTask.ResourceFiles {
// Must specify exactly one of AutoStorageContainerName, StorageContainerUrl or HttpUrl
sourceCount := 0
if referenceFile.AutoStorageContainerName != nil {
sourceCount++
}
if referenceFile.StorageContainerURL != nil {
sourceCount++
}
if referenceFile.HTTPURL != nil {
sourceCount++
}
if sourceCount != 1 {
return fmt.Errorf("Exactly one of auto_storage_container_name, storage_container_url and http_url must be specified")
}

if referenceFile.BlobPrefix != nil {
if referenceFile.AutoStorageContainerName == nil && referenceFile.StorageContainerURL == nil {
return fmt.Errorf("auto_storage_container_name or storage_container_url must be specified when using blob_prefix")
}
}

if referenceFile.HTTPURL != nil {
if referenceFile.FilePath == nil {
return fmt.Errorf("file_path must be specified when using http_url")
}
}
}
}
}

return nil
}
Loading

0 comments on commit adb0f1e

Please sign in to comment.