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 for the metadata property (#4611) #5309

Merged
merged 9 commits into from
Jan 8, 2020
31 changes: 31 additions & 0 deletions azurerm/helpers/azure/batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,34 @@ func ValidateAzureRMBatchPoolName(v interface{}, k string) (warnings []string, e

return warnings, errors
}

// ExpandBatchMetaData expands Batch pool metadata
func ExpandBatchMetaData(input map[string]interface{}) *[]batch.MetadataItem {
output := []batch.MetadataItem{}

for k, v := range input {
name := k
value := v.(string)
output = append(output, batch.MetadataItem{
Name: &name,
Value: &value,
})
}

return &output
}

// FlattenBatchMetaData flattens a Batch pool metadata
func FlattenBatchMetaData(metadatas *[]batch.MetadataItem) map[string]interface{} {
output := make(map[string]interface{})

if metadatas == nil {
return output
}

for _, metadata := range *metadatas {
output[*metadata.Name] = *metadata.Value
sergiupantiru marked this conversation as resolved.
Show resolved Hide resolved
}

return output
}
8 changes: 8 additions & 0 deletions azurerm/internal/services/batch/data_source_batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ func dataSourceArmBatchPool() *schema.Resource {
},
},
},
"metadata": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand Down Expand Up @@ -339,6 +346,7 @@ func dataSourceArmBatchPoolRead(d *schema.ResourceData, meta interface{}) error
}

d.Set("start_task", azure.FlattenBatchPoolStartTask(props.StartTask))
d.Set("metadata", azure.FlattenBatchMetaData(props.Metadata))
}

return nil
Expand Down
19 changes: 19 additions & 0 deletions azurerm/internal/services/batch/resource_arm_batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ func resourceArmBatchPool() *schema.Resource {
},
},
},
"metadata": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validate.NoEmptyStrings,
},
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
},
},
}
}
Expand Down Expand Up @@ -479,6 +487,9 @@ func resourceArmBatchPoolCreate(d *schema.ResourceData, meta interface{}) error
return err
}

metaDataRaw := d.Get("metadata").(map[string]interface{})
parameters.PoolProperties.Metadata = azure.ExpandBatchMetaData(metaDataRaw)

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 @@ -583,6 +594,13 @@ func resourceArmBatchPoolUpdate(d *schema.ResourceData, meta interface{}) error
return err
}

if d.HasChange("metadata") {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("[DEBUG] Updating the MetaData for Batch pool %q (Account name %q / Resource Group %q)..", poolName, accountName, id.ResourceGroup)
metaDataRaw := d.Get("metadata").(map[string]interface{})

parameters.PoolProperties.Metadata = azure.ExpandBatchMetaData(metaDataRaw)
}

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 @@ -655,6 +673,7 @@ func resourceArmBatchPoolRead(d *schema.ResourceData, meta interface{}) error {
}

d.Set("start_task", azure.FlattenBatchPoolStartTask(props.StartTask))
d.Set("metadata", azure.FlattenBatchMetaData(props.Metadata))
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestAccDataSourceAzureRMBatchPool_complete(t *testing.T) {
resource.TestCheckResourceAttr(data.ResourceName, "container_configuration.0.container_registries.#", "1"),
resource.TestCheckResourceAttr(data.ResourceName, "container_configuration.0.container_registries.0.registry_server", "myContainerRegistry.azurecr.io"),
resource.TestCheckResourceAttr(data.ResourceName, "container_configuration.0.container_registries.0.user_name", "myUserName"),
resource.TestCheckResourceAttr(data.ResourceName, "metadata.tagName", "Example tag"),
),
},
},
Expand Down Expand Up @@ -149,6 +150,10 @@ resource "azurerm_batch_pool" "test" {
}
}
}

metadata = {
"tagName"= "Example tag"
}
}

data "azurerm_batch_pool" "test" {
Expand Down
4 changes: 4 additions & 0 deletions examples/batch/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ resource "azurerm_batch_pool" "fixed" {
}
}
}

metadata ={
"tagName"= "Example tag"
}
}

resource "azurerm_batch_pool" "autopool" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/batch_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ The following arguments are supported:

* `container_configuration` - (Optional) The container configuration used in the pool's VMs.

* `metadata` - (Optional) A map of custom batch pool metadata.

-> **NOTE:** For Windows compute nodes, the Batch service installs the certificates to the specified certificate store and location. For Linux compute nodes, the certificates are stored in a directory inside the task working directory and an environment variable `AZ_BATCH_CERTIFICATES_DIR` is supplied to the task to query for this location. For certificates with visibility of `remoteUser`, a `certs` directory is created in the user's home directory (e.g., `/home/{user-name}/certs`) and certificates are placed in that directory.

~> **Please Note:** `fixed_scale` and `auto_scale` blocks cannot be used both at the same time.
Expand Down