Skip to content

Commit

Permalink
Merge pull request #1 from tommypraeger/master
Browse files Browse the repository at this point in the history
interactions category
  • Loading branch information
UnleashedMind authored Sep 14, 2018
2 parents b17e17a + f1fc72a commit fd33758
Show file tree
Hide file tree
Showing 24 changed files with 2,114 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ To test your category, do the following:<br>

Before pushing code or sending a pull request, do the following:
- At the command line, run `npm run lint` at the top-level directory. This invokes lerna to check for lint errors in all of our packages.
- You can use `eslint` to fix some of the lint errors. To use it, go to the package that has errors and run `lint-fx`
- You can use `eslint` to fix some of the lint errors. To use it, go to the package that has errors and run `lint-fix`
- If there are any remaining lint errors, resolve them manually. Linting your code is a best practice that ensures good code quality so it's important that you don't skip this step.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"author": "Amazon Web Services",
"license": "Apache-2.0",
"dependencies": {
"fuzzy": "^0.1.3",
"inquirer-autocomplete-prompt": "^1.0.1",
"lerna": "^2.11.0"
}
}
14 changes: 14 additions & 0 deletions packages/amplify-category-interactions/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "airbnb-base",
"rules": {
"no-param-reassign": "off",
"no-return-await" : "off",
"no-continue": "off",
"no-await-in-loop": "off",
"no-use-before-define": "off",
"no-console": "off",
"global-require": "off",
"import/no-dynamic-require": "off",
"consistent-return": "off"
}
}
12 changes: 12 additions & 0 deletions packages/amplify-category-interactions/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Amplify CLI Analytics Plugin

## Commands Summary

The current set of commands supported by the Amplify Interactions Category Plugin

| Command | Description |
| --- | --- |
| amplify interactions add | Takes you through a CLI flow to add an interactions resource to your backend |
| amplify interactions update | Takes you through steps in the CLI to update an interactions resource. |
| amplify interactions push | Provisions only interactions cloud resources with the latest local developments |
| amplify interactions remove | Removes interactions resource from your local backend which would be removed from the cloud on the next push command |
31 changes: 31 additions & 0 deletions packages/amplify-category-interactions/commands/interactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const featureName = 'interactions';

module.exports = {
name: featureName,
run: async (context) => {
const header = `amplify ${featureName} <subcommand>`;

const commands = [
{
name: 'add',
description: `Takes you through a CLI flow to add an ${featureName} resource to your local backend`,
},
{
name: 'update',
description: `Takes you through a CLI flow to update an ${featureName} resource`,
},
{
name: 'push',
description: `Provisions only ${featureName} cloud resources with the latest local developments`,
},
{
name: 'remove',
description: `Removes ${featureName} resource from your local backend which would be removed from the cloud on the next push command`,
},
];

context.amplify.showHelp(header, commands);

context.print.info('');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fs = require('fs');

const subcommand = 'add';
const category = 'interactions';
const servicesMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../../provider-utils/supported-services.json`));

let options;

module.exports = {
name: subcommand,
run: async (context) => {
const { amplify } = context;

return amplify.serviceSelectionPrompt(context, category, servicesMetadata)
.then((result) => {
options = {
service: result.service,
providerPlugin: result.providerName,
build: true,
};
const providerController = require(`../../provider-utils/${result.providerName}/index`);
if (!providerController) {
context.print.error('Provider not configured for this category');
return;
}
return providerController.addResource(context, category, result.service);
})
.then(resourceName => amplify.updateamplifyMetaAfterResourceAdd(
category,
resourceName,
options,
))
.then(() => context.print.success('Successfully added resource'))
.catch((err) => {
context.print.info(err.stack);
context.print.error('There was an error adding the interactions resource');
});
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const subcommand = 'push';
const category = 'interactions';

module.exports = {
name: subcommand,
run: async (context) => {
const { amplify, parameters } = context;
const resourceName = parameters.first;

return amplify.pushResources(context, category, resourceName)
.catch((err) => {
context.print.info(err.stack);
context.print.error('There was an error pushing the interactions resource');
});
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const subcommand = 'remove';
const category = 'interactions';

module.exports = {
name: subcommand,
run: async (context) => {
const { amplify, parameters } = context;
const resourceName = parameters.first;

return amplify.removeResource(context, category, resourceName)
.then(() => context.print.success('Successfully removed resource'))
.catch((err) => {
context.print.info(err.stack);
context.print.error('There was an error removing the interactions resource');
});
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require('fs');

const subcommand = 'update';
const category = 'interactions';
const servicesMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../../provider-utils/supported-services.json`));


module.exports = {
name: subcommand,
alias: ['configure'],
run: async (context) => {
const { amplify } = context;

return amplify.serviceSelectionPrompt(context, category, servicesMetadata)
.then((result) => {
const providerController = require(`../../provider-utils/${result.providerName}/index`);
if (!providerController) {
context.print.error('Provider not configured for this category');
return;
}
return providerController.updateResource(context, category, result.service);
})
.then(() => context.print.success('Successfully updated resource'))
.catch((err) => {
context.print.info(err.stack);
context.print.error('There was an error updating the interactions resource');
});
},
};
20 changes: 20 additions & 0 deletions packages/amplify-category-interactions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "amplify-category-interactions",
"version": "0.1.0",
"description": "amplify-cli interactions plugin",
"dependencies": {
"eslint": "^4.19.1",
"fs-extra": "^7.0.0",
"inquirer": "^3.2.1",
"uuid": "^3.3.2"
},
"scripts": {
"lint": "eslint .",
"lint-fix": "eslint . --fix"
},
"license": "Apache-2.0",
"devDependencies": {
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.12.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Lex chatbot creation from Amplify CLI",
"Parameters": {
},
"Metadata": {
"AWS::CloudFormation::Interface": {
"ParameterGroups": [
{
"Label": {
"default": "Creating lex chatbot"
},
"Parameters": [
"inputs"
]
}
]
}
},
"Resources": {
"LambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.handler",
"FunctionName": "<%= props.functionName %>",
"Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] },
"Runtime": "nodejs6.10",
"Timeout": "300"
}
},

"LambdaExecutionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"RoleName": "<%= props.roleName %>",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
},
"Policies": [
{
"PolicyName": "<%= props.cloudWatchPolicyName %>",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
},
{
"PolicyName": "<%= props.lexPolicyName %>",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lex:*"
],
"Resource": "*"
}
]
}
}
]
}
},
"LambdaFunctionOutputs<%= props.shortId %>": {
"Type": "Custom::LambdaCallout",
"Properties": {
"ServiceToken": {
"Fn::GetAtt": [
"LambdaFunction",
"Arn"
]
}
}
}
},
"Outputs": {
"Region": {
"Value": {
"Ref": "AWS::Region"
}
},
"FunctionArn": {
"Value": {
"Fn::GetAtt": [
"LambdaFunction",
"Arn"
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const uuid = require('uuid');

const getAllDefaults = (project) => {
const name = project.projectConfig.projectName.toLowerCase().replace('-', '_');
const [shortId] = uuid().split('-');
const region = project.amplifyMeta.providers.awscloudformation.Region;
const defaults = {
resourceName: `lex${shortId}`,
botName: `${name}_bot`,
sessionTimeout: 5,
lexPolicyName: `lexPolicy${shortId}`,
authPolicyName: `lex_amplify_${shortId}`,
unauthPolicyName: `lex_amplify_${shortId}`,
roleName: `lexLambdaRole${shortId}`,
functionName: `${name}_cfnlambda_${shortId}`,
cloudWatchPolicyName: `cloudWatchPolicy${shortId}`,
region,
};
return defaults;
};

module.exports = {
getAllDefaults,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Copyright 2015 Amazon Web Services, Inc. or its affiliates. All Rights Reserved.
This file is licensed to you under the AWS Customer Agreement (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at http://aws.amazon.com/agreement/ .
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied.
See the License for the specific language governing permissions and limitations under the License. */

exports.SUCCESS = 'SUCCESS';
exports.FAILED = 'FAILED';

exports.send = function (event, context, responseStatus, responseData, physicalResourceId, noEcho) {
const responseBody = JSON.stringify({
Status: responseStatus,
Reason: `See the details in CloudWatch Log Stream: ${context.logStreamName}`,
PhysicalResourceId: physicalResourceId || context.logStreamName,
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
NoEcho: noEcho || false,
Data: responseData,
});

console.log('Response body:\n', responseBody);

const https = require('https');
const url = require('url');

const parsedUrl = url.parse(event.ResponseURL);
const options = {
hostname: parsedUrl.hostname,
port: 443,
path: parsedUrl.path,
method: 'PUT',
headers: {
'content-type': '',
'content-length': responseBody.length,
},
};

const request = https.request(options, (response) => {
console.log(`Status code: ${response.statusCode}`);
console.log(`Status message: ${response.statusMessage}`);
context.done();
});

request.on('error', (error) => {
console.log(`send(..) failed executing https.request(..): ${error}`);
context.done();
});

request.write(responseBody);
request.end();
};
Loading

0 comments on commit fd33758

Please sign in to comment.