github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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  	"log"
    13  	"strconv"
    14  	"strings"
    15  	"testing"
    16  
    17  	"github.com/hashicorp/terraform/helper/acctest"
    18  	"github.com/hashicorp/terraform/helper/resource"
    19  	"github.com/hashicorp/terraform/terraform"
    20  
    21  	"google.golang.org/api/sqladmin/v1beta4"
    22  )
    23  
    24  func init() {
    25  	resource.AddTestSweepers("gcp_sql_db_instance", &resource.Sweeper{
    26  		Name: "gcp_sql_db_instance",
    27  		F:    testSweepDatabases,
    28  	})
    29  }
    30  
    31  func testSweepDatabases(region string) error {
    32  	config, err := sharedConfigForRegion(region)
    33  	if err != nil {
    34  		return fmt.Errorf("error getting shared config for region: %s", err)
    35  	}
    36  
    37  	err = config.loadAndValidate()
    38  	if err != nil {
    39  		log.Fatalf("error loading: %s", err)
    40  	}
    41  
    42  	found, err := config.clientSqlAdmin.Instances.List(config.Project).Do()
    43  	if err != nil {
    44  		log.Fatalf("error listing databases: %s", err)
    45  	}
    46  
    47  	if len(found.Items) == 0 {
    48  		log.Printf("No databases found")
    49  		return nil
    50  	}
    51  
    52  	for _, d := range found.Items {
    53  		var testDbInstance bool
    54  		for _, testName := range []string{"tf-lw-", "sqldatabasetest"} {
    55  			// only destroy instances we know to fit our test naming pattern
    56  			if strings.HasPrefix(d.Name, testName) {
    57  				testDbInstance = true
    58  			}
    59  		}
    60  
    61  		if !testDbInstance {
    62  			continue
    63  		}
    64  
    65  		log.Printf("Destroying SQL Instance (%s)", d.Name)
    66  
    67  		// replicas need to be stopped and destroyed before destroying a master
    68  		// instance. The ordering slice tracks replica databases for a given master
    69  		// and we call destroy on them before destroying the master
    70  		var ordering []string
    71  		for _, replicaName := range d.ReplicaNames {
    72  			// need to stop replication before being able to destroy a database
    73  			op, err := config.clientSqlAdmin.Instances.StopReplica(config.Project, replicaName).Do()
    74  
    75  			if err != nil {
    76  				return fmt.Errorf("error, failed to stop replica instance (%s) for instance (%s): %s", replicaName, d.Name, err)
    77  			}
    78  
    79  			err = sqladminOperationWait(config, op, "Stop Replica")
    80  			if err != nil {
    81  				if strings.Contains(err.Error(), "does not exist") {
    82  					log.Printf("Replication operation not found")
    83  				} else {
    84  					return err
    85  				}
    86  			}
    87  
    88  			ordering = append(ordering, replicaName)
    89  		}
    90  
    91  		// ordering has a list of replicas (or none), now add the primary to the end
    92  		ordering = append(ordering, d.Name)
    93  
    94  		for _, db := range ordering {
    95  			// destroy instances, replicas first
    96  			op, err := config.clientSqlAdmin.Instances.Delete(config.Project, db).Do()
    97  
    98  			if err != nil {
    99  				if strings.Contains(err.Error(), "409") {
   100  					// the GCP api can return a 409 error after the delete operation
   101  					// reaches a successful end
   102  					log.Printf("Operation not found, got 409 response")
   103  					continue
   104  				}
   105  
   106  				return fmt.Errorf("Error, failed to delete instance %s: %s", db, err)
   107  			}
   108  
   109  			err = sqladminOperationWait(config, op, "Delete Instance")
   110  			if err != nil {
   111  				if strings.Contains(err.Error(), "does not exist") {
   112  					log.Printf("SQL instance not found")
   113  					continue
   114  				}
   115  				return err
   116  			}
   117  		}
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  func TestAccGoogleSqlDatabaseInstance_basic(t *testing.T) {
   124  	var instance sqladmin.DatabaseInstance
   125  	databaseID := acctest.RandInt()
   126  
   127  	resource.Test(t, resource.TestCase{
   128  		PreCheck:     func() { testAccPreCheck(t) },
   129  		Providers:    testAccProviders,
   130  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   131  		Steps: []resource.TestStep{
   132  			resource.TestStep{
   133  				Config: fmt.Sprintf(
   134  					testGoogleSqlDatabaseInstance_basic, databaseID),
   135  				Check: resource.ComposeTestCheckFunc(
   136  					testAccCheckGoogleSqlDatabaseInstanceExists(
   137  						"google_sql_database_instance.instance", &instance),
   138  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   139  						"google_sql_database_instance.instance", &instance),
   140  				),
   141  			},
   142  		},
   143  	})
   144  }
   145  
   146  func TestAccGoogleSqlDatabaseInstance_basic2(t *testing.T) {
   147  	var instance sqladmin.DatabaseInstance
   148  
   149  	resource.Test(t, resource.TestCase{
   150  		PreCheck:     func() { testAccPreCheck(t) },
   151  		Providers:    testAccProviders,
   152  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   153  		Steps: []resource.TestStep{
   154  			resource.TestStep{
   155  				Config: testGoogleSqlDatabaseInstance_basic2,
   156  				Check: resource.ComposeTestCheckFunc(
   157  					testAccCheckGoogleSqlDatabaseInstanceExists(
   158  						"google_sql_database_instance.instance", &instance),
   159  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   160  						"google_sql_database_instance.instance", &instance),
   161  				),
   162  			},
   163  		},
   164  	})
   165  }
   166  
   167  func TestAccGoogleSqlDatabaseInstance_basic3(t *testing.T) {
   168  	var instance sqladmin.DatabaseInstance
   169  	databaseID := acctest.RandInt()
   170  
   171  	resource.Test(t, resource.TestCase{
   172  		PreCheck:     func() { testAccPreCheck(t) },
   173  		Providers:    testAccProviders,
   174  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   175  		Steps: []resource.TestStep{
   176  			resource.TestStep{
   177  				Config: fmt.Sprintf(
   178  					testGoogleSqlDatabaseInstance_basic3, databaseID),
   179  				Check: resource.ComposeTestCheckFunc(
   180  					testAccCheckGoogleSqlDatabaseInstanceExists(
   181  						"google_sql_database_instance.instance", &instance),
   182  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   183  						"google_sql_database_instance.instance", &instance),
   184  					testAccCheckGoogleSqlDatabaseRootUserDoesNotExist(
   185  						&instance),
   186  				),
   187  			},
   188  		},
   189  	})
   190  }
   191  func TestAccGoogleSqlDatabaseInstance_settings_basic(t *testing.T) {
   192  	var instance sqladmin.DatabaseInstance
   193  	databaseID := acctest.RandInt()
   194  
   195  	resource.Test(t, resource.TestCase{
   196  		PreCheck:     func() { testAccPreCheck(t) },
   197  		Providers:    testAccProviders,
   198  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   199  		Steps: []resource.TestStep{
   200  			resource.TestStep{
   201  				Config: fmt.Sprintf(
   202  					testGoogleSqlDatabaseInstance_settings, databaseID),
   203  				Check: resource.ComposeTestCheckFunc(
   204  					testAccCheckGoogleSqlDatabaseInstanceExists(
   205  						"google_sql_database_instance.instance", &instance),
   206  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   207  						"google_sql_database_instance.instance", &instance),
   208  				),
   209  			},
   210  		},
   211  	})
   212  }
   213  
   214  func TestAccGoogleSqlDatabaseInstance_slave(t *testing.T) {
   215  	var instance sqladmin.DatabaseInstance
   216  	masterID := acctest.RandInt()
   217  	slaveID := acctest.RandInt()
   218  
   219  	resource.Test(t, resource.TestCase{
   220  		PreCheck:     func() { testAccPreCheck(t) },
   221  		Providers:    testAccProviders,
   222  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   223  		Steps: []resource.TestStep{
   224  			resource.TestStep{
   225  				Config: fmt.Sprintf(
   226  					testGoogleSqlDatabaseInstance_slave, masterID, slaveID),
   227  				Check: resource.ComposeTestCheckFunc(
   228  					testAccCheckGoogleSqlDatabaseInstanceExists(
   229  						"google_sql_database_instance.instance_master", &instance),
   230  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   231  						"google_sql_database_instance.instance_master", &instance),
   232  					testAccCheckGoogleSqlDatabaseInstanceExists(
   233  						"google_sql_database_instance.instance_slave", &instance),
   234  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   235  						"google_sql_database_instance.instance_slave", &instance),
   236  				),
   237  			},
   238  		},
   239  	})
   240  }
   241  
   242  func TestAccGoogleSqlDatabaseInstance_diskspecs(t *testing.T) {
   243  	var instance sqladmin.DatabaseInstance
   244  	masterID := acctest.RandInt()
   245  
   246  	resource.Test(t, resource.TestCase{
   247  		PreCheck:     func() { testAccPreCheck(t) },
   248  		Providers:    testAccProviders,
   249  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   250  		Steps: []resource.TestStep{
   251  			resource.TestStep{
   252  				Config: fmt.Sprintf(
   253  					testGoogleSqlDatabaseInstance_diskspecs, masterID),
   254  				Check: resource.ComposeTestCheckFunc(
   255  					testAccCheckGoogleSqlDatabaseInstanceExists(
   256  						"google_sql_database_instance.instance", &instance),
   257  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   258  						"google_sql_database_instance.instance", &instance),
   259  				),
   260  			},
   261  		},
   262  	})
   263  }
   264  
   265  func TestAccGoogleSqlDatabaseInstance_maintenance(t *testing.T) {
   266  	var instance sqladmin.DatabaseInstance
   267  	masterID := acctest.RandInt()
   268  
   269  	resource.Test(t, resource.TestCase{
   270  		PreCheck:     func() { testAccPreCheck(t) },
   271  		Providers:    testAccProviders,
   272  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   273  		Steps: []resource.TestStep{
   274  			resource.TestStep{
   275  				Config: fmt.Sprintf(
   276  					testGoogleSqlDatabaseInstance_maintenance, masterID),
   277  				Check: resource.ComposeTestCheckFunc(
   278  					testAccCheckGoogleSqlDatabaseInstanceExists(
   279  						"google_sql_database_instance.instance", &instance),
   280  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   281  						"google_sql_database_instance.instance", &instance),
   282  				),
   283  			},
   284  		},
   285  	})
   286  }
   287  
   288  func TestAccGoogleSqlDatabaseInstance_settings_upgrade(t *testing.T) {
   289  	var instance sqladmin.DatabaseInstance
   290  	databaseID := acctest.RandInt()
   291  
   292  	resource.Test(t, resource.TestCase{
   293  		PreCheck:     func() { testAccPreCheck(t) },
   294  		Providers:    testAccProviders,
   295  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   296  		Steps: []resource.TestStep{
   297  			resource.TestStep{
   298  				Config: fmt.Sprintf(
   299  					testGoogleSqlDatabaseInstance_basic, databaseID),
   300  				Check: resource.ComposeTestCheckFunc(
   301  					testAccCheckGoogleSqlDatabaseInstanceExists(
   302  						"google_sql_database_instance.instance", &instance),
   303  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   304  						"google_sql_database_instance.instance", &instance),
   305  				),
   306  			},
   307  			resource.TestStep{
   308  				Config: fmt.Sprintf(
   309  					testGoogleSqlDatabaseInstance_settings, databaseID),
   310  				Check: resource.ComposeTestCheckFunc(
   311  					testAccCheckGoogleSqlDatabaseInstanceExists(
   312  						"google_sql_database_instance.instance", &instance),
   313  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   314  						"google_sql_database_instance.instance", &instance),
   315  				),
   316  			},
   317  		},
   318  	})
   319  }
   320  
   321  func TestAccGoogleSqlDatabaseInstance_settings_downgrade(t *testing.T) {
   322  	var instance sqladmin.DatabaseInstance
   323  	databaseID := acctest.RandInt()
   324  
   325  	resource.Test(t, resource.TestCase{
   326  		PreCheck:     func() { testAccPreCheck(t) },
   327  		Providers:    testAccProviders,
   328  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   329  		Steps: []resource.TestStep{
   330  			resource.TestStep{
   331  				Config: fmt.Sprintf(
   332  					testGoogleSqlDatabaseInstance_settings, databaseID),
   333  				Check: resource.ComposeTestCheckFunc(
   334  					testAccCheckGoogleSqlDatabaseInstanceExists(
   335  						"google_sql_database_instance.instance", &instance),
   336  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   337  						"google_sql_database_instance.instance", &instance),
   338  				),
   339  			},
   340  			resource.TestStep{
   341  				Config: fmt.Sprintf(
   342  					testGoogleSqlDatabaseInstance_basic, databaseID),
   343  				Check: resource.ComposeTestCheckFunc(
   344  					testAccCheckGoogleSqlDatabaseInstanceExists(
   345  						"google_sql_database_instance.instance", &instance),
   346  					testAccCheckGoogleSqlDatabaseInstanceEquals(
   347  						"google_sql_database_instance.instance", &instance),
   348  				),
   349  			},
   350  		},
   351  	})
   352  }
   353  
   354  // GH-4222
   355  func TestAccGoogleSqlDatabaseInstance_authNets(t *testing.T) {
   356  	// var instance sqladmin.DatabaseInstance
   357  	databaseID := acctest.RandInt()
   358  
   359  	resource.Test(t, resource.TestCase{
   360  		PreCheck:     func() { testAccPreCheck(t) },
   361  		Providers:    testAccProviders,
   362  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   363  		Steps: []resource.TestStep{
   364  			resource.TestStep{
   365  				Config: fmt.Sprintf(
   366  					testGoogleSqlDatabaseInstance_authNets_step1, databaseID),
   367  			},
   368  			resource.TestStep{
   369  				Config: fmt.Sprintf(
   370  					testGoogleSqlDatabaseInstance_authNets_step2, databaseID),
   371  			},
   372  			resource.TestStep{
   373  				Config: fmt.Sprintf(
   374  					testGoogleSqlDatabaseInstance_authNets_step1, databaseID),
   375  			},
   376  		},
   377  	})
   378  }
   379  
   380  // Tests that a SQL instance can be referenced from more than one other resource without
   381  // throwing an error during provisioning, see #9018.
   382  func TestAccGoogleSqlDatabaseInstance_multipleOperations(t *testing.T) {
   383  	databaseID, instanceID, userID := acctest.RandString(8), acctest.RandString(8), acctest.RandString(8)
   384  
   385  	resource.Test(t, resource.TestCase{
   386  		PreCheck:     func() { testAccPreCheck(t) },
   387  		Providers:    testAccProviders,
   388  		CheckDestroy: testAccGoogleSqlDatabaseInstanceDestroy,
   389  		Steps: []resource.TestStep{
   390  			resource.TestStep{
   391  				Config: fmt.Sprintf(
   392  					testGoogleSqlDatabaseInstance_multipleOperations, databaseID, instanceID, userID),
   393  			},
   394  		},
   395  	})
   396  }
   397  
   398  func testAccCheckGoogleSqlDatabaseInstanceEquals(n string,
   399  	instance *sqladmin.DatabaseInstance) resource.TestCheckFunc {
   400  	return func(s *terraform.State) error {
   401  		rs, ok := s.RootModule().Resources[n]
   402  		if !ok {
   403  			return fmt.Errorf("Not found: %s", n)
   404  		}
   405  		attributes := rs.Primary.Attributes
   406  
   407  		server := instance.Name
   408  		local := attributes["name"]
   409  		if server != local {
   410  			return fmt.Errorf("Error name mismatch, (%s, %s)", server, local)
   411  		}
   412  
   413  		server = instance.Settings.Tier
   414  		local = attributes["settings.0.tier"]
   415  		if server != local {
   416  			return fmt.Errorf("Error settings.tier mismatch, (%s, %s)", server, local)
   417  		}
   418  
   419  		server = strings.TrimPrefix(instance.MasterInstanceName, instance.Project+":")
   420  		local = attributes["master_instance_name"]
   421  		if server != local && len(server) > 0 && len(local) > 0 {
   422  			return fmt.Errorf("Error master_instance_name mismatch, (%s, %s)", server, local)
   423  		}
   424  
   425  		server = instance.Settings.ActivationPolicy
   426  		local = attributes["settings.0.activation_policy"]
   427  		if server != local && len(server) > 0 && len(local) > 0 {
   428  			return fmt.Errorf("Error settings.activation_policy mismatch, (%s, %s)", server, local)
   429  		}
   430  
   431  		if instance.Settings.BackupConfiguration != nil {
   432  			server = strconv.FormatBool(instance.Settings.BackupConfiguration.BinaryLogEnabled)
   433  			local = attributes["settings.0.backup_configuration.0.binary_log_enabled"]
   434  			if server != local && len(server) > 0 && len(local) > 0 {
   435  				return fmt.Errorf("Error settings.backup_configuration.binary_log_enabled mismatch, (%s, %s)", server, local)
   436  			}
   437  
   438  			server = strconv.FormatBool(instance.Settings.BackupConfiguration.Enabled)
   439  			local = attributes["settings.0.backup_configuration.0.enabled"]
   440  			if server != local && len(server) > 0 && len(local) > 0 {
   441  				return fmt.Errorf("Error settings.backup_configuration.enabled mismatch, (%s, %s)", server, local)
   442  			}
   443  
   444  			server = instance.Settings.BackupConfiguration.StartTime
   445  			local = attributes["settings.0.backup_configuration.0.start_time"]
   446  			if server != local && len(server) > 0 && len(local) > 0 {
   447  				return fmt.Errorf("Error settings.backup_configuration.start_time mismatch, (%s, %s)", server, local)
   448  			}
   449  		}
   450  
   451  		server = strconv.FormatBool(instance.Settings.CrashSafeReplicationEnabled)
   452  		local = attributes["settings.0.crash_safe_replication"]
   453  		if server != local && len(server) > 0 && len(local) > 0 {
   454  			return fmt.Errorf("Error settings.crash_safe_replication mismatch, (%s, %s)", server, local)
   455  		}
   456  
   457  		server = strconv.FormatBool(instance.Settings.StorageAutoResize)
   458  		local = attributes["settings.0.disk_autoresize"]
   459  		if server != local && len(server) > 0 && len(local) > 0 {
   460  			return fmt.Errorf("Error settings.disk_autoresize mismatch, (%s, %s)", server, local)
   461  		}
   462  
   463  		server = strconv.FormatInt(instance.Settings.DataDiskSizeGb, 10)
   464  		local = attributes["settings.0.disk_size"]
   465  		if server != local && len(server) > 0 && len(local) > 0 && local != "0" {
   466  			return fmt.Errorf("Error settings.disk_size mismatch, (%s, %s)", server, local)
   467  		}
   468  
   469  		server = instance.Settings.DataDiskType
   470  		local = attributes["settings.0.disk_type"]
   471  		if server != local && len(server) > 0 && len(local) > 0 {
   472  			return fmt.Errorf("Error settings.disk_type mismatch, (%s, %s)", server, local)
   473  		}
   474  
   475  		if instance.Settings.IpConfiguration != nil {
   476  			server = strconv.FormatBool(instance.Settings.IpConfiguration.Ipv4Enabled)
   477  			local = attributes["settings.0.ip_configuration.0.ipv4_enabled"]
   478  			if server != local && len(server) > 0 && len(local) > 0 {
   479  				return fmt.Errorf("Error settings.ip_configuration.ipv4_enabled mismatch, (%s, %s)", server, local)
   480  			}
   481  
   482  			server = strconv.FormatBool(instance.Settings.IpConfiguration.RequireSsl)
   483  			local = attributes["settings.0.ip_configuration.0.require_ssl"]
   484  			if server != local && len(server) > 0 && len(local) > 0 {
   485  				return fmt.Errorf("Error settings.ip_configuration.require_ssl mismatch, (%s, %s)", server, local)
   486  			}
   487  		}
   488  
   489  		if instance.Settings.LocationPreference != nil {
   490  			server = instance.Settings.LocationPreference.FollowGaeApplication
   491  			local = attributes["settings.0.location_preference.0.follow_gae_application"]
   492  			if server != local && len(server) > 0 && len(local) > 0 {
   493  				return fmt.Errorf("Error settings.location_preference.follow_gae_application mismatch, (%s, %s)", server, local)
   494  			}
   495  
   496  			server = instance.Settings.LocationPreference.Zone
   497  			local = attributes["settings.0.location_preference.0.zone"]
   498  			if server != local && len(server) > 0 && len(local) > 0 {
   499  				return fmt.Errorf("Error settings.location_preference.zone mismatch, (%s, %s)", server, local)
   500  			}
   501  		}
   502  
   503  		if instance.Settings.MaintenanceWindow != nil {
   504  			server = strconv.FormatInt(instance.Settings.MaintenanceWindow.Day, 10)
   505  			local = attributes["settings.0.maintenance_window.0.day"]
   506  			if server != local && len(server) > 0 && len(local) > 0 {
   507  				return fmt.Errorf("Error settings.maintenance_window.day mismatch, (%s, %s)", server, local)
   508  			}
   509  
   510  			server = strconv.FormatInt(instance.Settings.MaintenanceWindow.Hour, 10)
   511  			local = attributes["settings.0.maintenance_window.0.hour"]
   512  			if server != local && len(server) > 0 && len(local) > 0 {
   513  				return fmt.Errorf("Error settings.maintenance_window.hour mismatch, (%s, %s)", server, local)
   514  			}
   515  
   516  			server = instance.Settings.MaintenanceWindow.UpdateTrack
   517  			local = attributes["settings.0.maintenance_window.0.update_track"]
   518  			if server != local && len(server) > 0 && len(local) > 0 {
   519  				return fmt.Errorf("Error settings.maintenance_window.update_track mismatch, (%s, %s)", server, local)
   520  			}
   521  		}
   522  
   523  		server = instance.Settings.PricingPlan
   524  		local = attributes["settings.0.pricing_plan"]
   525  		if server != local && len(server) > 0 && len(local) > 0 {
   526  			return fmt.Errorf("Error settings.pricing_plan mismatch, (%s, %s)", server, local)
   527  		}
   528  
   529  		if instance.ReplicaConfiguration != nil {
   530  			server = strconv.FormatBool(instance.ReplicaConfiguration.FailoverTarget)
   531  			local = attributes["replica_configuration.0.failover_target"]
   532  			if server != local && len(server) > 0 && len(local) > 0 {
   533  				return fmt.Errorf("Error replica_configuration.failover_target mismatch, (%s, %s)", server, local)
   534  			}
   535  		}
   536  
   537  		return nil
   538  	}
   539  }
   540  
   541  func testAccCheckGoogleSqlDatabaseInstanceExists(n string,
   542  	instance *sqladmin.DatabaseInstance) resource.TestCheckFunc {
   543  	return func(s *terraform.State) error {
   544  		config := testAccProvider.Meta().(*Config)
   545  		rs, ok := s.RootModule().Resources[n]
   546  		if !ok {
   547  			return fmt.Errorf("Not found: %s", n)
   548  		}
   549  
   550  		found, err := config.clientSqlAdmin.Instances.Get(config.Project,
   551  			rs.Primary.Attributes["name"]).Do()
   552  
   553  		*instance = *found
   554  
   555  		if err != nil {
   556  			return fmt.Errorf("Not found: %s", n)
   557  		}
   558  
   559  		return nil
   560  	}
   561  }
   562  
   563  func testAccGoogleSqlDatabaseInstanceDestroy(s *terraform.State) error {
   564  	for _, rs := range s.RootModule().Resources {
   565  		config := testAccProvider.Meta().(*Config)
   566  		if rs.Type != "google_sql_database_instance" {
   567  			continue
   568  		}
   569  
   570  		_, err := config.clientSqlAdmin.Instances.Get(config.Project,
   571  			rs.Primary.Attributes["name"]).Do()
   572  		if err == nil {
   573  			return fmt.Errorf("Database Instance still exists")
   574  		}
   575  	}
   576  
   577  	return nil
   578  }
   579  
   580  func testAccCheckGoogleSqlDatabaseRootUserDoesNotExist(
   581  	instance *sqladmin.DatabaseInstance) resource.TestCheckFunc {
   582  	return func(s *terraform.State) error {
   583  		config := testAccProvider.Meta().(*Config)
   584  
   585  		users, err := config.clientSqlAdmin.Users.List(config.Project, instance.Name).Do()
   586  
   587  		if err != nil {
   588  			return fmt.Errorf("Could not list database users for %q: %s", instance.Name, err)
   589  		}
   590  
   591  		for _, u := range users.Items {
   592  			if u.Name == "root" && u.Host == "%" {
   593  				return fmt.Errorf("%v@%v user still exists", u.Name, u.Host)
   594  			}
   595  		}
   596  
   597  		return nil
   598  	}
   599  }
   600  
   601  var testGoogleSqlDatabaseInstance_basic = `
   602  resource "google_sql_database_instance" "instance" {
   603  	name = "tf-lw-%d"
   604  	region = "us-central"
   605  	settings {
   606  		tier = "D0"
   607  		crash_safe_replication = false
   608  	}
   609  }
   610  `
   611  
   612  var testGoogleSqlDatabaseInstance_basic2 = `
   613  resource "google_sql_database_instance" "instance" {
   614  	region = "us-central"
   615  	settings {
   616  		tier = "D0"
   617  		crash_safe_replication = false
   618  	}
   619  }
   620  `
   621  var testGoogleSqlDatabaseInstance_basic3 = `
   622  resource "google_sql_database_instance" "instance" {
   623  	name = "tf-lw-%d"
   624  	region = "us-central"
   625  	settings {
   626  		tier = "db-f1-micro"
   627  	}
   628  }
   629  `
   630  
   631  var testGoogleSqlDatabaseInstance_settings = `
   632  resource "google_sql_database_instance" "instance" {
   633  	name = "tf-lw-%d"
   634  	region = "us-central"
   635  	settings {
   636  		tier = "D0"
   637  		crash_safe_replication = false
   638  		replication_type = "ASYNCHRONOUS"
   639  		location_preference {
   640  			zone = "us-central1-f"
   641  		}
   642  
   643  		ip_configuration {
   644  			ipv4_enabled = "true"
   645  			authorized_networks {
   646  				value = "108.12.12.12"
   647  				name = "misc"
   648  				expiration_time = "2017-11-15T16:19:00.094Z"
   649  			}
   650  		}
   651  
   652  		backup_configuration {
   653  			enabled = "true"
   654  			start_time = "19:19"
   655  		}
   656  
   657  		activation_policy = "ON_DEMAND"
   658  	}
   659  }
   660  `
   661  
   662  // Note - this test is not feasible to run unless we generate
   663  // backups first.
   664  var testGoogleSqlDatabaseInstance_replica = `
   665  resource "google_sql_database_instance" "instance_master" {
   666  	name = "tf-lw-%d"
   667  	database_version = "MYSQL_5_6"
   668  	region = "us-east1"
   669  
   670  	settings {
   671  		tier = "D0"
   672  		crash_safe_replication = true
   673  
   674  		backup_configuration {
   675  			enabled = true
   676  			start_time = "00:00"
   677  			binary_log_enabled = true
   678  		}
   679  	}
   680  }
   681  
   682  resource "google_sql_database_instance" "instance" {
   683  	name = "tf-lw-%d"
   684  	database_version = "MYSQL_5_6"
   685  	region = "us-central"
   686  
   687  	settings {
   688  		tier = "D0"
   689  	}
   690  
   691  	master_instance_name = "${google_sql_database_instance.instance_master.name}"
   692  
   693  	replica_configuration {
   694  		ca_certificate = "${file("~/tmp/fake.pem")}"
   695  		client_certificate = "${file("~/tmp/fake.pem")}"
   696  		client_key = "${file("~/tmp/fake.pem")}"
   697  		connect_retry_interval = 100
   698  		master_heartbeat_period = 10000
   699  		password = "password"
   700  		username = "username"
   701  		ssl_cipher = "ALL"
   702  		verify_server_certificate = false
   703  	}
   704  }
   705  `
   706  
   707  var testGoogleSqlDatabaseInstance_slave = `
   708  resource "google_sql_database_instance" "instance_master" {
   709  	name = "tf-lw-%d"
   710  	region = "us-central1"
   711  
   712  	settings {
   713  		tier = "db-f1-micro"
   714  
   715  		backup_configuration {
   716  			enabled = true
   717  			binary_log_enabled = true
   718  		}
   719  	}
   720  }
   721  
   722  resource "google_sql_database_instance" "instance_slave" {
   723  	name = "tf-lw-%d"
   724  	region = "us-central1"
   725  
   726  	master_instance_name = "${google_sql_database_instance.instance_master.name}"
   727  
   728  	settings {
   729  		tier = "db-f1-micro"
   730  	}
   731  }
   732  `
   733  
   734  var testGoogleSqlDatabaseInstance_diskspecs = `
   735  resource "google_sql_database_instance" "instance" {
   736  	name = "tf-lw-%d"
   737  	region = "us-central1"
   738  
   739  	settings {
   740  		tier = "db-f1-micro"
   741  		disk_autoresize = true
   742  		disk_size = 15
   743  		disk_type = "PD_HDD"
   744  	}
   745  }
   746  `
   747  
   748  var testGoogleSqlDatabaseInstance_maintenance = `
   749  resource "google_sql_database_instance" "instance" {
   750  	name = "tf-lw-%d"
   751  	region = "us-central1"
   752  
   753  	settings {
   754  		tier = "db-f1-micro"
   755  
   756  		maintenance_window {
   757  		  day  = 7
   758  		  hour = 3
   759  			update_track = "canary"
   760  	  }
   761  	}
   762  }
   763  `
   764  
   765  var testGoogleSqlDatabaseInstance_authNets_step1 = `
   766  resource "google_sql_database_instance" "instance" {
   767  	name = "tf-lw-%d"
   768  	region = "us-central"
   769  	settings {
   770  		tier = "D0"
   771  		crash_safe_replication = false
   772  
   773  		ip_configuration {
   774  			ipv4_enabled = "true"
   775  			authorized_networks {
   776  				value = "108.12.12.12"
   777  				name = "misc"
   778  				expiration_time = "2017-11-15T16:19:00.094Z"
   779  			}
   780  		}
   781  	}
   782  }
   783  `
   784  
   785  var testGoogleSqlDatabaseInstance_authNets_step2 = `
   786  resource "google_sql_database_instance" "instance" {
   787  	name = "tf-lw-%d"
   788  	region = "us-central"
   789  	settings {
   790  		tier = "D0"
   791  		crash_safe_replication = false
   792  
   793  		ip_configuration {
   794  			ipv4_enabled = "true"
   795  		}
   796  	}
   797  }
   798  `
   799  
   800  var testGoogleSqlDatabaseInstance_multipleOperations = `
   801  resource "google_sql_database_instance" "instance" {
   802  	name = "tf-test-%s"
   803  	region = "us-central"
   804  	settings {
   805  		tier = "D0"
   806  		crash_safe_replication = false
   807  	}
   808  }
   809  
   810  resource "google_sql_database" "database" {
   811  	name = "tf-test-%s"
   812  	instance = "${google_sql_database_instance.instance.name}"
   813  }
   814  
   815  resource "google_sql_user" "user" {
   816  	name = "tf-test-%s"
   817  	instance = "${google_sql_database_instance.instance.name}"
   818  	host = "google.com"
   819  	password = "hunter2"
   820  }
   821  `