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": resourceGroupNameDiffSuppressSchema(),
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
"location": locationForDataSourceSchema(),
"storage_account_id": {
Type: schema.TypeString,
Computed: true,
},
"pool_allocation_mode": {
Type: schema.TypeString,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

since this is the Data Source this field isn't set-able, so we can remove the Optional here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

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 autoStorage := resp.AutoStorage; autoStorage != nil {
d.Set("storage_account_id", autoStorage.StorageAccountID)
}

d.Set("pool_allocation_mode", resp.PoolAllocationMode)
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

flattenAndSetTags(d, resp.Tags)

return nil
}
50 changes: 50 additions & 0 deletions azurerm/data_source_batch_account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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()
name := fmt.Sprintf("acctestbatchaccount%d", ri)
location := testLocation()
config := testAccDataSourceAzureRMBatchAccount_basic(name, location)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", name),
resource.TestCheckResourceAttr(dataSourceName, "location", azureRMNormalizeLocation(location)),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(dataSourceName, "tags.env", "test"),
),
},
},
})
}

func testAccDataSourceAzureRMBatchAccount_basic(name string, location string) string {
return fmt.Sprintf(`
resource "azurerm_batch_account" "test" {
name = "%s"
location = "%s"
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

tags {
env = "test"
}
}

data "azurerm_batch_account" "test" {
name = "${azurerm_batch_account.test.name}"
}
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
`, name, location)
}
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