github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_dns_srv_record_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/jen20/riviera/dns"
    11  )
    12  
    13  func TestAccAzureRMDnsSrvRecord_basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMDnsSrvRecord_basic, ri, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMDnsSrvRecordDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMDnsSrvRecordExists("azurerm_dns_srv_record.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAzureRMDnsSrvRecord_updateRecords(t *testing.T) {
    33  	ri := acctest.RandInt()
    34  	preConfig := fmt.Sprintf(testAccAzureRMDnsSrvRecord_basic, ri, ri, ri)
    35  	postConfig := fmt.Sprintf(testAccAzureRMDnsSrvRecord_updateRecords, ri, ri, ri)
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testCheckAzureRMDnsSrvRecordDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: preConfig,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testCheckAzureRMDnsSrvRecordExists("azurerm_dns_srv_record.test"),
    46  					resource.TestCheckResourceAttr(
    47  						"azurerm_dns_srv_record.test", "record.#", "2"),
    48  				),
    49  			},
    50  
    51  			resource.TestStep{
    52  				Config: postConfig,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testCheckAzureRMDnsSrvRecordExists("azurerm_dns_srv_record.test"),
    55  					resource.TestCheckResourceAttr(
    56  						"azurerm_dns_srv_record.test", "record.#", "3"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccAzureRMDnsSrvRecord_withTags(t *testing.T) {
    64  	ri := acctest.RandInt()
    65  	preConfig := fmt.Sprintf(testAccAzureRMDnsSrvRecord_withTags, ri, ri, ri)
    66  	postConfig := fmt.Sprintf(testAccAzureRMDnsSrvRecord_withTagsUpdate, ri, ri, ri)
    67  
    68  	resource.Test(t, resource.TestCase{
    69  		PreCheck:     func() { testAccPreCheck(t) },
    70  		Providers:    testAccProviders,
    71  		CheckDestroy: testCheckAzureRMDnsSrvRecordDestroy,
    72  		Steps: []resource.TestStep{
    73  			resource.TestStep{
    74  				Config: preConfig,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testCheckAzureRMDnsSrvRecordExists("azurerm_dns_srv_record.test"),
    77  					resource.TestCheckResourceAttr(
    78  						"azurerm_dns_srv_record.test", "tags.%", "2"),
    79  				),
    80  			},
    81  
    82  			resource.TestStep{
    83  				Config: postConfig,
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testCheckAzureRMDnsSrvRecordExists("azurerm_dns_srv_record.test"),
    86  					resource.TestCheckResourceAttr(
    87  						"azurerm_dns_srv_record.test", "tags.%", "1"),
    88  				),
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func testCheckAzureRMDnsSrvRecordExists(name string) resource.TestCheckFunc {
    95  	return func(s *terraform.State) error {
    96  		// Ensure we have enough information in state to look up in API
    97  		rs, ok := s.RootModule().Resources[name]
    98  		if !ok {
    99  			return fmt.Errorf("Not found: %s", name)
   100  		}
   101  
   102  		conn := testAccProvider.Meta().(*ArmClient).rivieraClient
   103  
   104  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
   105  		readRequest.Command = &dns.GetSRVRecordSet{}
   106  
   107  		readResponse, err := readRequest.Execute()
   108  		if err != nil {
   109  			return fmt.Errorf("Bad: GetSRVRecordSet: %s", err)
   110  		}
   111  		if !readResponse.IsSuccessful() {
   112  			return fmt.Errorf("Bad: GetSRVRecordSet: %s", readResponse.Error)
   113  		}
   114  
   115  		return nil
   116  	}
   117  }
   118  
   119  func testCheckAzureRMDnsSrvRecordDestroy(s *terraform.State) error {
   120  	conn := testAccProvider.Meta().(*ArmClient).rivieraClient
   121  
   122  	for _, rs := range s.RootModule().Resources {
   123  		if rs.Type != "azurerm_dns_srv_record" {
   124  			continue
   125  		}
   126  
   127  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
   128  		readRequest.Command = &dns.GetSRVRecordSet{}
   129  
   130  		readResponse, err := readRequest.Execute()
   131  		if err != nil {
   132  			return fmt.Errorf("Bad: GetSRVRecordSet: %s", err)
   133  		}
   134  
   135  		if readResponse.IsSuccessful() {
   136  			return fmt.Errorf("Bad: DNS SRV Record still exists: %s", readResponse.Error)
   137  		}
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  var testAccAzureRMDnsSrvRecord_basic = `
   144  resource "azurerm_resource_group" "test" {
   145      name = "acctestRG_%d"
   146      location = "West US"
   147  }
   148  resource "azurerm_dns_zone" "test" {
   149      name = "acctestzone%d.com"
   150      resource_group_name = "${azurerm_resource_group.test.name}"
   151  }
   152  
   153  resource "azurerm_dns_srv_record" "test" {
   154      name = "myarecord%d"
   155      resource_group_name = "${azurerm_resource_group.test.name}"
   156      zone_name = "${azurerm_dns_zone.test.name}"
   157      ttl = "300"
   158  
   159      record {
   160  	priority = 1
   161  	weight = 5
   162  	port = 8080
   163  	target = "target1.contoso.com"
   164      }
   165  
   166      record {
   167  	priority = 2
   168  	weight = 25
   169  	port = 8080
   170  	target = "target2.contoso.com"
   171      }
   172  }
   173  `
   174  
   175  var testAccAzureRMDnsSrvRecord_updateRecords = `
   176  resource "azurerm_resource_group" "test" {
   177      name = "acctestRG_%d"
   178      location = "West US"
   179  }
   180  resource "azurerm_dns_zone" "test" {
   181      name = "acctestzone%d.com"
   182      resource_group_name = "${azurerm_resource_group.test.name}"
   183  }
   184  
   185  resource "azurerm_dns_srv_record" "test" {
   186      name = "myarecord%d"
   187      resource_group_name = "${azurerm_resource_group.test.name}"
   188      zone_name = "${azurerm_dns_zone.test.name}"
   189      ttl = "300"
   190  
   191      record {
   192  	priority = 1
   193  	weight = 5
   194  	port = 8080
   195  	target = "target1.contoso.com"
   196      }
   197  
   198      record {
   199  	priority = 2
   200  	weight = 25
   201  	port = 8080
   202  	target = "target2.contoso.com"
   203      }
   204  
   205      record {
   206  	priority = 3
   207  	weight = 100
   208  	port = 8080
   209  	target = "target3.contoso.com"
   210      }
   211  }
   212  `
   213  
   214  var testAccAzureRMDnsSrvRecord_withTags = `
   215  resource "azurerm_resource_group" "test" {
   216      name = "acctestRG_%d"
   217      location = "West US"
   218  }
   219  resource "azurerm_dns_zone" "test" {
   220      name = "acctestzone%d.com"
   221      resource_group_name = "${azurerm_resource_group.test.name}"
   222  }
   223  
   224  resource "azurerm_dns_srv_record" "test" {
   225      name = "myarecord%d"
   226      resource_group_name = "${azurerm_resource_group.test.name}"
   227      zone_name = "${azurerm_dns_zone.test.name}"
   228      ttl = "300"
   229  
   230      record {
   231  	priority = 1
   232  	weight = 5
   233  	port = 8080
   234  	target = "target1.contoso.com"
   235      }
   236  
   237      record {
   238  	priority = 2
   239  	weight = 25
   240  	port = 8080
   241  	target = "target2.contoso.com"
   242      }
   243  
   244      tags {
   245  	environment = "Production"
   246  	cost_center = "MSFT"
   247      }
   248  }
   249  `
   250  
   251  var testAccAzureRMDnsSrvRecord_withTagsUpdate = `
   252  resource "azurerm_resource_group" "test" {
   253      name = "acctestRG_%d"
   254      location = "West US"
   255  }
   256  resource "azurerm_dns_zone" "test" {
   257      name = "acctestzone%d.com"
   258      resource_group_name = "${azurerm_resource_group.test.name}"
   259  }
   260  
   261  resource "azurerm_dns_srv_record" "test" {
   262      name = "myarecord%d"
   263      resource_group_name = "${azurerm_resource_group.test.name}"
   264      zone_name = "${azurerm_dns_zone.test.name}"
   265      ttl = "300"
   266  
   267      record {
   268  	priority = 1
   269  	weight = 5
   270  	port = 8080
   271  	target = "target1.contoso.com"
   272      }
   273  
   274      record {
   275  	priority = 2
   276  	weight = 25
   277  	port = 8080
   278  	target = "target2.contoso.com"
   279      }
   280  
   281      tags {
   282  	environment = "staging"
   283      }
   284  }
   285  `