diff --git a/google-beta/resource_sql_database.go b/google-beta/resource_sql_database.go index 2f9bb2e1f0..98b91b106b 100644 --- a/google-beta/resource_sql_database.go +++ b/google-beta/resource_sql_database.go @@ -318,6 +318,7 @@ func resourceSqlDatabaseImport(d *schema.ResourceData, meta interface{}) ([]*sch "instances/(?P[^/]+)/databases/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+):(?P[^/]+)", "(?P[^/]+)", }, d, config); err != nil { return nil, err diff --git a/google-beta/resource_sql_database_generated_test.go b/google-beta/resource_sql_database_generated_test.go new file mode 100644 index 0000000000..3c0c25060c --- /dev/null +++ b/google-beta/resource_sql_database_generated_test.go @@ -0,0 +1,91 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccSqlDatabase_sqlDatabaseBasicExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckSqlDatabaseDestroy, + Steps: []resource.TestStep{ + { + Config: testAccSqlDatabase_sqlDatabaseBasicExample(context), + }, + { + ResourceName: "google_sql_database.database", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccSqlDatabase_sqlDatabaseBasicExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_sql_database" "database" { + name = "my-database%{random_suffix}" + instance = "${google_sql_database_instance.instance.name}" +} + +resource "google_sql_database_instance" "instance" { + name = "my-database-instance%{random_suffix}" + region = "us-central" + settings { + tier = "D0" + } +} +`, context) +} + +func testAccCheckSqlDatabaseDestroy(s *terraform.State) error { + for name, rs := range s.RootModule().Resources { + if rs.Type != "google_sql_database" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := testAccProvider.Meta().(*Config) + + url, err := replaceVarsForTest(config, rs, "{{SqlBasePath}}projects/{{project}}/instances/{{instance}}/databases/{{name}}") + if err != nil { + return err + } + + _, err = sendRequest(config, "GET", "", url, nil) + if err == nil { + return fmt.Errorf("SqlDatabase still exists at %s", url) + } + } + + return nil +} diff --git a/website/docs/r/sql_database.html.markdown b/website/docs/r/sql_database.html.markdown index 8395517fb6..c0b4b2c04c 100644 --- a/website/docs/r/sql_database.html.markdown +++ b/website/docs/r/sql_database.html.markdown @@ -1,39 +1,52 @@ --- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in +# .github/CONTRIBUTING.md. +# +# ---------------------------------------------------------------------------- layout: "google" page_title: "Google: google_sql_database" -sidebar_current: "docs-google-sql-database-x" +sidebar_current: "docs-google-sql-database" description: |- - Creates a new SQL database in Google Cloud SQL. + Represents a SQL database inside the Cloud SQL instance, hosted in + Google's cloud. --- # google\_sql\_database -Creates a new Google SQL Database on a Google SQL Database Instance. For more information, see -the [official documentation](https://cloud.google.com/sql/), -or the [JSON API](https://cloud.google.com/sql/docs/admin-api/v1beta4/databases). +Represents a SQL database inside the Cloud SQL instance, hosted in +Google's cloud. -## Example Usage -Example creating a SQL Database. -```hcl -resource "random_id" "db_name_suffix" { - byte_length = 4 -} + +## Example Usage - Sql Database Basic -resource "google_sql_database_instance" "master" { - name = "master-instance-${random_id.db_name_suffix.hex}" - settings { - tier = "D0" - } +```hcl +resource "google_sql_database" "database" { + name = "my-database" + instance = "${google_sql_database_instance.instance.name}" } -resource "google_sql_database" "users" { - name = "users-db" - instance = "${google_sql_database_instance.master.name}" - charset = "latin1" - collation = "latin1_swedish_ci" +resource "google_sql_database_instance" "instance" { + name = "my-database-instance" + region = "us-central" + settings { + tier = "D0" + } } ``` @@ -41,43 +54,67 @@ resource "google_sql_database" "users" { The following arguments are supported: -* `name` - (Required) The name of the database. -* `instance` - (Required) The name of containing instance. +* `name` - + (Required) + The name of the database in the Cloud SQL instance. + This does not include the project ID or instance name. + +* `instance` - + (Required) + The name of the Cloud SQL instance. This does not include the project + ID. + - - - -* `project` - (Optional) The ID of the project in which the resource belongs. If it - is not provided, the provider project is used. -* `charset` - (Optional) The charset value. See MySQL's - [Supported Character Sets and Collations](https://dev.mysql.com/doc/refman/5.7/en/charset-charsets.html) - and Postgres' [Character Set Support](https://www.postgresql.org/docs/9.6/static/multibyte.html) - for more details and supported values. Postgres databases are in beta - and have limited `charset` support; they only support a value of `UTF8` at creation time. +* `charset` - + (Optional) + The charset value. See MySQL's + [Supported Character Sets and Collations](https://dev.mysql.com/doc/refman/5.7/en/charset-charsets.html) + and Postgres' [Character Set Support](https://www.postgresql.org/docs/9.6/static/multibyte.html) + for more details and supported values. Postgres databases only support + a value of `UTF8` at creation time. + +* `collation` - + (Optional) + The collation value. See MySQL's + [Supported Character Sets and Collations](https://dev.mysql.com/doc/refman/5.7/en/charset-charsets.html) + and Postgres' [Collation Support](https://www.postgresql.org/docs/9.6/static/collation.html) + for more details and supported values. Postgres databases only support + a value of `en_US.UTF8` at creation time. -* `collation` - (Optional) The collation value. See MySQL's - [Supported Character Sets and Collations](https://dev.mysql.com/doc/refman/5.7/en/charset-charsets.html) - and Postgres' [Collation Support](https://www.postgresql.org/docs/9.6/static/collation.html) - for more details and supported values. Postgres databases are in beta - and have limited `collation` support; they only support a value of `en_US.UTF8` at creation time. +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. -## Attributes Reference -In addition to the arguments listed above, the following computed attributes are -exported: -* `self_link` - The URI of the created resource. +## Timeouts + +This resource provides the following +[Timeouts](/docs/configuration/resources.html#timeouts) configuration options: + +- `create` - Default is 15 minutes. +- `update` - Default is 10 minutes. +- `delete` - Default is 10 minutes. ## Import -SQL databases can be imported using one of any of these accepted formats: +Database can be imported using any of these accepted formats: ``` -$ terraform import google_sql_database.database projects/{{project}}/instances/{{instance}}/databases/{{name}} -$ terraform import google_sql_database.database {{project}}/{{instance}}/{{name}} -$ terraform import google_sql_database.database instances/{{name}}/databases/{{name}} -$ terraform import google_sql_database.database {{instance}}/{{name}} -$ terraform import google_sql_database.database {{name}} - +$ terraform import google_sql_database.default projects/{{project}}/instances/{{instance}}/databases/{{name}} +$ terraform import google_sql_database.default instances/{{instance}}/databases/{{name}} +$ terraform import google_sql_database.default {{project}}/{{instance}}/{{name}} +$ terraform import google_sql_database.default {{instance}}/{{name}} +$ terraform import google_sql_database.default {{instance}}:{{name}} +$ terraform import google_sql_database.default {{name}} ``` + +-> If you're importing a resource with beta features, make sure to include `-provider=google-beta` +as an argument so that Terraform uses the correct provider to import your resource. + +## User Project Overrides + +This resource supports [User Project Overrides](https://www.terraform.io/docs/providers/google/provider_reference.html#user_project_override).