Skip to content

Commit

Permalink
feat: add cross-project support (#1079)
Browse files Browse the repository at this point in the history
* add projectId override

* update nodejs-common version

* add tests

* update test
  • Loading branch information
steffnay authored Feb 10, 2022
1 parent ed13f5e commit f6a7456
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions src/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface DatasetDeleteOptions {

export interface DatasetOptions {
location?: string;
projectId?: string;
}

export type CreateDatasetOptions = bigquery.IDataset;
Expand Down Expand Up @@ -121,6 +122,7 @@ export type TableCallback = ResourceCallback<Table, bigquery.ITable>;
class Dataset extends ServiceObject {
bigQuery: BigQuery;
location?: string;
projectId?: string;
getModelsStream(options?: GetModelsOptions): ResourceStream<Model> {
// placeholder body, overwritten in constructor
return new ResourceStream<Model>({}, () => {});
Expand Down Expand Up @@ -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
Expand All @@ -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;
},
});
Expand Down
27 changes: 27 additions & 0 deletions test/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';

Expand Down

0 comments on commit f6a7456

Please sign in to comment.