Skip to content

Commit 2e70b07

Browse files
stephenplusplusJustinBeckwith
authored andcommitted
feat: Add Storage#getServiceAccount(). (#331)
1 parent e4cd270 commit 2e70b07

3 files changed

Lines changed: 166 additions & 14 deletions

File tree

handwritten/storage/src/index.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,82 @@ class Storage extends common.Service {
442442
}
443443
);
444444
}
445+
446+
/**
447+
* @typedef {array} GetServiceAccountResponse
448+
* @property {object} 0 The service account resource.
449+
* @property {object} 1 The full
450+
* [API response](https://cloud.google.com/storage/docs/json_api/v1/projects/serviceAccount#resource).
451+
*/
452+
/**
453+
* @callback GetServiceAccountCallback
454+
* @param {?Error} err Request error, if any.
455+
* @param {object} serviceAccount The serviceAccount resource.
456+
* @param {string} serviceAccount.emailAddress The service account email
457+
* address.
458+
* @param {object} apiResponse The full
459+
* [API response](https://cloud.google.com/storage/docs/json_api/v1/projects/serviceAccount#resource).
460+
*/
461+
/**
462+
* Get the email address of this project's Google Cloud Storage service
463+
* account.
464+
*
465+
* @see [Projects.serviceAccount: get API Documentation]{@link https://cloud.google.com/storage/docs/json_api/v1/projects/serviceAccount/get}
466+
* @see [Projects.serviceAccount Resource]{@link https://cloud.google.com/storage/docs/json_api/v1/projects/serviceAccount#resource}
467+
*
468+
* @param {object} [options] Configuration object.
469+
* @param {string} [options.userProject] User project to be billed for this
470+
* request.
471+
* @param {GetServiceAccountCallback} [callback] Callback function.
472+
* @returns {Promise<GetServiceAccountResponse>}
473+
*
474+
* @example
475+
* const storage = require('@google-cloud/storage')();
476+
*
477+
* storage.getServiceAccount(function(err, serviceAccount, apiResponse) {
478+
* if (!err) {
479+
* const serviceAccountEmail = serviceAccount.emailAddress;
480+
* }
481+
* });
482+
*
483+
* //-
484+
* // If the callback is omitted, we'll return a Promise.
485+
* //-
486+
* storage.getServiceAccount().then(function(data) {
487+
* const serviceAccountEmail = data[0].emailAddress;
488+
* const apiResponse = data[1];
489+
* });
490+
*/
491+
getServiceAccount(options, callback) {
492+
if (!callback) {
493+
callback = options;
494+
options = {};
495+
}
496+
497+
this.request(
498+
{
499+
uri: `/projects/${this.projectId}/serviceAccount`,
500+
qs: options,
501+
},
502+
(err, resp) => {
503+
if (err) {
504+
callback(err, null, resp);
505+
return;
506+
}
507+
508+
const camelCaseResponse = {};
509+
510+
for (let prop in resp) {
511+
let camelCaseProp = prop.replace(/_(\w)/g, (_, match) =>
512+
match.toUpperCase()
513+
);
514+
camelCaseResponse[camelCaseProp] = resp[prop];
515+
}
516+
517+
callback(null, camelCaseResponse, resp);
518+
}
519+
);
520+
}
445521
}
446522

447523
// Allow creating a `Storage` instance without using the `new` keyword. (#173)

handwritten/storage/system-test/storage.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,22 +1846,16 @@ describe('storage', function() {
18461846
return;
18471847
}
18481848

1849-
storage.request(
1850-
{
1851-
uri:
1852-
'https://www.googleapis.com/storage/v1/projects/{{projectId}}/serviceAccount',
1853-
},
1854-
function(err, resp) {
1855-
if (err) {
1856-
next(err);
1857-
return;
1858-
}
1849+
storage.getServiceAccount(function(err, serviceAccount) {
1850+
if (err) {
1851+
next(err);
1852+
return;
1853+
}
18591854

1860-
SERVICE_ACCOUNT_EMAIL = resp.email_address;
1855+
SERVICE_ACCOUNT_EMAIL = serviceAccount.emailAddress;
18611856

1862-
next();
1863-
}
1864-
);
1857+
next();
1858+
});
18651859
},
18661860

18671861
function grantPermissionToServiceAccount(next) {

handwritten/storage/test/index.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,86 @@ describe('Storage', function() {
434434
});
435435
});
436436
});
437+
438+
describe('getServiceAccount', function() {
439+
it('should make the correct request', function(done) {
440+
storage.request = function(reqOpts) {
441+
assert.strictEqual(
442+
reqOpts.uri,
443+
`/projects/${storage.projectId}/serviceAccount`
444+
);
445+
assert.deepStrictEqual(reqOpts.qs, {});
446+
done();
447+
};
448+
449+
storage.getServiceAccount(assert.ifError);
450+
});
451+
452+
it('should allow user options', function(done) {
453+
const options = {};
454+
455+
storage.request = function(reqOpts) {
456+
assert.strictEqual(reqOpts.qs, options);
457+
done();
458+
};
459+
460+
storage.getServiceAccount(options, assert.ifError);
461+
});
462+
463+
describe('error', function() {
464+
const ERROR = new Error('Error.');
465+
const API_RESPONSE = {};
466+
467+
beforeEach(function() {
468+
storage.request = function(reqOpts, callback) {
469+
callback(ERROR, API_RESPONSE);
470+
};
471+
});
472+
473+
it('should return the error and apiResponse', function(done) {
474+
storage.getServiceAccount(function(err, serviceAccount, apiResponse) {
475+
assert.strictEqual(err, ERROR);
476+
assert.strictEqual(serviceAccount, null);
477+
assert.strictEqual(apiResponse, API_RESPONSE);
478+
done();
479+
});
480+
});
481+
});
482+
483+
describe('success', function() {
484+
const API_RESPONSE = {};
485+
486+
beforeEach(function() {
487+
storage.request = function(reqOpts, callback) {
488+
callback(null, API_RESPONSE);
489+
};
490+
});
491+
492+
it('should convert snake_case response to camelCase', function(done) {
493+
const apiResponse = {
494+
snake_case: true,
495+
};
496+
497+
storage.request = function(reqOpts, callback) {
498+
callback(null, apiResponse);
499+
};
500+
501+
storage.getServiceAccount(function(err, serviceAccount) {
502+
assert.ifError(err);
503+
assert.strictEqual(serviceAccount.snakeCase, apiResponse.snake_case);
504+
assert.strictEqual(serviceAccount.snake_case, undefined);
505+
done();
506+
});
507+
});
508+
509+
it('should return the serviceAccount and apiResponse', function(done) {
510+
storage.getServiceAccount(function(err, serviceAccount, apiResponse) {
511+
assert.ifError(err);
512+
assert.deepStrictEqual(serviceAccount, {});
513+
assert.strictEqual(apiResponse, API_RESPONSE);
514+
done();
515+
});
516+
});
517+
});
518+
});
437519
});

0 commit comments

Comments
 (0)