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: Trusted Private Cloud support, use the universeDomain parameter #5026

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function main(parent) {
};

// Run request
const iterable = await memcacheClient.listInstancesAsync(request);
const iterable = memcacheClient.listInstancesAsync(request);
for await (const response of iterable) {
console.log(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function main(parent) {
};

// Run request
const iterable = await memcacheClient.listInstancesAsync(request);
const iterable = memcacheClient.listInstancesAsync(request);
for await (const response of iterable) {
console.log(response);
}
Expand Down
58 changes: 52 additions & 6 deletions packages/google-cloud-memcache/src/v1/cloud_memcache_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type {
import {Transform} from 'stream';
import * as protos from '../../protos/protos';
import jsonProtos = require('../../protos/protos.json');

/**
* Client JSON configuration object, loaded from
* `src/v1/cloud_memcache_client_config.json`.
Expand Down Expand Up @@ -68,6 +69,8 @@ export class CloudMemcacheClient {
private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
private _protos: {};
private _defaults: {[method: string]: gax.CallSettings};
private _universeDomain: string;
private _servicePath: string;
auth: gax.GoogleAuth;
descriptors: Descriptors = {
page: {},
Expand Down Expand Up @@ -127,8 +130,20 @@ export class CloudMemcacheClient {
) {
// Ensure that options include all the required fields.
const staticMembers = this.constructor as typeof CloudMemcacheClient;
if (
opts?.universe_domain &&
opts?.universeDomain &&
opts?.universe_domain !== opts?.universeDomain
) {
throw new Error(
'Please set either universe_domain or universeDomain, but not both.'
);
}
this._universeDomain =
opts?.universeDomain ?? opts?.universe_domain ?? 'googleapis.com';
this._servicePath = 'memcache.' + this._universeDomain;
const servicePath =
opts?.servicePath || opts?.apiEndpoint || staticMembers.servicePath;
opts?.servicePath || opts?.apiEndpoint || this._servicePath;
this._providedCustomServicePath = !!(
opts?.servicePath || opts?.apiEndpoint
);
Expand All @@ -143,7 +158,7 @@ export class CloudMemcacheClient {
opts.numericEnums = true;

// If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case.
if (servicePath !== staticMembers.servicePath && !('scopes' in opts)) {
if (servicePath !== this._servicePath && !('scopes' in opts)) {
opts['scopes'] = staticMembers.scopes;
}

Expand All @@ -168,10 +183,10 @@ export class CloudMemcacheClient {
this.auth.useJWTAccessWithScope = true;

// Set defaultServicePath on the auth object.
this.auth.defaultServicePath = staticMembers.servicePath;
this.auth.defaultServicePath = this._servicePath;

// Set the default scopes in auth client if needed.
if (servicePath === staticMembers.servicePath) {
if (servicePath === this._servicePath) {
this.auth.defaultScopes = staticMembers.scopes;
}
this.locationsClient = new this._gaxModule.LocationsClient(
Expand Down Expand Up @@ -426,21 +441,52 @@ export class CloudMemcacheClient {

/**
* The DNS address for this API service.
* @deprecated Use the apiEndpoint method of the client instance.
* @returns {string} The DNS address for this service.
*/
static get servicePath() {
if (
typeof process !== undefined &&
typeof process.emitWarning === 'function'
) {
process.emitWarning(
'Static servicePath is deprecated, please use the instance method instead.',
'DeprecationWarning'
);
}
return 'memcache.googleapis.com';
}

/**
* The DNS address for this API service - same as servicePath(),
* exists for compatibility reasons.
* The DNS address for this API service - same as servicePath.
* @deprecated Use the apiEndpoint method of the client instance.
* @returns {string} The DNS address for this service.
*/
static get apiEndpoint() {
if (
typeof process !== undefined &&
typeof process.emitWarning === 'function'
) {
process.emitWarning(
'Static apiEndpoint is deprecated, please use the instance method instead.',
'DeprecationWarning'
);
}
return 'memcache.googleapis.com';
}

/**
* The DNS address for this API service.
* @returns {string} The DNS address for this service.
*/
get apiEndpoint() {
return this._servicePath;
}

get universeDomain() {
return this._universeDomain;
}

/**
* The port for this API service.
* @returns {number} The default port for this service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type {
import {Transform} from 'stream';
import * as protos from '../../protos/protos';
import jsonProtos = require('../../protos/protos.json');

/**
* Client JSON configuration object, loaded from
* `src/v1beta2/cloud_memcache_client_config.json`.
Expand Down Expand Up @@ -68,6 +69,8 @@ export class CloudMemcacheClient {
private _gaxGrpc: gax.GrpcClient | gax.fallback.GrpcClient;
private _protos: {};
private _defaults: {[method: string]: gax.CallSettings};
private _universeDomain: string;
private _servicePath: string;
auth: gax.GoogleAuth;
descriptors: Descriptors = {
page: {},
Expand Down Expand Up @@ -127,8 +130,20 @@ export class CloudMemcacheClient {
) {
// Ensure that options include all the required fields.
const staticMembers = this.constructor as typeof CloudMemcacheClient;
if (
opts?.universe_domain &&
opts?.universeDomain &&
opts?.universe_domain !== opts?.universeDomain
) {
throw new Error(
'Please set either universe_domain or universeDomain, but not both.'
);
}
this._universeDomain =
opts?.universeDomain ?? opts?.universe_domain ?? 'googleapis.com';
this._servicePath = 'memcache.' + this._universeDomain;
const servicePath =
opts?.servicePath || opts?.apiEndpoint || staticMembers.servicePath;
opts?.servicePath || opts?.apiEndpoint || this._servicePath;
this._providedCustomServicePath = !!(
opts?.servicePath || opts?.apiEndpoint
);
Expand All @@ -143,7 +158,7 @@ export class CloudMemcacheClient {
opts.numericEnums = true;

// If scopes are unset in options and we're connecting to a non-default endpoint, set scopes just in case.
if (servicePath !== staticMembers.servicePath && !('scopes' in opts)) {
if (servicePath !== this._servicePath && !('scopes' in opts)) {
opts['scopes'] = staticMembers.scopes;
}

Expand All @@ -168,10 +183,10 @@ export class CloudMemcacheClient {
this.auth.useJWTAccessWithScope = true;

// Set defaultServicePath on the auth object.
this.auth.defaultServicePath = staticMembers.servicePath;
this.auth.defaultServicePath = this._servicePath;

// Set the default scopes in auth client if needed.
if (servicePath === staticMembers.servicePath) {
if (servicePath === this._servicePath) {
this.auth.defaultScopes = staticMembers.scopes;
}
this.locationsClient = new this._gaxModule.LocationsClient(
Expand Down Expand Up @@ -438,21 +453,52 @@ export class CloudMemcacheClient {

/**
* The DNS address for this API service.
* @deprecated Use the apiEndpoint method of the client instance.
* @returns {string} The DNS address for this service.
*/
static get servicePath() {
if (
typeof process !== undefined &&
typeof process.emitWarning === 'function'
) {
process.emitWarning(
'Static servicePath is deprecated, please use the instance method instead.',
'DeprecationWarning'
);
}
return 'memcache.googleapis.com';
}

/**
* The DNS address for this API service - same as servicePath(),
* exists for compatibility reasons.
* The DNS address for this API service - same as servicePath.
* @deprecated Use the apiEndpoint method of the client instance.
* @returns {string} The DNS address for this service.
*/
static get apiEndpoint() {
if (
typeof process !== undefined &&
typeof process.emitWarning === 'function'
) {
process.emitWarning(
'Static apiEndpoint is deprecated, please use the instance method instead.',
'DeprecationWarning'
);
}
return 'memcache.googleapis.com';
}

/**
* The DNS address for this API service.
* @returns {string} The DNS address for this service.
*/
get apiEndpoint() {
return this._servicePath;
}

get universeDomain() {
return this._universeDomain;
}

/**
* The port for this API service.
* @returns {number} The default port for this service.
Expand Down
62 changes: 54 additions & 8 deletions packages/google-cloud-memcache/test/gapic_cloud_memcache_v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,62 @@ function stubAsyncIterationCall<ResponseType>(

describe('v1.CloudMemcacheClient', () => {
describe('Common methods', () => {
it('has servicePath', () => {
const servicePath =
cloudmemcacheModule.v1.CloudMemcacheClient.servicePath;
assert(servicePath);
it('has apiEndpoint', () => {
const client = new cloudmemcacheModule.v1.CloudMemcacheClient();
const apiEndpoint = client.apiEndpoint;
assert.strictEqual(apiEndpoint, 'memcache.googleapis.com');
});

it('has apiEndpoint', () => {
const apiEndpoint =
cloudmemcacheModule.v1.CloudMemcacheClient.apiEndpoint;
assert(apiEndpoint);
it('has universeDomain', () => {
const client = new cloudmemcacheModule.v1.CloudMemcacheClient();
const universeDomain = client.universeDomain;
assert.strictEqual(universeDomain, 'googleapis.com');
});

if (
typeof process !== 'undefined' &&
typeof process.emitWarning === 'function'
) {
it('throws DeprecationWarning if static servicePath is used', () => {
const stub = sinon.stub(process, 'emitWarning');
const servicePath =
cloudmemcacheModule.v1.CloudMemcacheClient.servicePath;
assert.strictEqual(servicePath, 'memcache.googleapis.com');
assert(stub.called);
stub.restore();
});

it('throws DeprecationWarning if static apiEndpoint is used', () => {
const stub = sinon.stub(process, 'emitWarning');
const apiEndpoint =
cloudmemcacheModule.v1.CloudMemcacheClient.apiEndpoint;
assert.strictEqual(apiEndpoint, 'memcache.googleapis.com');
assert(stub.called);
stub.restore();
});
}
it('sets apiEndpoint according to universe domain camelCase', () => {
const client = new cloudmemcacheModule.v1.CloudMemcacheClient({
universeDomain: 'example.com',
});
const servicePath = client.apiEndpoint;
assert.strictEqual(servicePath, 'memcache.example.com');
});

it('sets apiEndpoint according to universe domain snakeCase', () => {
const client = new cloudmemcacheModule.v1.CloudMemcacheClient({
universe_domain: 'example.com',
});
const servicePath = client.apiEndpoint;
assert.strictEqual(servicePath, 'memcache.example.com');
});
it('does not allow setting both universeDomain and universe_domain', () => {
assert.throws(() => {
new cloudmemcacheModule.v1.CloudMemcacheClient({
universe_domain: 'example.com',
universeDomain: 'example.net',
});
});
});

it('has port', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,62 @@ function stubAsyncIterationCall<ResponseType>(

describe('v1beta2.CloudMemcacheClient', () => {
describe('Common methods', () => {
it('has servicePath', () => {
const servicePath =
cloudmemcacheModule.v1beta2.CloudMemcacheClient.servicePath;
assert(servicePath);
it('has apiEndpoint', () => {
const client = new cloudmemcacheModule.v1beta2.CloudMemcacheClient();
const apiEndpoint = client.apiEndpoint;
assert.strictEqual(apiEndpoint, 'memcache.googleapis.com');
});

it('has apiEndpoint', () => {
const apiEndpoint =
cloudmemcacheModule.v1beta2.CloudMemcacheClient.apiEndpoint;
assert(apiEndpoint);
it('has universeDomain', () => {
const client = new cloudmemcacheModule.v1beta2.CloudMemcacheClient();
const universeDomain = client.universeDomain;
assert.strictEqual(universeDomain, 'googleapis.com');
});

if (
typeof process !== 'undefined' &&
typeof process.emitWarning === 'function'
) {
it('throws DeprecationWarning if static servicePath is used', () => {
const stub = sinon.stub(process, 'emitWarning');
const servicePath =
cloudmemcacheModule.v1beta2.CloudMemcacheClient.servicePath;
assert.strictEqual(servicePath, 'memcache.googleapis.com');
assert(stub.called);
stub.restore();
});

it('throws DeprecationWarning if static apiEndpoint is used', () => {
const stub = sinon.stub(process, 'emitWarning');
const apiEndpoint =
cloudmemcacheModule.v1beta2.CloudMemcacheClient.apiEndpoint;
assert.strictEqual(apiEndpoint, 'memcache.googleapis.com');
assert(stub.called);
stub.restore();
});
}
it('sets apiEndpoint according to universe domain camelCase', () => {
const client = new cloudmemcacheModule.v1beta2.CloudMemcacheClient({
universeDomain: 'example.com',
});
const servicePath = client.apiEndpoint;
assert.strictEqual(servicePath, 'memcache.example.com');
});

it('sets apiEndpoint according to universe domain snakeCase', () => {
const client = new cloudmemcacheModule.v1beta2.CloudMemcacheClient({
universe_domain: 'example.com',
});
const servicePath = client.apiEndpoint;
assert.strictEqual(servicePath, 'memcache.example.com');
});
it('does not allow setting both universeDomain and universe_domain', () => {
assert.throws(() => {
new cloudmemcacheModule.v1beta2.CloudMemcacheClient({
universe_domain: 'example.com',
universeDomain: 'example.net',
});
});
});

it('has port', () => {
Expand Down
Loading
Loading