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

Support for Azure Batch - Pool #2461

Merged
merged 21 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type ArmClient struct {

// Batch
batchAccountClient batch.AccountClient
batchPoolClient batch.PoolClient

// CDN
cdnCustomDomainsClient cdn.CustomDomainsClient
Expand Down Expand Up @@ -540,6 +541,10 @@ func (c *ArmClient) registerBatchClients(endpoint, subscriptionId string, auth a
batchAccount := batch.NewAccountClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&batchAccount.Client, auth)
c.batchAccountClient = batchAccount

batchPool := batch.NewPoolClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&batchPool.Client, auth)
c.batchPoolClient = batchPool
}

func (c *ArmClient) registerCDNClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
Expand Down
218 changes: 218 additions & 0 deletions azurerm/data_source_batch_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmBatchPool() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmBatchPoolRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateAzureRMBatchPoolName,
},
"resource_group_name": resourceGroupNameForDataSourceSchema(),
"account_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAzureRMBatchAccountName,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
},
"vm_size": {
Type: schema.TypeString,
Computed: true,
},
"fixed_scale": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_dedicated_nodes": {
Type: schema.TypeInt,
Computed: true,
},
"target_low_priority_nodes": {
Type: schema.TypeInt,
Computed: true,
},
"resize_timeout": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"auto_scale": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"evaluation_interval": {
Type: schema.TypeString,
Computed: true,
},
"formula": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"storage_image_reference": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},

"publisher": {
Type: schema.TypeString,
Computed: true,
},

"offer": {
Type: schema.TypeString,
Computed: true,
},

"sku": {
Type: schema.TypeString,
Computed: true,
},

"version": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"node_agent_sku_id": {
Type: schema.TypeString,
Computed: true,
},
"start_task": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"command_line": {
Type: schema.TypeString,
Required: true,
},

"max_task_retry_count": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
},

"wait_for_success": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"environment": {
Type: schema.TypeMap,
Optional: true,
},

"user_identity": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"user_name": {
Type: schema.TypeString,
Computed: true,
},
"auto_user": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"elevation_level": {
Type: schema.TypeString,
Computed: true,
},
"scope": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
},
},
},
},
},
},
}
}

func dataSourceArmBatchPoolRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).batchPoolClient

name := d.Get("name").(string)
accountName := d.Get("account_name").(string)
resourceGroup := d.Get("resource_group_name").(string)

ctx := meta.(*ArmClient).StopContext
resp, err := client.Get(ctx, resourceGroup, accountName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Batch pool %q in account %q (Resource Group %q) was not found", name, accountName, resourceGroup)
}
return fmt.Errorf("Error making Read request on AzureRM Batch pool %q: %+v", name, err)
}

d.SetId(*resp.ID)

d.Set("name", name)
d.Set("account_name", accountName)
d.Set("resource_group_name", resourceGroup)

if props := resp.PoolProperties; props != nil {
d.Set("vm_size", props.VMSize)

if scaleSettings := props.ScaleSettings; scaleSettings != nil {
if err := d.Set("auto_scale", azure.FlattenBatchPoolAutoScaleSettings(scaleSettings.AutoScale)); err != nil {
return fmt.Errorf("Error flattening `auto_scale`: %+v", err)
}
if err := d.Set("fixed_scale", azure.FlattenBatchPoolFixedScaleSettings(scaleSettings.FixedScale)); err != nil {
return fmt.Errorf("Error flattening `fixed_scale `: %+v", err)
}
}

if props.DeploymentConfiguration != nil &&
props.DeploymentConfiguration.VirtualMachineConfiguration != nil &&
props.DeploymentConfiguration.VirtualMachineConfiguration.ImageReference != nil {

imageReference := props.DeploymentConfiguration.VirtualMachineConfiguration.ImageReference

d.Set("storage_image_reference", azure.FlattenBatchPoolImageReference(imageReference))
d.Set("node_agent_sku_id", props.DeploymentConfiguration.VirtualMachineConfiguration.NodeAgentSkuID)
}

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

return nil
}
122 changes: 122 additions & 0 deletions azurerm/data_source_batch_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAzureRMBatchPool_complete(t *testing.T) {
dataSourceName := "data.azurerm_batch_pool.test"
ri := acctest.RandInt()
rs := acctest.RandString(4)
location := testLocation()
config := testAccDataSourceAzureRMBatchPool_complete(ri, rs, location)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("testaccpool%s", rs)),
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, "fixed_scale.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "fixed_scale.0.target_dedicated_nodes", "2"),
resource.TestCheckResourceAttr(dataSourceName, "fixed_scale.0.resize_timeout", "PT15M"),
resource.TestCheckResourceAttr(dataSourceName, "fixed_scale.0.target_low_priority_nodes", "0"),
resource.TestCheckResourceAttr(dataSourceName, "node_agent_sku_id", "batch.node.ubuntu 16.04"),
resource.TestCheckResourceAttr(dataSourceName, "start_task.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "start_task.0.max_task_retry_count", "1"),
resource.TestCheckResourceAttr(dataSourceName, "start_task.0.environment.%", "1"),
resource.TestCheckResourceAttr(dataSourceName, "start_task.0.environment.env", "TEST"),
resource.TestCheckResourceAttr(dataSourceName, "start_task.0.user_identity.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "start_task.0.user_identity.0.auto_user.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "start_task.0.user_identity.0.auto_user.0.scope", "Task"),
resource.TestCheckResourceAttr(dataSourceName, "start_task.0.user_identity.0.auto_user.0.elevation_level", "NonAdmin"),
),
},
},
})
}

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

resource "azurerm_storage_account" "test" {
name = "testaccsa%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_batch_account" "test" {
name = "testaccbatch%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
pool_allocation_mode = "BatchService"
storage_account_id = "${azurerm_storage_account.test.id}"

tags {
env = "test"
}
}

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

fixed_scale {
target_dedicated_nodes = 2
resize_timeout = "PT15M"
}

storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04.0-LTS"
version = "latest"
}

start_task {
command_line = "echo 'Hello World from $env'"
max_task_retry_count = 1
wait_for_success = true

environment {
env = "TEST"
}

user_identity {
auto_user {
elevation_level = "NonAdmin"
scope = "Task"
}
}
}
}

data "azurerm_batch_pool" "test" {
name = "${azurerm_batch_pool.test.name}"
account_name = "${azurerm_batch_pool.test.account_name}"
resource_group_name = "${azurerm_batch_pool.test.resource_group_name}"
}
`, rInt, location, rString, rString, rString)
}
Loading