Skip to content

Commit

Permalink
Fix reading connection_id from API (#4319) (#2792)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Dec 16, 2020
1 parent 089bdff commit b82e8be
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/4319.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
bigquery: fixed a bug in `google_bigquery_connection` that caused the resource to function incorrectly when `connection_id` was unset
```
43 changes: 43 additions & 0 deletions google-beta/resource_bigquery_connection_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func resourceBigqueryConnectionConnection() *schema.Resource {
},
"connection_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Optional connection id that should be assigned to the created connection.`,
Expand Down Expand Up @@ -145,6 +146,12 @@ func resourceBigqueryConnectionConnectionCreate(d *schema.ResourceData, meta int
}

obj := make(map[string]interface{})
connection_idProp, err := expandBigqueryConnectionConnectionConnectionId(d.Get("connection_id"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("connection_id"); !isEmptyValue(reflect.ValueOf(connection_idProp)) && (ok || !reflect.DeepEqual(v, connection_idProp)) {
obj["connection_id"] = connection_idProp
}
friendlyNameProp, err := expandBigqueryConnectionConnectionFriendlyName(d.Get("friendly_name"), d, config)
if err != nil {
return err
Expand All @@ -164,6 +171,11 @@ func resourceBigqueryConnectionConnectionCreate(d *schema.ResourceData, meta int
obj["cloudSql"] = cloudSqlProp
}

obj, err = resourceBigqueryConnectionConnectionEncoder(d, meta, obj)
if err != nil {
return err
}

url, err := replaceVars(d, config, "{{BigqueryConnectionBasePath}}projects/{{project}}/locations/{{location}}/connections?connectionId={{connection_id}}")
if err != nil {
return err
Expand Down Expand Up @@ -200,6 +212,14 @@ func resourceBigqueryConnectionConnectionCreate(d *schema.ResourceData, meta int

log.Printf("[DEBUG] Finished creating Connection %q: %#v", d.Id(), res)

if isEmptyValue(reflect.ValueOf(d.Get("connection_id"))) {
// connection id is set by API when unset and required to GET the connection
// it is set by reading the "name" field rather than a field in the response
if err := d.Set("connection_id", flattenBigqueryConnectionConnectionConnectionId("", d, config)); err != nil {
return fmt.Errorf("Error reading Connection: %s", err)
}
}

return resourceBigqueryConnectionConnectionRead(d, meta)
}

Expand Down Expand Up @@ -240,6 +260,9 @@ func resourceBigqueryConnectionConnectionRead(d *schema.ResourceData, meta inter
if err := d.Set("name", flattenBigqueryConnectionConnectionName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading Connection: %s", err)
}
if err := d.Set("connection_id", flattenBigqueryConnectionConnectionConnectionId(res["connection_id"], d, config)); err != nil {
return fmt.Errorf("Error reading Connection: %s", err)
}
if err := d.Set("friendly_name", flattenBigqueryConnectionConnectionFriendlyName(res["friendlyName"], d, config)); err != nil {
return fmt.Errorf("Error reading Connection: %s", err)
}
Expand Down Expand Up @@ -291,6 +314,11 @@ func resourceBigqueryConnectionConnectionUpdate(d *schema.ResourceData, meta int
obj["cloudSql"] = cloudSqlProp
}

obj, err = resourceBigqueryConnectionConnectionEncoder(d, meta, obj)
if err != nil {
return err
}

url, err := replaceVars(d, config, "{{BigqueryConnectionBasePath}}projects/{{project}}/locations/{{location}}/connections/{{connection_id}}")
if err != nil {
return err
Expand Down Expand Up @@ -394,6 +422,11 @@ func flattenBigqueryConnectionConnectionName(v interface{}, d *schema.ResourceDa
return v
}

func flattenBigqueryConnectionConnectionConnectionId(v interface{}, d *schema.ResourceData, config *Config) interface{} {
parts := strings.Split(d.Get("name").(string), "/")
return parts[len(parts)-1]
}

func flattenBigqueryConnectionConnectionFriendlyName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
Expand Down Expand Up @@ -446,6 +479,10 @@ func flattenBigqueryConnectionConnectionCloudSqlType(v interface{}, d *schema.Re
return v
}

func expandBigqueryConnectionConnectionConnectionId(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandBigqueryConnectionConnectionFriendlyName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -539,3 +576,9 @@ func expandBigqueryConnectionConnectionCloudSqlCredentialPassword(v interface{},
func expandBigqueryConnectionConnectionCloudSqlType(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func resourceBigqueryConnectionConnectionEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
// connection_id is needed to qualify the URL but cannot be sent in the body
delete(obj, "connection_id")
return obj, nil
}
2 changes: 1 addition & 1 deletion google-beta/resource_dataflow_flex_template_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/compute/v1"
)

func TestAccDataflowFlexTemplateJob_basic(t *testing.T) {
Expand Down

0 comments on commit b82e8be

Please sign in to comment.