github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_sql_server_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/sql"
    11  )
    12  
    13  func TestAccAzureRMSqlServer_basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMSqlServer_basic, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMSqlServerDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMSqlServerExists("azurerm_sql_server.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAzureRMSqlServer_withTags(t *testing.T) {
    33  	ri := acctest.RandInt()
    34  	preConfig := fmt.Sprintf(testAccAzureRMSqlServer_withTags, ri, ri)
    35  	postConfig := fmt.Sprintf(testAccAzureRMSqlServer_withTagsUpdated, ri, ri)
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testCheckAzureRMSqlServerDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: preConfig,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testCheckAzureRMSqlServerExists("azurerm_sql_server.test"),
    46  					resource.TestCheckResourceAttr(
    47  						"azurerm_sql_server.test", "tags.%", "2"),
    48  				),
    49  			},
    50  
    51  			resource.TestStep{
    52  				Config: postConfig,
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testCheckAzureRMSqlServerExists("azurerm_sql_server.test"),
    55  					resource.TestCheckResourceAttr(
    56  						"azurerm_sql_server.test", "tags.%", "1"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func testCheckAzureRMSqlServerExists(name string) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  
    66  		rs, ok := s.RootModule().Resources[name]
    67  		if !ok {
    68  			return fmt.Errorf("Not found: %s", name)
    69  		}
    70  
    71  		conn := testAccProvider.Meta().(*ArmClient).rivieraClient
    72  
    73  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
    74  		readRequest.Command = &sql.GetServer{}
    75  
    76  		readResponse, err := readRequest.Execute()
    77  		if err != nil {
    78  			return fmt.Errorf("Bad: GetServer: %s", err)
    79  		}
    80  		if !readResponse.IsSuccessful() {
    81  			return fmt.Errorf("Bad: GetServer: %s", readResponse.Error)
    82  		}
    83  
    84  		return nil
    85  	}
    86  }
    87  
    88  func testCheckAzureRMSqlServerDestroy(s *terraform.State) error {
    89  	conn := testAccProvider.Meta().(*ArmClient).rivieraClient
    90  
    91  	for _, rs := range s.RootModule().Resources {
    92  		if rs.Type != "azurerm_sql_server" {
    93  			continue
    94  		}
    95  
    96  		readRequest := conn.NewRequestForURI(rs.Primary.ID)
    97  		readRequest.Command = &sql.GetServer{}
    98  
    99  		readResponse, err := readRequest.Execute()
   100  		if err != nil {
   101  			return fmt.Errorf("Bad: GetServer: %s", err)
   102  		}
   103  
   104  		if readResponse.IsSuccessful() {
   105  			return fmt.Errorf("Bad: SQL Server still exists: %s", readResponse.Error)
   106  		}
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  var testAccAzureRMSqlServer_basic = `
   113  resource "azurerm_resource_group" "test" {
   114      name = "acctestRG_%d"
   115      location = "West US"
   116  }
   117  resource "azurerm_sql_server" "test" {
   118      name = "acctestsqlserver%d"
   119      resource_group_name = "${azurerm_resource_group.test.name}"
   120      location = "West US"
   121      version = "12.0"
   122      administrator_login = "mradministrator"
   123      administrator_login_password = "thisIsDog11"
   124  }
   125  `
   126  
   127  var testAccAzureRMSqlServer_withTags = `
   128  resource "azurerm_resource_group" "test" {
   129      name = "acctestRG_%d"
   130      location = "West US"
   131  }
   132  resource "azurerm_sql_server" "test" {
   133      name = "acctestsqlserver%d"
   134      resource_group_name = "${azurerm_resource_group.test.name}"
   135      location = "West US"
   136      version = "12.0"
   137      administrator_login = "mradministrator"
   138      administrator_login_password = "thisIsDog11"
   139  
   140      tags {
   141      	environment = "staging"
   142      	database = "test"
   143      }
   144  }
   145  `
   146  
   147  var testAccAzureRMSqlServer_withTagsUpdated = `
   148  resource "azurerm_resource_group" "test" {
   149      name = "acctestRG_%d"
   150      location = "West US"
   151  }
   152  resource "azurerm_sql_server" "test" {
   153      name = "acctestsqlserver%d"
   154      resource_group_name = "${azurerm_resource_group.test.name}"
   155      location = "West US"
   156      version = "12.0"
   157      administrator_login = "mradministrator"
   158      administrator_login_password = "thisIsDog11"
   159  
   160      tags {
   161      	environment = "production"
   162      }
   163  }
   164  `