Skip to content

Commit

Permalink
Fix for SQL database import formats
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
rileykarson authored and modular-magician committed Aug 16, 2019
1 parent 6fe812f commit 93b03c5
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 46 deletions.
1 change: 1 addition & 0 deletions google/resource_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func resourceSqlDatabaseImport(d *schema.ResourceData, meta interface{}) ([]*sch
"instances/(?P<instance>[^/]+)/databases/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<instance>[^/]+)/(?P<name>[^/]+)",
"(?P<instance>[^/]+)/(?P<name>[^/]+)",
"(?P<instance>[^/]+):(?P<name>[^/]+)",
"(?P<name>[^/]+)",
}, d, config); err != nil {
return nil, err
Expand Down
91 changes: 91 additions & 0 deletions google/resource_sql_database_generated_test.go
Original file line number Diff line number Diff line change
@@ -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
}
129 changes: 83 additions & 46 deletions website/docs/r/sql_database.html.markdown
Original file line number Diff line number Diff line change
@@ -1,83 +1,120 @@
---
# ----------------------------------------------------------------------------
#
# *** 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
}
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=sql_database_basic&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## 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"
}
}
```

## Argument Reference

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).

0 comments on commit 93b03c5

Please sign in to comment.