Skip to content

Commit

Permalink
fix(amplify-category-notifications): remove env name (#1440)
Browse files Browse the repository at this point in the history
* fix(amplify-category-notifications): remove env name

remove env name from the resource name in the notification category

fix #1372

* update
  • Loading branch information
UnleashedMind committed Jun 10, 2019
1 parent a4c2547 commit 19ff63c
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ function checkIfNotificationsCategoryExists(context) {
if (amplifyMeta.notifications) {
const categoryResources = amplifyMeta.notifications;
Object.keys(categoryResources).forEach((resource) => {
if (categoryResources[resource].service === serviceName) {
if (categoryResources[resource].service === serviceName &&
categoryResources[resource].output.Id) {
pinpointApp = {};
pinpointApp.appId = categoryResources[resource].output.Id;
pinpointApp.appName = categoryResources[resource].output.Name;
pinpointApp.appName = resource;
}
});
}
Expand Down
5 changes: 0 additions & 5 deletions packages/amplify-category-notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ async function initEnv(context) {
await multiEnvManager.initEnv(context);
}

async function initEnvPush(context) {
await multiEnvManager.initEnvPush(context);
}

async function migrate(context) {
await multiEnvManager.migrate(context);
}
Expand All @@ -25,7 +21,6 @@ module.exports = {
console,
deletePinpointAppForEnv,
initEnv,
initEnvPush,
migrate,
};

43 changes: 16 additions & 27 deletions packages/amplify-category-notifications/lib/multi-env-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ async function initEnv(context) {
return pinpointNotificationsMeta;
}

// this function will be called after init and init-push are separated.
async function initEnvPush(context, pinpointNotificationsMeta) {//eslint-disable-line
// await pushChanges(context, pinpointNotificationsMeta);//eslint-disable-line
}

async function constructPinpointNotificationsMeta(context) {
let pinpointApp;
let serviceBackendConfig;
Expand Down Expand Up @@ -47,11 +42,11 @@ async function constructPinpointNotificationsMeta(context) {
const backendConfig = context.amplify.readJsonFile(backendConfigFilePath);
if (backendConfig[constants.CategoryName]) {
const categoryConfig = backendConfig[constants.CategoryName];
const services = Object.keys(categoryConfig);
for (let i = 0; i < services.length; i++) {
serviceBackendConfig = categoryConfig[services[i]];
const resources = Object.keys(categoryConfig);
for (let i = 0; i < resources.length; i++) {
serviceBackendConfig = categoryConfig[resources[i]];
if (serviceBackendConfig.service === constants.PinpointName) {
serviceBackendConfig.Name = services[i];
serviceBackendConfig.resourceName = resources[i];
break;
}
}
Expand Down Expand Up @@ -157,14 +152,9 @@ async function pushChanges(context, pinpointNotificationsMeta) {
}
});

const { amplifyMeta } = context.exeInfo;
amplifyMeta[constants.CategoryName] = {};
amplifyMeta[constants.CategoryName][pinpointNotificationsMeta.Name] = pinpointNotificationsMeta;
delete pinpointNotificationsMeta.channels;
delete pinpointNotificationsMeta.Name;
await pinpointHelper.ensurePinpointApp(context, pinpointNotificationsMeta.resourceName);

const tasks = [];
tasks.push(() => pinpointHelper.ensurePinpointApp(context));
channelsToEnable.forEach((channel) => {
tasks.push(() => notificationManager.enableChannel(context, channel));
});
Expand All @@ -184,9 +174,9 @@ async function writeData(context) {
if (categoryMeta) {
const availableChannels = notificationManager.getAvailableChannels();
const enabledChannels = [];
const services = Object.keys(categoryMeta);
for (let i = 0; i < services.length; i++) {
const serviceMeta = categoryMeta[services[i]];
const resources = Object.keys(categoryMeta);
for (let i = 0; i < resources.length; i++) {
const serviceMeta = categoryMeta[resources[i]];
if (serviceMeta.service === constants.PinpointName &&
serviceMeta.output &&
serviceMeta.output.Id) {
Expand All @@ -196,7 +186,7 @@ async function writeData(context) {
}
});
pinpointMeta = {
serviceName: services[i],
serviceName: resources[i],
service: serviceMeta.service,
channels: enabledChannels,
Name: serviceMeta.output.Name,
Expand Down Expand Up @@ -249,11 +239,11 @@ function writeBackendConfig(context, pinpointMeta, backendConfigFilePath) {
const backendConfig = context.amplify.readJsonFile(backendConfigFilePath);
backendConfig[constants.CategoryName] = backendConfig[constants.CategoryName] || {};

const services = Object.keys(backendConfig[constants.CategoryName]);
for (let i = 0; i < services.length; i++) {
const serviceMeta = backendConfig[constants.CategoryName][services[i]];
const resources = Object.keys(backendConfig[constants.CategoryName]);
for (let i = 0; i < resources.length; i++) {
const serviceMeta = backendConfig[constants.CategoryName][resources[i]];
if (serviceMeta.service === constants.PinpointName) {
delete backendConfig[constants.CategoryName][services[i]];
delete backendConfig[constants.CategoryName][resources[i]];
}
}

Expand Down Expand Up @@ -289,9 +279,9 @@ function extractMigrationInfo(context) {
const { amplifyMeta, localEnvInfo } = context.migrationInfo;
if (amplifyMeta[constants.CategoryName]) {
const categoryMeta = amplifyMeta[constants.CategoryName];
const services = Object.keys(categoryMeta);
for (let i = 0; i < services.length; i++) {
const service = services[i];
const resources = Object.keys(categoryMeta);
for (let i = 0; i < resources.length; i++) {
const service = resources[i];
const serviceMeta = amplifyMeta[constants.CategoryName][service];
if (serviceMeta.service === constants.PinpointName) {
migrationInfo = {};
Expand Down Expand Up @@ -356,7 +346,6 @@ function fillTeamProviderInfo(context, migrationInfo) {

module.exports = {
initEnv,
initEnvPush,
deletePinpointAppForEnv,
writeData,
migrate,
Expand Down
166 changes: 83 additions & 83 deletions packages/amplify-category-notifications/lib/pinpoint-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,85 @@ function getPinpointApp(context) {
return pinpointApp;
}

async function ensurePinpointApp(context) {
const { amplifyMeta } = context.exeInfo;
let pinpointApp = scanCategoryMetaForPinpoint(amplifyMeta[constants.CategoryName]);
if (!pinpointApp) {
async function ensurePinpointApp(context, resourceName) {
const { amplifyMeta, localEnvInfo } = context.exeInfo;
const scanOptions = {
isRegulatingResourceName: true,
envName: localEnvInfo.envName,
};
let pinpointApp = scanCategoryMetaForPinpoint(amplifyMeta[constants.CategoryName], scanOptions);
if (pinpointApp) {
resourceName = scanOptions.regulatedResourceName;
} else {
pinpointApp = scanCategoryMetaForPinpoint(amplifyMeta[constants.AnalyticsCategoryName]);
if (pinpointApp) {
if (!pinpointApp.Name) {
pinpointApp = await getApp(context, pinpointApp.Id);
}
amplifyMeta[constants.CategoryName] = {};
amplifyMeta[constants.CategoryName][pinpointApp.Name] = {
service: constants.PinpointName,
output: {
Name: pinpointApp.Name,
Id: pinpointApp.Id,
Region: pinpointApp.Region,
},
};
resourceName = generateResourceName(pinpointApp.Name, localEnvInfo.envName);
constructResourceMeta(amplifyMeta, resourceName, pinpointApp);
} else {
context.print.info('');
pinpointApp = await createPinpointApp(context);
resourceName = await createPinpointApp(context, resourceName);
}
}
context.exeInfo.pinpointApp = pinpointApp;
context.exeInfo.serviceMeta =
context.exeInfo.amplifyMeta[constants.CategoryName][pinpointApp.Name];
context.exeInfo.amplifyMeta[constants.CategoryName][resourceName];
context.exeInfo.pinpointApp = context.exeInfo.serviceMeta.output;
}

// rerouce name is consistent cross environments
function generateResourceName(pinpointAppName, envName) {
return pinpointAppName.replace(getEnvTagPattern(envName), '');
}

function generatePinpoinAppName(resourceName, envName) {
return resourceName + getEnvTagPattern(envName);
}

function getEnvTagPattern(envName) {
return envName === 'NONE' ? '' : `-${envName}`;
}

async function createPinpointApp(context) {
async function createPinpointApp(context, resourceName) {
const { projectConfig, amplifyMeta, localEnvInfo } = context.exeInfo;
let pinpointProjectName = localEnvInfo.envName === 'NONE' ?
(projectConfig.projectName + context.amplify.makeId(5)) :
`${projectConfig.projectName + context.amplify.makeId(5)}-${localEnvInfo.envName}`;

context.print.info('An Amazon Pinpoint project will be created for notifications.');

if (!context.exeInfo.inputParams || !context.exeInfo.inputParams.yes) {
const answer = await inquirer.prompt({
name: 'projectName',
type: 'input',
message: 'Pinpoint project name',
default: pinpointProjectName,
validate: (name) => {
let result = false;
let message = '';
if (name && name.length > 0) {
result = true;
} else {
message = 'Project name can not be empty.';
}
return result || message;
},
});
pinpointProjectName = answer.projectName;
if (!resourceName) {
resourceName = projectConfig.projectName + context.amplify.makeId(5);
if (!context.exeInfo.inputParams || !context.exeInfo.inputParams.yes) {
const answer = await inquirer.prompt({
name: 'resourceNameInput',
type: 'input',
message: 'Provide your pinpoint resource name: ',
default: resourceName,
validate: (name) => {
let result = false;
let message = '';
if (name && name.length > 0) {
result = true;
} else {
message = 'Your pinpoint resource name can not be empty.';
}
return result || message;
},
});
resourceName = answer.resourceNameInput;
}
}

const pinpointApp = await createApp(context, pinpointProjectName);
amplifyMeta[constants.CategoryName] = {};
amplifyMeta[constants.CategoryName][pinpointApp.Name] = {
const pinpointAppName = generatePinpoinAppName(resourceName, localEnvInfo.envName);
const pinpointApp = await createApp(context, pinpointAppName);
constructResourceMeta(amplifyMeta, resourceName, pinpointApp);
context.exeInfo.pinpointApp = pinpointApp; // needed for authHelper.ensureAuth(context);

context.print.info('');
await authHelper.ensureAuth(context);
context.print.info('');

return resourceName;
}

function constructResourceMeta(amplifyMeta, resourceName, pinpointApp) {
amplifyMeta[constants.CategoryName] = amplifyMeta[constants.CategoryName] || {};
amplifyMeta[constants.CategoryName][resourceName] = {
service: constants.PinpointName,
output: {
Name: pinpointApp.Name,
Expand All @@ -84,13 +104,6 @@ async function createPinpointApp(context) {
},
lastPushTimeStamp: new Date(),
};

context.exeInfo.pinpointApp = pinpointApp;
context.print.info('');
await authHelper.ensureAuth(context);
context.print.info('');

return pinpointApp;
}

async function deletePinpointApp(context) {
Expand All @@ -106,30 +119,37 @@ async function deletePinpointApp(context) {
}
}

function scanCategoryMetaForPinpoint(categoryMeta) {
function scanCategoryMetaForPinpoint(categoryMeta, options) {
let result;
if (categoryMeta) {
const services = Object.keys(categoryMeta);
for (let i = 0; i < services.length; i++) {
const serviceMeta = categoryMeta[services[i]];
let resourceName;
const resources = Object.keys(categoryMeta);
for (let i = 0; i < resources.length; i++) {
resourceName = resources[i];
const serviceMeta = categoryMeta[resourceName];
if (serviceMeta.service === constants.PinpointName &&
serviceMeta.output &&
serviceMeta.output.Id) {
result = {
Id: serviceMeta.output.Id,
};
if (serviceMeta.output.Name) {
result.Name = serviceMeta.output.Name;
} else if (serviceMeta.output.appName) {
result.Name = serviceMeta.output.appName;
}
if (serviceMeta.output.Region) {
result.Region = serviceMeta.output.Region;
result.Name = serviceMeta.output.Name || serviceMeta.output.appName;
result.Region = serviceMeta.output.Region;

if (options && options.isRegulatingResourceName) {
const regulatedResourceName = generateResourceName(result.Name, options.envName);
options.regulatedResourceName = regulatedResourceName;
if (resourceName !== regulatedResourceName) {
categoryMeta[regulatedResourceName] = serviceMeta;
delete categoryMeta[resourceName];
}
}

break;
}
}
}

return result;
}

Expand Down Expand Up @@ -171,26 +191,6 @@ async function createApp(context, pinpointAppName) {
});
}

async function getApp(context, pinpointAppId) {
const params = {
ApplicationId: pinpointAppId,
};
spinner.start('Retrieving Pinpoint app information.');
const pinpointClient = await getPinpointClient(context, 'get');
return new Promise((resolve, reject) => {
pinpointClient.getApp(params, (err, data) => {
if (err) {
spinner.fail('Pinpoint project retrieval error');
reject(err);
} else {
spinner.succeed(`Successfully retrieved Pinpoint project: ${data.ApplicationResponse.Name}`);
data.ApplicationResponse.Region = pinpointClient.config.region;
resolve(data.ApplicationResponse);
}
});
});
}

async function deleteApp(context, pinpointAppId) {
const params = {
ApplicationId: pinpointAppId,
Expand Down

0 comments on commit 19ff63c

Please sign in to comment.