Skip to content

Commit

Permalink
feat(packages/twilio-run): regionalize toolkit config and api (#433)
Browse files Browse the repository at this point in the history
* feat(packages/twilio-run): regionalize the .twiliodeployinfo

* feat: regional support for config files

* chore(runtime-handler/packager.json): override a check

* chore: typescript override for lodash types

* refactor: using optional chaining

* fix: passing missing region param to related functions
  • Loading branch information
mbeldartwilio authored Nov 7, 2022
1 parent d4fde96 commit 30d5bdb
Show file tree
Hide file tree
Showing 20 changed files with 170 additions and 29 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
},
"overrides": {
"@types/prettier": "2.6.0",
"@types/express-serve-static-core": "ts3.9"
"@types/express-serve-static-core": "ts3.9",
"@types/lodash": "ts3.9"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
},
"overrides": {
"@types/prettier": "2.6.0",
"@types/express-serve-static-core": "ts3.9"
"@types/express-serve-static-core": "ts3.9",
"@types/lodash": "ts3.9"
},
"gitHead": "6db273648ed19474f4125042556b10c051529912"
}
3 changes: 2 additions & 1 deletion packages/serverless-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"upath": "^1.1.2"
},
"overrides": {
"@types/prettier": "2.6.0"
"@types/prettier": "2.6.0",
"@types/lodash": "ts3.9"
},
"gitHead": "6db273648ed19474f4125042556b10c051529912"
}
14 changes: 14 additions & 0 deletions packages/serverless-api/src/api/utils/__tests__/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ describe('getApiUrl', () => {
const url = getApiUrl(DEFAULT_TEST_CLIENT_CONFIG);
expect(url).toBe('https://serverless.sydney.au2.twilio.com/v1');
});

test('handles edge with only eligible region with variable', () => {
process.env.TWILIO_REGION = 'au1';
const url = getApiUrl(DEFAULT_TEST_CLIENT_CONFIG);
expect(url).toBe('https://serverless.sydney.au1.twilio.com/v1');
});

test('handles edge with only eligible region with params', () => {
const url = getApiUrl({
...DEFAULT_TEST_CLIENT_CONFIG,
region: 'us1',
});
expect(url).toBe('https://serverless.ashburn.us1.twilio.com/v1');
});
});
17 changes: 16 additions & 1 deletion packages/serverless-api/src/api/utils/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { ClientConfig } from '../../types';

const regionEdgeMap: { [index: string]: string } = {
us1: 'ashburn',
au1: 'sydney',
ie1: 'dublin',
'stage-us1': 'ashburn',
'stage-au1': 'sydney',
};

export function getApiUrl(
config: ClientConfig,
product = 'serverless',
apiVersion = 'v1'
): string {
const configEdge = config.edge || process.env.TWILIO_EDGE;
const configRegion = config.region || process.env.TWILIO_REGION;
const region = configRegion ? `${configRegion}.` : '';

if (!configEdge && configRegion) {
const defaultEdge = regionEdgeMap[configRegion]
? `${regionEdgeMap[configRegion]}.`
: '';
return `https://${product}.${defaultEdge}${region}twilio.com/${apiVersion}`;
}

const edge = configEdge ? `${configEdge}.` : '';
const region = configRegion ? `${configRegion}.` : '';
return `https://${product}.${edge}${region}twilio.com/${apiVersion}`;
}
65 changes: 65 additions & 0 deletions packages/twilio-run/__tests__/config/global.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,69 @@ describe('readSpecializedConfig', () => {
env: '.env.stage',
});
});

test('account + region config override', () => {
__setTestConfig({
serviceSid: 'ZS11112222111122221111222211112222',
env: '.env.example',
commands: {
deploy: {
functionsFolder: '/tmp/functions',
},
},
environments: {
prod: {
serviceSid: 'ZS11112222111122221111222211112223',
env: '.env.prod',
},
},
projects: {
'AC11112222111122221111222211114444:au1': {
serviceSid: 'ZS11112222111122221111222211114444',
},
'AC11112222111122221111222211114444:ie1': {
serviceSid: 'ZS11112222111122221111222211114445',
},
AC11112222111122221111222211114444: {
serviceSid: 'ZS11112222111122221111222211114446',
},
},
});

expect(
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy', {
environmentSuffix: 'prod',
username: 'AC11112222111122221111222211114444',
region: 'ie1',
})
).toEqual({
serviceSid: 'ZS11112222111122221111222211114445',
functionsFolder: '/tmp/functions',
env: '.env.prod',
});

expect(
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy', {
environmentSuffix: 'prod',
username: 'AC11112222111122221111222211114444',
region: 'au1',
})
).toEqual({
serviceSid: 'ZS11112222111122221111222211114444',
functionsFolder: '/tmp/functions',
env: '.env.prod',
});

expect(
readSpecializedConfig('/tmp', '.twilioserverlessrc', 'deploy', {
environmentSuffix: 'prod',
username: 'AC11112222111122221111222211114444',
region: 'us1',
})
).toEqual({
serviceSid: 'ZS11112222111122221111222211114446',
functionsFolder: '/tmp/functions',
env: '.env.prod',
});
});
});
3 changes: 2 additions & 1 deletion packages/twilio-run/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@
"node": ">=12.22.1"
},
"overrides": {
"@types/express-serve-static-core": "ts3.9"
"@types/express-serve-static-core": "ts3.9",
"@types/lodash": "ts3.9"
},
"gitHead": "6db273648ed19474f4125042556b10c051529912"
}
3 changes: 2 additions & 1 deletion packages/twilio-run/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export async function handler(
buildSid,
config.username.startsWith('AC')
? config.username
: externalCliOptions && externalCliOptions.accountSid
: externalCliOptions && externalCliOptions.accountSid,
config.region
);
} catch (err) {
handleError(err, spinner, flags, config);
Expand Down
4 changes: 3 additions & 1 deletion packages/twilio-run/src/config/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export async function getConfigFromFlags(
(externalCliOptions && externalCliOptions.accountSid) ||
undefined,
environmentSuffix: flags.environment,
region: flags.region,
});

flags = mergeFlagsAndConfig<DeployCliFlags>(configFlags, flags, cliInfo);
Expand All @@ -98,7 +99,8 @@ export async function getConfigFromFlags(
? flags.username
: username.startsWith('AC')
? username
: externalCliOptions?.accountSid
: externalCliOptions?.accountSid,
flags.region
));

const pkgJson = await readPackageJsonContent(flags);
Expand Down
4 changes: 3 additions & 1 deletion packages/twilio-run/src/config/env/env-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function getConfigFromFlags(
(externalCliOptions && externalCliOptions.accountSid) ||
undefined,
environmentSuffix: flags.environment,
region: flags.region,
});

flags = mergeFlagsAndConfig<EnvGetFlags>(configFlags, flags, cliInfo);
Expand All @@ -71,7 +72,8 @@ export async function getConfigFromFlags(
? flags.username
: username.startsWith('AC')
? username
: externalCliOptions?.accountSid
: externalCliOptions?.accountSid,
flags.region
));

let serviceName = await getServiceNameFromFlags(flags);
Expand Down
4 changes: 3 additions & 1 deletion packages/twilio-run/src/config/env/env-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export async function getConfigFromFlags(
(externalCliOptions && externalCliOptions.accountSid) ||
undefined,
environmentSuffix: flags.environment,
region: flags.region,
});

flags = mergeFlagsAndConfig<EnvImportFlags>(configFlags, flags, cliInfo);
Expand All @@ -72,7 +73,8 @@ export async function getConfigFromFlags(
? flags.username
: username.startsWith('AC')
? username
: externalCliOptions?.accountSid
: externalCliOptions?.accountSid,
flags.region
));

let serviceName = await getServiceNameFromFlags(flags);
Expand Down
4 changes: 3 additions & 1 deletion packages/twilio-run/src/config/env/env-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export async function getConfigFromFlags(
(externalCliOptions && externalCliOptions.accountSid) ||
undefined,
environmentSuffix: flags.environment,
region: flags.region,
});

flags = mergeFlagsAndConfig<EnvListFlags>(configFlags, flags, cliInfo);
Expand All @@ -75,7 +76,8 @@ export async function getConfigFromFlags(
? flags.username
: username.startsWith('AC')
? username
: externalCliOptions?.accountSid
: externalCliOptions?.accountSid,
flags.region
));

let serviceName = await getServiceNameFromFlags(flags);
Expand Down
4 changes: 3 additions & 1 deletion packages/twilio-run/src/config/env/env-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export async function getConfigFromFlags(
(externalCliOptions && externalCliOptions.accountSid) ||
undefined,
environmentSuffix: flags.environment,
region: flags.region,
});

flags = mergeFlagsAndConfig<EnvSetFlags>(configFlags, flags, cliInfo);
Expand All @@ -72,7 +73,8 @@ export async function getConfigFromFlags(
? flags.username
: username.startsWith('AC')
? username
: externalCliOptions?.accountSid
: externalCliOptions?.accountSid,
flags.region
));

let serviceName = await getServiceNameFromFlags(flags);
Expand Down
4 changes: 3 additions & 1 deletion packages/twilio-run/src/config/env/env-unset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function getConfigFromFlags(
(externalCliOptions && externalCliOptions.accountSid) ||
undefined,
environmentSuffix: flags.environment,
region: flags.region,
});

flags = mergeFlagsAndConfig<EnvUnsetFlags>(configFlags, flags, cliInfo);
Expand All @@ -71,7 +72,8 @@ export async function getConfigFromFlags(
? flags.username
: username.startsWith('AC')
? username
: externalCliOptions?.accountSid
: externalCliOptions?.accountSid,
flags.region
));

let serviceName = await getServiceNameFromFlags(flags);
Expand Down
19 changes: 14 additions & 5 deletions packages/twilio-run/src/config/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getConfig } from './utils/configLoader';
export type SpecializedConfigOptions = {
username: string;
environmentSuffix: string;
region: string;
};

export const EXCLUDED_FLAGS = [
Expand Down Expand Up @@ -68,12 +69,20 @@ export function readSpecializedConfig<T extends CommandConfigurationNames>(
opts &&
opts.username &&
projectsConfig &&
projectsConfig[opts.username]
(projectsConfig[opts.username] ||
projectsConfig[`${opts.username}:${opts.region}`])
) {
result = {
...result,
...projectsConfig[opts.username],
};
if (projectsConfig[`${opts.username}:${opts.region}`]) {
result = {
...result,
...projectsConfig[`${opts.username}:${opts.region}`],
};
} else if (opts.region === 'us1' || opts.region === undefined) {
result = {
...result,
...projectsConfig[opts.username],
};
}
}

EXCLUDED_FLAGS.forEach((key) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/twilio-run/src/config/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export async function getConfigFromFlags(
(externalCliOptions && externalCliOptions.accountSid) ||
undefined,
environmentSuffix: flags.environment,
region: flags.region,
});

flags = mergeFlagsAndConfig<ListCliFlags>(configFlags, flags, cliInfo);
Expand All @@ -85,7 +86,8 @@ export async function getConfigFromFlags(
? flags.username
: username.startsWith('AC')
? username
: externalCliOptions?.accountSid
: externalCliOptions?.accountSid,
flags.region
));

let serviceName = await getServiceNameFromFlags(flags);
Expand Down
4 changes: 3 additions & 1 deletion packages/twilio-run/src/config/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export async function getConfigFromFlags(
(externalCliOptions && externalCliOptions.accountSid) ||
undefined,
environmentSuffix: environment,
region: flags.region,
});

flags = mergeFlagsAndConfig<LogsCliFlags>(configFlags, flags, cliInfo);
Expand Down Expand Up @@ -85,7 +86,8 @@ export async function getConfigFromFlags(
? flags.username
: username.startsWith('AC')
? username
: externalCliOptions?.accountSid
: externalCliOptions?.accountSid,
flags.region
));

const serviceSid = checkForValidServiceSid(command, potentialServiceSid);
Expand Down
4 changes: 3 additions & 1 deletion packages/twilio-run/src/config/promote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function getConfigFromFlags(
(externalCliOptions && externalCliOptions.accountSid) ||
undefined,
environmentSuffix: flags.environment,
region: flags.region,
});

flags = mergeFlagsAndConfig<PromoteCliFlags>(configFlags, flags, cliInfo);
Expand All @@ -82,7 +83,8 @@ export async function getConfigFromFlags(
? flags.username
: username.startsWith('AC')
? username
: externalCliOptions?.accountSid
: externalCliOptions?.accountSid,
flags.region
));

const serviceSid = checkForValidServiceSid(command, potentialServiceSid);
Expand Down
Loading

0 comments on commit 30d5bdb

Please sign in to comment.