Skip to content

Commit

Permalink
refactor: sqlAdminRootUrl to sqlAdminAPIEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
edosrecki committed Sep 27, 2023
1 parent e6e916d commit aeecd31
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class CloudSQLInstanceMap extends Map {
}

interface ConnectorOptions {
sqlAdminRootUrl?: string;
sqlAdminAPIEndpoint?: string;
}

// The Connector class is the main public API to interact
Expand All @@ -156,9 +156,9 @@ export class Connector {
private readonly instances: CloudSQLInstanceMap;
private readonly sqlAdminFetcher: SQLAdminFetcher;

constructor({sqlAdminRootUrl}: ConnectorOptions = {}) {
constructor({sqlAdminAPIEndpoint}: ConnectorOptions = {}) {
this.instances = new CloudSQLInstanceMap();
this.sqlAdminFetcher = new SQLAdminFetcher({sqlAdminRootUrl});
this.sqlAdminFetcher = new SQLAdminFetcher({sqlAdminAPIEndpoint});
}

// Connector.getOptions is a method that accepts a Cloud SQL instance
Expand Down
6 changes: 3 additions & 3 deletions src/sqladmin-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ function cleanGaxiosConfig() {

export interface SQLAdminFetcherOptions {
loginAuth?: GoogleAuth;
sqlAdminRootUrl?: string;
sqlAdminAPIEndpoint?: string;
}

export class SQLAdminFetcher {
private readonly client: sqladmin_v1beta4.Sqladmin;
private readonly auth: GoogleAuth;

constructor({loginAuth, sqlAdminRootUrl}: SQLAdminFetcherOptions = {}) {
constructor({loginAuth, sqlAdminAPIEndpoint}: SQLAdminFetcherOptions = {}) {
const auth = new GoogleAuth({
scopes: ['https://www.googleapis.com/auth/sqlservice.admin'],
});
this.client = new Sqladmin({
rootUrl: sqlAdminRootUrl,
rootUrl: sqlAdminAPIEndpoint,
auth,
userAgentDirectives: [
{
Expand Down
16 changes: 8 additions & 8 deletions test/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,22 +627,22 @@ t.test('Connector force refresh on socket connection error', async t => {
connector.close();
});

t.test('Connector, custom sqlAdminRootUrl', async t => {
const expectedSqlAdminRootUrl = 'https://sqladmin.mydomain.com';
let actualSqlAdminRootUrl: string | undefined;
t.test('Connector, custom sqlAdminAPIEndpoint', async t => {
const expectedsqlAdminAPIEndpoint = 'https://sqladmin.mydomain.com';
let actualsqlAdminAPIEndpoint: string | undefined;
// mocks sql admin fetcher to check that the custom
// sqlAdminRootUrl is correctly passed into it
// sqlAdminAPIEndpoint is correctly passed into it
const {Connector} = t.mock('../src/connector', {
'../src/sqladmin-fetcher': {
SQLAdminFetcher: class {
constructor({sqlAdminRootUrl}: SQLAdminFetcherOptions) {
actualSqlAdminRootUrl = sqlAdminRootUrl;
constructor({sqlAdminAPIEndpoint}: SQLAdminFetcherOptions) {
actualsqlAdminAPIEndpoint = sqlAdminAPIEndpoint;
}
},
},
});

new Connector({sqlAdminRootUrl: expectedSqlAdminRootUrl});
new Connector({sqlAdminAPIEndpoint: expectedsqlAdminAPIEndpoint});

t.same(actualSqlAdminRootUrl, expectedSqlAdminRootUrl);
t.same(actualsqlAdminAPIEndpoint, expectedsqlAdminAPIEndpoint);
});
24 changes: 14 additions & 10 deletions test/sqladmin-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ const ephCertResponse = {
const mockRequest = (
instanceInfo: InstanceConnectionInfo,
overrides?: sqladmin_v1beta4.Schema$ConnectSettings,
sqlAdminRootUrl?: string
sqlAdminAPIEndpoint?: string
): void => {
const {projectId, regionId, instanceId} = instanceInfo;

nock(sqlAdminRootUrl ?? 'https://sqladmin.googleapis.com')
nock(sqlAdminAPIEndpoint ?? 'https://sqladmin.googleapis.com')
.get(
`/sql/v1beta4/projects/${projectId}/instances/${instanceId}/connectSettings`
)
Expand Down Expand Up @@ -101,15 +101,15 @@ t.test('getInstanceMetadata', async t => {

t.test('getInstanceMetadata custom SQL Admin API endpoint', async t => {
setupCredentials(t);
const sqlAdminRootUrl = 'https://sqladmin.mydomain.com';
const sqlAdminAPIEndpoint = 'https://sqladmin.mydomain.com';
const instanceConnectionInfo: InstanceConnectionInfo = {
projectId: 'my-project',
regionId: 'us-east1',
instanceId: 'my-instance',
};
mockRequest(instanceConnectionInfo, {}, sqlAdminRootUrl);
mockRequest(instanceConnectionInfo, {}, sqlAdminAPIEndpoint);

const fetcher = new SQLAdminFetcher({sqlAdminRootUrl});
const fetcher = new SQLAdminFetcher({sqlAdminAPIEndpoint});
const instanceMetadata = await fetcher.getInstanceMetadata(
instanceConnectionInfo
);
Expand Down Expand Up @@ -252,11 +252,11 @@ t.test('getInstanceMetadata invalid region', async t => {
const mockGenerateEphemeralCertRequest = (
instanceInfo: InstanceConnectionInfo,
overrides?: sqladmin_v1beta4.Schema$GenerateEphemeralCertResponse,
sqlAdminRootUrl?: string
sqlAdminAPIEndpoint?: string
): void => {
const {projectId, instanceId} = instanceInfo;

nock(sqlAdminRootUrl ?? 'https://sqladmin.googleapis.com')
nock(sqlAdminAPIEndpoint ?? 'https://sqladmin.googleapis.com')
.post(
`/sql/v1beta4/projects/${projectId}/instances/${instanceId}:generateEphemeralCert`
)
Expand Down Expand Up @@ -294,15 +294,19 @@ t.test('getEphemeralCertificate', async t => {

t.test('getEphemeralCertificate custom SQL Admin API endpoint', async t => {
setupCredentials(t);
const sqlAdminRootUrl = 'https://sqladmin.mydomain.com';
const sqlAdminAPIEndpoint = 'https://sqladmin.mydomain.com';
const instanceConnectionInfo: InstanceConnectionInfo = {
projectId: 'my-project',
regionId: 'us-east1',
instanceId: 'my-instance',
};
mockGenerateEphemeralCertRequest(instanceConnectionInfo, {}, sqlAdminRootUrl);
mockGenerateEphemeralCertRequest(
instanceConnectionInfo,
{},
sqlAdminAPIEndpoint
);

const fetcher = new SQLAdminFetcher({sqlAdminRootUrl});
const fetcher = new SQLAdminFetcher({sqlAdminAPIEndpoint});
const ephemeralCert = await fetcher.getEphemeralCertificate(
instanceConnectionInfo,
'key',
Expand Down

0 comments on commit aeecd31

Please sign in to comment.