github.com/danrjohnson/terraform@v0.7.0-rc2.0.20160627135212-d0fc1fa086ff/builtin/providers/azurerm/resource_arm_template_deployment_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMTemplateDeployment_basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMTemplateDeployment_basicExample, ri, ri)
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy,
    20  		Steps: []resource.TestStep{
    21  			{
    22  				Config: config,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccAzureRMTemplateDeployment_withParams(t *testing.T) {
    32  	ri := acctest.RandInt()
    33  	config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withParams, ri, ri, ri)
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy,
    38  		Steps: []resource.TestStep{
    39  			{
    40  				Config: config,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"),
    43  					resource.TestCheckResourceAttr("azurerm_template_deployment.test", "outputs.testOutput", "Output Value"),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func testCheckAzureRMTemplateDeploymentExists(name string) resource.TestCheckFunc {
    51  	return func(s *terraform.State) error {
    52  		// Ensure we have enough information in state to look up in API
    53  		rs, ok := s.RootModule().Resources[name]
    54  		if !ok {
    55  			return fmt.Errorf("Not found: %s", name)
    56  		}
    57  
    58  		name := rs.Primary.Attributes["name"]
    59  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    60  		if !hasResourceGroup {
    61  			return fmt.Errorf("Bad: no resource group found in state for template deployment: %s", name)
    62  		}
    63  
    64  		conn := testAccProvider.Meta().(*ArmClient).deploymentsClient
    65  
    66  		resp, err := conn.Get(resourceGroup, name)
    67  		if err != nil {
    68  			return fmt.Errorf("Bad: Get on deploymentsClient: %s", err)
    69  		}
    70  
    71  		if resp.StatusCode == http.StatusNotFound {
    72  			return fmt.Errorf("Bad: TemplateDeployment %q (resource group: %q) does not exist", name, resourceGroup)
    73  		}
    74  
    75  		return nil
    76  	}
    77  }
    78  
    79  func testCheckAzureRMTemplateDeploymentDestroy(s *terraform.State) error {
    80  	conn := testAccProvider.Meta().(*ArmClient).vmClient
    81  
    82  	for _, rs := range s.RootModule().Resources {
    83  		if rs.Type != "azurerm_template_deployment" {
    84  			continue
    85  		}
    86  
    87  		name := rs.Primary.Attributes["name"]
    88  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
    89  
    90  		resp, err := conn.Get(resourceGroup, name, "")
    91  
    92  		if err != nil {
    93  			return nil
    94  		}
    95  
    96  		if resp.StatusCode != http.StatusNotFound {
    97  			return fmt.Errorf("Template Deployment still exists:\n%#v", resp.Properties)
    98  		}
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  var testAccAzureRMTemplateDeployment_basicExample = `
   105    resource "azurerm_resource_group" "test" {
   106      name = "acctestrg-%d"
   107      location = "West US"
   108    }
   109  
   110    resource "azurerm_template_deployment" "test" {
   111      name = "acctesttemplate-%d"
   112      resource_group_name = "${azurerm_resource_group.test.name}"
   113      template_body = <<DEPLOY
   114  {
   115    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   116    "contentVersion": "1.0.0.0",
   117    "parameters": {
   118      "storageAccountType": {
   119        "type": "string",
   120        "defaultValue": "Standard_LRS",
   121        "allowedValues": [
   122          "Standard_LRS",
   123          "Standard_GRS",
   124          "Standard_ZRS"
   125        ],
   126        "metadata": {
   127          "description": "Storage Account type"
   128        }
   129      }
   130    },
   131    "variables": {
   132      "location": "[resourceGroup().location]",
   133      "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]",
   134      "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]",
   135      "publicIPAddressType": "Dynamic",
   136      "apiVersion": "2015-06-15",
   137      "dnsLabelPrefix": "terraform-tdacctest"
   138    },
   139    "resources": [
   140      {
   141        "type": "Microsoft.Storage/storageAccounts",
   142        "name": "[variables('storageAccountName')]",
   143        "apiVersion": "[variables('apiVersion')]",
   144        "location": "[variables('location')]",
   145        "properties": {
   146          "accountType": "[parameters('storageAccountType')]"
   147        }
   148      },
   149      {
   150        "type": "Microsoft.Network/publicIPAddresses",
   151        "apiVersion": "[variables('apiVersion')]",
   152        "name": "[variables('publicIPAddressName')]",
   153        "location": "[variables('location')]",
   154        "properties": {
   155          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   156          "dnsSettings": {
   157            "domainNameLabel": "[variables('dnsLabelPrefix')]"
   158          }
   159        }
   160      }
   161    ]
   162  }
   163  DEPLOY
   164      deployment_mode = "Complete"
   165    }
   166  
   167  `
   168  
   169  var testAccAzureRMTemplateDeployment_withParams = `
   170    resource "azurerm_resource_group" "test" {
   171      name = "acctestrg-%d"
   172      location = "West US"
   173    }
   174  
   175    output "test" {
   176      value = "${azurerm_template_deployment.test.outputs.testOutput}"
   177    }
   178  
   179    resource "azurerm_template_deployment" "test" {
   180      name = "acctesttemplate-%d"
   181      resource_group_name = "${azurerm_resource_group.test.name}"
   182      template_body = <<DEPLOY
   183  {
   184    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   185    "contentVersion": "1.0.0.0",
   186    "parameters": {
   187      "storageAccountType": {
   188        "type": "string",
   189        "defaultValue": "Standard_LRS",
   190        "allowedValues": [
   191          "Standard_LRS",
   192          "Standard_GRS",
   193          "Standard_ZRS"
   194        ],
   195        "metadata": {
   196          "description": "Storage Account type"
   197        }
   198      },
   199      "dnsLabelPrefix": {
   200        "type": "string",
   201        "metadata": {
   202          "description": "DNS Label for the Public IP. Must be lowercase. It should match with the following regular expression: ^[a-z][a-z0-9-]{1,61}[a-z0-9]$ or it will raise an error."
   203        }
   204      }
   205    },
   206    "variables": {
   207      "location": "[resourceGroup().location]",
   208      "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]",
   209      "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]",
   210      "publicIPAddressType": "Dynamic",
   211      "apiVersion": "2015-06-15"
   212    },
   213    "resources": [
   214      {
   215        "type": "Microsoft.Storage/storageAccounts",
   216        "name": "[variables('storageAccountName')]",
   217        "apiVersion": "[variables('apiVersion')]",
   218        "location": "[variables('location')]",
   219        "properties": {
   220          "accountType": "[parameters('storageAccountType')]"
   221        }
   222      },
   223      {
   224        "type": "Microsoft.Network/publicIPAddresses",
   225        "apiVersion": "[variables('apiVersion')]",
   226        "name": "[variables('publicIPAddressName')]",
   227        "location": "[variables('location')]",
   228        "properties": {
   229          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   230          "dnsSettings": {
   231            "domainNameLabel": "[parameters('dnsLabelPrefix')]"
   232          }
   233        }
   234      }
   235    ],
   236    "outputs": {
   237      "testOutput": {
   238        "type": "string",
   239        "value": "Output Value"
   240      }
   241    }
   242  }
   243  DEPLOY
   244      parameters {
   245  	dnsLabelPrefix = "terraform-test-%d"
   246  	storageAccountType = "Standard_GRS"
   247      }
   248      deployment_mode = "Complete"
   249    }
   250  
   251  `