Skip to content

Commit

Permalink
feat: multienv support for Notifications (#440)
Browse files Browse the repository at this point in the history
* resolve env config issue

* minor fix

* through lint

* minor fix

* add multienv support for hosting

* minor fix

* work in progressg

* work in progress

* work in progress

* work in progress

* move write amplify meta into the multi-env-manager

* insert pull methods for the channels

* work in progress

* work in progess

* through lint

* minor fix

* add categories input params

* work in progress

* add input params to channel enable logic

* add input params validation for the channels

* through lint

* work in progress

* work in progress

* through lint

* through lint

* work in progress

* minor fix

* through push

* tested with issue fixes

* through lint

* fix tests

* changes per reivew comments

* changes per review request

* changes per review request
  • Loading branch information
UnleashedMind authored and kaustavghosh06 committed Nov 15, 2018
1 parent 8e0198d commit a2964d4
Show file tree
Hide file tree
Showing 20 changed files with 590 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ describe('apns-cert-config', () => {
const mockPassword = 'mock_password';

const mockAnswers = {
filePath: mockFielPath,
password: mockPassword
P12FilePath: mockFielPath,
P12FilePassword: mockPassword
};
const mockP12DecoderReturn = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('apns-key-config', () => {
BundleId: mockBundleId,
TeamId: mockTeamId,
TokenKeyId: mockTokenKeyId,
filePath: mockFielPath
P8FilePath: mockFielPath
};
const mockP8DecoderReturn = 'mockP8DecoderReturn';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const inquirer = require('inquirer');
const pinpointHelper = require('../../lib/pinpoint-helper');
const notificationManager = require('../../lib/notifications-manager');
const writeAmplifyMeta = require('../../lib/writeAmplifyMeta');
const multiEnvManager = require('../../lib/multi-env-manager');

module.exports = {
name: 'add',
Expand Down Expand Up @@ -30,7 +30,7 @@ module.exports = {
if (channelName) {
await pinpointHelper.ensurePinpointApp(context);
await notificationManager.enableChannel(context, channelName);
writeAmplifyMeta(context);
multiEnvManager.writeData(context);
}
} else {
context.print.info('All the available notification channels have already been enabled.');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const inquirer = require('inquirer');
const pinpointHelper = require('../../lib/pinpoint-helper');
const notificationManager = require('../../lib/notifications-manager');
const writeAmplifyMeta = require('../../lib/writeAmplifyMeta');
const multiEnvManager = require('../../lib/multi-env-manager');

module.exports = {
name: 'configure',
Expand All @@ -24,7 +24,7 @@ module.exports = {

await pinpointHelper.ensurePinpointApp(context);
await notificationManager.configureChannel(context, channelName);
writeAmplifyMeta(context);
multiEnvManager.writeData(context);

return context;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const inquirer = require('inquirer');
const pinpointHelper = require('../../lib/pinpoint-helper');
const notificationManager = require('../../lib/notifications-manager');
const writeAmplifyMeta = require('../../lib/writeAmplifyMeta');
const multiEnvManager = require('../../lib/multi-env-manager');

const PinpointApp = 'The Pinpoint application';
const Cancel = 'Cancel';
Expand Down Expand Up @@ -37,7 +37,7 @@ module.exports = {
if (channelName !== PinpointApp) {
await pinpointHelper.ensurePinpointApp(context);
await notificationManager.disableChannel(context, channelName);
writeAmplifyMeta(context);
multiEnvManager.writeData(context);
} else if (pinpointHelper.isAnalyticsAdded(context)) {
context.print.error('Execution aborted.');
context.print.info('You have an analytics resource in your backend tied to the Amazon Pinpoint resource');
Expand All @@ -52,7 +52,7 @@ module.exports = {
if (answer.deletePinpointApp) {
await pinpointHelper.deletePinpointApp(context);
context.print.info('The Pinpoint application has been successfully deleted.');
writeAmplifyMeta(context);
multiEnvManager.writeData(context);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/amplify-category-notifications/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const pinpointHelper = require('./lib/pinpoint-helper');
const multiEnvManager = require('./lib/multi-env-manager');

async function console(context) {
await pinpointHelper.console(context);
Expand All @@ -9,8 +10,18 @@ async function deletePinpointApp(context) {
await pinpointHelper.deletePinpointApp(context);
}

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

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

module.exports = {
console,
deletePinpointApp,
initEnv,
initEnvPush,
};

38 changes: 21 additions & 17 deletions packages/amplify-category-notifications/lib/apns-cert-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ const inquirer = require('inquirer');
const p12decoder = require('./p12decoder');
const validateFilePath = require('./validateFilepath');

async function run() {
const questions = [
{
name: 'filePath',
type: 'input',
message: 'The certificate file path (.p12): ',
validate: validateFilePath,
},
{
name: 'password',
type: 'input',
message: 'The certificate password (if any): ',
},
];

const answers = await inquirer.prompt(questions);
const certificateConfig = await p12decoder.run(answers);
async function run(channelInput) {
let certificateConfig;
if (channelInput) {
certificateConfig = await p12decoder.run(channelInput);
} else {
const questions = [
{
name: 'P12FilePath',
type: 'input',
message: 'The certificate file path (.p12): ',
validate: validateFilePath,
},
{
name: 'P12FilePassword',
type: 'input',
message: 'The certificate password (if any): ',
},
];
const answers = await inquirer.prompt(questions);
certificateConfig = await p12decoder.run(answers);
}

return certificateConfig;
}
Expand Down
60 changes: 33 additions & 27 deletions packages/amplify-category-notifications/lib/apns-key-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@ const inquirer = require('inquirer');
const p8decoder = require('./p8decoder');
const validateFilePath = require('./validateFilepath');

async function run() {
const questions = [
{
name: 'BundleId',
type: 'input',
message: 'The bundle id used for APNs Tokens: ',
},
{
name: 'TeamId',
type: 'input',
message: 'The team id used for APNs Tokens: ',
},
{
name: 'TokenKeyId',
type: 'input',
message: 'The key id used for APNs Tokens: ',
},
{
name: 'filePath',
type: 'input',
message: 'The key file path (.p8): ',
validate: validateFilePath,
},
];
const keyConfig = await inquirer.prompt(questions);
keyConfig.TokenKey = await p8decoder.run(keyConfig.filePath);
delete keyConfig.filePath;
async function run(channelInput) {
let keyConfig;
if (channelInput) {
keyConfig = channelInput;
} else {
const questions = [
{
name: 'BundleId',
type: 'input',
message: 'The bundle id used for APNs Tokens: ',
},
{
name: 'TeamId',
type: 'input',
message: 'The team id used for APNs Tokens: ',
},
{
name: 'TokenKeyId',
type: 'input',
message: 'The key id used for APNs Tokens: ',
},
{
name: 'P8FilePath',
type: 'input',
message: 'The key file path (.p8): ',
validate: validateFilePath,
},
];
keyConfig = await inquirer.prompt(questions);
}

keyConfig.TokenKey = await p8decoder.run(keyConfig.P8FilePath);
delete keyConfig.P8FilePath;

return keyConfig;
}
Expand Down
103 changes: 77 additions & 26 deletions packages/amplify-category-notifications/lib/channel-APNS.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const inquirer = require('inquirer');
const ora = require('ora');
const fs = require('fs-extra');

const channelName = 'APNS';
const spinner = ora('');
Expand Down Expand Up @@ -40,44 +41,47 @@ async function configure(context) {
}

async function enable(context, successMessage) {
let channelOutput = {};
if (context.exeInfo.serviceMeta.output[channelName]) {
channelOutput = context.exeInfo.serviceMeta.output[channelName];
let channelInput;
let answers;
if (context.exeInfo.pinpointInputParams && context.exeInfo.pinpointInputParams[channelName]) {
channelInput = validateInputParams(context.exeInfo.pinpointInputParams[channelName]);
answers = {
DefaultAuthenticationMethod: channelInput.DefaultAuthenticationMethod,
};
} else {
let channelOutput = {};
if (context.exeInfo.serviceMeta.output[channelName]) {
channelOutput = context.exeInfo.serviceMeta.output[channelName];
}
const question = {
name: 'DefaultAuthenticationMethod',
type: 'list',
message: 'Choose authentication method used for APNs',
choices: ['Certificate', 'Key'],
default: channelOutput.DefaultAuthenticationMethod || 'Certificate',
};
answers = await inquirer.prompt(question);
}

const APNSChannelRequest = { Enabled: true };

const { DefaultAuthenticationMethod } = channelOutput;

let keyConfig;
let certificateConfig;

const answers = await inquirer.prompt({
name: 'DefaultAuthenticationMethod',
type: 'list',
message: 'Choose authentication method used for APNs',
choices: ['Certificate', 'Key'],
default: DefaultAuthenticationMethod || 'Certificate',
});

APNSChannelRequest.DefaultAuthenticationMethod = answers.DefaultAuthenticationMethod;

try {
if (APNSChannelRequest.DefaultAuthenticationMethod === 'Key') {
keyConfig = await configureKey.run();
if (answers.DefaultAuthenticationMethod === 'Key') {
const keyConfig = await configureKey.run(channelInput);
Object.assign(answers, keyConfig);
} else {
certificateConfig = await configureCertificate.run();
const certificateConfig = await configureCertificate.run(channelInput);
Object.assign(answers, certificateConfig);
}
} catch (err) {
context.print.error(err.message);
process.exit(1);
}

Object.assign(APNSChannelRequest, keyConfig, certificateConfig);

const params = {
ApplicationId: context.exeInfo.serviceMeta.output.Id,
APNSChannelRequest,
APNSChannelRequest: {
...answers,
Enabled: true,
},
};

spinner.start('Updating APNS Channel.');
Expand All @@ -98,6 +102,33 @@ async function enable(context, successMessage) {
});
}

function validateInputParams(channelInput) {
if (channelInput.DefaultAuthenticationMethod) {
const authMethod = channelInput.DefaultAuthenticationMethod;
if (authMethod === 'Certificate') {
if (!channelInput.P12FilePath) {
throw new Error('P12FilePath is missing for the APNS channel');
} else if (!fs.existsSync(channelInput.P12FilePath)) {
throw new Error(`P12 file ${channelInput.P12FilePath} can NOT be found for the APNS channel`);
}
} else if (authMethod === 'Key') {
if (!channelInput.BundleId || !channelInput.TeamId || !channelInput.TokenKeyId) {
throw new Error('Missing BundleId, TeamId or TokenKeyId for the APNS channel');
} else if (!channelInput.P8FilePath) {
throw new Error('P8FilePath is missing for the APNS channel');
} else if (!fs.existsSync(channelInput.P8FilePath)) {
throw new Error(`P8 file ${channelInput.P8FilePath} can NOT be found for the APNS channel`);
}
} else {
throw new Error(`DefaultAuthenticationMethod ${authMethod} is unrecognized for the APNS channel`);
}
} else {
throw new Error('DefaultAuthenticationMethod is missing for the APNS channel');
}

return channelInput;
}

function disable(context) {
const params = {
ApplicationId: context.exeInfo.serviceMeta.output.Id,
Expand All @@ -120,8 +151,28 @@ function disable(context) {
});
}

function pull(context, pinpointApp) {
const params = {
ApplicationId: pinpointApp.Id,
};
spinner.start(`Pulling ${channelName} Channel.`);
return new Promise((resolve, reject) => {
context.exeInfo.pinpointClient.getApnsChannel(params, (err, data) => {
if (err) {
spinner.fail(`get channel ${channelName} error`);
reject(err);
} else {
spinner.succeed(`get ${channelName} channel successful`);
pinpointApp[channelName] = data.APNSChannelResponse;
resolve(data.APNSChannelResponse);
}
});
});
}

module.exports = {
configure,
enable,
disable,
pull,
};

0 comments on commit a2964d4

Please sign in to comment.