Skip to content

Commit

Permalink
wip: support for Azure Batch pool
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorioland committed Dec 5, 2018
1 parent ec101b5 commit 7888ed0
Show file tree
Hide file tree
Showing 6 changed files with 619 additions and 0 deletions.
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type ArmClient struct {

// Batch
batchAccountClient batch.AccountClient
batchPoolClient batch.PoolClient

// CDN
cdnCustomDomainsClient cdn.CustomDomainsClient
Expand Down Expand Up @@ -532,6 +533,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
162 changes: 162 additions & 0 deletions azurerm/data_source_batch_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
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: validateAzureRMBatchPoolName,
},
"resource_group_name": resourceGroupNameDiffSuppressSchema(),
"account_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAzureRMBatchAccountName,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
},
"vm_size": {
Type: schema.TypeString,
Computed: true,
},
"scale_mode": {
Type: schema.TypeString,
Computed: true,
},
"target_dedicated_nodes": {
Type: schema.TypeInt,
Computed: true,
},
"target_low_priority_nodes": {
Type: schema.TypeInt,
Computed: true,
},
"resize_timeout": {
Type: schema.TypeString,
Computed: true,
},
"autoscale_evaluation_interval": {
Type: schema.TypeString,
Computed: true,
},
"autoscale_formula": {
Type: schema.TypeString,
Computed: true,
},
"storage_image_reference": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"publisher": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"offer": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"sku": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
},
Set: resourceArmVirtualMachineStorageImageReferenceHash,
},
"node_agent_sku_id": {
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)
d.Set("display_name", resp.DisplayName)
d.Set("vm_size", resp.VMSize)

if resp.ScaleSettings != nil {
if resp.ScaleSettings.AutoScale != nil {
d.Set("scale_mode", azure.BatchPoolAutoScale)
d.Set("autoscale_evaluation_interval", resp.ScaleSettings.AutoScale.EvaluationInterval)
d.Set("autoscale_formula", resp.ScaleSettings.AutoScale.Formula)
} else if resp.ScaleSettings.FixedScale != nil {
d.Set("scale_mode", azure.BatchPoolFixedScale)
d.Set("target_dedicated_nodes", resp.ScaleSettings.FixedScale.TargetDedicatedNodes)
d.Set("target_low_priority_nodes", resp.ScaleSettings.FixedScale.TargetLowPriorityNodes)
d.Set("resize_timeout", resp.ScaleSettings.FixedScale.ResizeTimeout)
}
}

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

imageReference := resp.DeploymentConfiguration.VirtualMachineConfiguration.ImageReference
storageImageRef := schema.Set{F: resourceArmVirtualMachineStorageImageReferenceHash}
storageImageRef.Add(
map[string]string{
"publisher": *imageReference.Publisher,
"offer": *imageReference.Offer,
"sku": *imageReference.Sku,
"version": *imageReference.Version,
})
}

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

type BatchPoolScaleMode string

const (
BatchPoolAutoScale = "Auto"
BatchPoolFixedScale = "Fixed"
)
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_app_service": dataSourceArmAppService(),
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_batch_account": dataSourceArmBatchAccount(),
"azurerm_batch_pool": dataSourceArmBatchPool(),
"azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(),
"azurerm_cdn_profile": dataSourceArmCdnProfile(),
"azurerm_client_config": dataSourceArmClientConfig(),
Expand Down Expand Up @@ -149,6 +150,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_autoscale_setting": resourceArmAutoScaleSetting(),
"azurerm_availability_set": resourceArmAvailabilitySet(),
"azurerm_batch_account": resourceArmBatchAccount(),
"azurerm_batch_pool": resourceArmBatchPool(),
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
"azurerm_cdn_profile": resourceArmCdnProfile(),
"azurerm_cognitive_account": resourceArmCognitiveAccount(),
Expand Down
Loading

0 comments on commit 7888ed0

Please sign in to comment.