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

feat(bigquery): add table/view samples to cloud-client (3/3) #10036

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

alarconesparza
Copy link
Contributor

@alarconesparza alarconesparza commented Mar 3, 2025

Description

Part of internal b/394478489

Third part of bigquery cloud-client (3/3).

(#10034, #10035, #10036)

This part adds samples 2, 4 and 6 from the following list:

Samples

  1. bigquery_view_dataset_access policy
  2. bigquery_view_table_or_view_access_policy
  3. bigquery_grant_access_to_dataset
  4. bigquery_grant_access_to_table_or_view
  5. bigquery_revoke_dataset_access
  6. bigquery_revoke_access_to_table_or_view

Some of the samples are part of java-bigquery/.../bigquery repo, but are deprecated or the content does not match the docs.

Checklist

  • I have followed Sample Format Guide
  • pom.xml parent set to latest shared-configuration
  • Appropriate changes to README are included in PR
  • 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)
  • Tests pass: mvn clean verify required
  • Lint passes: mvn -P lint checkstyle:check required
  • Static Analysis: mvn -P lint clean compile pmd:cpd-check spotbugs:check advisory only
  • 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 Mar 3, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @alarconesparza, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request adds samples to the google-cloud-java BigQuery client library for managing table and view access policies. Specifically, it introduces samples for viewing, granting, and revoking access to tables or views. The changes include the addition of new Java files containing the sample code, a pom.xml file for managing dependencies, and integration tests to verify the functionality of the samples.

Highlights

  • New Samples: Adds samples for viewing, granting, and revoking access to BigQuery tables and views.
  • IAM Policy Management: Demonstrates how to programmatically manage IAM policies for BigQuery resources using the Java client library.
  • Integration Tests: Includes integration tests to ensure the samples function correctly in a real BigQuery environment.

Changelog

Click here to see the changelog
  • bigquery/cloud-client/snippets/pom.xml
    • Added a new pom.xml file to manage dependencies for the BigQuery samples.
    • Configured the project with necessary dependencies like google-cloud-bigquery, junit, and google-truth.
    • Set the parent pom to the latest shared-configuration version 1.2.0.
  • bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GetTableOrViewAccessPolicy.java
    • Created a new sample to view the access policy of a BigQuery table or view.
    • Uses BigQueryOptions to initialize the BigQuery client.
    • Retrieves the IAM policy for a given table or view using TableId.
    • Prints the IAM policy details to the console.
  • bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GrantAccessToTableOrView.java
    • Created a new sample to grant access to a BigQuery table or view.
    • Adds a new user identity to the current IAM policy with a specified role.
    • Updates the IAM policy using bigquery.setIamPolicy().
  • bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/RevokeAccessToTableOrView.java
    • Created a new sample to revoke access to a BigQuery table or view.
    • Removes specified identities or roles from the IAM policy bindings.
    • Updates the IAM policy using bigquery.setIamPolicy().
    • Handles cases where roles or identities might be empty after removal.
  • bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GetTableOrViewAccessPolicyIT.java
    • Added integration tests for the GetTableOrViewAccessPolicy sample.
    • Sets up temporary datasets, tables, and views for testing.
    • Verifies that the sample retrieves the IAM policy successfully.
    • Uses RemoteBigQueryHelper to manage BigQuery resources.
  • bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GrantAccessToTableOrViewIT.java
    • Added integration tests for the GrantAccessToTableOrView sample.
    • Sets up temporary datasets, tables, and views for testing.
    • Verifies that the sample grants access successfully.
    • Uses RemoteBigQueryHelper to manage BigQuery resources.
  • bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/RevokeAccessToTableOrViewIT.java
    • Added integration tests for the RevokeAccessToTableOrView sample.
    • Sets up temporary datasets, tables, and views for testing.
    • Verifies that the sample revokes access successfully.
    • Uses RemoteBigQueryHelper to manage BigQuery resources.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Trivia time!

What is the primary use case for BigQuery?

Click here for the answer
BigQuery is primarily used for analyzing large datasets. It is often used for business intelligence, data warehousing, and ad-hoc querying.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request introduces new samples for managing BigQuery table/view access policies. The code is generally well-structured and follows common Java conventions. However, there are a few areas that could be improved for clarity and correctness.

Summary of Findings

  • TODO comments: The code contains TODO comments that should be addressed before merging. Consider providing a more specific implementation or removing the comment if it's no longer relevant.
  • Inmutable vs Immutable: There is a typo in the RevokeAccessToTableOrView.java file, where inmutable should be immutable.

Merge Readiness

The pull request is not quite ready for merging. The TODO comments should be addressed, and the typo in RevokeAccessToTableOrView.java should be fixed. Once these issues are resolved, the code should be ready for another review and potential merging. I am unable to directly approve this pull request, and recommend that others review and approve this code before merging.

Comment on lines +30 to +34
// TODO(developer): Replace these variables before running the sample.
// Project, dataset and resource (table or view) from which to get the access policy.
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String resourceName = "MY_RESOURCE_NAME";

Choose a reason for hiding this comment

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

medium

Consider using environment variables for these values instead of hardcoding them, to make the sample more configurable and secure. This would also align with the checklist item regarding environment variables.

Suggested change
// TODO(developer): Replace these variables before running the sample.
// Project, dataset and resource (table or view) from which to get the access policy.
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String resourceName = "MY_RESOURCE_NAME";
// Project, dataset and resource (table or view) from which to get the access policy.
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
String datasetName = System.getenv("BIGQUERY_DATASET_NAME");
String resourceName = System.getenv("BIGQUERY_RESOURCE_NAME");

Comment on lines +31 to +35
// TODO(developer): Replace these variables before running the sample.
// Project, dataset and resource (table or view) from which to get the access policy.
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String resourceName = "MY_TABLE_NAME";

Choose a reason for hiding this comment

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

medium

Consider using environment variables for these values instead of hardcoding them, to make the sample more configurable and secure. This would also align with the checklist item regarding environment variables.

Suggested change
// TODO(developer): Replace these variables before running the sample.
// Project, dataset and resource (table or view) from which to get the access policy.
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String resourceName = "MY_TABLE_NAME";
// Project, dataset and resource (table or view) from which to get the access policy.
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
String datasetName = System.getenv("BIGQUERY_DATASET_NAME");
String resourceName = System.getenv("BIGQUERY_RESOURCE_NAME");

Comment on lines +37 to +39
Role role = Role.of("roles/bigquery.dataViewer");
// Identity to add to the policy access
Identity identity = Identity.user("[email protected]");

Choose a reason for hiding this comment

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

medium

Consider using environment variables for these values instead of hardcoding them, to make the sample more configurable and secure. This would also align with the checklist item regarding environment variables.

Suggested change
Role role = Role.of("roles/bigquery.dataViewer");
// Identity to add to the policy access
Identity identity = Identity.user("[email protected]");
// Role to add to the policy access
Role role = Role.of(System.getenv("BIGQUERY_ROLE"));
// Identity to add to the policy access
Identity identity = Identity.user(System.getenv("BIGQUERY_USER"));

Comment on lines +35 to +39
// TODO(developer): Replace these variables before running the sample.
// Project, dataset and resource (table or view) from which to get the access policy
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String resourceName = "MY_RESOURCE_NAME";

Choose a reason for hiding this comment

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

medium

Consider using environment variables for these values instead of hardcoding them, to make the sample more configurable and secure. This would also align with the checklist item regarding environment variables.

Suggested change
// TODO(developer): Replace these variables before running the sample.
// Project, dataset and resource (table or view) from which to get the access policy
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String resourceName = "MY_RESOURCE_NAME";
// Project, dataset and resource (table or view) from which to get the access policy
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
String datasetName = System.getenv("BIGQUERY_DATASET_NAME");
String resourceName = System.getenv("BIGQUERY_RESOURCE_NAME");

Comment on lines +41 to +43
Role role = Role.of("roles/bigquery.dataViewer");
// Identity to remove from the access policy
Identity user = Identity.user("[email protected]");

Choose a reason for hiding this comment

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

medium

Consider using environment variables for these values instead of hardcoding them, to make the sample more configurable and secure. This would also align with the checklist item regarding environment variables.

Suggested change
Role role = Role.of("roles/bigquery.dataViewer");
// Identity to remove from the access policy
Identity user = Identity.user("[email protected]");
// Role to remove from the access policy
Role role = Role.of(System.getenv("BIGQUERY_ROLE"));
// Identity to remove from the access policy
Identity user = Identity.user(System.getenv("BIGQUERY_USER"));

// Remove either identities or roles, or both from bindings and replace it in
// the current IAM policy.
Policy policy = bigquery.getIamPolicy(tableId);
// Create a copy of a inmutable map.

Choose a reason for hiding this comment

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

medium

Typo: inmutable should be immutable.

Suggested change
// Create a copy of a inmutable map.
// Create a copy of an immutable map.

// Remove one identity in all the existing roles.
for (Role roleKey : bindings.keySet()) {
if (bindings.get(roleKey).contains(identity)) {
// Create a copy of a inmutable set if the identity is present in the role.

Choose a reason for hiding this comment

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

medium

Typo: inmutable should be immutable.

Suggested change
// Create a copy of a inmutable set if the identity is present in the role.
// Create a copy of an immutable set if the identity is present in the role.

@alarconesparza alarconesparza changed the title feat(bigquery): add table/view samples to cloud-client feat(bigquery): add table/view samples to cloud-client (3/3) Mar 3, 2025
@alarconesparza alarconesparza marked this pull request as ready for review March 11, 2025 23:33
@alarconesparza alarconesparza requested review from yoshi-approver and a team as code owners March 11, 2025 23:33
Copy link

snippet-bot bot commented Mar 11, 2025

Here is the summary of changes.

You are about to add 3 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

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.

3 participants