Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Set bigquery connection import id correctly. (#4159)
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Nov 30, 2020
commit 3dba879716c6b70ac77349d0f91f537d8346a523
3 changes: 3 additions & 0 deletions .changelog/4159.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
bigqueryconnection: fixed failure to import a resource if it has a non-default project or location.
```
14 changes: 8 additions & 6 deletions google-beta/resource_bigquery_connection_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func resourceBigqueryConnectionConnectionCreate(d *schema.ResourceData, meta int
}

// Store the ID now
id, err := replaceVars(d, config, "{{name}}")
id, err := replaceVars(d, config, "projects/{{project}}/locations/{{location}}/connections/{{connection_id}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
Expand All @@ -210,7 +210,7 @@ func resourceBigqueryConnectionConnectionRead(d *schema.ResourceData, meta inter
return err
}

url, err := replaceVars(d, config, "{{BigqueryConnectionBasePath}}{{name}}")
url, err := replaceVars(d, config, "{{BigqueryConnectionBasePath}}projects/{{project}}/locations/{{location}}/connections/{{connection_id}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -291,7 +291,7 @@ func resourceBigqueryConnectionConnectionUpdate(d *schema.ResourceData, meta int
obj["cloudSql"] = cloudSqlProp
}

url, err := replaceVars(d, config, "{{BigqueryConnectionBasePath}}{{name}}")
url, err := replaceVars(d, config, "{{BigqueryConnectionBasePath}}projects/{{project}}/locations/{{location}}/connections/{{connection_id}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -348,7 +348,7 @@ func resourceBigqueryConnectionConnectionDelete(d *schema.ResourceData, meta int
}
billingProject = project

url, err := replaceVars(d, config, "{{BigqueryConnectionBasePath}}{{name}}")
url, err := replaceVars(d, config, "{{BigqueryConnectionBasePath}}projects/{{project}}/locations/{{location}}/connections/{{connection_id}}")
if err != nil {
return err
}
Expand All @@ -373,13 +373,15 @@ func resourceBigqueryConnectionConnectionDelete(d *schema.ResourceData, meta int
func resourceBigqueryConnectionConnectionImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"(?P<name>[^/]+)",
"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/connections/(?P<connection_id>[^/]+)",
"(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<connection_id>[^/]+)",
"(?P<location>[^/]+)/(?P<connection_id>[^/]+)",
}, d, config); err != nil {
return nil, err
}

// Replace import id for the resource id
id, err := replaceVars(d, config, "{{name}}")
id, err := replaceVars(d, config, "projects/{{project}}/locations/{{location}}/connections/{{connection_id}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func testAccCheckBigqueryConnectionConnectionDestroyProducer(t *testing.T) func(

config := googleProviderConfig(t)

url, err := replaceVarsForTest(config, rs, "{{BigqueryConnectionBasePath}}{{name}}")
url, err := replaceVarsForTest(config, rs, "{{BigqueryConnectionBasePath}}projects/{{project}}/locations/{{location}}/connections/{{connection_id}}")
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,23 @@ func testSweepBigqueryConnectionConnection(region string) error {
nonPrefixCount := 0
for _, ri := range rl {
obj := ri.(map[string]interface{})
if obj["name"] == nil {
log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName)
var name string
// Id detected in the delete URL, attempt to use id.
if obj["id"] != nil {
name = GetResourceNameFromSelfLink(obj["id"].(string))
} else if obj["name"] != nil {
name = GetResourceNameFromSelfLink(obj["name"].(string))
} else {
log.Printf("[INFO][SWEEPER_LOG] %s resource name and id were nil", resourceName)
return nil
}

name := GetResourceNameFromSelfLink(obj["name"].(string))
// Skip resources that shouldn't be sweeped
if !isSweepableTestResource(name) {
nonPrefixCount++
continue
}

deleteTemplate := "https://bigqueryconnection.googleapis.com/v1beta1/{{name}}"
deleteTemplate := "https://bigqueryconnection.googleapis.com/v1beta1/projects/{{project}}/locations/{{location}}/connections/{{connection_id}}"
deleteUrl, err := replaceVars(d, config, deleteTemplate)
if err != nil {
log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err)
Expand Down
12 changes: 12 additions & 0 deletions google-beta/resource_bigquery_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@ func TestAccBigqueryConnectionConnection_bigqueryConnectionBasic(t *testing.T) {
{
Config: testAccBigqueryConnectionConnection_bigqueryConnectionBasic(context),
},
{
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"cloud_sql.0.credential.0.password", "cloud_sql.0.credential.0.username"},
ResourceName: "google_bigquery_connection.connection",
},
{
Config: testAccBigqueryConnectionConnection_bigqueryConnectionBasicUpdate(context),
},
{
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"cloud_sql.0.credential.0.password", "cloud_sql.0.credential.0.username"},
ResourceName: "google_bigquery_connection.connection",
},
},
})
}
Expand Down
6 changes: 4 additions & 2 deletions website/docs/r/bigquery_connection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ The `credential` block supports:

In addition to the arguments listed above, the following computed attributes are exported:

* `id` - an identifier for the resource with format `{{name}}`
* `id` - an identifier for the resource with format `projects/{{project}}/locations/{{location}}/connections/{{connection_id}}`

* `name` -
The resource name of the connection in the form of:
Expand All @@ -245,7 +245,9 @@ This resource provides the following
Connection can be imported using any of these accepted formats:

```
$ terraform import google_bigquery_connection.default {{name}}
$ terraform import google_bigquery_connection.default projects/{{project}}/locations/{{location}}/connections/{{connection_id}}
$ terraform import google_bigquery_connection.default {{project}}/{{location}}/{{connection_id}}
$ terraform import google_bigquery_connection.default {{location}}/{{connection_id}}
```

## User Project Overrides
Expand Down