Skip to content

Commit 744febe

Browse files
modular-magicianrileykarson
authored andcommitted
Fix for SQL database import formats (#4279)
Signed-off-by: Modular Magician <[email protected]>
1 parent 7671abc commit 744febe

File tree

3 files changed

+175
-46
lines changed

3 files changed

+175
-46
lines changed

google/resource_sql_database.go

+1
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ func resourceSqlDatabaseImport(d *schema.ResourceData, meta interface{}) ([]*sch
318318
"instances/(?P<instance>[^/]+)/databases/(?P<name>[^/]+)",
319319
"(?P<project>[^/]+)/(?P<instance>[^/]+)/(?P<name>[^/]+)",
320320
"(?P<instance>[^/]+)/(?P<name>[^/]+)",
321+
"(?P<instance>[^/]+):(?P<name>[^/]+)",
321322
"(?P<name>[^/]+)",
322323
}, d, config); err != nil {
323324
return nil, err
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// ----------------------------------------------------------------------------
2+
//
3+
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
4+
//
5+
// ----------------------------------------------------------------------------
6+
//
7+
// This file is automatically generated by Magic Modules and manual
8+
// changes will be clobbered when the file is regenerated.
9+
//
10+
// Please read more about how to change this file in
11+
// .github/CONTRIBUTING.md.
12+
//
13+
// ----------------------------------------------------------------------------
14+
15+
package google
16+
17+
import (
18+
"fmt"
19+
"strings"
20+
"testing"
21+
22+
"github.com/hashicorp/terraform/helper/acctest"
23+
"github.com/hashicorp/terraform/helper/resource"
24+
"github.com/hashicorp/terraform/terraform"
25+
)
26+
27+
func TestAccSqlDatabase_sqlDatabaseBasicExample(t *testing.T) {
28+
t.Parallel()
29+
30+
context := map[string]interface{}{
31+
"random_suffix": acctest.RandString(10),
32+
}
33+
34+
resource.Test(t, resource.TestCase{
35+
PreCheck: func() { testAccPreCheck(t) },
36+
Providers: testAccProviders,
37+
CheckDestroy: testAccCheckSqlDatabaseDestroy,
38+
Steps: []resource.TestStep{
39+
{
40+
Config: testAccSqlDatabase_sqlDatabaseBasicExample(context),
41+
},
42+
{
43+
ResourceName: "google_sql_database.database",
44+
ImportState: true,
45+
ImportStateVerify: true,
46+
},
47+
},
48+
})
49+
}
50+
51+
func testAccSqlDatabase_sqlDatabaseBasicExample(context map[string]interface{}) string {
52+
return Nprintf(`
53+
resource "google_sql_database" "database" {
54+
name = "my-database%{random_suffix}"
55+
instance = "${google_sql_database_instance.instance.name}"
56+
}
57+
58+
resource "google_sql_database_instance" "instance" {
59+
name = "my-database-instance%{random_suffix}"
60+
region = "us-central"
61+
settings {
62+
tier = "D0"
63+
}
64+
}
65+
`, context)
66+
}
67+
68+
func testAccCheckSqlDatabaseDestroy(s *terraform.State) error {
69+
for name, rs := range s.RootModule().Resources {
70+
if rs.Type != "google_sql_database" {
71+
continue
72+
}
73+
if strings.HasPrefix(name, "data.") {
74+
continue
75+
}
76+
77+
config := testAccProvider.Meta().(*Config)
78+
79+
url, err := replaceVarsForTest(config, rs, "{{SqlBasePath}}projects/{{project}}/instances/{{instance}}/databases/{{name}}")
80+
if err != nil {
81+
return err
82+
}
83+
84+
_, err = sendRequest(config, "GET", "", url, nil)
85+
if err == nil {
86+
return fmt.Errorf("SqlDatabase still exists at %s", url)
87+
}
88+
}
89+
90+
return nil
91+
}
+83-46
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,120 @@
11
---
2+
# ----------------------------------------------------------------------------
3+
#
4+
# *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
5+
#
6+
# ----------------------------------------------------------------------------
7+
#
8+
# This file is automatically generated by Magic Modules and manual
9+
# changes will be clobbered when the file is regenerated.
10+
#
11+
# Please read more about how to change this file in
12+
# .github/CONTRIBUTING.md.
13+
#
14+
# ----------------------------------------------------------------------------
215
layout: "google"
316
page_title: "Google: google_sql_database"
4-
sidebar_current: "docs-google-sql-database-x"
17+
sidebar_current: "docs-google-sql-database"
518
description: |-
6-
Creates a new SQL database in Google Cloud SQL.
19+
Represents a SQL database inside the Cloud SQL instance, hosted in
20+
Google's cloud.
721
---
822

923
# google\_sql\_database
1024

11-
Creates a new Google SQL Database on a Google SQL Database Instance. For more information, see
12-
the [official documentation](https://cloud.google.com/sql/),
13-
or the [JSON API](https://cloud.google.com/sql/docs/admin-api/v1beta4/databases).
25+
Represents a SQL database inside the Cloud SQL instance, hosted in
26+
Google's cloud.
1427

15-
## Example Usage
1628

17-
Example creating a SQL Database.
1829

19-
```hcl
20-
resource "random_id" "db_name_suffix" {
21-
byte_length = 4
22-
}
30+
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
31+
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jpy.wang%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">
32+
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
33+
</a>
34+
</div>
35+
## Example Usage - Sql Database Basic
2336

24-
resource "google_sql_database_instance" "master" {
25-
name = "master-instance-${random_id.db_name_suffix.hex}"
2637

27-
settings {
28-
tier = "D0"
29-
}
38+
```hcl
39+
resource "google_sql_database" "database" {
40+
name = "my-database"
41+
instance = "${google_sql_database_instance.instance.name}"
3042
}
3143
32-
resource "google_sql_database" "users" {
33-
name = "users-db"
34-
instance = "${google_sql_database_instance.master.name}"
35-
charset = "latin1"
36-
collation = "latin1_swedish_ci"
44+
resource "google_sql_database_instance" "instance" {
45+
name = "my-database-instance"
46+
region = "us-central"
47+
settings {
48+
tier = "D0"
49+
}
3750
}
3851
```
3952

4053
## Argument Reference
4154

4255
The following arguments are supported:
4356

44-
* `name` - (Required) The name of the database.
4557

46-
* `instance` - (Required) The name of containing instance.
58+
* `name` -
59+
(Required)
60+
The name of the database in the Cloud SQL instance.
61+
This does not include the project ID or instance name.
62+
63+
* `instance` -
64+
(Required)
65+
The name of the Cloud SQL instance. This does not include the project
66+
ID.
67+
4768

4869
- - -
4970

50-
* `project` - (Optional) The ID of the project in which the resource belongs. If it
51-
is not provided, the provider project is used.
5271

53-
* `charset` - (Optional) The charset value. See MySQL's
54-
[Supported Character Sets and Collations](https://dev.mysql.com/doc/refman/5.7/en/charset-charsets.html)
55-
and Postgres' [Character Set Support](https://www.postgresql.org/docs/9.6/static/multibyte.html)
56-
for more details and supported values. Postgres databases are in beta
57-
and have limited `charset` support; they only support a value of `UTF8` at creation time.
72+
* `charset` -
73+
(Optional)
74+
The charset value. See MySQL's
75+
[Supported Character Sets and Collations](https://dev.mysql.com/doc/refman/5.7/en/charset-charsets.html)
76+
and Postgres' [Character Set Support](https://www.postgresql.org/docs/9.6/static/multibyte.html)
77+
for more details and supported values. Postgres databases only support
78+
a value of `UTF8` at creation time.
79+
80+
* `collation` -
81+
(Optional)
82+
The collation value. See MySQL's
83+
[Supported Character Sets and Collations](https://dev.mysql.com/doc/refman/5.7/en/charset-charsets.html)
84+
and Postgres' [Collation Support](https://www.postgresql.org/docs/9.6/static/collation.html)
85+
for more details and supported values. Postgres databases only support
86+
a value of `en_US.UTF8` at creation time.
5887

59-
* `collation` - (Optional) The collation value. See MySQL's
60-
[Supported Character Sets and Collations](https://dev.mysql.com/doc/refman/5.7/en/charset-charsets.html)
61-
and Postgres' [Collation Support](https://www.postgresql.org/docs/9.6/static/collation.html)
62-
for more details and supported values. Postgres databases are in beta
63-
and have limited `collation` support; they only support a value of `en_US.UTF8` at creation time.
88+
* `project` - (Optional) The ID of the project in which the resource belongs.
89+
If it is not provided, the provider project is used.
6490

65-
## Attributes Reference
6691

67-
In addition to the arguments listed above, the following computed attributes are
68-
exported:
6992

70-
* `self_link` - The URI of the created resource.
93+
## Timeouts
94+
95+
This resource provides the following
96+
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:
97+
98+
- `create` - Default is 15 minutes.
99+
- `update` - Default is 10 minutes.
100+
- `delete` - Default is 10 minutes.
71101

72102
## Import
73103

74-
SQL databases can be imported using one of any of these accepted formats:
104+
Database can be imported using any of these accepted formats:
75105

76106
```
77-
$ terraform import google_sql_database.database projects/{{project}}/instances/{{instance}}/databases/{{name}}
78-
$ terraform import google_sql_database.database {{project}}/{{instance}}/{{name}}
79-
$ terraform import google_sql_database.database instances/{{name}}/databases/{{name}}
80-
$ terraform import google_sql_database.database {{instance}}/{{name}}
81-
$ terraform import google_sql_database.database {{name}}
82-
107+
$ terraform import google_sql_database.default projects/{{project}}/instances/{{instance}}/databases/{{name}}
108+
$ terraform import google_sql_database.default instances/{{instance}}/databases/{{name}}
109+
$ terraform import google_sql_database.default {{project}}/{{instance}}/{{name}}
110+
$ terraform import google_sql_database.default {{instance}}/{{name}}
111+
$ terraform import google_sql_database.default {{instance}}:{{name}}
112+
$ terraform import google_sql_database.default {{name}}
83113
```
114+
115+
-> If you're importing a resource with beta features, make sure to include `-provider=google-beta`
116+
as an argument so that Terraform uses the correct provider to import your resource.
117+
118+
## User Project Overrides
119+
120+
This resource supports [User Project Overrides](https://www.terraform.io/docs/providers/google/provider_reference.html#user_project_override).

0 commit comments

Comments
 (0)