github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_template_deployment_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"regexp"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAzureRMTemplateDeployment_basic(t *testing.T) {
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccAzureRMTemplateDeployment_basicExample, ri, ri)
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAzureRMTemplateDeployment_disappears(t *testing.T) {
    33  	ri := acctest.RandInt()
    34  	config := fmt.Sprintf(testAccAzureRMTemplateDeployment_basicExample, ri, ri)
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy,
    39  		Steps: []resource.TestStep{
    40  			{
    41  				Config: config,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"),
    44  					testCheckAzureRMTemplateDeploymentDisappears("azurerm_template_deployment.test"),
    45  				),
    46  				ExpectNonEmptyPlan: true,
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func TestAccAzureRMTemplateDeployment_withParams(t *testing.T) {
    53  	ri := acctest.RandInt()
    54  	config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withParams, ri, ri, ri)
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck:     func() { testAccPreCheck(t) },
    57  		Providers:    testAccProviders,
    58  		CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy,
    59  		Steps: []resource.TestStep{
    60  			{
    61  				Config: config,
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"),
    64  					resource.TestCheckResourceAttr("azurerm_template_deployment.test", "outputs.testOutput", "Output Value"),
    65  				),
    66  			},
    67  		},
    68  	})
    69  }
    70  
    71  func TestAccAzureRMTemplateDeployment_withError(t *testing.T) {
    72  	ri := acctest.RandInt()
    73  	config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withError, ri, ri)
    74  	resource.Test(t, resource.TestCase{
    75  		PreCheck:     func() { testAccPreCheck(t) },
    76  		Providers:    testAccProviders,
    77  		CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy,
    78  		Steps: []resource.TestStep{
    79  			{
    80  				Config:      config,
    81  				ExpectError: regexp.MustCompile("The deployment operation failed"),
    82  			},
    83  		},
    84  	})
    85  }
    86  
    87  func testCheckAzureRMTemplateDeploymentExists(name string) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  		// Ensure we have enough information in state to look up in API
    90  		rs, ok := s.RootModule().Resources[name]
    91  		if !ok {
    92  			return fmt.Errorf("Not found: %s", name)
    93  		}
    94  
    95  		name := rs.Primary.Attributes["name"]
    96  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    97  		if !hasResourceGroup {
    98  			return fmt.Errorf("Bad: no resource group found in state for template deployment: %s", name)
    99  		}
   100  
   101  		conn := testAccProvider.Meta().(*ArmClient).deploymentsClient
   102  
   103  		resp, err := conn.Get(resourceGroup, name)
   104  		if err != nil {
   105  			return fmt.Errorf("Bad: Get on deploymentsClient: %s", err)
   106  		}
   107  
   108  		if resp.StatusCode == http.StatusNotFound {
   109  			return fmt.Errorf("Bad: TemplateDeployment %q (resource group: %q) does not exist", name, resourceGroup)
   110  		}
   111  
   112  		return nil
   113  	}
   114  }
   115  
   116  func testCheckAzureRMTemplateDeploymentDisappears(name string) resource.TestCheckFunc {
   117  	return func(s *terraform.State) error {
   118  		// Ensure we have enough information in state to look up in API
   119  		rs, ok := s.RootModule().Resources[name]
   120  		if !ok {
   121  			return fmt.Errorf("Not found: %s", name)
   122  		}
   123  
   124  		name := rs.Primary.Attributes["name"]
   125  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   126  		if !hasResourceGroup {
   127  			return fmt.Errorf("Bad: no resource group found in state for template deployment: %s", name)
   128  		}
   129  
   130  		conn := testAccProvider.Meta().(*ArmClient).deploymentsClient
   131  
   132  		_, err := conn.Delete(resourceGroup, name, make(chan struct{}))
   133  		if err != nil {
   134  			return fmt.Errorf("Bad: Delete on deploymentsClient: %s", err)
   135  		}
   136  
   137  		return nil
   138  	}
   139  }
   140  
   141  func testCheckAzureRMTemplateDeploymentDestroy(s *terraform.State) error {
   142  	conn := testAccProvider.Meta().(*ArmClient).vmClient
   143  
   144  	for _, rs := range s.RootModule().Resources {
   145  		if rs.Type != "azurerm_template_deployment" {
   146  			continue
   147  		}
   148  
   149  		name := rs.Primary.Attributes["name"]
   150  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   151  
   152  		resp, err := conn.Get(resourceGroup, name, "")
   153  
   154  		if err != nil {
   155  			return nil
   156  		}
   157  
   158  		if resp.StatusCode != http.StatusNotFound {
   159  			return fmt.Errorf("Template Deployment still exists:\n%#v", resp.Properties)
   160  		}
   161  	}
   162  
   163  	return nil
   164  }
   165  
   166  var testAccAzureRMTemplateDeployment_basicExample = `
   167    resource "azurerm_resource_group" "test" {
   168      name = "acctestRG-%d"
   169      location = "West US"
   170    }
   171  
   172    resource "azurerm_template_deployment" "test" {
   173      name = "acctesttemplate-%d"
   174      resource_group_name = "${azurerm_resource_group.test.name}"
   175      template_body = <<DEPLOY
   176  {
   177    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   178    "contentVersion": "1.0.0.0",
   179    "parameters": {
   180      "storageAccountType": {
   181        "type": "string",
   182        "defaultValue": "Standard_LRS",
   183        "allowedValues": [
   184          "Standard_LRS",
   185          "Standard_GRS",
   186          "Standard_ZRS"
   187        ],
   188        "metadata": {
   189          "description": "Storage Account type"
   190        }
   191      }
   192    },
   193    "variables": {
   194      "location": "[resourceGroup().location]",
   195      "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]",
   196      "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]",
   197      "publicIPAddressType": "Dynamic",
   198      "apiVersion": "2015-06-15",
   199      "dnsLabelPrefix": "terraform-tdacctest"
   200    },
   201    "resources": [
   202      {
   203        "type": "Microsoft.Storage/storageAccounts",
   204        "name": "[variables('storageAccountName')]",
   205        "apiVersion": "[variables('apiVersion')]",
   206        "location": "[variables('location')]",
   207        "properties": {
   208          "accountType": "[parameters('storageAccountType')]"
   209        }
   210      },
   211      {
   212        "type": "Microsoft.Network/publicIPAddresses",
   213        "apiVersion": "[variables('apiVersion')]",
   214        "name": "[variables('publicIPAddressName')]",
   215        "location": "[variables('location')]",
   216        "properties": {
   217          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   218          "dnsSettings": {
   219            "domainNameLabel": "[variables('dnsLabelPrefix')]"
   220          }
   221        }
   222      }
   223    ]
   224  }
   225  DEPLOY
   226      deployment_mode = "Complete"
   227    }
   228  
   229  `
   230  
   231  var testAccAzureRMTemplateDeployment_withParams = `
   232    resource "azurerm_resource_group" "test" {
   233      name = "acctestRG-%d"
   234      location = "West US"
   235    }
   236  
   237    output "test" {
   238      value = "${azurerm_template_deployment.test.outputs.testOutput}"
   239    }
   240  
   241    resource "azurerm_template_deployment" "test" {
   242      name = "acctesttemplate-%d"
   243      resource_group_name = "${azurerm_resource_group.test.name}"
   244      template_body = <<DEPLOY
   245  {
   246    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   247    "contentVersion": "1.0.0.0",
   248    "parameters": {
   249      "storageAccountType": {
   250        "type": "string",
   251        "defaultValue": "Standard_LRS",
   252        "allowedValues": [
   253          "Standard_LRS",
   254          "Standard_GRS",
   255          "Standard_ZRS"
   256        ],
   257        "metadata": {
   258          "description": "Storage Account type"
   259        }
   260      },
   261      "dnsLabelPrefix": {
   262        "type": "string",
   263        "metadata": {
   264          "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."
   265        }
   266      }
   267    },
   268    "variables": {
   269      "location": "[resourceGroup().location]",
   270      "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]",
   271      "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]",
   272      "publicIPAddressType": "Dynamic",
   273      "apiVersion": "2015-06-15"
   274    },
   275    "resources": [
   276      {
   277        "type": "Microsoft.Storage/storageAccounts",
   278        "name": "[variables('storageAccountName')]",
   279        "apiVersion": "[variables('apiVersion')]",
   280        "location": "[variables('location')]",
   281        "properties": {
   282          "accountType": "[parameters('storageAccountType')]"
   283        }
   284      },
   285      {
   286        "type": "Microsoft.Network/publicIPAddresses",
   287        "apiVersion": "[variables('apiVersion')]",
   288        "name": "[variables('publicIPAddressName')]",
   289        "location": "[variables('location')]",
   290        "properties": {
   291          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   292          "dnsSettings": {
   293            "domainNameLabel": "[parameters('dnsLabelPrefix')]"
   294          }
   295        }
   296      }
   297    ],
   298    "outputs": {
   299      "testOutput": {
   300        "type": "string",
   301        "value": "Output Value"
   302      }
   303    }
   304  }
   305  DEPLOY
   306      parameters {
   307  	dnsLabelPrefix = "terraform-test-%d"
   308  	storageAccountType = "Standard_GRS"
   309      }
   310      deployment_mode = "Complete"
   311    }
   312  
   313  `
   314  
   315  // StorageAccount name is too long, forces error
   316  var testAccAzureRMTemplateDeployment_withError = `
   317    resource "azurerm_resource_group" "test" {
   318      name = "acctestRG-%d"
   319      location = "West US"
   320    }
   321  
   322    output "test" {
   323      value = "${azurerm_template_deployment.test.outputs.testOutput}"
   324    }
   325  
   326    resource "azurerm_template_deployment" "test" {
   327      name = "acctesttemplate-%d"
   328      resource_group_name = "${azurerm_resource_group.test.name}"
   329      template_body = <<DEPLOY
   330  {
   331    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   332    "contentVersion": "1.0.0.0",
   333    "parameters": {
   334      "storageAccountType": {
   335        "type": "string",
   336        "defaultValue": "Standard_LRS",
   337        "allowedValues": [
   338          "Standard_LRS",
   339          "Standard_GRS",
   340          "Standard_ZRS"
   341        ],
   342        "metadata": {
   343          "description": "Storage Account type"
   344        }
   345      }
   346    },
   347    "variables": {
   348      "location": "[resourceGroup().location]",
   349      "storageAccountName": "badStorageAccountNameTooLong",
   350      "apiVersion": "2015-06-15"
   351    },
   352    "resources": [
   353      {
   354        "type": "Microsoft.Storage/storageAccounts",
   355        "name": "[variables('storageAccountName')]",
   356        "apiVersion": "[variables('apiVersion')]",
   357        "location": "[variables('location')]",
   358        "properties": {
   359          "accountType": "[parameters('storageAccountType')]"
   360        }
   361      }
   362    ],
   363    "outputs": {
   364      "testOutput": {
   365        "type": "string",
   366        "value": "Output Value"
   367      }
   368    }
   369  }
   370  DEPLOY
   371      parameters {
   372          storageAccountType = "Standard_GRS"
   373      }
   374      deployment_mode = "Complete"
   375    }
   376  `