[go: up one dir, main page]

Skip to content

Commit

Permalink
feat: add set client endpoint sample (#1170)
Browse files Browse the repository at this point in the history
* feat: add set client endpoint sample

* fix: lint issues

* fix: add region tag and remove async function

* fix: move example to internal function

* fix(bigquery): follow parameter style guide for samples

* fix: update license file header with 2023

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Lo Ferris <50979514+loferris@users.noreply.github.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 25, 2023
1 parent 388897f commit df1dcd3
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -169,6 +169,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-bigquery/tr
| Relax Column Load Append | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/relaxColumnLoadAppend.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/relaxColumnLoadAppend.js,samples/README.md) |
| Relax Column Query Append | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/relaxColumnQueryAppend.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/relaxColumnQueryAppend.js,samples/README.md) |
| Remove Table Clustering | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/removeTableClustering.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/removeTableClustering.js,samples/README.md) |
| Set Client Endpoint | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/setClientEndpoint.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/setClientEndpoint.js,samples/README.md) |
| Set User Agent | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/setUserAgent.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/setUserAgent.js,samples/README.md) |
| Table Exists | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/tableExists.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/tableExists.js,samples/README.md) |
| Undelete Table | [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/undeleteTable.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/undeleteTable.js,samples/README.md) |
Expand Down
18 changes: 18 additions & 0 deletions samples/README.md
Expand Up @@ -102,6 +102,7 @@
* [Relax Column Load Append](#relax-column-load-append)
* [Relax Column Query Append](#relax-column-query-append)
* [Remove Table Clustering](#remove-table-clustering)
* [Set Client Endpoint](#set-client-endpoint)
* [Set User Agent](#set-user-agent)
* [Table Exists](#table-exists)
* [Undelete Table](#undelete-table)
Expand Down Expand Up @@ -1678,6 +1679,23 @@ __Usage:__



### Set Client Endpoint

View the [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/setClientEndpoint.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-bigquery&page=editor&open_in_editor=samples/setClientEndpoint.js,samples/README.md)

__Usage:__


`node samples/setClientEndpoint.js`


-----




### Set User Agent

View the [source code](https://github.com/googleapis/nodejs-bigquery/blob/main/samples/setUserAgent.js).
Expand Down
40 changes: 40 additions & 0 deletions samples/setClientEndpoint.js
@@ -0,0 +1,40 @@
// Copyright 2023 Google LLC
//
// 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.

'use strict';

function main(region = 'my-region') {
// [START bigquery_set_client_endpoint]
// Import the Google Cloud client library
const {BigQuery} = require('@google-cloud/bigquery');

function setClientEndpoint() {
// Create a bigquery client pointing to a specific endpoint

/**
* TODO(developer): Uncomment the following line of code and fill in your region before running the sample.
*/
// const region = 'my-region';

const bigquery = new BigQuery({
apiEndpoint: `${region}-bigquery.googleapis.com`,
});

console.log('API Endpoint:');
console.log(bigquery.apiEndpoint);
}
// [END bigquery_set_client_endpoint]
setClientEndpoint();
}
main(...process.argv.slice(2));
9 changes: 9 additions & 0 deletions samples/test/clients.test.js
Expand Up @@ -26,4 +26,13 @@ describe('Client', () => {
assert.match(output, /User agent:/);
assert.match(output, /my-user-agent/);
});
it('should should set client endpoint', async () => {
let output = execSync('node setClientEndpoint.js us-east4');
assert.match(output, /API Endpoint:/);
assert.match(output, /https:\/\/us-east4-bigquery.googleapis.com/);

output = execSync('node setClientEndpoint.js eu');
assert.match(output, /API Endpoint:/);
assert.match(output, /https:\/\/eu-bigquery.googleapis.com/);
});
});
29 changes: 29 additions & 0 deletions samples/test/datasets.test.js
Expand Up @@ -49,6 +49,35 @@ describe('Datasets', () => {
assert.ok(exists);
});

it('should create a dataset using a regional endpoint', async () => {
const euBigquery = new BigQuery({
apiEndpoint: 'eu-bigquery.googleapis.com',
});
const euDatasetId = datasetId + '_eu';
await euBigquery.createDataset(euDatasetId, {
location: 'eu',
});
const [exists] = await euBigquery.dataset(euDatasetId).exists();
assert.ok(exists);
});

it('should fail to create a dataset using a different region from the client endpoint', async () => {
const usEast4Bigquery = new BigQuery({
apiEndpoint: 'us-east4-bigquery.googleapis.com',
});
const usDatasetId = datasetId + '_us';
let error;
try {
await usEast4Bigquery.createDataset(usDatasetId, {
location: 'us-central1',
});
} catch (err) {
error = err;
}
assert.isNotNull(error);
assert.equal(error.message, 'Invalid storage region');
});

it('should list datasets', async () => {
const output = execSync('node listDatasets.js');
assert.match(output, /Datasets:/);
Expand Down

0 comments on commit df1dcd3

Please sign in to comment.