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

feature(bigquery): add features for BigQuery access policies #5144

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

OremGLG
Copy link
Contributor

@OremGLG OremGLG commented Feb 12, 2025

Description

Add sample for viewing access policy to dataset

Step of Internal: b/394478489

https://cloud.google.com/bigquery/docs/control-access-to-resources-iam#view_the_access_policy_of_a_dataset
https://cloud.google.com/bigquery/docs/control-access-to-resources-iam#view_the_access_policy_of_a_table_or_view
https://cloud.google.com/bigquery/docs/control-access-to-resources-iam#grant_access_to_a_table_or_view
https://cloud.google.com/bigquery/docs/control-access-to-resources-iam#revoke_access_to_a_table_or_view

Checklist

  • I have followed Contributing Guidelines from CONTRIBUTING.MD
  • Tests pass: go test -v ./.. (see Testing)
  • Code formatted: gofmt (see Formatting)
  • Vetting pass: go vet (see Formatting)
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

@product-auto-label product-auto-label bot added api: bigquery Issues related to the BigQuery API. samples Issues that are directly related to samples. labels Feb 12, 2025

ctx := context.Background()

// Creates bq client.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest not using bq, as it could be confusing for newcomers, perhaps "BigQuery client", or only "Client".
For Python I'm using:

# Instantiate a client.
    bigquery_client = bigquery.Client()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll update the comments

}

// Table schema.
sampleSchema := bigquery.Schema{
Copy link
Contributor

@eapl-gemugami eapl-gemugami Feb 14, 2025

Choose a reason for hiding this comment

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

If the test doesn't require a specific table schema, you could use something simpler.
For Python I'm using

sample_schema = [
    bigquery.SchemaField("id", "INTEGER", mode="REQUIRED"),
]

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 think that you meant to create a simplier schema, because the schema should be created anyways. I'll make it simplier

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, I've reworded for clarity, thanks!


// Sets view query.
viewMetadata := &bigquery.TableMetadata{
ViewQuery: fmt.Sprintf("SELECT UPPER(full_name) FROM `%s.%s` ORDER BY age ASC", datasetName, tableName),
Copy link
Contributor

@eapl-gemugami eapl-gemugami Feb 14, 2025

Choose a reason for hiding this comment

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

Following my previous comment, as the tests need to be as simple as possible (as per David's comment), I'd suggest going with something like
view.view_query = f"SELECT * FROM {table}"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, following that

func TestGrantAccessTable(t *testing.T) {
tc := testutil.SystemTest(t)

datasetName := "my_new_dataset_go"
Copy link
Contributor

@eapl-gemugami eapl-gemugami Feb 14, 2025

Choose a reason for hiding this comment

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

Check your best practices for Go's testing. I don't think 'my_new_dataset_go' is the right name in this test, as sounds more suited for samples. I'd advise of using a name related with the sample you are running.

In Python a Prefixer is being used to avoid possible conflicts among test runs:

PREFIX = prefixer.create_prefix()
DATASET_ID = f"{PREFIX}_view_access_policies"
TABLE_NAME = f"{PREFIX}_view_access_policies_table"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. It makes sense if tests are running concurrently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the BigQuery API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants