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): WIP add samples for access policies #3975

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

Conversation

hivanalejandro
Copy link
Contributor

@hivanalejandro hivanalejandro commented Feb 13, 2025

Description

Add sample for viewing access policy to dataset

Step of Internal: b/394478489

  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

Checklist

- Configure main app.js entry point
- Configure viewDatasetAccessPolicy.js file
- Add viewTableOrViewAccessPolicy to the app.js entry point
- Configure viewTableOrViewAccessPolicy.js file
@hivanalejandro hivanalejandro requested review from a team as code owners February 13, 2025 20:01
Copy link

snippet-bot bot commented Feb 13, 2025

Here is the summary of changes.

You are about to add 2 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

@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 13, 2025
@hivanalejandro hivanalejandro marked this pull request as draft February 13, 2025 20:01
@hivanalejandro hivanalejandro changed the title feat(bigquery): WIP create sample 'viewTableOrViewAccessPolicy' & 'viewDatasetAccessPolicy' feat(bigquery): WIP feat(bigquery): add samples for access policies Feb 20, 2025
@hivanalejandro hivanalejandro changed the title feat(bigquery): WIP feat(bigquery): add samples for access policies feat(bigquery): WIP add samples for access policies Feb 20, 2025
Copy link
Contributor

@eapl-gemugami eapl-gemugami left a comment

Choose a reason for hiding this comment

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

I left a few comments. Please let me know if you require further details.

/**
* View access policies for a BigQuery dataset
*
* @param {object} [overrideValues] Optional parameters to override defaults
Copy link
Contributor

Choose a reason for hiding this comment

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

David Cavazos advised in this Python PR Review against Overriding parameters as it may be confusing. Having these things commented out as TODOs and passing them as arguments to the function.

For an example see:

function main(
projectId,
zone,
instanceName,
machineType = 'n1-standard-1',
sourceImage = 'projects/debian-cloud/global/images/family/debian-11',
networkName = 'global/networks/default'
) {
// [START compute_instances_create]
/**
* TODO(developer): Uncomment and replace these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const zone = 'europe-central2-b'
// const instanceName = 'YOUR_INSTANCE_NAME'
// const machineType = 'n1-standard-1';
// const sourceImage = 'projects/debian-cloud/global/images/family/debian-11';
// const networkName = 'global/networks/default';

const [metadata] = await dataset.getMetadata();

// Shows the Access policy as a list of access entries
console.log("Access entries:", metadata.access);
Copy link
Contributor

Choose a reason for hiding this comment

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

Based on the conversation with iennae and this Python PR Review it's better to only show one way, not the 'pretty output', and also how to access each parameter.

I have refactored it here, I think it's cleaner:
https://github.com/GoogleCloudPlatform/python-docs-samples/blob/72c9bbc6fea691022105d14b4f152ea4e029c2c7/bigquery/cloud-client/view_dataset_access_policy.py#L38-L43

sinon.stub(BigQuery.prototype, "dataset").callsFake(bigQueryStub.dataset);

// Spy on console.log
consoleLogSpy = sinon.spy(console, "log");
Copy link
Contributor

Choose a reason for hiding this comment

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

Based on this Python PR Review
Please don't check stdout/stderr directly. This was a very early approach but it's very error prone and results in (Python) flaky tests. If there's something you want to check from the sample, return it from the sample function and check the value here directly.

You may use this pattern instead?

const outputCreate = execSync(
`node create-instance-templates/createTemplate ${projectId} ${templateName}`
);
assert.match(outputCreate, /Instance template created./);

await viewDatasetAccessPolicy({ datasetId });

// Verify BigQuery client was called correctly
assert.ok(consoleLogSpy.calledWith("Access entries:", [sampleAccessEntry]));
Copy link
Contributor

Choose a reason for hiding this comment

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

See my previous comment about checking stdout/stederr


async function main() {
try {
const projectId = process.env.GOOGLE_CLOUD_PROJECT;
Copy link
Contributor

Choose a reason for hiding this comment

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

For Python I found that the previous developer was using the client project, instead of this environment variable, as we do for other samples.

As the tests are still passing, I left the client.project instead.

conftest.py#L38

members: ['user:[email protected]'],
},
],
etag: 'CAE=',
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't seen this etag before, where does it come from?
For instance I know that "ACAB" is a default empty policy (view_table_or_view_access_policy_test.py#L21)

assert.ok(bigQueryStub.dataset().table.calledWith(params.resourceName));

assert.ok(
consoleLogSpy.calledWith(
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if spying on the console is the best practice for tests on Node.
Otherwise, It could be done on the returned object.

/**
* View access policies for a BigQuery dataset
*
* @param {object} [overrideValues] Optional parameters to override defaults
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as for previous samples on object vs Object.

// Get properties for an AccessEntry
if (metadata.access && metadata.access.length > 0) {
console.log(`Details for Access entry 0 in dataset '${datasetId}':`);
console.log(`Role: ${metadata.access[0].role || 'N/A'}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is N/A a good practice for JS? 'undefined' or blank could be more natural for the developer?

await viewDatasetAccessPolicy({datasetId});

// Verify BigQuery client was called correctly
assert.ok(bigQueryStub.dataset.calledWith(datasetId));
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these tests run on the API or are they mocked?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tests are mocked

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