Skip to content

Commit 005422a

Browse files
authored
feat: Add support for Table IAM policies (#892)
* add table acl functions * remove extraneous * add tests * refactor to handle undefined/null
1 parent 4485b4c commit 005422a

File tree

4 files changed

+906
-243
lines changed

4 files changed

+906
-243
lines changed

src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export {
9090
CreateExtractJobOptions,
9191
File,
9292
FormattedMetadata,
93+
GetPolicyOptions,
9394
GetRowsOptions,
9495
InsertRowsCallback,
9596
InsertRowsOptions,
@@ -99,9 +100,16 @@ export {
99100
JobMetadataCallback,
100101
JobMetadataResponse,
101102
JobResponse,
103+
PermissionsCallback,
104+
PermissionsResponse,
105+
Policy,
106+
PolicyCallback,
107+
PolicyRequest,
108+
PolicyResponse,
102109
RowMetadata,
103110
RowsCallback,
104111
RowsResponse,
112+
SetPolicyOptions,
105113
SetTableMetadataOptions,
106114
Table,
107115
TableField,

src/table.ts

+132
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ export interface PartialInsertFailure {
143143
row: RowMetadata;
144144
}
145145

146+
export type Policy = bigquery.IPolicy;
147+
export type GetPolicyOptions = bigquery.IGetPolicyOptions;
148+
export type SetPolicyOptions = Omit<bigquery.ISetIamPolicyRequest, 'policy'>;
149+
export type PolicyRequest = bigquery.IGetIamPolicyRequest;
150+
export type PolicyResponse = [Policy];
151+
export type PolicyCallback = RequestCallback<PolicyResponse>;
152+
export type PermissionsResponse = [bigquery.ITestIamPermissionsResponse];
153+
export type PermissionsCallback = RequestCallback<PermissionsResponse>;
154+
146155
/**
147156
* The file formats accepted by BigQuery.
148157
*
@@ -2237,6 +2246,129 @@ class Table extends common.ServiceObject {
22372246
const body = Table.formatMetadata_(metadata as TableMetadata);
22382247
super.setMetadata(body, callback!);
22392248
}
2249+
2250+
getIamPolicy(
2251+
optionsOrCallback?: GetPolicyOptions | PolicyCallback
2252+
): Promise<PolicyResponse>;
2253+
getIamPolicy(options: GetPolicyOptions, callback: PolicyCallback): void;
2254+
/**
2255+
* Run a query scoped to your dataset.
2256+
* @returns {Promise}
2257+
*/
2258+
getIamPolicy(
2259+
optionsOrCallback?: GetPolicyOptions,
2260+
cb?: PolicyCallback
2261+
): void | Promise<PolicyResponse> {
2262+
const options =
2263+
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
2264+
const callback =
2265+
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
2266+
2267+
if (
2268+
typeof options.requestedPolicyVersion === 'number' &&
2269+
options.requestedPolicyVersion !== 1
2270+
) {
2271+
throw new Error('Only IAM policy version 1 is supported.');
2272+
}
2273+
2274+
const json = extend(true, {}, {options});
2275+
2276+
this.request(
2277+
{
2278+
method: 'POST',
2279+
uri: '/:getIamPolicy',
2280+
json,
2281+
},
2282+
(err, resp) => {
2283+
if (err) {
2284+
callback!(err, null);
2285+
return;
2286+
}
2287+
callback!(null, resp);
2288+
}
2289+
);
2290+
}
2291+
2292+
setIamPolicy(
2293+
policy: Policy,
2294+
options?: SetPolicyOptions
2295+
): Promise<PolicyResponse>;
2296+
setIamPolicy(
2297+
policy: Policy,
2298+
options: SetPolicyOptions,
2299+
callback: PolicyCallback
2300+
): void;
2301+
setIamPolicy(policy: Policy, callback: PolicyCallback): void;
2302+
/**
2303+
* Run a query scoped to your dataset.
2304+
* @returns {Promise}
2305+
*/
2306+
setIamPolicy(
2307+
policy: Policy,
2308+
optionsOrCallback?: SetPolicyOptions | PolicyCallback,
2309+
cb?: PolicyCallback
2310+
): void | Promise<PolicyResponse> {
2311+
const options =
2312+
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
2313+
const callback =
2314+
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
2315+
2316+
if (policy.version && policy.version !== 1) {
2317+
throw new Error('Only IAM policy version 1 is supported.');
2318+
}
2319+
2320+
const json = extend(true, {}, options, {policy});
2321+
2322+
this.request(
2323+
{
2324+
method: 'POST',
2325+
uri: '/:setIamPolicy',
2326+
json,
2327+
},
2328+
(err, resp) => {
2329+
if (err) {
2330+
callback!(err, null);
2331+
return;
2332+
}
2333+
callback!(null, resp);
2334+
}
2335+
);
2336+
}
2337+
2338+
testIamPermissions(
2339+
permissions: string | string[]
2340+
): Promise<PermissionsResponse>;
2341+
testIamPermissions(
2342+
permissions: string | string[],
2343+
callback: PermissionsCallback
2344+
): void;
2345+
/**
2346+
* Run a query scoped to your dataset.
2347+
* @returns {Promise}
2348+
*/
2349+
testIamPermissions(
2350+
permissions: string | string[],
2351+
callback?: PermissionsCallback
2352+
): void | Promise<PermissionsResponse> {
2353+
permissions = arrify(permissions);
2354+
2355+
const json = extend(true, {}, {permissions});
2356+
2357+
this.request(
2358+
{
2359+
method: 'POST',
2360+
uri: '/:testIamPermissions',
2361+
json,
2362+
},
2363+
(err, resp) => {
2364+
if (err) {
2365+
callback!(err, null);
2366+
return;
2367+
}
2368+
callback!(null, resp);
2369+
}
2370+
);
2371+
}
22402372
}
22412373

22422374
/*! Developer Documentation

0 commit comments

Comments
 (0)