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

New Resource: Batch Application #3825

Merged
merged 4 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ type ArmClient struct {

// Batch
batchAccountClient batch.AccountClient
batchApplicationClient batch.ApplicationClient
batchCertificateClient batch.CertificateClient
batchPoolClient batch.PoolClient

Expand Down Expand Up @@ -453,6 +454,10 @@ func (c *ArmClient) registerBatchClients(endpoint, subscriptionId string, auth a
c.configureClient(&batchAccount.Client, auth)
c.batchAccountClient = batchAccount

batchApplicationClient := batch.NewApplicationClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&batchApplicationClient.Client, auth)
c.batchApplicationClient = batchApplicationClient

batchCertificateClient := batch.NewCertificateClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&batchCertificateClient.Client, auth)
c.batchCertificateClient = batchCertificateClient
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_azuread_service_principal_password": resourceArmActiveDirectoryServicePrincipalPassword(),
"azurerm_azuread_service_principal": resourceArmActiveDirectoryServicePrincipal(),
"azurerm_batch_account": resourceArmBatchAccount(),
"azurerm_batch_application": resourceArmBatchApplication(),
"azurerm_batch_certificate": resourceArmBatchCertificate(),
"azurerm_batch_pool": resourceArmBatchPool(),
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
Expand Down
184 changes: 184 additions & 0 deletions azurerm/resource_arm_batch_application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2018-12-01/batch"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmBatchApplication() *schema.Resource {
return &schema.Resource{
Create: resourceArmBatchApplicationCreate,
Read: resourceArmBatchApplicationRead,
Update: resourceArmBatchApplicationUpdate,
Delete: resourceArmBatchApplicationDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we validate this?


"resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(),

"account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we validate this?


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

"default_version": {
Type: schema.TypeString,
Optional: true,
},

"display_name": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func resourceArmBatchApplicationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).batchApplicationClient
ctx := meta.(*ArmClient).StopContext

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

if requireResourcesToBeImported {
resp, err := client.Get(ctx, resourceGroup, accountName, name)
if err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error checking for present of existing Batch Application %q (Account Name %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
}
}
if !utils.ResponseWasNotFound(resp.Response) {
return tf.ImportAsExistsError("azurerm_batch_application", *resp.ID)
}
}

allowUpdates := d.Get("allow_updates").(bool)
defaultVersion := d.Get("default_version").(string)
displayName := d.Get("display_name").(string)

parameters := batch.Application{
ApplicationProperties: &batch.ApplicationProperties{
AllowUpdates: utils.Bool(allowUpdates),
DefaultVersion: utils.String(defaultVersion),
DisplayName: utils.String(displayName),
},
}

if _, err := client.Create(ctx, resourceGroup, accountName, name, &parameters); err != nil {
return fmt.Errorf("Error creating Batch Application %q (Account Name %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
}

resp, err := client.Get(ctx, resourceGroup, accountName, name)
if err != nil {
return fmt.Errorf("Error retrieving Batch Application %q (Account Name %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
}
if resp.ID == nil {
return fmt.Errorf("Cannot read Batch Application %q (Account Name %q / Resource Group %q) ID", name, accountName, resourceGroup)
}
d.SetId(*resp.ID)

return resourceArmBatchApplicationRead(d, meta)
}

func resourceArmBatchApplicationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).batchApplicationClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
accountName := id.Path["batchAccounts"]
name := id.Path["applications"]

resp, err := client.Get(ctx, resourceGroup, accountName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Batch Application %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Batch Application %q (Account Name %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
}

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("account_name", accountName)
if applicationProperties := resp.ApplicationProperties; applicationProperties != nil {
d.Set("allow_updates", applicationProperties.AllowUpdates)
d.Set("default_version", applicationProperties.DefaultVersion)
d.Set("display_name", applicationProperties.DisplayName)
}

return nil
}

func resourceArmBatchApplicationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).batchApplicationClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
accountName := d.Get("account_name").(string)
allowUpdates := d.Get("allow_updates").(bool)
defaultVersion := d.Get("default_version").(string)
displayName := d.Get("display_name").(string)

parameters := batch.Application{
ApplicationProperties: &batch.ApplicationProperties{
AllowUpdates: utils.Bool(allowUpdates),
DefaultVersion: utils.String(defaultVersion),
DisplayName: utils.String(displayName),
},
}

if _, err := client.Update(ctx, resourceGroup, accountName, name, parameters); err != nil {
return fmt.Errorf("Error updating Batch Application %q (Account Name %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
}

return resourceArmBatchApplicationRead(d, meta)
}

func resourceArmBatchApplicationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).batchApplicationClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
accountName := id.Path["batchAccounts"]
name := id.Path["applications"]

if _, err := client.Delete(ctx, resourceGroup, accountName, name); err != nil {
return fmt.Errorf("Error deleting Batch Application %q (Account Name %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
}

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

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func TestAccAzureRMBatchApplication_basic(t *testing.T) {
resourceName := "azurerm_batch_application.test"
ri := tf.AccRandTimeInt()
rs := strings.ToLower(acctest.RandString(11))
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMBatchApplicationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMBatchApplication_basic(ri, rs, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMBatchApplicationExists(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckAzureRMBatchApplicationExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Batch Application not found: %s", resourceName)
}

name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
accountName := rs.Primary.Attributes["account_name"]

client := testAccProvider.Meta().(*ArmClient).batchApplicationClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

if resp, err := client.Get(ctx, resourceGroup, accountName, name); err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Batch Application %q (Account Name %q / Resource Group %q) does not exist", name, accountName, resourceGroup)
}
return fmt.Errorf("Bad: Get on batchApplicationClient: %+v", err)
}

return nil
}
}

func testCheckAzureRMBatchApplicationDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).batchApplicationClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_batch_application" {
continue
}

name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
accountName := rs.Primary.Attributes["account_name"]

if resp, err := client.Get(ctx, resourceGroup, accountName, name); err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Get on batchApplicationClient: %+v", err)
}
}

return nil
}

return nil
}

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

resource "azurerm_storage_account" "test" {
name = "acctestsa%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 = "acctestba%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}"
}

resource "azurerm_batch_application" "test" {
name = "acctestbatchapp-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
account_name = "${azurerm_batch_account.test.name}"
}
`, rInt, location, rString, rString, rInt)
}
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@
<a href="/docs/providers/azurerm/r/batch_account.html">azurerm_batch_account</a>
</li>

<li>
<a href="/docs/providers/azurerm/r/batch_application.html">azurerm_batch_application</a>
</li>

<li>
<a href="/docs/providers/azurerm/r/batch_certificate.html">azurerm_batch_certificate</a>
</li>
Expand Down
Loading