Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Bigquery Views #230

Merged
merged 9 commits into from
Aug 26, 2017
Merged

Support Bigquery Views #230

merged 9 commits into from
Aug 26, 2017

Conversation

jimmy-btn
Copy link
Contributor

Extend the Google provider for Terraform to support Bigquery Views.

Make a few drive by cleanups in the process:

  • Amend the Basic Table test to actually verify that a valid table has been created
  • Support modifying Table Schema by parsing computed IDs correctly
  • Assign a default location to Tables created before Bigquery supported two zones

Copy link
Contributor

@danawillow danawillow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jimmy-btn! Just a few comments but looks good overall.

@@ -238,6 +238,12 @@ func resourceBigQueryDatasetRead(d *schema.ResourceData, meta interface{}) error
d.Set("dataset_id", res.DatasetReference.DatasetId)
d.Set("default_table_expiration_ms", res.DefaultTableExpirationMs)

// Older Tables in BigQuery have no Location set in the API response. We can safely assume that these tables
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only going to come up during import, right? If so I wouldn't mind a comment that mentions that just to be extra clear. Also mind moving the other d.Set for location into an else with this clause?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea - Done!

MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
// Type: [Required] The only type supported is DAY, which will generate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment was accidentally pasted from a different one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was! Thank you for catching. I've updated using the language from the API documentation.

Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it makes sense to add the other view attributes, or do they not make sense in terraform? (useLegacySql, userDefinedFunctionResources)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added support for useLegacySQL - I think that makes a lot of sense.

It makes sense to capture userDefinedFunctionResources in terraform, but I'm not particularly familiar with them. I'll try and carve out some time to ramp up and get them added, but I'd recommend doing that in a second CL/pass.

@@ -202,12 +219,17 @@ func resourceTable(d *schema.ResourceData, meta interface{}) (*bigquery.Table, e
},
}

// TODO(jmcgill): How do I do nested objects in a terraform configuration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you still need this TODO?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved.

@@ -64,11 +85,40 @@ func testAccBigQueryTableExists(n string) resource.TestCheckFunc {
return fmt.Errorf("No ID is set")
}
config := testAccProvider.Meta().(*Config)
_, err := config.clientBigQuery.Tables.Get(config.Project, rs.Primary.Attributes["dataset_id"], rs.Primary.Attributes["name"]).Do()
table, err := config.clientBigQuery.Tables.Get(config.Project, rs.Primary.Attributes["dataset_id"], rs.Primary.Attributes["table_id"]).Do()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks! Want to fix that up in testAccCheckBigQueryTableDestroy too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@danawillow danawillow self-assigned this Aug 17, 2017
@danawillow
Copy link
Contributor

For tracking purposes: this fixes #190

Copy link
Contributor

@danawillow danawillow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jimmy-btn! Just a tiny bit more.

vd := &bigquery.ViewDefinition{Query: raw["query"].(string)}

if v, ok := raw["use_legacy_sql"]; ok {
vd.UseLegacySql = v.(bool)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll also need to set vd.ForceSendFields = append(vd.ForceSendFields, "UseLegacySql" so if it gets updated to false, it still gets sent along the wire (since false values end up defaulting to empty)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - thank you!

Would you like me to add an end to end test for this particular case (UseLegaclySQL = false)? Running these AF tests costs $$$ so I wasn't sure on how to balance coverage vs cost.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! You'd want to do a test specifically for updating it, since the default is false so if you set it to false explicitly on create it won't make a difference whether you forced it to send. If cost is an issue for you let me know and I'm happy to run it and post the results.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Tests added for converting useLegacySQL from true to false - The API is kind enough to reject views with incorrect syntax when using new-SQL so I am now extra confident that this is working as expected!

func flattenView(vd *bigquery.ViewDefinition) []map[string]interface{} {
result := map[string]interface{}{"query": vd.Query}

if vd.UseLegacySql {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact: this already actually gets stored in state whether it's true or false because of the way terraform handles objects, so we may as well just simplify the logic here and always set it anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Should I skip the check on line 436 (in expandView) too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually nevermind; I need to expand the value anyway. That extra error check is harmless.

Copy link
Contributor

@danawillow danawillow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jimmy-btn, looks good!

@danawillow danawillow merged commit d23e9c6 into hashicorp:master Aug 26, 2017
@jimmy-btn jimmy-btn deleted the jimmy/support-bigquery-views branch August 29, 2017 16:52
negz pushed a commit to negz/terraform-provider-google that referenced this pull request Oct 17, 2017
* Support views in Terraform.BigQuery

* Add tests for Table with view, and fix existing Table test

* Remove dead code

* run gofmt

* Address comments

* Address review comments and add support for use_legacy_sql

* Force transmission/storage of UseLegacySQL

* Trying to fix tests

* add tests for useLegacySQL
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
<!-- This change is generated by MagicModules. -->
/cc @rileykarson
@ghost
Copy link

ghost commented Mar 31, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants