github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_sql_database_instance_test.go (about)

     1  package google
     2  
     3  /**
     4   * Note! You must run these tests once at a time. Google Cloud SQL does
     5   * not allow you to reuse a database for a short time after you reserved it,
     6   * and for this reason the tests will fail if the same config is used serveral
     7   * times in short succession.
     8   */
     9  
    10  import (
    11  	"fmt"
    12  	"strconv"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/hashicorp/terraform/helper/acctest"
    17  	"github.com/hashicorp/terraform/helper/resource"
    18  	"github.com/hashicorp/terraform/terraform"
    19  
    20  	"google.golang.org/api/sqladmin/v1beta4"
    21  )
    22  
    23  func TestAccGoogleSqlDatabaseInstance_basic(t *testing.T) {
    24  	var instance sqladmin.DatabaseInstance
    25  	databaseID := acctest.RandInt()
    26  
    27  	resource.Test(t, resource.TestCase{
    28  		PreCheck:     func() { testAccPreCheck(t) },
    29  		Providers:    testAccProviders,
    30  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
    31  		Steps: []resource.TestStep{
    32  			resource.TestStep{
    33  				Config: fmt.Sprintf(
    34  					testGoogleSqlDatabaseInstance_basic, databaseID),
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckGoogleSqlDatabaseInstanceExists(
    37  						"google_sql_database_instance.instance", &instance),
    38  					testAccCheckGoogleSqlDatabaseInstanceEquals(
    39  						"google_sql_database_instance.instance", &instance),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func TestAccGoogleSqlDatabaseInstance_basic2(t *testing.T) {
    47  	var instance sqladmin.DatabaseInstance
    48  
    49  	resource.Test(t, resource.TestCase{
    50  		PreCheck:     func() { testAccPreCheck(t) },
    51  		Providers:    testAccProviders,
    52  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
    53  		Steps: []resource.TestStep{
    54  			resource.TestStep{
    55  				Config: testGoogleSqlDatabaseInstance_basic2,
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckGoogleSqlDatabaseInstanceExists(
    58  						"google_sql_database_instance.instance", &instance),
    59  					testAccCheckGoogleSqlDatabaseInstanceEquals(
    60  						"google_sql_database_instance.instance", &instance),
    61  				),
    62  			},
    63  		},
    64  	})
    65  }
    66  
    67  func TestAccGoogleSqlDatabaseInstance_basic3(t *testing.T) {
    68  	var instance sqladmin.DatabaseInstance
    69  	databaseID := acctest.RandInt()
    70  
    71  	resource.Test(t, resource.TestCase{
    72  		PreCheck:     func() { testAccPreCheck(t) },
    73  		Providers:    testAccProviders,
    74  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
    75  		Steps: []resource.TestStep{
    76  			resource.TestStep{
    77  				Config: fmt.Sprintf(
    78  					testGoogleSqlDatabaseInstance_basic3, databaseID),
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testAccCheckGoogleSqlDatabaseInstanceExists(
    81  						"google_sql_database_instance.instance", &instance),
    82  					testAccCheckGoogleSqlDatabaseInstanceEquals(
    83  						"google_sql_database_instance.instance", &instance),
    84  					testAccCheckGoogleSqlDatabaseRootUserDoesNotExist(
    85  						&instance),
    86  				),
    87  			},
    88  		},
    89  	})
    90  }
    91  func TestAccGoogleSqlDatabaseInstance_settings_basic(t *testing.T) {
    92  	var instance sqladmin.DatabaseInstance
    93  	databaseID := acctest.RandInt()
    94  
    95  	resource.Test(t, resource.TestCase{
    96  		PreCheck:     func() { testAccPreCheck(t) },
    97  		Providers:    testAccProviders,
    98  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
    99  		Steps: []resource.TestStep{
   100  			resource.TestStep{
   101  				Config: fmt.Sprintf(
   102  					testGoogleSqlDatabaseInstance_settings, databaseID),
   103  				Check: resource.ComposeTestCheckFunc(
   104  					testAccCheckGoogleSqlDatabaseInstanceExists(
   105  						"google_sql_database_instance.instance", &instance),
   106  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   107  						"google_sql_database_instance.instance", &instance),
   108  				),
   109  			},
   110  		},
   111  	})
   112  }
   113  
   114  func TestAccGoogleSqlDatabaseInstance_slave(t *testing.T) {
   115  	var instance sqladmin.DatabaseInstance
   116  	masterID := acctest.RandInt()
   117  	slaveID := acctest.RandInt()
   118  
   119  	resource.Test(t, resource.TestCase{
   120  		PreCheck:     func() { testAccPreCheck(t) },
   121  		Providers:    testAccProviders,
   122  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   123  		Steps: []resource.TestStep{
   124  			resource.TestStep{
   125  				Config: fmt.Sprintf(
   126  					testGoogleSqlDatabaseInstance_slave, masterID, slaveID),
   127  				Check: resource.ComposeTestCheckFunc(
   128  					testAccCheckGoogleSqlDatabaseInstanceExists(
   129  						"google_sql_database_instance.instance_master", &instance),
   130  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   131  						"google_sql_database_instance.instance_master", &instance),
   132  					testAccCheckGoogleSqlDatabaseInstanceExists(
   133  						"google_sql_database_instance.instance_slave", &instance),
   134  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   135  						"google_sql_database_instance.instance_slave", &instance),
   136  				),
   137  			},
   138  		},
   139  	})
   140  }
   141  
   142  func TestAccGoogleSqlDatabaseInstance_diskspecs(t *testing.T) {
   143  	var instance sqladmin.DatabaseInstance
   144  	masterID := acctest.RandInt()
   145  
   146  	resource.Test(t, resource.TestCase{
   147  		PreCheck:     func() { testAccPreCheck(t) },
   148  		Providers:    testAccProviders,
   149  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   150  		Steps: []resource.TestStep{
   151  			resource.TestStep{
   152  				Config: fmt.Sprintf(
   153  					testGoogleSqlDatabaseInstance_diskspecs, masterID),
   154  				Check: resource.ComposeTestCheckFunc(
   155  					testAccCheckGoogleSqlDatabaseInstanceExists(
   156  						"google_sql_database_instance.instance", &instance),
   157  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   158  						"google_sql_database_instance.instance", &instance),
   159  				),
   160  			},
   161  		},
   162  	})
   163  }
   164  
   165  func TestAccGoogleSqlDatabaseInstance_maintenance(t *testing.T) {
   166  	var instance sqladmin.DatabaseInstance
   167  	masterID := acctest.RandInt()
   168  
   169  	resource.Test(t, resource.TestCase{
   170  		PreCheck:     func() { testAccPreCheck(t) },
   171  		Providers:    testAccProviders,
   172  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   173  		Steps: []resource.TestStep{
   174  			resource.TestStep{
   175  				Config: fmt.Sprintf(
   176  					testGoogleSqlDatabaseInstance_maintenance, masterID),
   177  				Check: resource.ComposeTestCheckFunc(
   178  					testAccCheckGoogleSqlDatabaseInstanceExists(
   179  						"google_sql_database_instance.instance", &instance),
   180  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   181  						"google_sql_database_instance.instance", &instance),
   182  				),
   183  			},
   184  		},
   185  	})
   186  }
   187  
   188  func TestAccGoogleSqlDatabaseInstance_settings_upgrade(t *testing.T) {
   189  	var instance sqladmin.DatabaseInstance
   190  	databaseID := acctest.RandInt()
   191  
   192  	resource.Test(t, resource.TestCase{
   193  		PreCheck:     func() { testAccPreCheck(t) },
   194  		Providers:    testAccProviders,
   195  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   196  		Steps: []resource.TestStep{
   197  			resource.TestStep{
   198  				Config: fmt.Sprintf(
   199  					testGoogleSqlDatabaseInstance_basic, databaseID),
   200  				Check: resource.ComposeTestCheckFunc(
   201  					testAccCheckGoogleSqlDatabaseInstanceExists(
   202  						"google_sql_database_instance.instance", &instance),
   203  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   204  						"google_sql_database_instance.instance", &instance),
   205  				),
   206  			},
   207  			resource.TestStep{
   208  				Config: fmt.Sprintf(
   209  					testGoogleSqlDatabaseInstance_settings, databaseID),
   210  				Check: resource.ComposeTestCheckFunc(
   211  					testAccCheckGoogleSqlDatabaseInstanceExists(
   212  						"google_sql_database_instance.instance", &instance),
   213  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   214  						"google_sql_database_instance.instance", &instance),
   215  				),
   216  			},
   217  		},
   218  	})
   219  }
   220  
   221  func TestAccGoogleSqlDatabaseInstance_settings_downgrade(t *testing.T) {
   222  	var instance sqladmin.DatabaseInstance
   223  	databaseID := acctest.RandInt()
   224  
   225  	resource.Test(t, resource.TestCase{
   226  		PreCheck:     func() { testAccPreCheck(t) },
   227  		Providers:    testAccProviders,
   228  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   229  		Steps: []resource.TestStep{
   230  			resource.TestStep{
   231  				Config: fmt.Sprintf(
   232  					testGoogleSqlDatabaseInstance_settings, databaseID),
   233  				Check: resource.ComposeTestCheckFunc(
   234  					testAccCheckGoogleSqlDatabaseInstanceExists(
   235  						"google_sql_database_instance.instance", &instance),
   236  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   237  						"google_sql_database_instance.instance", &instance),
   238  				),
   239  			},
   240  			resource.TestStep{
   241  				Config: fmt.Sprintf(
   242  					testGoogleSqlDatabaseInstance_basic, databaseID),
   243  				Check: resource.ComposeTestCheckFunc(
   244  					testAccCheckGoogleSqlDatabaseInstanceExists(
   245  						"google_sql_database_instance.instance", &instance),
   246  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   247  						"google_sql_database_instance.instance", &instance),
   248  				),
   249  			},
   250  		},
   251  	})
   252  }
   253  
   254  // GH-4222
   255  func TestAccGoogleSqlDatabaseInstance_authNets(t *testing.T) {
   256  	// var instance sqladmin.DatabaseInstance
   257  	databaseID := acctest.RandInt()
   258  
   259  	resource.Test(t, resource.TestCase{
   260  		PreCheck:     func() { testAccPreCheck(t) },
   261  		Providers:    testAccProviders,
   262  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   263  		Steps: []resource.TestStep{
   264  			resource.TestStep{
   265  				Config: fmt.Sprintf(
   266  					testGoogleSqlDatabaseInstance_authNets_step1, databaseID),
   267  			},
   268  			resource.TestStep{
   269  				Config: fmt.Sprintf(
   270  					testGoogleSqlDatabaseInstance_authNets_step2, databaseID),
   271  			},
   272  			resource.TestStep{
   273  				Config: fmt.Sprintf(
   274  					testGoogleSqlDatabaseInstance_authNets_step1, databaseID),
   275  			},
   276  		},
   277  	})
   278  }
   279  
   280  func testAccCheckGoogleSqlDatabaseInstanceEquals(n string,
   281  	instance *sqladmin.DatabaseInstance) resource.TestCheckFunc {
   282  	return func(s *terraform.State) error {
   283  		rs, ok := s.RootModule().Resources[n]
   284  		if !ok {
   285  			return fmt.Errorf("Not found: %s", n)
   286  		}
   287  		attributes := rs.Primary.Attributes
   288  
   289  		server := instance.Name
   290  		local := attributes["name"]
   291  		if server != local {
   292  			return fmt.Errorf("Error name mismatch, (%s, %s)", server, local)
   293  		}
   294  
   295  		server = instance.Settings.Tier
   296  		local = attributes["settings.0.tier"]
   297  		if server != local {
   298  			return fmt.Errorf("Error settings.tier mismatch, (%s, %s)", server, local)
   299  		}
   300  
   301  		server = strings.TrimPrefix(instance.MasterInstanceName, instance.Project+":")
   302  		local = attributes["master_instance_name"]
   303  		if server != local && len(server) > 0 && len(local) > 0 {
   304  			return fmt.Errorf("Error master_instance_name mismatch, (%s, %s)", server, local)
   305  		}
   306  
   307  		server = instance.Settings.ActivationPolicy
   308  		local = attributes["settings.0.activation_policy"]
   309  		if server != local && len(server) > 0 && len(local) > 0 {
   310  			return fmt.Errorf("Error settings.activation_policy mismatch, (%s, %s)", server, local)
   311  		}
   312  
   313  		if instance.Settings.BackupConfiguration != nil {
   314  			server = strconv.FormatBool(instance.Settings.BackupConfiguration.BinaryLogEnabled)
   315  			local = attributes["settings.0.backup_configuration.0.binary_log_enabled"]
   316  			if server != local && len(server) > 0 && len(local) > 0 {
   317  				return fmt.Errorf("Error settings.backup_configuration.binary_log_enabled mismatch, (%s, %s)", server, local)
   318  			}
   319  
   320  			server = strconv.FormatBool(instance.Settings.BackupConfiguration.Enabled)
   321  			local = attributes["settings.0.backup_configuration.0.enabled"]
   322  			if server != local && len(server) > 0 && len(local) > 0 {
   323  				return fmt.Errorf("Error settings.backup_configuration.enabled mismatch, (%s, %s)", server, local)
   324  			}
   325  
   326  			server = instance.Settings.BackupConfiguration.StartTime
   327  			local = attributes["settings.0.backup_configuration.0.start_time"]
   328  			if server != local && len(server) > 0 && len(local) > 0 {
   329  				return fmt.Errorf("Error settings.backup_configuration.start_time mismatch, (%s, %s)", server, local)
   330  			}
   331  		}
   332  
   333  		server = strconv.FormatBool(instance.Settings.CrashSafeReplicationEnabled)
   334  		local = attributes["settings.0.crash_safe_replication"]
   335  		if server != local && len(server) > 0 && len(local) > 0 {
   336  			return fmt.Errorf("Error settings.crash_safe_replication mismatch, (%s, %s)", server, local)
   337  		}
   338  
   339  		server = strconv.FormatBool(instance.Settings.StorageAutoResize)
   340  		local = attributes["settings.0.disk_autoresize"]
   341  		if server != local && len(server) > 0 && len(local) > 0 {
   342  			return fmt.Errorf("Error settings.disk_autoresize mismatch, (%s, %s)", server, local)
   343  		}
   344  
   345  		server = strconv.FormatInt(instance.Settings.DataDiskSizeGb, 10)
   346  		local = attributes["settings.0.disk_size"]
   347  		if server != local && len(server) > 0 && len(local) > 0 && local != "0" {
   348  			return fmt.Errorf("Error settings.disk_size mismatch, (%s, %s)", server, local)
   349  		}
   350  
   351  		server = instance.Settings.DataDiskType
   352  		local = attributes["settings.0.disk_type"]
   353  		if server != local && len(server) > 0 && len(local) > 0 {
   354  			return fmt.Errorf("Error settings.disk_type mismatch, (%s, %s)", server, local)
   355  		}
   356  
   357  		if instance.Settings.IpConfiguration != nil {
   358  			server = strconv.FormatBool(instance.Settings.IpConfiguration.Ipv4Enabled)
   359  			local = attributes["settings.0.ip_configuration.0.ipv4_enabled"]
   360  			if server != local && len(server) > 0 && len(local) > 0 {
   361  				return fmt.Errorf("Error settings.ip_configuration.ipv4_enabled mismatch, (%s, %s)", server, local)
   362  			}
   363  
   364  			server = strconv.FormatBool(instance.Settings.IpConfiguration.RequireSsl)
   365  			local = attributes["settings.0.ip_configuration.0.require_ssl"]
   366  			if server != local && len(server) > 0 && len(local) > 0 {
   367  				return fmt.Errorf("Error settings.ip_configuration.require_ssl mismatch, (%s, %s)", server, local)
   368  			}
   369  		}
   370  
   371  		if instance.Settings.LocationPreference != nil {
   372  			server = instance.Settings.LocationPreference.FollowGaeApplication
   373  			local = attributes["settings.0.location_preference.0.follow_gae_application"]
   374  			if server != local && len(server) > 0 && len(local) > 0 {
   375  				return fmt.Errorf("Error settings.location_preference.follow_gae_application mismatch, (%s, %s)", server, local)
   376  			}
   377  
   378  			server = instance.Settings.LocationPreference.Zone
   379  			local = attributes["settings.0.location_preference.0.zone"]
   380  			if server != local && len(server) > 0 && len(local) > 0 {
   381  				return fmt.Errorf("Error settings.location_preference.zone mismatch, (%s, %s)", server, local)
   382  			}
   383  		}
   384  
   385  		if instance.Settings.MaintenanceWindow != nil {
   386  			server = strconv.FormatInt(instance.Settings.MaintenanceWindow.Day, 10)
   387  			local = attributes["settings.0.maintenance_window.0.day"]
   388  			if server != local && len(server) > 0 && len(local) > 0 {
   389  				return fmt.Errorf("Error settings.maintenance_window.day mismatch, (%s, %s)", server, local)
   390  			}
   391  
   392  			server = strconv.FormatInt(instance.Settings.MaintenanceWindow.Hour, 10)
   393  			local = attributes["settings.0.maintenance_window.0.hour"]
   394  			if server != local && len(server) > 0 && len(local) > 0 {
   395  				return fmt.Errorf("Error settings.maintenance_window.hour mismatch, (%s, %s)", server, local)
   396  			}
   397  
   398  			server = instance.Settings.MaintenanceWindow.UpdateTrack
   399  			local = attributes["settings.0.maintenance_window.0.update_track"]
   400  			if server != local && len(server) > 0 && len(local) > 0 {
   401  				return fmt.Errorf("Error settings.maintenance_window.update_track mismatch, (%s, %s)", server, local)
   402  			}
   403  		}
   404  
   405  		server = instance.Settings.PricingPlan
   406  		local = attributes["settings.0.pricing_plan"]
   407  		if server != local && len(server) > 0 && len(local) > 0 {
   408  			return fmt.Errorf("Error settings.pricing_plan mismatch, (%s, %s)", server, local)
   409  		}
   410  
   411  		if instance.ReplicaConfiguration != nil &&
   412  			instance.ReplicaConfiguration.MysqlReplicaConfiguration != nil {
   413  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.CaCertificate
   414  			local = attributes["replica_configuration.0.ca_certificate"]
   415  			if server != local && len(server) > 0 && len(local) > 0 {
   416  				return fmt.Errorf("Error replica_configuration.ca_certificate mismatch, (%s, %s)", server, local)
   417  			}
   418  
   419  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.ClientCertificate
   420  			local = attributes["replica_configuration.0.client_certificate"]
   421  			if server != local && len(server) > 0 && len(local) > 0 {
   422  				return fmt.Errorf("Error replica_configuration.client_certificate mismatch, (%s, %s)", server, local)
   423  			}
   424  
   425  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.ClientKey
   426  			local = attributes["replica_configuration.0.client_key"]
   427  			if server != local && len(server) > 0 && len(local) > 0 {
   428  				return fmt.Errorf("Error replica_configuration.client_key mismatch, (%s, %s)", server, local)
   429  			}
   430  
   431  			server = strconv.FormatInt(instance.ReplicaConfiguration.MysqlReplicaConfiguration.ConnectRetryInterval, 10)
   432  			local = attributes["replica_configuration.0.connect_retry_interval"]
   433  			if server != local && len(server) > 0 && len(local) > 0 {
   434  				return fmt.Errorf("Error replica_configuration.connect_retry_interval mismatch, (%s, %s)", server, local)
   435  			}
   436  
   437  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.DumpFilePath
   438  			local = attributes["replica_configuration.0.dump_file_path"]
   439  			if server != local && len(server) > 0 && len(local) > 0 {
   440  				return fmt.Errorf("Error replica_configuration.dump_file_path mismatch, (%s, %s)", server, local)
   441  			}
   442  
   443  			server = strconv.FormatInt(instance.ReplicaConfiguration.MysqlReplicaConfiguration.MasterHeartbeatPeriod, 10)
   444  			local = attributes["replica_configuration.0.master_heartbeat_period"]
   445  			if server != local && len(server) > 0 && len(local) > 0 {
   446  				return fmt.Errorf("Error replica_configuration.master_heartbeat_period mismatch, (%s, %s)", server, local)
   447  			}
   448  
   449  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.Password
   450  			local = attributes["replica_configuration.0.password"]
   451  			if server != local && len(server) > 0 && len(local) > 0 {
   452  				return fmt.Errorf("Error replica_configuration.password mismatch, (%s, %s)", server, local)
   453  			}
   454  
   455  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.SslCipher
   456  			local = attributes["replica_configuration.0.ssl_cipher"]
   457  			if server != local && len(server) > 0 && len(local) > 0 {
   458  				return fmt.Errorf("Error replica_configuration.ssl_cipher mismatch, (%s, %s)", server, local)
   459  			}
   460  
   461  			server = instance.ReplicaConfiguration.MysqlReplicaConfiguration.Username
   462  			local = attributes["replica_configuration.0.username"]
   463  			if server != local && len(server) > 0 && len(local) > 0 {
   464  				return fmt.Errorf("Error replica_configuration.username mismatch, (%s, %s)", server, local)
   465  			}
   466  
   467  			server = strconv.FormatBool(instance.ReplicaConfiguration.MysqlReplicaConfiguration.VerifyServerCertificate)
   468  			local = attributes["replica_configuration.0.verify_server_certificate"]
   469  			if server != local && len(server) > 0 && len(local) > 0 {
   470  				return fmt.Errorf("Error replica_configuration.verify_server_certificate mismatch, (%s, %s)", server, local)
   471  			}
   472  		}
   473  
   474  		return nil
   475  	}
   476  }
   477  
   478  func testAccCheckGoogleSqlDatabaseInstanceExists(n string,
   479  	instance *sqladmin.DatabaseInstance) resource.TestCheckFunc {
   480  	return func(s *terraform.State) error {
   481  		config := testAccProvider.Meta().(*Config)
   482  		rs, ok := s.RootModule().Resources[n]
   483  		if !ok {
   484  			return fmt.Errorf("Not found: %s", n)
   485  		}
   486  
   487  		found, err := config.clientSqlAdmin.Instances.Get(config.Project,
   488  			rs.Primary.Attributes["name"]).Do()
   489  
   490  		*instance = *found
   491  
   492  		if err != nil {
   493  			return fmt.Errorf("Not found: %s", n)
   494  		}
   495  
   496  		return nil
   497  	}
   498  }
   499  
   500  func testAccGoogleSqlDatabaseInstanceDestroy(s *terraform.State) error {
   501  	for _, rs := range s.RootModule().Resources {
   502  		config := testAccProvider.Meta().(*Config)
   503  		if rs.Type != "google_sql_database_instance" {
   504  			continue
   505  		}
   506  
   507  		_, err := config.clientSqlAdmin.Instances.Get(config.Project,
   508  			rs.Primary.Attributes["name"]).Do()
   509  		if err == nil {
   510  			return fmt.Errorf("Database Instance still exists")
   511  		}
   512  	}
   513  
   514  	return nil
   515  }
   516  
   517  func testAccCheckGoogleSqlDatabaseRootUserDoesNotExist(
   518  	instance *sqladmin.DatabaseInstance) resource.TestCheckFunc {
   519  	return func(s *terraform.State) error {
   520  		config := testAccProvider.Meta().(*Config)
   521  
   522  		users, err := config.clientSqlAdmin.Users.List(config.Project, instance.Name).Do()
   523  
   524  		if err != nil {
   525  			return fmt.Errorf("Could not list database users for %q: %s", instance.Name, err)
   526  		}
   527  
   528  		for _, u := range users.Items {
   529  			if u.Name == "root" && u.Host == "%" {
   530  				return fmt.Errorf("%v@%v user still exists", u.Name, u.Host)
   531  			}
   532  		}
   533  
   534  		return nil
   535  	}
   536  }
   537  
   538  var testGoogleSqlDatabaseInstance_basic = `
   539  resource "google_sql_database_instance" "instance" {
   540  	name = "tf-lw-%d"
   541  	region = "us-central"
   542  	settings {
   543  		tier = "D0"
   544  		crash_safe_replication = false
   545  	}
   546  }
   547  `
   548  
   549  var testGoogleSqlDatabaseInstance_basic2 = `
   550  resource "google_sql_database_instance" "instance" {
   551  	region = "us-central"
   552  	settings {
   553  		tier = "D0"
   554  		crash_safe_replication = false
   555  	}
   556  }
   557  `
   558  var testGoogleSqlDatabaseInstance_basic3 = `
   559  resource "google_sql_database_instance" "instance" {
   560  	name = "tf-lw-%d"
   561  	region = "us-central"
   562  	settings {
   563  		tier = "db-f1-micro"
   564  	}
   565  }
   566  `
   567  
   568  var testGoogleSqlDatabaseInstance_settings = `
   569  resource "google_sql_database_instance" "instance" {
   570  	name = "tf-lw-%d"
   571  	region = "us-central"
   572  	settings {
   573  		tier = "D0"
   574  		crash_safe_replication = false
   575  		replication_type = "ASYNCHRONOUS"
   576  		location_preference {
   577  			zone = "us-central1-f"
   578  		}
   579  
   580  		ip_configuration {
   581  			ipv4_enabled = "true"
   582  			authorized_networks {
   583  				value = "108.12.12.12"
   584  				name = "misc"
   585  				expiration_time = "2017-11-15T16:19:00.094Z"
   586  			}
   587  		}
   588  
   589  		backup_configuration {
   590  			enabled = "true"
   591  			start_time = "19:19"
   592  		}
   593  
   594  		activation_policy = "ON_DEMAND"
   595  	}
   596  }
   597  `
   598  
   599  // Note - this test is not feasible to run unless we generate
   600  // backups first.
   601  var testGoogleSqlDatabaseInstance_replica = `
   602  resource "google_sql_database_instance" "instance_master" {
   603  	name = "tf-lw-%d"
   604  	database_version = "MYSQL_5_6"
   605  	region = "us-east1"
   606  
   607  	settings {
   608  		tier = "D0"
   609  		crash_safe_replication = true
   610  
   611  		backup_configuration {
   612  			enabled = true
   613  			start_time = "00:00"
   614  			binary_log_enabled = true
   615  		}
   616  	}
   617  }
   618  
   619  resource "google_sql_database_instance" "instance" {
   620  	name = "tf-lw-%d"
   621  	database_version = "MYSQL_5_6"
   622  	region = "us-central"
   623  
   624  	settings {
   625  		tier = "D0"
   626  	}
   627  
   628  	master_instance_name = "${google_sql_database_instance.instance_master.name}"
   629  
   630  	replica_configuration {
   631  		ca_certificate = "${file("~/tmp/fake.pem")}"
   632  		client_certificate = "${file("~/tmp/fake.pem")}"
   633  		client_key = "${file("~/tmp/fake.pem")}"
   634  		connect_retry_interval = 100
   635  		master_heartbeat_period = 10000
   636  		password = "password"
   637  		username = "username"
   638  		ssl_cipher = "ALL"
   639  		verify_server_certificate = false
   640  	}
   641  }
   642  `
   643  
   644  var testGoogleSqlDatabaseInstance_slave = `
   645  resource "google_sql_database_instance" "instance_master" {
   646  	name = "tf-lw-%d"
   647  	region = "us-central1"
   648  
   649  	settings {
   650  		tier = "db-f1-micro"
   651  
   652  		backup_configuration {
   653  			enabled = true
   654  			binary_log_enabled = true
   655  		}
   656  	}
   657  }
   658  
   659  resource "google_sql_database_instance" "instance_slave" {
   660  	name = "tf-lw-%d"
   661  	region = "us-central1"
   662  
   663  	master_instance_name = "${google_sql_database_instance.instance_master.name}"
   664  
   665  	settings {
   666  		tier = "db-f1-micro"
   667  	}
   668  }
   669  `
   670  
   671  var testGoogleSqlDatabaseInstance_diskspecs = `
   672  resource "google_sql_database_instance" "instance" {
   673  	name = "tf-lw-%d"
   674  	region = "us-central1"
   675  
   676  	settings {
   677  		tier = "db-f1-micro"
   678  		disk_autoresize = true
   679  		disk_size = 15
   680  		disk_type = "PD_HDD"
   681  	}
   682  }
   683  `
   684  
   685  var testGoogleSqlDatabaseInstance_maintenance = `
   686  resource "google_sql_database_instance" "instance" {
   687  	name = "tf-lw-%d"
   688  	region = "us-central1"
   689  
   690  	settings {
   691  		tier = "db-f1-micro"
   692  
   693  		maintenance_window {
   694  		  day  = 7
   695  		  hour = 3
   696  			update_track = "canary"
   697  	  }
   698  	}
   699  }
   700  `
   701  
   702  var testGoogleSqlDatabaseInstance_authNets_step1 = `
   703  resource "google_sql_database_instance" "instance" {
   704  	name = "tf-lw-%d"
   705  	region = "us-central"
   706  	settings {
   707  		tier = "D0"
   708  		crash_safe_replication = false
   709  
   710  		ip_configuration {
   711  			ipv4_enabled = "true"
   712  			authorized_networks {
   713  				value = "108.12.12.12"
   714  				name = "misc"
   715  				expiration_time = "2017-11-15T16:19:00.094Z"
   716  			}
   717  		}
   718  	}
   719  }
   720  `
   721  
   722  var testGoogleSqlDatabaseInstance_authNets_step2 = `
   723  resource "google_sql_database_instance" "instance" {
   724  	name = "tf-lw-%d"
   725  	region = "us-central"
   726  	settings {
   727  		tier = "D0"
   728  		crash_safe_replication = false
   729  
   730  		ip_configuration {
   731  			ipv4_enabled = "true"
   732  		}
   733  	}
   734  }
   735  `