github.com/jonasi/terraform@v0.6.10-0.20160125170522-e865c342cc1f/builtin/providers/azurerm/resource_arm_public_ip_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 TestResourceAzureRMPublicIpAllocation_validation(t *testing.T) {
    14  	cases := []struct {
    15  		Value    string
    16  		ErrCount int
    17  	}{
    18  		{
    19  			Value:    "Random",
    20  			ErrCount: 1,
    21  		},
    22  		{
    23  			Value:    "Static",
    24  			ErrCount: 0,
    25  		},
    26  		{
    27  			Value:    "Dynamic",
    28  			ErrCount: 0,
    29  		},
    30  		{
    31  			Value:    "STATIC",
    32  			ErrCount: 0,
    33  		},
    34  		{
    35  			Value:    "static",
    36  			ErrCount: 0,
    37  		},
    38  	}
    39  
    40  	for _, tc := range cases {
    41  		_, errors := validatePublicIpAllocation(tc.Value, "azurerm_public_ip")
    42  
    43  		if len(errors) != tc.ErrCount {
    44  			t.Fatalf("Expected the Azure RM Public IP allocation to trigger a validation error")
    45  		}
    46  	}
    47  }
    48  
    49  func TestResourceAzureRMPublicIpDomainNameLabel_validation(t *testing.T) {
    50  	cases := []struct {
    51  		Value    string
    52  		ErrCount int
    53  	}{
    54  		{
    55  			Value:    "tEsting123",
    56  			ErrCount: 1,
    57  		},
    58  		{
    59  			Value:    "testing123!",
    60  			ErrCount: 1,
    61  		},
    62  		{
    63  			Value:    "testing123-",
    64  			ErrCount: 1,
    65  		},
    66  		{
    67  			Value:    acctest.RandString(80),
    68  			ErrCount: 1,
    69  		},
    70  	}
    71  
    72  	for _, tc := range cases {
    73  		_, errors := validatePublicIpDomainNameLabel(tc.Value, "azurerm_public_ip")
    74  
    75  		if len(errors) != tc.ErrCount {
    76  			t.Fatalf("Expected the Azure RM Public IP Domain Name Label to trigger a validation error")
    77  		}
    78  	}
    79  }
    80  
    81  func TestAccAzureRMPublicIpStatic_basic(t *testing.T) {
    82  
    83  	resource.Test(t, resource.TestCase{
    84  		PreCheck:     func() { testAccPreCheck(t) },
    85  		Providers:    testAccProviders,
    86  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
    87  		Steps: []resource.TestStep{
    88  			resource.TestStep{
    89  				Config: testAccAzureRMVPublicIpStatic_basic,
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
    92  				),
    93  			},
    94  		},
    95  	})
    96  }
    97  
    98  func TestAccAzureRMPublicIpStatic_withTags(t *testing.T) {
    99  
   100  	resource.Test(t, resource.TestCase{
   101  		PreCheck:     func() { testAccPreCheck(t) },
   102  		Providers:    testAccProviders,
   103  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
   104  		Steps: []resource.TestStep{
   105  			resource.TestStep{
   106  				Config: testAccAzureRMVPublicIpStatic_withTags,
   107  				Check: resource.ComposeTestCheckFunc(
   108  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   109  					resource.TestCheckResourceAttr(
   110  						"azurerm_public_ip.test", "tags.#", "2"),
   111  					resource.TestCheckResourceAttr(
   112  						"azurerm_public_ip.test", "tags.environment", "Production"),
   113  					resource.TestCheckResourceAttr(
   114  						"azurerm_public_ip.test", "tags.cost_center", "MSFT"),
   115  				),
   116  			},
   117  
   118  			resource.TestStep{
   119  				Config: testAccAzureRMVPublicIpStatic_withTagsUpdate,
   120  				Check: resource.ComposeTestCheckFunc(
   121  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   122  					resource.TestCheckResourceAttr(
   123  						"azurerm_public_ip.test", "tags.#", "1"),
   124  					resource.TestCheckResourceAttr(
   125  						"azurerm_public_ip.test", "tags.environment", "staging"),
   126  				),
   127  			},
   128  		},
   129  	})
   130  }
   131  
   132  func TestAccAzureRMPublicIpStatic_update(t *testing.T) {
   133  
   134  	resource.Test(t, resource.TestCase{
   135  		PreCheck:     func() { testAccPreCheck(t) },
   136  		Providers:    testAccProviders,
   137  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
   138  		Steps: []resource.TestStep{
   139  			resource.TestStep{
   140  				Config: testAccAzureRMVPublicIpStatic_basic,
   141  				Check: resource.ComposeTestCheckFunc(
   142  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   143  				),
   144  			},
   145  
   146  			resource.TestStep{
   147  				Config: testAccAzureRMVPublicIpStatic_update,
   148  				Check: resource.ComposeTestCheckFunc(
   149  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   150  					resource.TestCheckResourceAttr(
   151  						"azurerm_public_ip.test", "domain_name_label", "mylabel01"),
   152  				),
   153  			},
   154  		},
   155  	})
   156  }
   157  
   158  func TestAccAzureRMPublicIpDynamic_basic(t *testing.T) {
   159  
   160  	resource.Test(t, resource.TestCase{
   161  		PreCheck:     func() { testAccPreCheck(t) },
   162  		Providers:    testAccProviders,
   163  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
   164  		Steps: []resource.TestStep{
   165  			resource.TestStep{
   166  				Config: testAccAzureRMVPublicIpDynamic_basic,
   167  				Check: resource.ComposeTestCheckFunc(
   168  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   169  				),
   170  			},
   171  		},
   172  	})
   173  }
   174  
   175  func testCheckAzureRMPublicIpExists(name string) resource.TestCheckFunc {
   176  	return func(s *terraform.State) error {
   177  		// Ensure we have enough information in state to look up in API
   178  		rs, ok := s.RootModule().Resources[name]
   179  		if !ok {
   180  			return fmt.Errorf("Not found: %s", name)
   181  		}
   182  
   183  		availSetName := rs.Primary.Attributes["name"]
   184  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   185  		if !hasResourceGroup {
   186  			return fmt.Errorf("Bad: no resource group found in state for public ip: %s", availSetName)
   187  		}
   188  
   189  		conn := testAccProvider.Meta().(*ArmClient).publicIPClient
   190  
   191  		resp, err := conn.Get(resourceGroup, availSetName, "")
   192  		if err != nil {
   193  			return fmt.Errorf("Bad: Get on publicIPClient: %s", err)
   194  		}
   195  
   196  		if resp.StatusCode == http.StatusNotFound {
   197  			return fmt.Errorf("Bad: Public IP %q (resource group: %q) does not exist", name, resourceGroup)
   198  		}
   199  
   200  		return nil
   201  	}
   202  }
   203  
   204  func testCheckAzureRMPublicIpDestroy(s *terraform.State) error {
   205  	conn := testAccProvider.Meta().(*ArmClient).publicIPClient
   206  
   207  	for _, rs := range s.RootModule().Resources {
   208  		if rs.Type != "azurerm_public_ip" {
   209  			continue
   210  		}
   211  
   212  		name := rs.Primary.Attributes["name"]
   213  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   214  
   215  		resp, err := conn.Get(resourceGroup, name, "")
   216  
   217  		if err != nil {
   218  			return nil
   219  		}
   220  
   221  		if resp.StatusCode != http.StatusNotFound {
   222  			return fmt.Errorf("Public IP still exists:\n%#v", resp.Properties)
   223  		}
   224  	}
   225  
   226  	return nil
   227  }
   228  
   229  var testAccAzureRMVPublicIpStatic_basic = `
   230  resource "azurerm_resource_group" "test" {
   231      name = "acceptanceTestResourceGroup1"
   232      location = "West US"
   233  }
   234  resource "azurerm_public_ip" "test" {
   235      name = "acceptanceTestPublicIp1"
   236      location = "West US"
   237      resource_group_name = "${azurerm_resource_group.test.name}"
   238      public_ip_address_allocation = "static"
   239  }
   240  `
   241  
   242  var testAccAzureRMVPublicIpStatic_update = `
   243  resource "azurerm_resource_group" "test" {
   244      name = "acceptanceTestResourceGroup1"
   245      location = "West US"
   246  }
   247  resource "azurerm_public_ip" "test" {
   248      name = "acceptanceTestPublicIp1"
   249      location = "West US"
   250      resource_group_name = "${azurerm_resource_group.test.name}"
   251      public_ip_address_allocation = "static"
   252      domain_name_label = "mylabel01"
   253  }
   254  `
   255  
   256  var testAccAzureRMVPublicIpDynamic_basic = `
   257  resource "azurerm_resource_group" "test" {
   258      name = "acceptanceTestResourceGroup2"
   259      location = "West US"
   260  }
   261  resource "azurerm_public_ip" "test" {
   262      name = "acceptanceTestPublicIp2"
   263      location = "West US"
   264      resource_group_name = "${azurerm_resource_group.test.name}"
   265      public_ip_address_allocation = "dynamic"
   266  }
   267  `
   268  
   269  var testAccAzureRMVPublicIpStatic_withTags = `
   270  resource "azurerm_resource_group" "test" {
   271      name = "acceptanceTestResourceGroup1"
   272      location = "West US"
   273  }
   274  resource "azurerm_public_ip" "test" {
   275      name = "acceptanceTestPublicIp1"
   276      location = "West US"
   277      resource_group_name = "${azurerm_resource_group.test.name}"
   278      public_ip_address_allocation = "static"
   279  
   280      tags {
   281  	environment = "Production"
   282  	cost_center = "MSFT"
   283      }
   284  }
   285  `
   286  
   287  var testAccAzureRMVPublicIpStatic_withTagsUpdate = `
   288  resource "azurerm_resource_group" "test" {
   289      name = "acceptanceTestResourceGroup1"
   290      location = "West US"
   291  }
   292  resource "azurerm_public_ip" "test" {
   293      name = "acceptanceTestPublicIp1"
   294      location = "West US"
   295      resource_group_name = "${azurerm_resource_group.test.name}"
   296      public_ip_address_allocation = "static"
   297  
   298      tags {
   299  	environment = "staging"
   300      }
   301  }
   302  `