Skip to content

Commit

Permalink
Add credentials to bigquery connection. (#3546)
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-henderson authored May 27, 2020
1 parent 295991b commit bd27a0d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
13 changes: 13 additions & 0 deletions products/bigqueryconnection/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ objects:
name: 'database'
description: Database name.
required: true
- !ruby/object:Api::Type::NestedObject
name: credential
description: Cloud SQL properties.
required: true
properties:
- !ruby/object:Api::Type::String
name: username
description: Username for database.
required: true
- !ruby/object:Api::Type::String
name: password
description: Password for database.
required: true
- !ruby/object:Api::Type::Enum
name: 'type'
description: Type of the Cloud SQL database.
Expand Down
7 changes: 7 additions & 0 deletions products/bigqueryconnection/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
legacy_name: bigquery
overrides: !ruby/object:Overrides::ResourceOverrides
Connection: !ruby/object:Overrides::Terraform::ResourceOverride
properties:
cloudSql.credential: !ruby/object:Overrides::Terraform::PropertyOverride
custom_flatten: 'templates/terraform/custom_flatten/bigquery_connection_flatten.go.erb'
cloudSql.credential.password: !ruby/object:Overrides::Terraform::PropertyOverride
sensitive: true
id_format: "{{name}}"
import_format: ["{{name}}"]
examples:
Expand All @@ -24,12 +29,14 @@ overrides: !ruby/object:Overrides::ResourceOverrides
primary_resource_id: "connection"
vars:
database_instance_name: "my-database-instance"
username: "user"
- !ruby/object:Provider::Terraform::Examples
min_version: beta
name: "bigquery_connection_full"
primary_resource_id: "connection"
vars:
database_instance_name: "my-database-instance"
username: "user"
connection_id: "my-connection"
# This is for copying files over
files: !ruby/object:Provider::Config::Files
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%# The license inside this block applies to this file.
# Copyright 2020 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
-%>
func flatten<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return []interface{}{
map[string]interface{}{
"username": d.Get("cloud_sql.0.credential.0.username"),
"password": d.Get("cloud_sql.0.credential.0.password"),
},
}
}
16 changes: 16 additions & 0 deletions templates/terraform/examples/bigquery_connection_basic.tf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ resource "google_sql_database" "db" {
name = "db"
}

resource "random_password" "pwd" {
length = 16
special = false
}

resource "google_sql_user" "user" {
provider = google-beta
name = "<%= ctx[:vars]['username'] %>"
instance = google_sql_database_instance.instance.name
password = random_password.pwd.result
}

resource "google_bigquery_connection" "<%= ctx[:primary_resource_id] %>" {
provider = google-beta
friendly_name = "👋"
Expand All @@ -22,5 +34,9 @@ resource "google_bigquery_connection" "<%= ctx[:primary_resource_id] %>" {
instance_id = google_sql_database_instance.instance.connection_name
database = google_sql_database.db.name
type = "POSTGRES"
credential {
username = google_sql_user.user.name
password = google_sql_user.user.password
}
}
}
16 changes: 16 additions & 0 deletions templates/terraform/examples/bigquery_connection_full.tf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ resource "google_sql_database" "db" {
name = "db"
}

resource "random_password" "pwd" {
length = 16
special = false
}

resource "google_sql_user" "user" {
provider = google-beta
name = "<%= ctx[:vars]['username'] %>"
instance = google_sql_database_instance.instance.name
password = random_password.pwd.result
}

resource "google_bigquery_connection" "<%= ctx[:primary_resource_id] %>" {
provider = google-beta
connection_id = "<%= ctx[:vars]['connection_id'] %>"
Expand All @@ -24,5 +36,9 @@ resource "google_bigquery_connection" "<%= ctx[:primary_resource_id] %>" {
instance_id = google_sql_database_instance.instance.connection_name
database = google_sql_database.db.name
type = "POSTGRES"
credential {
username = google_sql_user.user.name
password = google_sql_user.user.password
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ resource "google_sql_database" "db" {
name = "db"
}

resource "random_password" "pwd" {
length = 16
special = false
}

resource "google_sql_user" "user" {
provider = google-beta
name = "username"
instance = google_sql_database_instance.instance.name
password = random_password.pwd.result
}

resource "google_bigquery_connection" "connection" {
provider = google-beta
connection_id = "tf-test-my-connection%{random_suffix}"
Expand All @@ -58,6 +70,10 @@ resource "google_bigquery_connection" "connection" {
instance_id = google_sql_database_instance.instance.connection_name
database = google_sql_database.db.name
type = "POSTGRES"
credential {
username = google_sql_user.user.name
password = google_sql_user.user.password
}
}
}
`, context)
Expand All @@ -81,6 +97,18 @@ resource "google_sql_database" "db" {
name = "db2"
}

resource "random_password" "pwd" {
length = 16
special = false
}

resource "google_sql_user" "user" {
provider = google-beta
name = "username"
instance = google_sql_database_instance.instance.name
password = random_password.pwd.result
}

resource "google_bigquery_connection" "connection" {
provider = google-beta
connection_id = "tf-test-my-connection%{random_suffix}"
Expand All @@ -91,6 +119,10 @@ resource "google_bigquery_connection" "connection" {
instance_id = google_sql_database_instance.instance.connection_name
database = google_sql_database.db.name
type = "MYSQL"
credential {
username = google_sql_user.user.name
password = google_sql_user.user.password
}
}
}
`, context)
Expand Down

0 comments on commit bd27a0d

Please sign in to comment.