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 - Account #2428

Merged
merged 13 commits into from
Dec 14, 2018
11 changes: 11 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources"
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/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 Expand Up @@ -133,6 +134,9 @@ type ArmClient struct {
// Autoscale Settings
autoscaleSettingsClient insights.AutoscaleSettingsClient

// Batch
batchAccountClient batch.AccountClient

// CDN
cdnCustomDomainsClient cdn.CustomDomainsClient
cdnEndpointsClient cdn.EndpointsClient
Expand Down Expand Up @@ -415,6 +419,7 @@ func getArmClient(c *authentication.Config, skipProviderRegistration bool) (*Arm
client.registerAppInsightsClients(endpoint, c.SubscriptionID, auth)
client.registerAutomationClients(endpoint, c.SubscriptionID, auth)
client.registerAuthentication(endpoint, graphEndpoint, c.SubscriptionID, c.TenantID, auth, graphAuth)
client.registerBatchClients(endpoint, c.SubscriptionID, auth)
client.registerCDNClients(endpoint, c.SubscriptionID, auth)
client.registerCognitiveServiceClients(endpoint, c.SubscriptionID, auth)
client.registerComputeClients(endpoint, c.SubscriptionID, auth)
Expand Down Expand Up @@ -523,6 +528,12 @@ func (c *ArmClient) registerAuthentication(endpoint, graphEndpoint, subscription
c.servicePrincipalsClient = servicePrincipalsClient
}

func (c *ArmClient) registerBatchClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
batchAccount := batch.NewAccountClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&batchAccount.Client, auth)
c.batchAccountClient = batchAccount
}

func (c *ArmClient) registerCDNClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
customDomainsClient := cdn.NewCustomDomainsClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&customDomainsClient.Client, auth)
Expand Down
71 changes: 71 additions & 0 deletions azurerm/data_source_batch_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package azurerm

import (
"fmt"

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

func dataSourceArmBatchAccount() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmBatchAccountRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRMBatchAccountName,
},
"resource_group_name": resourceGroupNameForDataSourceSchema(),
"location": locationForDataSourceSchema(),
"storage_account_id": {
Type: schema.TypeString,
Computed: true,
},
"pool_allocation_mode": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsForDataSourceSchema(),
},
}
}

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

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

ctx := meta.(*ArmClient).StopContext
resp, err := client.Get(ctx, resourceGroup, name)

if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Batch account %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error making Read request on AzureRM Batch account %q: %+v", name, err)
}

d.SetId(*resp.ID)

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

if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

if props := resp.AccountProperties; props != nil {
if autoStorage := props.AutoStorage; autoStorage != nil {
d.Set("storage_account_id", autoStorage.StorageAccountID)
}
d.Set("pool_allocation_mode", props.PoolAllocationMode)
}

flattenAndSetTags(d, resp.Tags)

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

import (
"fmt"
"testing"

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

func TestAccDataSourceAzureRMBatchAccount_basic(t *testing.T) {
katbyte marked this conversation as resolved.
Show resolved Hide resolved
dataSourceName := "data.azurerm_batch_account.test"
ri := acctest.RandInt()
rs := acctest.RandString(4)
location := testLocation()
config := testAccDataSourceAzureRMBatchAccount_basic(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("testaccbatch%s", rs)),
resource.TestCheckResourceAttr(dataSourceName, "location", azureRMNormalizeLocation(location)),
resource.TestCheckResourceAttr(dataSourceName, "pool_allocation_mode", "BatchService"),
),
},
},
})
}

func TestAccDataSourceAzureRMBatchAccount_complete(t *testing.T) {
dataSourceName := "data.azurerm_batch_account.test"
ri := acctest.RandInt()
rs := acctest.RandString(4)
location := testLocation()
config := testAccDataSourceAzureRMBatchAccount_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("testaccbatch%s", rs)),
resource.TestCheckResourceAttr(dataSourceName, "location", azureRMNormalizeLocation(location)),
resource.TestCheckResourceAttr(dataSourceName, "pool_allocation_mode", "BatchService"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(dataSourceName, "tags.env", "test"),
),
},
},
})
}

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

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"
}

data "azurerm_batch_account" "test" {
name = "${azurerm_batch_account.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rString)
}

func testAccDataSourceAzureRMBatchAccount_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"
}
}

data "azurerm_batch_account" "test" {
name = "${azurerm_batch_account.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rString, rString)
}
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(),
"azurerm_app_service": dataSourceArmAppService(),
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_batch_account": dataSourceArmBatchAccount(),
"azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(),
"azurerm_cdn_profile": dataSourceArmCdnProfile(),
"azurerm_client_config": dataSourceArmClientConfig(),
Expand Down Expand Up @@ -147,6 +148,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_automation_schedule": resourceArmAutomationSchedule(),
"azurerm_autoscale_setting": resourceArmAutoScaleSetting(),
"azurerm_availability_set": resourceArmAvailabilitySet(),
"azurerm_batch_account": resourceArmBatchAccount(),
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
"azurerm_cdn_profile": resourceArmCdnProfile(),
"azurerm_cognitive_account": resourceArmCognitiveAccount(),
Expand Down
Loading