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: add set client endpoint sample #1170

Merged
merged 9 commits into from
Jan 25, 2023
40 changes: 40 additions & 0 deletions samples/setClientEndpoint.js
Original file line number Diff line number Diff line change
@@ -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();
}
alvarowolfx marked this conversation as resolved.
Show resolved Hide resolved
main(...process.argv.slice(2));
9 changes: 9 additions & 0 deletions samples/test/clients.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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