-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(), | ||
|
||
"account_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, ¶meters); 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we validate this?