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

IC for adding container_configuration to batch pool resource. #3311

Merged
merged 6 commits into from
May 12, 2019
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
30 changes: 24 additions & 6 deletions azurerm/data_source_batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ func dataSourceArmBatchPool() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"container_configuration": {
katbyte marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"certificate": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -274,14 +286,20 @@ func dataSourceArmBatchPoolRead(d *schema.ResourceData, meta interface{}) error
}
}

if props.DeploymentConfiguration != nil &&
props.DeploymentConfiguration.VirtualMachineConfiguration != nil &&
props.DeploymentConfiguration.VirtualMachineConfiguration.ImageReference != nil {
if dcfg := props.DeploymentConfiguration; dcfg != nil {
if vmcfg := dcfg.VirtualMachineConfiguration; vmcfg != nil {
if err := d.Set("container_configuration", azure.FlattenBatchPoolContainerConfiguration(vmcfg.ContainerConfiguration)); err != nil {
return fmt.Errorf("error setting `container_configuration`: %v", err)
}

imageReference := props.DeploymentConfiguration.VirtualMachineConfiguration.ImageReference
if err := d.Set("storage_image_reference", azure.FlattenBatchPoolImageReference(vmcfg.ImageReference)); err != nil {
return fmt.Errorf("error setting `storage_image_reference`: %v", err)
}

d.Set("storage_image_reference", azure.FlattenBatchPoolImageReference(imageReference))
d.Set("node_agent_sku_id", props.DeploymentConfiguration.VirtualMachineConfiguration.NodeAgentSkuID)
if err := d.Set("node_agent_sku_id", vmcfg.NodeAgentSkuID); err != nil {
return fmt.Errorf("error setting `node_agent_sku_id`: %v", err)
}
}
}

if err := d.Set("certificate", azure.FlattenBatchPoolCertificateReferences(props.Certificates)); err != nil {
Expand Down
17 changes: 11 additions & 6 deletions azurerm/data_source_batch_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func TestAccDataSourceAzureRMBatchPool_complete(t *testing.T) {
resource.TestCheckResourceAttr(dataSourceName, "account_name", fmt.Sprintf("testaccbatch%s", rs)),
resource.TestCheckResourceAttr(dataSourceName, "vm_size", "STANDARD_A1"),
resource.TestCheckResourceAttr(dataSourceName, "storage_image_reference.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "storage_image_reference.0.publisher", "Canonical"),
resource.TestCheckResourceAttr(dataSourceName, "storage_image_reference.0.sku", "16.04.0-LTS"),
resource.TestCheckResourceAttr(dataSourceName, "storage_image_reference.0.offer", "UbuntuServer"),
resource.TestCheckResourceAttr(dataSourceName, "storage_image_reference.0.publisher", "microsoft-azure-batch"),
resource.TestCheckResourceAttr(dataSourceName, "storage_image_reference.0.sku", "16-04-lts"),
resource.TestCheckResourceAttr(dataSourceName, "storage_image_reference.0.offer", "ubuntu-server-container"),
resource.TestCheckResourceAttr(dataSourceName, "fixed_scale.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "fixed_scale.0.target_dedicated_nodes", "2"),
resource.TestCheckResourceAttr(dataSourceName, "fixed_scale.0.resize_timeout", "PT15M"),
Expand All @@ -53,6 +53,7 @@ func TestAccDataSourceAzureRMBatchPool_complete(t *testing.T) {
resource.TestCheckResourceAttr(dataSourceName, "certificate.0.visibility.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "certificate.0.visibility.3294600504", "StartTask"),
resource.TestCheckResourceAttr(dataSourceName, "certificate.0.visibility.4077195354", "RemoteUser"),
resource.TestCheckResourceAttr(dataSourceName, "container_configuration.0.type", "DockerCompatible"),
),
},
},
Expand Down Expand Up @@ -111,9 +112,9 @@ resource "azurerm_batch_pool" "test" {
}

storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04.0-LTS"
publisher = "microsoft-azure-batch"
offer = "ubuntu-server-container"
sku = "16-04-lts"
version = "latest"
}

Expand All @@ -122,6 +123,10 @@ resource "azurerm_batch_pool" "test" {
store_location = "CurrentUser"
visibility = [ "StartTask", "RemoteUser" ]
}

container_configuration {
type = "DockerCompatible"
}

start_task {
command_line = "echo 'Hello World from $env'"
Expand Down
32 changes: 32 additions & 0 deletions azurerm/helpers/azure/batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ func FlattenBatchPoolCertificateReferences(armCertificates *[]batch.CertificateR
return output
}

// FlattenBatchPoolContainerConfiguration flattens a Batch pool container configuration
func FlattenBatchPoolContainerConfiguration(armContainerConfiguration *batch.ContainerConfiguration) interface{} {

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

if armContainerConfiguration == nil {
return nil
}

if armContainerConfiguration.Type != nil {
result["type"] = *armContainerConfiguration.Type
}

return []interface{}{result}
}

// ExpandBatchPoolImageReference expands Batch pool image reference
func ExpandBatchPoolImageReference(list []interface{}) (*batch.ImageReference, error) {
if len(list) == 0 {
Expand All @@ -214,6 +230,22 @@ func ExpandBatchPoolImageReference(list []interface{}) (*batch.ImageReference, e
return imageRef, nil
}

// ExpandBatchPoolContainerConfiguration expands the Batch pool container configuration
func ExpandBatchPoolContainerConfiguration(list []interface{}) (*batch.ContainerConfiguration, error) {
if len(list) == 0 {
return nil, nil
}

containerConfiguration := list[0].(map[string]interface{})
containerType := containerConfiguration["type"].(string)

containerConf := &batch.ContainerConfiguration{
Type: &containerType,
}

return containerConf, nil
}

// ExpandBatchPoolCertificateReferences expands Batch pool certificate references
func ExpandBatchPoolCertificateReferences(list []interface{}) (*[]batch.CertificateReference, error) {
result := []batch.CertificateReference{}
Expand Down
31 changes: 29 additions & 2 deletions azurerm/resource_arm_batch_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ func resourceArmBatchPool() *schema.Resource {
},
},
},
"container_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
},
},
"storage_image_reference": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -385,10 +399,17 @@ func resourceArmBatchPoolCreate(d *schema.ResourceData, meta interface{}) error
parameters.PoolProperties.StartTask = startTask
}

containerConfigurationSet := d.Get("container_configuration").([]interface{})
containerConfiguration, err := azure.ExpandBatchPoolContainerConfiguration(containerConfigurationSet)
if err != nil {
return fmt.Errorf("Error creating Batch pool %q (Resource Group %q): %+v", poolName, resourceGroup, err)
}

parameters.PoolProperties.DeploymentConfiguration = &batch.DeploymentConfiguration{
VirtualMachineConfiguration: &batch.VirtualMachineConfiguration{
NodeAgentSkuID: &nodeAgentSkuID,
ImageReference: imageReference,
NodeAgentSkuID: &nodeAgentSkuID,
ImageReference: imageReference,
ContainerConfiguration: containerConfiguration,
},
}

Expand Down Expand Up @@ -567,6 +588,12 @@ func resourceArmBatchPoolRead(d *schema.ResourceData, meta interface{}) error {
d.Set("node_agent_sku_id", props.DeploymentConfiguration.VirtualMachineConfiguration.NodeAgentSkuID)
}

if dcfg := props.DeploymentConfiguration; dcfg != nil {
if vmcfg := dcfg.VirtualMachineConfiguration; vmcfg != nil {
d.Set("container_configuration", azure.FlattenBatchPoolContainerConfiguration(vmcfg.ContainerConfiguration))
}
}

if err := d.Set("certificate", azure.FlattenBatchPoolCertificateReferences(props.Certificates)); err != nil {
return fmt.Errorf("Error flattening `certificate`: %+v", err)
}
Expand Down
67 changes: 67 additions & 0 deletions azurerm/resource_arm_batch_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,27 @@ func TestAccAzureRMBatchPool_validateResourceFileWithoutSource(t *testing.T) {
})
}

func TestAccAzureRMBatchPool_container(t *testing.T) {
resourceName := "azurerm_batch_pool.test"
ri := tf.AccRandTimeInt()
rs := acctest.RandString(4)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMBatchPoolDestroy,
Steps: []resource.TestStep{
{
Config: testaccAzureRMBatchPoolContainerConfiguration(ri, rs, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMBatchPoolExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "container_configuration.0.type", "DockerCompatible"),
),
},
},
})
}

func TestAccAzureRMBatchPool_validateResourceFileWithMultipleSources(t *testing.T) {
ri := tf.AccRandTimeInt()
rs := acctest.RandString(4)
Expand Down Expand Up @@ -919,3 +940,49 @@ resource "azurerm_batch_pool" "test" {

`, rInt, location, rString, rString)
}

func testaccAzureRMBatchPoolContainerConfiguration(rInt int, rString string, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "testaccbatch%d"
location = "%s"
}

resource "azurerm_container_registry" "test" {
name = "testregistry%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
sku = "Basic"
}

resource "azurerm_batch_account" "test" {
name = "testaccbatch%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
}

resource "azurerm_batch_pool" "test" {
name = "testaccpool%s"
resource_group_name = "${azurerm_resource_group.test.name}"
account_name = "${azurerm_batch_account.test.name}"
node_agent_sku_id = "batch.node.ubuntu 16.04"
vm_size = "Standard_A1"

fixed_scale {
target_dedicated_nodes = 1
}

storage_image_reference {
publisher = "microsoft-azure-batch"
offer = "ubuntu-server-container"
sku = "16-04-lts"
version = "latest"
}

container_configuration {
type = "DockerCompatible"
}
}

`, rInt, location, rString, rString, rString)
}
8 changes: 8 additions & 0 deletions website/docs/d/batch_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ The following attributes are exported:

katbyte marked this conversation as resolved.
Show resolved Hide resolved
* `certificate` - One or more `certificate` blocks that describe the certificates installed on each compute node in the pool.

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

---

A `fixed_scale` block exports the following:
Expand Down Expand Up @@ -128,3 +130,9 @@ A `resource_file` block exports the following:
* `http_url` - The URL of the file to download. If the URL is Azure Blob Storage, it must be readable using anonymous access.

* `storage_container_url` - The URL of the blob container within Azure Blob Storage.

---

A `container_configuration` block exports the following:

* `type` - The type of container configuration.
18 changes: 15 additions & 3 deletions website/docs/r/batch_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ EOF
}

storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04.0-LTS"
publisher = "microsoft-azure-batch"
offer = "ubuntu-server-container"
sku = "16-04-lts"
version = "latest"
}

container_configuration {
type = "DockerCompatible"
}

start_task {
command_line = "echo 'Hello World from $env'"
max_task_retry_count = 1
Expand Down Expand Up @@ -128,6 +132,8 @@ The following arguments are supported:

* `certificate` - (Optional) One or more `certificate` blocks that describe the certificates to be installed on each compute node in the pool.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like your missing the type attribute description here


-> **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 Expand Up @@ -200,6 +206,12 @@ A `certificate` block supports the following:

---

A `container_configuration` block supports the following:

* `type` - (Optional) The type of container configuration. Possible value is `DockerCompatible`.

---

A `resource_file` block supports the following:

* `auto_storage_container_name` - (Optional) The storage container name in the auto storage account.
Expand Down