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 resource header prefix #1196

Merged
merged 20 commits into from
Aug 26, 2020
Merged

Conversation

AVaksman
Copy link
Contributor

@AVaksman AVaksman commented Jul 28, 2020

  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #1186

Method Header
createInstance project
listInstances project
listInstancesStream project
listInstanceConfigs project
listInstanceConfigsStream project
deleteInstance instance
getInstance instance
updateInstance instance
createBackup instance
getBackup instance
updateBackup instance
deleteBackup instance
restoreDatabase instance
listBackups instance
listBackupOperations instance
listDatabaseOperations instance
createDatabase instance
listDatabases instance
dropDatabase database
getDatabase database
getDatabaseDdl database
updateDatabaseDdl database
partitionQuery database
partitionRead database
batchCreateSessions database
createSession database
listSessions database
listSessionsStream database
deleteSession database
getSession database
executeSql database
beginTransaction database
streamingRead database
executeStreamingSql database
executeBatchDml database
commit database
rollback database

@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Jul 28, 2020
@codecov
Copy link

codecov bot commented Jul 28, 2020

Codecov Report

Merging #1196 into master will increase coverage by 0.34%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1196      +/-   ##
==========================================
+ Coverage   98.24%   98.58%   +0.34%     
==========================================
  Files          23       23              
  Lines       21191    21275      +84     
  Branches     1199     1233      +34     
==========================================
+ Hits        20820    20975     +155     
+ Misses        368      297      -71     
  Partials        3        3              
Impacted Files Coverage Δ
src/backup.ts 99.62% <100.00%> (+<0.01%) ⬆️
src/batch-transaction.ts 100.00% <100.00%> (ø)
src/common.ts 100.00% <100.00%> (+100.00%) ⬆️
src/database.ts 99.92% <100.00%> (+<0.01%) ⬆️
src/index.ts 100.00% <100.00%> (ø)
src/instance.ts 100.00% <100.00%> (ø)
src/session.ts 100.00% <100.00%> (ø)
src/transaction.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7412122...bea4300. Read the comment docs.

@AVaksman
Copy link
Contributor Author

@skuruppu please verify the correctness of header to method

@AVaksman AVaksman marked this pull request as ready for review July 29, 2020 03:04
@AVaksman AVaksman requested review from a team as code owners July 29, 2020 03:04
Copy link
Contributor

@skuruppu skuruppu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for making this change, especially with having to switch between the various values that could be set for this header.

src/database.ts Outdated Show resolved Hide resolved
src/index.ts Outdated
delete reqOpts.gaxOptions;
const gaxOpts = addResourcePrefixHeader(
options.gaxOptions!,
'projects/' + this.projectId
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just add a this.formattedName_ field to this class?

(batchTransaction.session.parent as Database).formattedName_
)
);
console.log(gaxOpts);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove

test/batch-transaction.ts Outdated Show resolved Hide resolved
@stephenplusplus
Copy link
Contributor

Would it work to do this all in the request function, somewhere around here:

const requestFn = gaxClient[config.method].bind(
?

To handle the different values that are used for the header, the value could be passed to the function call, e.g.:

this.request({
  client: 'InstanceAdminClient',
  method: 'listInstanceConfigs',
  reqOpts,
  gaxOpts,
  resourcePrefixHeader: 'projects/' + this.projectId,
});

Or even:

this.request({
  client: 'InstanceAdminClient',
  method: 'listInstanceConfigs',
  reqOpts,
  gaxOpts,
  headers: {
    'google-cloud-resource-prefix': 'projects/' + this.projectId,
  },
});

Reducing it to this format should save a lot of code, and be a bit more future proof should some other requirement come along to set a property on the gaxOptions, like:

gaxOpts: addTrackingIdHeader(addResourcePrefixHeader({}, this.formattedName_)),

@AVaksman
Copy link
Contributor Author

AVaksman commented Aug 3, 2020

Different methods require a different level of granularity, project, instance, database.
The desired data can come in the reqOpts object under different properties, parent, name, database, backup.name, instance.name, session (in case of session, for header purposes the path would have to be shortened to database).

@stephenplusplus
Copy link
Contributor

I'm sorry if I'm not catching on quickly enough, but how does that invalidate my suggestion for code simplification? I'm just looking at replacing all of the calls to addResourcePrefixHeader(theValue) with something more generic and adaptable in the future.

export function addResourcePrefixHeader(
  gaxOpts: CallOptions,
  headerValue: string
): CallOptions {
  return extend(true, {}, gaxOpts, {
    otherArgs: {
      headers: {[CLOUD_RESOURCE_HEADER]: headerValue},
    },
  });
}

@AVaksman
Copy link
Contributor Author

AVaksman commented Aug 4, 2020

@stephenplusplus PTAL
Is it closer to what you had in mind?

Copy link
Contributor

@stephenplusplus stephenplusplus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thank you! I had some small tweaks to suggest in this review, but I appreciate you taking the time to make the changes.

src/index.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
@stephenplusplus
Copy link
Contributor

@AVaksman thank you for all of the changes! I think the resource header is set very nicely. There are a lot of seemingly unrelated changes to how reqOpts combines with gaxOpts. I wouldn't mind a separate PR for those, unless they are actually tied to this change?

@AVaksman
Copy link
Contributor Author

AVaksman commented Aug 10, 2020

Not tied to this change.
Will move it in a separate PR.

src/backup.ts Show resolved Hide resolved
test/batch-transaction.ts Outdated Show resolved Hide resolved
src/common.ts Show resolved Hide resolved
test/database.ts Outdated
@@ -999,7 +1010,7 @@ describe('Database', () => {
it('should accept gaxOptions', done => {
const gaxOptions = {};
database.request = config => {
assert.strictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the cases where our src/ code directly passes the gaxOptions object that the user gives, I think "strictEqual" is the right case. When the src/ code modifies the object, we should add some properties to the test's "gaxOptions" and "deepStrictEqual" that they're still there.

src/index.ts Show resolved Hide resolved
test/backup.ts Outdated Show resolved Hide resolved
@AVaksman
Copy link
Contributor Author

@stephenplusplus PTAL

Copy link
Contributor

@stephenplusplus stephenplusplus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Just these few gaxOptions lingering issues.

test/database.ts Outdated
@@ -993,7 +1006,7 @@ describe('Database', () => {
it('should accept gaxOptions', done => {
const gaxOptions = {};
database.request = config => {
assert.strictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test that would be a false positive, unless we put in fake properties or revert to comparing that the input object is strictly equal.

test/database.ts Outdated
@@ -1648,7 +1665,7 @@ describe('Database', () => {
it('should accept gaxOptions', done => {
const gaxOptions = {};
database.request = config => {
assert.strictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a test that would be a false positive, unless we put in fake properties or revert to comparing that the input object is strictly equal.

@AVaksman
Copy link
Contributor Author

Done, thank you!

src/session.ts Outdated
@@ -36,7 +36,9 @@ import {
CreateSessionOptions,
} from './database';
import {ServiceObjectConfig} from '@google-cloud/common';
import {NormalCallback} from './common';
import {NormalCallback, CLOUD_RESOURCE_HEADER} from './common';
// import {ServiceObjectConfig} from '@google-cloud/common';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// import {ServiceObjectConfig} from '@google-cloud/common';
// import {ServiceObjectConfig} from '@google-cloud/common';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these comments can be removed. (This and the next line)

src/session.ts Outdated
import {NormalCallback} from './common';
import {NormalCallback, CLOUD_RESOURCE_HEADER} from './common';
// import {ServiceObjectConfig} from '@google-cloud/common';
// import {NormalCallback} from './common';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// import {NormalCallback} from './common';
// import {NormalCallback} from './common';

@@ -181,7 +190,7 @@ describe('BatchTransaction', () => {
const RESPONSE = {partitions: PARTITIONS};

const QUERY = {a: 'b'};
const CONFIG = {reqOpts: QUERY};
const CONFIG = {reqOpts: QUERY, gaxOpts: {timeout: 1000}};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const CONFIG = {reqOpts: QUERY, gaxOpts: {timeout: 1000}};
const CONFIG = {reqOpts: QUERY};

It doesn't look like this function is doing anything with gaxOpts, or that we're testing it anywhere, so having this defined here could be confusing.

test/database.ts Outdated
@@ -1017,7 +1032,7 @@ describe('Database', () => {
it('should accept gaxOptions', done => {
const gaxOptions = {};
database.request = config => {
assert.strictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
assert.strictEqual(config.gaxOpts, gaxOptions);

If after making that change, the test fails, then we would want to keep deepStrictEqual, but also set a property on gaxOptions.

test/instance.ts Outdated
@@ -258,7 +269,7 @@ describe('Instance', () => {
it('should accept gaxOptions', done => {
const options = Object.assign({}, OPTIONS, {gaxOptions: {}});
instance.request = config => {
assert.strictEqual(config.gaxOpts, options.gaxOptions);
assert.deepStrictEqual(config.gaxOpts, options.gaxOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.deepStrictEqual(config.gaxOpts, options.gaxOptions);
assert.strictEqual(config.gaxOpts, options.gaxOptions);

If after making that change, the test fails, then we would want to keep deepStrictEqual, but also set a property on options.gaxOptions.

@@ -558,7 +575,7 @@ describe('Transaction', () => {
const {gaxOpts, reqOpts} = REQUEST_STREAM.lastCall.args[0];

assert.strictEqual(reqOpts.gaxOptions, undefined);
assert.strictEqual(gaxOpts, fakeQuery.gaxOptions);
assert.deepStrictEqual(gaxOpts, fakeQuery.gaxOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.deepStrictEqual(gaxOpts, fakeQuery.gaxOptions);
assert.strictEqual(gaxOpts, fakeQuery.gaxOptions);

If after making that change, the test fails, then we would want to keep deepStrictEqual, but also set a property on fakeQuery.gaxOptions.

@@ -1022,7 +1039,7 @@ describe('Transaction', () => {
it('should accept gaxOptions', done => {
const gaxOptions = {};
transaction.request = config => {
assert.strictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
assert.strictEqual(config.gaxOpts, gaxOptions);

If after making that change, the test fails, then we would want to keep deepStrictEqual, but also set a property on gaxOptions.

});

it('should accept gaxOptions', done => {
const gaxOptions = {};
transaction.request = config => {
assert.strictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
assert.strictEqual(config.gaxOpts, gaxOptions);

If after making that change, the test fails, then we would want to keep deepStrictEqual, but also set a property on gaxOptions.

});

it('should accept gaxOptions', done => {
const gaxOptions = {};
transaction.request = config => {
assert.strictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
assert.strictEqual(config.gaxOpts, gaxOptions);

If after making that change, the test fails, then we would want to keep deepStrictEqual, but also set a property on gaxOptions.

});

it('should accept gaxOptions', done => {
const gaxOptions = {};
transaction.request = config => {
assert.strictEqual(config.gaxOpts, gaxOptions);
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.deepStrictEqual(config.gaxOpts, gaxOptions);
assert.strictEqual(config.gaxOpts, gaxOptions);

If after making that change, the test fails, then we would want to keep deepStrictEqual, but also set a property on gaxOptions.

@product-auto-label product-auto-label bot added the api: spanner Issues related to the googleapis/nodejs-spanner API. label Aug 21, 2020
@AVaksman
Copy link
Contributor Author

I believe I addressed all latest comments PTAL @stephenplusplus

@AVaksman AVaksman added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Aug 24, 2020
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Aug 24, 2020
src/session.ts Outdated
@@ -36,7 +36,9 @@ import {
CreateSessionOptions,
} from './database';
import {ServiceObjectConfig} from '@google-cloud/common';
import {NormalCallback} from './common';
import {NormalCallback, CLOUD_RESOURCE_HEADER} from './common';
// import {ServiceObjectConfig} from '@google-cloud/common';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these comments can be removed. (This and the next line)

@AVaksman AVaksman added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Aug 25, 2020
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Aug 25, 2020
@AVaksman AVaksman merged commit 99744b6 into googleapis:master Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the googleapis/nodejs-spanner API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set the google-cloud-resource-prefix header in clients
4 participants