provider/azurerm: catch azurerm_template_deployment errors (#7644)

The error was ignored causing Terraform to report that the deployments was
successful rather than in a bad state. This commit cause the apply operation
to report the error.

Added a test which attempts to create a storage account with a name longer
than the maximum permitted length to force a failure.

```
TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMTemplateDeployment_ -timeout 120m
=== RUN   TestAccAzureRMTemplateDeployment_basic
--- PASS: TestAccAzureRMTemplateDeployment_basic (377.78s)
=== RUN   TestAccAzureRMTemplateDeployment_withParams
--- PASS: TestAccAzureRMTemplateDeployment_withParams (327.89s)
=== RUN   TestAccAzureRMTemplateDeployment_withError
--- PASS: TestAccAzureRMTemplateDeployment_withError (226.64s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/azurerm	932.440s
```
This commit is contained in:
Peter McAtominey 2016-07-14 16:06:58 +01:00 committed by Paul Stack
parent 324e78020d
commit 0fde61b9ab
2 changed files with 81 additions and 1 deletions

View File

@ -101,7 +101,7 @@ func resourceArmTemplateDeploymentCreate(d *schema.ResourceData, meta interface{
_, err := deployClient.CreateOrUpdate(resGroup, name, deployment, make(chan struct{}))
if err != nil {
return nil
return fmt.Errorf("Error creating deployment: %s", err)
}
read, err := deployClient.Get(resGroup, name)

View File

@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"net/http"
"regexp"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
@ -47,6 +48,22 @@ func TestAccAzureRMTemplateDeployment_withParams(t *testing.T) {
})
}
func TestAccAzureRMTemplateDeployment_withError(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withError, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("The deployment operation failed"),
},
},
})
}
func testCheckAzureRMTemplateDeploymentExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
@ -249,3 +266,66 @@ DEPLOY
}
`
// StorageAccount name is too long, forces error
var testAccAzureRMTemplateDeployment_withError = `
resource "azurerm_resource_group" "test" {
name = "acctestrg-%d"
location = "West US"
}
output "test" {
value = "${azurerm_template_deployment.test.outputs.testOutput}"
}
resource "azurerm_template_deployment" "test" {
name = "acctesttemplate-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
template_body = <<DEPLOY
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_ZRS"
],
"metadata": {
"description": "Storage Account type"
}
}
},
"variables": {
"location": "[resourceGroup().location]",
"storageAccountName": "badStorageAccountNameTooLong",
"apiVersion": "2015-06-15"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "[variables('apiVersion')]",
"location": "[variables('location')]",
"properties": {
"accountType": "[parameters('storageAccountType')]"
}
}
],
"outputs": {
"testOutput": {
"type": "string",
"value": "Output Value"
}
}
}
DEPLOY
parameters {
storageAccountType = "Standard_GRS"
}
deployment_mode = "Complete"
}
`