Skip to content

Commit

Permalink
feat: storage import
Browse files Browse the repository at this point in the history
  • Loading branch information
attilah committed Nov 17, 2020
1 parent f3df233 commit e31160e
Show file tree
Hide file tree
Showing 41 changed files with 3,001 additions and 218 deletions.
254 changes: 156 additions & 98 deletions .circleci/config.yml

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions packages/amplify-category-auth/src/commands/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ module.exports = {
const commands = [
{
name: 'add',
description: `Takes you through a CLI flow to add a ${featureName} resource to your local backend`,
description: `Takes you through a CLI flow to add an ${featureName} resource to your local backend`,
},
{
name: 'import',
description: `Takes you through a CLI flow to import an existing ${featureName} resource to your local backend`,
},
{
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`,
description: `Removes the ${featureName} resource from your local backend which would be removed from the cloud on the next push command`,
},
{
name: 'update',
Expand Down
16 changes: 16 additions & 0 deletions packages/amplify-category-auth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,21 @@ async function prePushAuthHook(context) {
await transformUserPoolGroupSchema(context);
}

async function importAuth(context) {
const { amplify } = context;
const servicesMetadata = require('./provider-utils/supported-services').supportedServices;
const existingAuth = amplify.getProjectDetails().amplifyMeta.auth || {};

if (Object.keys(existingAuth).length > 0) {
return context.print.warning('Auth has already been added to this project.');
}

const serviceSelection = await context.amplify.serviceSelectionPrompt(context, category, servicesMetadata);
const providerController = require(`./provider-utils/${serviceSelection.providerName}`);

return providerController.importResource(context, serviceSelection, undefined, undefined, false);
}

module.exports = {
externalAuthEnable,
checkRequirements,
Expand All @@ -423,4 +438,5 @@ module.exports = {
prePushAuthHook,
uploadFiles,
category,
importAuth,
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const importResource = async (
serviceSelection: ServiceSelection,
previousResourceParameters: ResourceParameters | undefined,
providerPluginInstance?: ProviderUtils,
printSuccessMessage: boolean = true,
): Promise<{ envSpecificParameters: EnvSpecificResourceParameters } | undefined> => {
// Load provider
const providerPlugin = providerPluginInstance || require(serviceSelection.provider);
Expand All @@ -57,7 +58,9 @@ export const importResource = async (

const { envSpecificParameters } = await updateStateFiles(context, questionParameters, answers, projectType, persistEnvParameters);

printSuccess(context, answers.authSelections!, answers.userPool!, answers.identityPool);
if (printSuccessMessage) {
printSuccess(context, answers.authSelections!, answers.userPool!, answers.identityPool);
}

return {
envSpecificParameters,
Expand All @@ -74,7 +77,7 @@ const printSuccess = (context: $TSContext, authSelections: AuthSelections, userP
context.print.info('');
context.print.info('Next steps:');
context.print.info('');
context.print.info("- This resource will be available for GraphQL APIs ('amplify add api')");
context.print.info(`- This resource will be available for GraphQL APIs ('amplify add api')`);
context.print.info('- Use Amplify libraries to add signup, signin, signout capabilities to your client');
context.print.info(' application.');
context.print.info(' - iOS: https://docs.amplify.aws/lib/auth/getting-started/q/platform/ios');
Expand Down Expand Up @@ -937,7 +940,8 @@ export const importedAuthEnvInit = async (
type: 'confirm',
message: importMessages.Questions.ImportPreviousResource(resourceName, sourceEnvParams.userPoolId, context.exeInfo.sourceEnvName),
footer: importMessages.ImportPreviousResourceFooter,
//default: 'Y',
initial: true,
format: (e: any) => (e ? 'Yes' : 'No'),
} as any);

if (!importExisting) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ export type UserPoolChoice = {
value: string;
};

// type Choices = [{ name?: string; value?: string; display?: string }];

export type ImportParameters = {
providerName: string;
userPoolList: UserPoolChoice[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ async function updateConfigOnEnvInit(context, category, service) {
},
resourceParams,
providerPlugin,
false,
);

if (importResult) {
Expand Down
23 changes: 8 additions & 15 deletions packages/amplify-category-storage/amplify-plugin.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
{
"name": "storage",
"type": "category",
"commands": [
"add",
"console",
"push",
"remove",
"update",
"help"
],
"commandAliases":{
"configure": "update"
},
"eventHandlers": []
}
"name": "storage",
"type": "category",
"commands": ["add", "import", "console", "push", "remove", "update", "help"],
"commandAliases": {
"configure": "update"
},
"eventHandlers": []
}
2 changes: 2 additions & 0 deletions packages/amplify-category-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
],
"dependencies": {
"amplify-cli-core": "1.6.0",
"amplify-util-import": "1.1.3",
"enquirer": "^2.3.6",
"fs-extra": "^8.1.0",
"inquirer": "^7.3.3",
"lodash": "^4.17.19",
"promise-sequential": "^1.1.1",
"uuid": "^3.4.0"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions packages/amplify-category-storage/src/commands/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ module.exports = {
name: 'add',
description: `Takes you through steps in the CLI to add a ${categoryName} resource to your local backend`,
},
{
name: 'import',
description: `Takes you through a CLI flow to import an existing ${categoryName} resource to your local backend`,
},
{
name: 'update',
description: `Takes you through steps in the CLI to update an ${categoryName} resource`,
Expand Down
17 changes: 17 additions & 0 deletions packages/amplify-category-storage/src/commands/storage/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { $TSContext } from 'amplify-cli-core';

const category = 'storage';

export const run = async (context: $TSContext) => {
const servicesMetadata = require('../../provider-utils/supported-services').supportedServices;

const serviceSelection = await context.amplify.serviceSelectionPrompt(context, category, servicesMetadata);
const providerController = require(`../../provider-utils/${serviceSelection.providerName}`);

if (!providerController) {
context.print.error('Provider not configured for this category');
return;
}

return providerController.importResource(context, category, serviceSelection);
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ module.exports = {

return providerController.updateResource(context, category, result.service);
})
.then(() => context.print.success('Successfully updated resource'))
.then(result => {
if (result) {
context.print.success('Successfully updated resource');
}
})
.catch(err => {
context.print.info(err.stack);
context.print.error('An error occurred when updating the storage resource');
Expand Down
45 changes: 45 additions & 0 deletions packages/amplify-category-storage/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const path = require('path');
const sequential = require('promise-sequential');
const { updateConfigOnEnvInit } = require('./provider-utils/awscloudformation');

const category = 'storage';

Expand Down Expand Up @@ -109,9 +111,52 @@ async function handleAmplifyEvent(context, args) {
context.print.info(`Received event args ${args}`);
}

async function initEnv(context) {
const { resourcesToBeSynced, allResources } = await context.amplify.getResourceStatus(category);
const isPulling = context.input.command === 'pull' || (context.input.command === 'env' && context.input.subCommands[0] === 'pull');
let toBeSynced = [];

if (resourcesToBeSynced && resourcesToBeSynced.length > 0) {
toBeSynced = resourcesToBeSynced.filter(b => b.category === category);
}

toBeSynced
.filter(storageResource => storageResource.sync === 'unlink')
.forEach(storageResource => {
context.amplify.removeResourceParameters(context, category, storageResource.resourceName);
});

let tasks = [];

// For pull change detection for import sees a difference, to avoid duplicate tasks we don't
// add the syncable resources, as allResources covers it, otherwise it is required for env add
// to populate the output value and such, these sync resources have the 'refresh' sync value.
if (!isPulling) {
tasks = tasks.concat(toBeSynced);
}

// check if this initialization is happening on a pull
if (isPulling && allResources.length > 0) {
tasks.push(...allResources);
}

const storageTasks = tasks.map(storageResource => {
const { resourceName, service } = storageResource;

return async () => {
const config = await updateConfigOnEnvInit(context, category, resourceName, service);

context.amplify.saveEnvResourceParameters(context, category, resourceName, config);
};
});

await sequential(storageTasks);
}

module.exports = {
add,
console,
initEnv,
migrate,
getPermissionPolicies,
executeAmplifyCommand,
Expand Down
Loading

0 comments on commit e31160e

Please sign in to comment.