github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  	ri := acctest.RandInt()
    84  	config := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri)
    85  
    86  	resource.Test(t, resource.TestCase{
    87  		PreCheck:     func() { testAccPreCheck(t) },
    88  		Providers:    testAccProviders,
    89  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
    90  		Steps: []resource.TestStep{
    91  			resource.TestStep{
    92  				Config: config,
    93  				Check: resource.ComposeTestCheckFunc(
    94  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
    95  				),
    96  			},
    97  		},
    98  	})
    99  }
   100  
   101  func TestAccAzureRMPublicIpStatic_idleTimeout(t *testing.T) {
   102  
   103  	ri := acctest.RandInt()
   104  	config := fmt.Sprintf(testAccAzureRMVPublicIpStatic_idleTimeout, ri, ri)
   105  
   106  	resource.Test(t, resource.TestCase{
   107  		PreCheck:     func() { testAccPreCheck(t) },
   108  		Providers:    testAccProviders,
   109  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
   110  		Steps: []resource.TestStep{
   111  			resource.TestStep{
   112  				Config: config,
   113  				Check: resource.ComposeTestCheckFunc(
   114  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   115  					resource.TestCheckResourceAttr(
   116  						"azurerm_public_ip.test",
   117  						"idle_timeout_in_minutes",
   118  						"30",
   119  					),
   120  				),
   121  			},
   122  		},
   123  	})
   124  }
   125  
   126  func TestAccAzureRMPublicIpStatic_withTags(t *testing.T) {
   127  
   128  	ri := acctest.RandInt()
   129  	preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTags, ri, ri)
   130  	postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTagsUpdate, ri, ri)
   131  
   132  	resource.Test(t, resource.TestCase{
   133  		PreCheck:     func() { testAccPreCheck(t) },
   134  		Providers:    testAccProviders,
   135  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
   136  		Steps: []resource.TestStep{
   137  			resource.TestStep{
   138  				Config: preConfig,
   139  				Check: resource.ComposeTestCheckFunc(
   140  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   141  					resource.TestCheckResourceAttr(
   142  						"azurerm_public_ip.test", "tags.%", "2"),
   143  					resource.TestCheckResourceAttr(
   144  						"azurerm_public_ip.test", "tags.environment", "Production"),
   145  					resource.TestCheckResourceAttr(
   146  						"azurerm_public_ip.test", "tags.cost_center", "MSFT"),
   147  				),
   148  			},
   149  
   150  			resource.TestStep{
   151  				Config: postConfig,
   152  				Check: resource.ComposeTestCheckFunc(
   153  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   154  					resource.TestCheckResourceAttr(
   155  						"azurerm_public_ip.test", "tags.%", "1"),
   156  					resource.TestCheckResourceAttr(
   157  						"azurerm_public_ip.test", "tags.environment", "staging"),
   158  				),
   159  			},
   160  		},
   161  	})
   162  }
   163  
   164  func TestAccAzureRMPublicIpStatic_update(t *testing.T) {
   165  
   166  	ri := acctest.RandInt()
   167  	preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri)
   168  	postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_update, ri, ri)
   169  
   170  	resource.Test(t, resource.TestCase{
   171  		PreCheck:     func() { testAccPreCheck(t) },
   172  		Providers:    testAccProviders,
   173  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
   174  		Steps: []resource.TestStep{
   175  			resource.TestStep{
   176  				Config: preConfig,
   177  				Check: resource.ComposeTestCheckFunc(
   178  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   179  				),
   180  			},
   181  
   182  			resource.TestStep{
   183  				Config: postConfig,
   184  				Check: resource.ComposeTestCheckFunc(
   185  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   186  					resource.TestCheckResourceAttr(
   187  						"azurerm_public_ip.test", "domain_name_label", "mylabel01"),
   188  				),
   189  			},
   190  		},
   191  	})
   192  }
   193  
   194  func TestAccAzureRMPublicIpDynamic_basic(t *testing.T) {
   195  
   196  	ri := acctest.RandInt()
   197  	config := fmt.Sprintf(testAccAzureRMVPublicIpDynamic_basic, ri, ri)
   198  
   199  	resource.Test(t, resource.TestCase{
   200  		PreCheck:     func() { testAccPreCheck(t) },
   201  		Providers:    testAccProviders,
   202  		CheckDestroy: testCheckAzureRMPublicIpDestroy,
   203  		Steps: []resource.TestStep{
   204  			resource.TestStep{
   205  				Config: config,
   206  				Check: resource.ComposeTestCheckFunc(
   207  					testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
   208  				),
   209  			},
   210  		},
   211  	})
   212  }
   213  
   214  func testCheckAzureRMPublicIpExists(name string) resource.TestCheckFunc {
   215  	return func(s *terraform.State) error {
   216  		// Ensure we have enough information in state to look up in API
   217  		rs, ok := s.RootModule().Resources[name]
   218  		if !ok {
   219  			return fmt.Errorf("Not found: %s", name)
   220  		}
   221  
   222  		availSetName := rs.Primary.Attributes["name"]
   223  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   224  		if !hasResourceGroup {
   225  			return fmt.Errorf("Bad: no resource group found in state for public ip: %s", availSetName)
   226  		}
   227  
   228  		conn := testAccProvider.Meta().(*ArmClient).publicIPClient
   229  
   230  		resp, err := conn.Get(resourceGroup, availSetName, "")
   231  		if err != nil {
   232  			return fmt.Errorf("Bad: Get on publicIPClient: %s", err)
   233  		}
   234  
   235  		if resp.StatusCode == http.StatusNotFound {
   236  			return fmt.Errorf("Bad: Public IP %q (resource group: %q) does not exist", name, resourceGroup)
   237  		}
   238  
   239  		return nil
   240  	}
   241  }
   242  
   243  func testCheckAzureRMPublicIpDestroy(s *terraform.State) error {
   244  	conn := testAccProvider.Meta().(*ArmClient).publicIPClient
   245  
   246  	for _, rs := range s.RootModule().Resources {
   247  		if rs.Type != "azurerm_public_ip" {
   248  			continue
   249  		}
   250  
   251  		name := rs.Primary.Attributes["name"]
   252  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   253  
   254  		resp, err := conn.Get(resourceGroup, name, "")
   255  
   256  		if err != nil {
   257  			return nil
   258  		}
   259  
   260  		if resp.StatusCode != http.StatusNotFound {
   261  			return fmt.Errorf("Public IP still exists:\n%#v", resp.Properties)
   262  		}
   263  	}
   264  
   265  	return nil
   266  }
   267  
   268  var testAccAzureRMVPublicIpStatic_basic = `
   269  resource "azurerm_resource_group" "test" {
   270      name = "acctestrg-%d"
   271      location = "West US"
   272  }
   273  resource "azurerm_public_ip" "test" {
   274      name = "acctestpublicip-%d"
   275      location = "West US"
   276      resource_group_name = "${azurerm_resource_group.test.name}"
   277      public_ip_address_allocation = "static"
   278  }
   279  `
   280  
   281  var testAccAzureRMVPublicIpStatic_update = `
   282  resource "azurerm_resource_group" "test" {
   283      name = "acctestrg-%d"
   284      location = "West US"
   285  }
   286  resource "azurerm_public_ip" "test" {
   287      name = "acctestpublicip-%d"
   288      location = "West US"
   289      resource_group_name = "${azurerm_resource_group.test.name}"
   290      public_ip_address_allocation = "static"
   291      domain_name_label = "mylabel01"
   292  }
   293  `
   294  
   295  var testAccAzureRMVPublicIpStatic_idleTimeout = `
   296  resource "azurerm_resource_group" "test" {
   297      name = "acctestrg-%d"
   298      location = "West US"
   299  }
   300  resource "azurerm_public_ip" "test" {
   301      name = "acctestpublicip-%d"
   302      location = "West US"
   303      resource_group_name = "${azurerm_resource_group.test.name}"
   304      public_ip_address_allocation = "static"
   305      idle_timeout_in_minutes = 30
   306  }
   307  `
   308  
   309  var testAccAzureRMVPublicIpDynamic_basic = `
   310  resource "azurerm_resource_group" "test" {
   311      name = "acctestrg-%d"
   312      location = "West US"
   313  }
   314  resource "azurerm_public_ip" "test" {
   315      name = "acctestpublicip-%d"
   316      location = "West US"
   317      resource_group_name = "${azurerm_resource_group.test.name}"
   318      public_ip_address_allocation = "dynamic"
   319  }
   320  `
   321  
   322  var testAccAzureRMVPublicIpStatic_withTags = `
   323  resource "azurerm_resource_group" "test" {
   324      name = "acctestrg-%d"
   325      location = "West US"
   326  }
   327  resource "azurerm_public_ip" "test" {
   328      name = "acctestpublicip-%d"
   329      location = "West US"
   330      resource_group_name = "${azurerm_resource_group.test.name}"
   331      public_ip_address_allocation = "static"
   332  
   333      tags {
   334  	environment = "Production"
   335  	cost_center = "MSFT"
   336      }
   337  }
   338  `
   339  
   340  var testAccAzureRMVPublicIpStatic_withTagsUpdate = `
   341  resource "azurerm_resource_group" "test" {
   342      name = "acctestrg-%d"
   343      location = "West US"
   344  }
   345  resource "azurerm_public_ip" "test" {
   346      name = "acctestpublicip-%d"
   347      location = "West US"
   348      resource_group_name = "${azurerm_resource_group.test.name}"
   349      public_ip_address_allocation = "static"
   350  
   351      tags {
   352  	environment = "staging"
   353      }
   354  }
   355  `