Skip to content

Commit

Permalink
Revert "fix: update migration msg, ddb import e2e, lgtm errors (aws-a…
Browse files Browse the repository at this point in the history
…mplify#8796)" (aws-amplify#8799)

This reverts commit 749366a.
  • Loading branch information
kaustavghosh06 authored Nov 11, 2021
1 parent 6830333 commit 4e0b774
Show file tree
Hide file tree
Showing 19 changed files with 183 additions and 198 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { $TSContext, stateManager } from 'amplify-cli-core';
import { $TSContext, AmplifyCategories, JSONUtilities, stateManager } from 'amplify-cli-core';
import * as path from 'path';
import { getSupportedServices } from '../../supported-services';
import { AuthInputState } from '../auth-inputs-manager/auth-input-state';
import { ProviderUtils } from '../import/types';
import { CognitoConfiguration } from '../service-walkthrough-types/awsCognito-user-input-types';
import { ServiceQuestionHeadlessResult } from '../service-walkthrough-types/cognito-user-input-types';
import { existsSync } from 'fs-extra';

export type UserPoolMessageConfiguration = {
mfaConfiguration?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $TSContext, AmplifyCategories, getMigrateResourceMessageForOverride } from 'amplify-cli-core';
import { $TSContext } from 'amplify-cli-core';
import { printer, prompter } from 'amplify-prompts';
import { AuthInputState } from '../auth-inputs-manager/auth-input-state';
import { generateAuthStackTemplate } from './generate-auth-stack-template';
Expand All @@ -10,11 +10,10 @@ export const checkAuthResourceMigration = async (context: $TSContext, authName:
if (!imported) {
const cliState = new AuthInputState(authName);
if (!cliState.cliInputFileExists()) {
printer.debug("cli-inputs.json doesn't exist");
printer.debug('Cli-inputs.json doesnt exist');
// put spinner here
const headlessMigrate = context.input.options?.yes || context.input.options?.forcePush || context.input.options?.headless;

if (headlessMigrate || (await prompter.yesOrNo(getMigrateResourceMessageForOverride(AmplifyCategories.AUTH, authName), true))) {
if (headlessMigrate || (await prompter.yesOrNo(`Do you want to migrate this ${authName} to support overrides?`, true))) {
// generate cli-inputs for migration from parameters.json
await migrateResourceToSupportOverride(authName);
// fetch cli Inputs again
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import {
AmplifyCategories,
AmplifySupportedService,
exitOnNextTick,
JSONUtilities,
pathManager,
ResourceAlreadyExistsError,
ResourceDoesNotExistError,
stateManager,
} from 'amplify-cli-core';
import { JSONUtilities, stateManager, pathManager } from 'amplify-cli-core';
import {
addTextractPolicies,
generateLambdaAccessForRekognition,
generateStorageAccessForRekognition,
removeTextractPolicies,
removeTextractPolicies
} from '../assets/identifyCFNGenerate';
import identifyAssets from '../assets/identifyQuestions';
import regionMapper from '../assets/regionMapping';
Expand All @@ -24,23 +15,31 @@ import {
invokeS3GetResourceName,
invokeS3GetUserInputs,
invokeS3RegisterAdminTrigger,
invokeS3RemoveAdminLambdaTrigger,
invokeS3RemoveAdminLambdaTrigger
} from './storage-api';
const { ResourceDoesNotExistError, ResourceAlreadyExistsError, exitOnNextTick } = require('amplify-cli-core');
const inquirer = require('inquirer');
const path = require('path');
const fs = require('fs-extra');
const os = require('os');
const uuid = require('uuid');

// keep in sync with ServiceName in amplify-category-function, but probably it will not change
const FunctionServiceNameLambdaFunction = 'Lambda';

// Predictions Info
const templateFilename = 'identify-template.json.ejs';
const identifyTypes = ['identifyText', 'identifyEntities', 'identifyLabels'];
let service = 'Rekognition';
const category = AmplifyCategories.PREDICTIONS;
const storageCategory = AmplifyCategories.STORAGE;
const functionCategory = AmplifyCategories.FUNCTION;
const category = 'predictions';
const storageCategory = 'storage';
const functionCategory = 'function';
const parametersFileName = 'parameters.json';
const amplifyMetaFilename = 'amplify-meta.json';
const s3defaultValuesFilename = 's3-defaults.js';
const s3TemplateFileName = 's3-cloudformation-template.json.ejs';
const s3CloudFormationTemplateFile = 's3-cloudformation-template.json';
const s3ServiceName = 'S3';
const prefixForAdminTrigger = 'protected/predictions/index-faces/';
// TODO support appsync

Expand Down Expand Up @@ -121,7 +120,7 @@ async function createAndRegisterAdminLambdaS3Trigger(context, predictionsResourc
async function configure(context, predictionsResourceObj, configMode /*add/update*/) {
const { amplify } = context;
const defaultValues = getAllDefaults(amplify.getProjectDetails());
const projectBackendDirPath = pathManager.getBackendDirPath();
const projectBackendDirPath = context.amplify.pathManager.getBackendDirPath();
let identifyType;

let parameters = {};
Expand Down Expand Up @@ -214,7 +213,7 @@ async function configure(context, predictionsResourceObj, configMode /*add/updat
const functionjsonString = JSON.stringify(functionParameters, null, 4);
fs.writeFileSync(functionparametersFilePath, functionjsonString, 'utf8');
} else if (parameters.resourceName) {
const s3ResourceName = s3ResourceAlreadyExists();
const s3ResourceName = s3ResourceAlreadyExists(context);
if (s3ResourceName) {
let s3UserInputs = await invokeS3GetUserInputs(context, s3ResourceName);
if (
Expand Down Expand Up @@ -254,7 +253,7 @@ async function configure(context, predictionsResourceObj, configMode /*add/updat
});

if (answers.folderPolicies === 'app' && parameters.resourceName && configMode != PREDICTIONS_WALKTHROUGH_MODE.ADD) {
addStorageIAMResourcestoIdentifyCFNFile(parameters.resourceName, s3Resource.resourceName);
addStorageIAMResourcestoIdentifyCFNFile(context, parameters.resourceName, s3Resource.resourceName);
}
}

Expand Down Expand Up @@ -484,14 +483,15 @@ async function addS3ForIdentity(context, storageAccess, bucketName, predictionsR
};
}

function s3ResourceAlreadyExists() {
const amplifyMeta = stateManager.getMeta();
function s3ResourceAlreadyExists(context) {
const { amplify } = context;
const { amplifyMeta } = amplify.getProjectDetails();
let resourceName;

if (amplifyMeta[storageCategory]) {
const categoryResources = amplifyMeta[storageCategory];
Object.keys(categoryResources).forEach(resource => {
if (categoryResources[resource].service === AmplifySupportedService.S3) {
if (categoryResources[resource].service === s3ServiceName) {
resourceName = resource;
}
});
Expand All @@ -501,7 +501,7 @@ function s3ResourceAlreadyExists() {
}

async function postCFNGenUpdateLambdaResourceInPredictions(context, predictionsResourceName, functionName, s3ResourceName) {
const projectBackendDirPath = pathManager.getBackendDirPath();
const projectBackendDirPath = context.amplify.pathManager.getBackendDirPath();
const identifyCFNFilePath = path.join(
projectBackendDirPath,
category,
Expand Down Expand Up @@ -573,7 +573,7 @@ async function createNewFunction(context, predictionsResourceName, s3ResourceNam
// Update amplify-meta and backend-config

const backendConfigs = {
service: AmplifySupportedService.LAMBDA,
service: FunctionServiceNameLambdaFunction,
providerPlugin: 'awscloudformation',
build: true,
};
Expand All @@ -584,8 +584,8 @@ async function createNewFunction(context, predictionsResourceName, s3ResourceNam
return functionName;
}

function addStorageIAMResourcestoIdentifyCFNFile(predictionsResourceName, s3ResourceName) {
const projectBackendDirPath = pathManager.getBackendDirPath();
function addStorageIAMResourcestoIdentifyCFNFile(context, predictionsResourceName, s3ResourceName) {
const projectBackendDirPath = context.amplify.pathManager.getBackendDirPath();
const identifyCFNFilePath = path.join(
projectBackendDirPath,
category,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import {
S3AccessType,
S3PermissionType,
S3TriggerFunctionType,
S3UserInputs,
S3UserInputs
} from '../../../../provider-utils/awscloudformation/service-walkthrough-types/s3-user-input-types';
import * as s3AuthAPI from '../../../../provider-utils/awscloudformation/service-walkthroughs/s3-auth-api';
import {
S3CLITriggerUpdateMenuOptions,
UserPermissionTypeOptions,
} from '../../../../provider-utils/awscloudformation/service-walkthroughs/s3-questions';
import { S3CLITriggerUpdateMenuOptions, UserPermissionTypeOptions } from '../../../../provider-utils/awscloudformation/service-walkthroughs/s3-questions';
import { MigrationParams, S3InputState } from '../../../../provider-utils/awscloudformation/service-walkthroughs/s3-user-input-state';
import { addWalkthrough, updateWalkthrough } from '../../../../provider-utils/awscloudformation/service-walkthroughs/s3-walkthrough';

Expand Down Expand Up @@ -760,7 +757,7 @@ describe('migrate s3 and update s3 permission walkthrough tests', () => {

prompter.confirmContinue = jest
.fn()
.mockReturnValueOnce(true) // Do you want to migrate...?
.mockReturnValueOnce(true) // File migration required to continue. Do you want to continue ?
.mockReturnValueOnce(false); // Do you want to add a Lambda Trigger ?

//Update CLI walkthrough (update auth permission)
Expand Down
55 changes: 24 additions & 31 deletions packages/amplify-category-storage/src/commands/storage/override.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
/*
entry code for amplify override root
entry code for amplify override root
*/

import {
$TSContext,
AmplifyCategories,
AmplifySupportedService,
CLISubCommandType,
generateOverrideSkeleton,
getMigrateResourceMessageForOverride,
pathManager,
stateManager,
} from 'amplify-cli-core';
import { generateOverrideSkeleton, $TSContext, stateManager, pathManager, CLISubCommandType } from 'amplify-cli-core';
import { printer, prompter } from 'amplify-prompts';
import * as path from 'path';
import { DDBStackTransform } from '../../provider-utils/awscloudformation/cdk-stack-builder/ddb-stack-transform';
import { AmplifyS3ResourceStackTransform } from '../../provider-utils/awscloudformation/cdk-stack-builder/s3-stack-transform';
import { DynamoDBInputState } from '../../provider-utils/awscloudformation/service-walkthroughs/dynamoDB-input-state';
import { DDBStackTransform } from '../../provider-utils/awscloudformation/cdk-stack-builder/ddb-stack-transform';
import * as path from 'path';
import { categoryName } from '../../constants';
import { AmplifySupportedService } from 'amplify-cli-core';
import { S3InputState } from '../../provider-utils/awscloudformation/service-walkthroughs/s3-user-input-state';
import { AmplifyS3ResourceStackTransform } from '../../provider-utils/awscloudformation/cdk-stack-builder/s3-stack-transform';

export const name = 'override';

export const run = async (context: $TSContext) => {
const amplifyMeta = stateManager.getMeta();
const storageResources: string[] = [];

if (amplifyMeta[AmplifyCategories.STORAGE]) {
Object.keys(amplifyMeta[AmplifyCategories.STORAGE]).forEach(resourceName => {
if (amplifyMeta[categoryName]) {
Object.keys(amplifyMeta[categoryName]).forEach(resourceName => {
storageResources.push(resourceName);
});
}
Expand All @@ -43,7 +36,7 @@ export const run = async (context: $TSContext) => {
selectedResourceName = await prompter.pick('Which resource would you like to add overrides for?', storageResources);
}

const destPath = pathManager.getResourceDirectoryPath(undefined, AmplifyCategories.STORAGE, selectedResourceName);
const destPath = pathManager.getResourceDirectoryPath(undefined, categoryName, selectedResourceName);

const srcPath = path.join(
__dirname,
Expand All @@ -52,34 +45,34 @@ export const run = async (context: $TSContext) => {
'..',
'resources',
'overrides-resource',
amplifyMeta[AmplifyCategories.STORAGE][selectedResourceName].service,
amplifyMeta[categoryName][selectedResourceName].service,
);

// Make sure to migrate first
if (amplifyMeta[AmplifyCategories.STORAGE][selectedResourceName].service === AmplifySupportedService.DYNAMODB) {
if (amplifyMeta[categoryName][selectedResourceName].service === AmplifySupportedService.DYNAMODB ) {
const resourceInputState = new DynamoDBInputState(selectedResourceName);
if (!resourceInputState.cliInputFileExists()) {
if (await prompter.yesOrNo(getMigrateResourceMessageForOverride(AmplifyCategories.STORAGE, selectedResourceName), true)) {
if (await prompter.yesOrNo('File migration required to continue. Do you want to continue?', true)) {
resourceInputState.migrate();
const stackGenerator = new DDBStackTransform(selectedResourceName);
await stackGenerator.transform();
} else {
return;
}
}
} else if (amplifyMeta[AmplifyCategories.STORAGE][selectedResourceName].service === AmplifySupportedService.S3) {
// S3 migration logic goes in here
const s3ResourceInputState = new S3InputState(selectedResourceName, undefined);
if (!s3ResourceInputState.cliInputFileExists()) {
if (await prompter.yesOrNo(getMigrateResourceMessageForOverride(AmplifyCategories.STORAGE, selectedResourceName), true)) {
await s3ResourceInputState.migrate(context); //migrate auth and storage config resources
const stackGenerator = new AmplifyS3ResourceStackTransform(selectedResourceName, context);
stackGenerator.transform(CLISubCommandType.MIGRATE);
} else {
return;
} else if (amplifyMeta[categoryName][selectedResourceName].service === AmplifySupportedService.S3 ) {
// S3 migration logic goes in here
const s3ResourceInputState = new S3InputState(selectedResourceName, undefined);
if (!s3ResourceInputState.cliInputFileExists()) {
if (await prompter.yesOrNo('File migration required to continue. Do you want to continue?', true)) {
await s3ResourceInputState.migrate(context); //migrate auth and storage config resources
const stackGenerator = new AmplifyS3ResourceStackTransform(selectedResourceName, context);
stackGenerator.transform( CLISubCommandType.MIGRATE );
} else {
return;
}
}
}
}

await generateOverrideSkeleton(context, srcPath, destPath);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $TSAny, $TSContext, $TSObject, AmplifyCategories, ServiceSelection, stateManager } from 'amplify-cli-core';
import { $TSAny, $TSContext, $TSObject, ServiceSelection, stateManager } from 'amplify-cli-core';
import { printer } from 'amplify-prompts';
import { IDynamoDBService } from 'amplify-util-import';
import Enquirer from 'enquirer';
Expand Down Expand Up @@ -89,9 +89,7 @@ const importServiceWalkthrough = async (
let tableList = await dynamoDB.listTables();

// Remove already present tables from choices
const storageResources = <{ service: string; output: { Name: string } }[]>(
Object.values(_.get(amplifyMeta, [AmplifyCategories.STORAGE], []))
);
const storageResources = <{ service: string; output: { Name: string } }[]>Object.values(_.get(amplifyMeta, ['storage'], []));
const dynamoDBResources = storageResources.filter(r => r.service === 'DynamoDB' && !!r.output && !!r.output.Name).map(r => r.output.Name);

tableList = tableList.filter(t => !dynamoDBResources.includes(t));
Expand Down Expand Up @@ -180,26 +178,20 @@ const updateStateFiles = async (
serviceType: 'imported',
};

stateManager.setResourceParametersJson(undefined, AmplifyCategories.STORAGE, answers.resourceName!, resourceParameters);
stateManager.setResourceParametersJson(undefined, 'storage', answers.resourceName!, resourceParameters);

// Add resource data to amplify-meta file and backend-config, since backend-config requires less information
// we have to do a separate update to it without duplicating the methods
const metaConfiguration = _.clone(backendConfiguration) as DynamoDBMetaConfiguration;
metaConfiguration.output = createMetaOutput(answers, questionParameters);

context.amplify.updateamplifyMetaAfterResourceAdd(
AmplifyCategories.STORAGE,
answers.resourceName!,
metaConfiguration,
backendConfiguration,
true,
);
context.amplify.updateamplifyMetaAfterResourceAdd('storage', answers.resourceName!, metaConfiguration, backendConfiguration, true);

// Update team provider-info
const envSpecificParameters: DynamoDBEnvSpecificResourceParameters = createEnvSpecificResourceParameters(answers, questionParameters);

if (updateEnvSpecificParameters) {
context.amplify.saveEnvResourceParameters(context, AmplifyCategories.STORAGE, answers.resourceName!, envSpecificParameters);
context.amplify.saveEnvResourceParameters(context, 'storage', answers.resourceName!, envSpecificParameters);
}

return {
Expand Down Expand Up @@ -306,7 +298,7 @@ export const importedDynamoDBEnvInit = async (
});

if (currentMeta) {
const currentResource = _.get(currentMeta, [AmplifyCategories.STORAGE, resourceName], undefined);
const currentResource = _.get(currentMeta, ['storage', resourceName], undefined);

if (currentResource && currentResource.output) {
const { Name, Region, Arn, StreamArn, PartitionKeyName, PartitionKeyType, SortKeyName, SortKeyType } = currentResource.output;
Expand All @@ -325,7 +317,7 @@ export const importedDynamoDBEnvInit = async (
// Check to see if we have a source environment set (in case of env add), and ask customer if the want to import the same resource
// from the existing environment or import a different one. Check if all the values are having some value that can be validated and
// if not fall back to full service walkthrough.
const sourceEnvParams = getSourceEnvParameters(context.exeInfo.sourceEnvName, AmplifyCategories.STORAGE, resourceName);
const sourceEnvParams = getSourceEnvParameters(context.exeInfo.sourceEnvName, 'storage', resourceName);

if (sourceEnvParams) {
const { importExisting } = await Enquirer.prompt<{ importExisting: boolean }>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
AmplifyCategories,
AmplifySupportedService,
exitOnNextTick,
getMigrateResourceMessageForOverride,
pathManager,
ResourceDoesNotExistError,
stateManager,
Expand All @@ -24,6 +23,8 @@ import {
import { DynamoDBInputState } from './dynamoDB-input-state';

// keep in sync with ServiceName in amplify-AmplifyCategories.STORAGE-function, but probably it will not change
const FunctionServiceNameLambdaFunction = 'Lambda';
const serviceName = 'DynamoDB';

export async function addWalkthrough(context: $TSContext, defaultValuesFilename: string) {
printer.blankLine();
Expand Down Expand Up @@ -70,7 +71,7 @@ export async function updateWalkthrough(context: $TSContext) {

Object.keys(amplifyMeta[AmplifyCategories.STORAGE]).forEach(resourceName => {
if (
amplifyMeta[AmplifyCategories.STORAGE][resourceName].service === AmplifySupportedService.DYNAMODB &&
amplifyMeta[AmplifyCategories.STORAGE][resourceName].service === serviceName &&
amplifyMeta[AmplifyCategories.STORAGE][resourceName].mobileHubMigrated !== true &&
amplifyMeta[AmplifyCategories.STORAGE][resourceName].serviceType !== 'imported'
) {
Expand All @@ -95,7 +96,7 @@ export async function updateWalkthrough(context: $TSContext) {

const headlessMigrate = context.input.options?.yes || context.input.options?.forcePush || context.input.options?.headless;
if (!cliInputsState.cliInputFileExists()) {
if (headlessMigrate || (await prompter.yesOrNo(getMigrateResourceMessageForOverride(AmplifyCategories.STORAGE, resourceName), true))) {
if (headlessMigrate || (await prompter.yesOrNo('File migration required to continue. Do you want to continue?', true))) {
cliInputsState.migrate();
const stackGenerator = new DDBStackTransform(resourceName);
stackGenerator.transform();
Expand Down
Loading

0 comments on commit 4e0b774

Please sign in to comment.