From f6a745682f2c25d6dfa676975d2af3d0984fe6c0 Mon Sep 17 00:00:00 2001
From: Steffany Brown <30247553+steffnay@users.noreply.github.com>
Date: Thu, 10 Feb 2022 13:41:51 -0800
Subject: [PATCH] feat: add cross-project support (#1079)
* add projectId override
* update nodejs-common version
* add tests
* update test
---
package.json | 2 +-
src/dataset.ts | 14 ++++++++++++++
test/dataset.ts | 27 +++++++++++++++++++++++++++
3 files changed, 42 insertions(+), 1 deletion(-)
diff --git a/package.json b/package.json
index a10a295f..3b70b61a 100644
--- a/package.json
+++ b/package.json
@@ -48,7 +48,7 @@
"precompile": "gts clean"
},
"dependencies": {
- "@google-cloud/common": "^3.1.0",
+ "@google-cloud/common": "^3.9.0",
"@google-cloud/paginator": "^3.0.0",
"@google-cloud/promisify": "^2.0.0",
"arrify": "^2.0.1",
diff --git a/src/dataset.ts b/src/dataset.ts
index d4275d45..92580538 100644
--- a/src/dataset.ts
+++ b/src/dataset.ts
@@ -53,6 +53,7 @@ export interface DatasetDeleteOptions {
export interface DatasetOptions {
location?: string;
+ projectId?: string;
}
export type CreateDatasetOptions = bigquery.IDataset;
@@ -121,6 +122,7 @@ export type TableCallback = ResourceCallback
;
class Dataset extends ServiceObject {
bigQuery: BigQuery;
location?: string;
+ projectId?: string;
getModelsStream(options?: GetModelsOptions): ResourceStream {
// placeholder body, overwritten in constructor
return new ResourceStream({}, () => {});
@@ -379,6 +381,10 @@ class Dataset extends ServiceObject {
this.location = options.location;
}
+ if (options?.projectId) {
+ this.projectId = options.projectId;
+ }
+
this.bigQuery = bigQuery;
// Catch all for read-modify-write cycle
@@ -389,6 +395,14 @@ class Dataset extends ServiceObject {
reqOpts.headers = reqOpts.headers || {};
reqOpts.headers['If-Match'] = reqOpts.json.etag;
}
+
+ if (this.projectId) {
+ // Override projectId if provided
+ reqOpts.uri = reqOpts.uri.replace(
+ `/projects/${this.bigQuery.projectId}/`,
+ `/projects/${this.projectId}/`
+ );
+ }
return reqOpts;
},
});
diff --git a/test/dataset.ts b/test/dataset.ts
index 1680e501..d92678ac 100644
--- a/test/dataset.ts
+++ b/test/dataset.ts
@@ -148,6 +148,14 @@ describe('BigQuery/Dataset', () => {
assert.strictEqual(ds.location, LOCATION);
});
+ it('should capture user provided projectId', () => {
+ const projectIdOverride = 'octavia';
+ const options = {projectId: projectIdOverride};
+ const ds = new Dataset(BIGQUERY, DATASET_ID, options);
+
+ assert.strictEqual(ds.projectId, projectIdOverride);
+ });
+
describe('createMethod', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let bq: any;
@@ -197,6 +205,25 @@ describe('BigQuery/Dataset', () => {
});
});
+ describe('projectId override interceptor', () => {
+ const projectIdOverride = 'DuBois';
+
+ it('should use projectId override uri', () => {
+ ds = new Dataset(BIGQUERY, DATASET_ID, {projectId: projectIdOverride});
+ const interceptor = ds.interceptors.pop();
+ const fakeReqOpts = {
+ method: 'PATCH',
+ json: {
+ etag: '',
+ },
+ uri: `/projects/${ds.bigQuery.projectId}/`,
+ };
+
+ const reqOpts = interceptor.request(fakeReqOpts);
+ assert.deepStrictEqual(reqOpts.uri, `/projects/${projectIdOverride}/`);
+ });
+ });
+
describe('etag interceptor', () => {
const FAKE_ETAG = 'abc';