Skip to content

Commit

Permalink
Add pre-push hook to verify region support (#8269)
Browse files Browse the repository at this point in the history
* fix(amplify-category-geo): add pre-push hook to verify region support

* fix(amplify-category-geo): remove test for unsupported region
  • Loading branch information
phani-srikar authored Sep 24, 2021
1 parent 422dd04 commit f2fa479
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/amplify-category-geo/amplify-plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"commandAliases":{
"configure": "update"
},
"eventHandlers": []
"eventHandlers": ["PrePush"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,4 @@ describe('remove command tests', () => {

expect(mockRemoveResource).toHaveBeenCalledWith(mockContext, service);
});

it('remove resource workflow is not invoked for unsupported region', async() => {
mockAmplifyMeta.providers[provider] = {
Region: 'eu-west-2'
};
stateManager.getMeta = jest.fn().mockReturnValue(mockAmplifyMeta);

await run(mockContext);

expect(mockRemoveResource).toBeCalledTimes(0);
});
});
5 changes: 0 additions & 5 deletions packages/amplify-category-geo/src/commands/geo/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import { supportedServices } from '../../supportedServices';
import { $TSAny, $TSContext } from 'amplify-cli-core';
import { removeResource } from '../../provider-controllers';
import { printer } from 'amplify-prompts';
import { verifySupportedRegion } from '../../service-utils/resourceUtils';

export const name = 'remove';

export const run = async(context: $TSContext) => {
const { amplify } = context;
try {
if(!verifySupportedRegion()) {
return;
}

const result: {service: string, providerName: string} = await amplify.serviceSelectionPrompt(context, category, supportedServices, chooseServiceMessageRemove);

if (result.providerName !== provider) {
Expand Down
20 changes: 15 additions & 5 deletions packages/amplify-category-geo/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { $TSContext, $TSObject, stateManager } from 'amplify-cli-core';
import { $TSContext, $TSObject, stateManager, exitOnNextTick, $TSAny } from 'amplify-cli-core';
import { category } from './constants';
import * as addCommand from './commands/geo/add';
import * as updateCommand from './commands/geo/update';
import * as removeCommand from './commands/geo/remove';
import * as consoleCommand from './commands/geo/console';
import * as helpCommand from './commands/geo/help';
import { getServicePermissionPolicies } from './service-utils/resourceUtils';
import { getServicePermissionPolicies, verifySupportedRegion, checkAnyGeoResourceExists } from './service-utils/resourceUtils';
import { ServiceName } from './service-utils/constants';
import { printer } from 'amplify-prompts';

Expand All @@ -32,9 +32,19 @@ export const executeAmplifyCommand = async (context: $TSContext) => {
}
};

export const handleAmplifyEvent = (context: $TSContext, args: $TSObject) => {
printer.info(`${category} handleAmplifyEvent to be implemented`);
printer.info(`Received event args ${args}`);
export const handleAmplifyEvent = async (context: $TSContext, args: $TSAny) => {
switch (args.event) {
case 'PrePush':
if((await checkAnyGeoResourceExists()) && !verifySupportedRegion()) {
const errMessage = 'Failed to create Geo resources';
await context.usageData.emitError(new Error(errMessage));
printer.info('Remove Geo resources using "amplify remove geo" and retry "amplify push"');
exitOnNextTick(1);
}
break;
default:
break;
}
};

export const getPermissionPolicies = (context: $TSContext, resourceOpsMapping: $TSObject) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,16 @@ export const getServicePermissionPolicies = (
export const verifySupportedRegion = (): boolean => {
const currentRegion = stateManager.getMeta()?.providers[provider]?.Region;
if(!supportedRegions.includes(currentRegion)) {
printer.error(`Geo category is not supported in your region: ${currentRegion}`);
printer.error(`Geo category is not supported in the region: [${currentRegion}]`);
return false;
}
return true;
};

/**
* Check if any Geo resource exists
*/
export const checkAnyGeoResourceExists = async (): Promise<boolean> => {
const geoMeta = stateManager.getMeta()?.[category];
return geoMeta && Object.keys(geoMeta) && Object.keys(geoMeta).length > 0;
}

0 comments on commit f2fa479

Please sign in to comment.