github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azure/resource_azure_sql_database_service_test.go (about) 1 package azure 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccAzureSqlDatabaseServiceBasic(t *testing.T) { 13 name := "azure_sql_database_service.foo" 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckAzureSqlDatabaseServiceDeleted, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccAzureSqlDatabaseServiceConfigBasic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccAzureSqlDatabaseServerGetName, 24 testAccCheckAzureSqlDatabaseServiceExists(name), 25 resource.TestCheckResourceAttr(name, "name", "terraform-testing-db"), 26 resource.TestCheckResourceAttrPtr(name, "database_server_name", 27 testAccAzureSqlServerName), 28 resource.TestCheckResourceAttr(name, "collation", 29 "SQL_Latin1_General_CP1_CI_AS"), 30 resource.TestCheckResourceAttr(name, "edition", "Standard"), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAzureSqlDatabaseServiceAdvanced(t *testing.T) { 38 name := "azure_sql_database_service.foo" 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccCheckAzureSqlDatabaseServiceDeleted, 44 Steps: []resource.TestStep{ 45 resource.TestStep{ 46 Config: testAccAzureSqlDatabaseServiceConfigAdvanced, 47 Check: resource.ComposeTestCheckFunc( 48 testAccAzureSqlDatabaseServerGetName, 49 testAccCheckAzureSqlDatabaseServiceExists(name), 50 resource.TestCheckResourceAttr(name, "name", "terraform-testing-db"), 51 resource.TestCheckResourceAttrPtr(name, "database_server_name", 52 testAccAzureSqlServerName), 53 resource.TestCheckResourceAttr(name, "edition", "Premium"), 54 resource.TestCheckResourceAttr(name, "collation", 55 "Arabic_BIN"), 56 resource.TestCheckResourceAttr(name, "max_size_bytes", "10737418240"), 57 resource.TestCheckResourceAttr(name, "service_level_id", 58 "7203483a-c4fb-4304-9e9f-17c71c904f5d"), 59 ), 60 }, 61 }, 62 }) 63 } 64 65 func TestAccAzureSqlDatabaseServiceUpdate(t *testing.T) { 66 name := "azure_sql_database_service.foo" 67 68 resource.Test(t, resource.TestCase{ 69 PreCheck: func() { testAccPreCheck(t) }, 70 Providers: testAccProviders, 71 CheckDestroy: testAccCheckAzureSqlDatabaseServiceDeleted, 72 Steps: []resource.TestStep{ 73 resource.TestStep{ 74 Config: testAccAzureSqlDatabaseServiceConfigAdvanced, 75 Check: resource.ComposeTestCheckFunc( 76 testAccAzureSqlDatabaseServerGetName, 77 testAccCheckAzureSqlDatabaseServiceExists(name), 78 resource.TestCheckResourceAttr(name, "name", "terraform-testing-db"), 79 resource.TestCheckResourceAttrPtr(name, "database_server_name", 80 testAccAzureSqlServerName), 81 resource.TestCheckResourceAttr(name, "edition", "Premium"), 82 resource.TestCheckResourceAttr(name, "collation", 83 "Arabic_BIN"), 84 resource.TestCheckResourceAttr(name, "max_size_bytes", "10737418240"), 85 resource.TestCheckResourceAttr(name, "service_level_id", 86 "7203483a-c4fb-4304-9e9f-17c71c904f5d"), 87 ), 88 }, 89 resource.TestStep{ 90 Config: testAccAzureSqlDatabaseServiceConfigUpdate, 91 Check: resource.ComposeTestCheckFunc( 92 testAccAzureSqlDatabaseServerGetName, 93 testAccCheckAzureSqlDatabaseServiceExists(name), 94 resource.TestCheckResourceAttr(name, "name", 95 "terraform-testing-db-renamed"), 96 resource.TestCheckResourceAttrPtr(name, "database_server_name", 97 testAccAzureSqlServerName), 98 resource.TestCheckResourceAttr(name, "edition", "Standard"), 99 resource.TestCheckResourceAttr(name, "collation", 100 "SQL_Latin1_General_CP1_CI_AS"), 101 resource.TestCheckResourceAttr(name, "max_size_bytes", "5368709120"), 102 resource.TestCheckResourceAttr(name, "service_level_id", 103 "f1173c43-91bd-4aaa-973c-54e79e15235b"), 104 ), 105 }, 106 }, 107 }) 108 } 109 110 func testAccCheckAzureSqlDatabaseServiceExists(name string) resource.TestCheckFunc { 111 return func(s *terraform.State) error { 112 resource, ok := s.RootModule().Resources[name] 113 if !ok { 114 return fmt.Errorf("SQL Service %s doesn't exist.", name) 115 } 116 117 if resource.Primary.ID == "" { 118 return fmt.Errorf("SQL Service %s resource ID not set.", name) 119 } 120 121 sqlClient := testAccProvider.Meta().(*Client).sqlClient 122 dbs, err := sqlClient.ListDatabases(*testAccAzureSqlServerName) 123 if err != nil { 124 return fmt.Errorf("Error issuing Azure SQL Service list request: %s", err) 125 } 126 127 for _, srv := range dbs.ServiceResources { 128 if srv.Name == resource.Primary.ID { 129 return nil 130 } 131 } 132 133 return fmt.Errorf("SQL Service %s doesn't exist.", name) 134 } 135 } 136 137 func testAccCheckAzureSqlDatabaseServiceDeleted(s *terraform.State) error { 138 for _, resource := range s.RootModule().Resources { 139 if resource.Type != "azure_sql_database_service" { 140 continue 141 } 142 143 if resource.Primary.ID == "" { 144 return fmt.Errorf("SQL Service resource ID not set.") 145 } 146 147 sqlClient := testAccProvider.Meta().(*Client).sqlClient 148 dbs, err := sqlClient.ListDatabases(*testAccAzureSqlServerName) 149 if err != nil { 150 // ¯\_(ツ)_/¯ 151 if strings.Contains(err.Error(), "Cannot open server") { 152 return nil 153 } 154 return fmt.Errorf("Error issuing Azure SQL Service list request: %s", err) 155 } 156 157 for _, srv := range dbs.ServiceResources { 158 if srv.Name == resource.Primary.ID { 159 return fmt.Errorf("SQL Service %s still exists.", resource.Primary.ID) 160 } 161 } 162 } 163 164 return nil 165 } 166 167 const testAccAzureSqlDatabaseServiceConfigBasic = testAccAzureSqlDatabaseServerConfig + ` 168 resource "azure_sql_database_service" "foo" { 169 name = "terraform-testing-db" 170 database_server_name = "${azure_sql_database_server.foo.name}" 171 edition = "Standard" 172 } 173 ` 174 175 const testAccAzureSqlDatabaseServiceConfigAdvanced = testAccAzureSqlDatabaseServerConfig + ` 176 resource "azure_sql_database_service" "foo" { 177 name = "terraform-testing-db" 178 database_server_name = "${azure_sql_database_server.foo.name}" 179 edition = "Premium" 180 collation = "Arabic_BIN" 181 max_size_bytes = "10737418240" 182 service_level_id = "7203483a-c4fb-4304-9e9f-17c71c904f5d" 183 } 184 ` 185 186 const testAccAzureSqlDatabaseServiceConfigUpdate = testAccAzureSqlDatabaseServerConfig + ` 187 resource "azure_sql_database_service" "foo" { 188 name = "terraform-testing-db-renamed" 189 database_server_name = "${azure_sql_database_server.foo.name}" 190 edition = "Standard" 191 collation = "SQL_Latin1_General_CP1_CI_AS" 192 max_size_bytes = "5368709120" 193 service_level_id = "f1173c43-91bd-4aaa-973c-54e79e15235b" 194 } 195 `