Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migration of Admin Queries on override command #9031

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions packages/amplify-category-api/src/commands/api/override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import {
AmplifyCategories,
AmplifySupportedService,
generateOverrideSkeleton,
getMigrateResourceMessageForOverride,
pathManager,
stateManager,
} from 'amplify-cli-core';
import { printer, prompter } from 'amplify-prompts';
import * as path from 'path';
import { checkAppsyncApiResourceMigration } from '../../provider-utils/awscloudformation/utils/check-appsync-api-migration';
import { ApigwInputState } from '../../provider-utils/awscloudformation/apigw-input-state';
import { AdminQueriesProps, ApigwInputState } from '../../provider-utils/awscloudformation/apigw-input-state';
import { ApigwStackTransform } from '../../provider-utils/awscloudformation/cdk-stack-builder';

export const name = 'override';
Expand Down Expand Up @@ -71,12 +70,19 @@ export const run = async (context: $TSContext) => {
// Migration logic goes in here
const apigwInputState = new ApigwInputState(context, selectedResourceName);
if (!apigwInputState.cliInputsFileExists()) {
if (await prompter.yesOrNo(getMigrateResourceMessageForOverride(AmplifyCategories.API, selectedResourceName, false), true)) {
if (selectedResourceName === 'AdminQueries') {
jhockett marked this conversation as resolved.
Show resolved Hide resolved
const { dependsOn } = amplifyMeta[AmplifyCategories.API][selectedResourceName];
const props: AdminQueriesProps = {
apiName: selectedResourceName,
authResourceName: dependsOn.filter(entry => entry.category === AmplifyCategories.AUTH)[0].resourceName,
jhockett marked this conversation as resolved.
Show resolved Hide resolved
functionName: dependsOn.filter(entry => entry.category === AmplifyCategories.FUNCTION)[0].resourceName,
dependsOn: dependsOn,
};
await apigwInputState.migrateAdminQueries(props);
} else {
await apigwInputState.migrateApigwResource(selectedResourceName);
const stackGenerator = new ApigwStackTransform(context, selectedResourceName);
stackGenerator.transform();
} else {
return;
}
}
await generateOverrideSkeleton(context, srcPath, destPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class ApigwInputState {
}
}

type AdminQueriesProps = {
export type AdminQueriesProps = {
apiName: string;
functionName: string;
authResourceName: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,9 @@ export class AmplifyApigwResourceStack extends cdk.Stack implements AmplifyApigw

private _setDeploymentResource = (resourceName: string) => {
this.deploymentResource = new apigw.CfnDeployment(this, `DeploymentAPIGW${resourceName}`, {
restApiId: cdk.Fn.ref(resourceName),
description: 'The Development stage deployment of your API.',
stageName: cdk.Fn.conditionIf('ShouldNotCreateEnvResources', 'Prod', cdk.Fn.ref('env')).toString(),
restApiId: cdk.Fn.ref(resourceName),
});
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import fs from 'fs-extra';
import { $TSContext, $TSObject, pathManager } from 'amplify-cli-core';
import { printer } from 'amplify-prompts';
import extract from 'extract-zip';
import * as fs from 'fs-extra';
import sequential from 'promise-sequential';
import { $TSContext, pathManager } from 'amplify-cli-core';
import { APIGateway } from './aws-utils/aws-apigw';

export async function downloadAPIModels(context: $TSContext, allResources) {
export async function downloadAPIModels(context: $TSContext, allResources: $TSObject[]) {
const { amplify } = context;
const projectConfig = amplify.getProjectConfig();

Expand All @@ -18,21 +19,21 @@ export async function downloadAPIModels(context: $TSContext, allResources) {
const promises = [];

if (resources.length > 0) {
context.print.info('\nCreating API models...');
printer.info('\nCreating API models...');
}

for (let i = 0; i < resources.length; i += 1) {
if (resources[i].output.ApiName) {
promises.push(() => extractAPIModel(context, resources[i], framework));
for (const resource of resources) {
if (resource.output.ApiName) {
promises.push(() => extractAPIModel(context, resource, framework));
}
}

return sequential(promises);
}

async function extractAPIModel(context: $TSContext, resource, framework) {
async function extractAPIModel(context: $TSContext, resource: $TSObject, framework: string) {
const apigw = await APIGateway.getInstance(context);
const apigwParams = getAPIGWRequestParams(context, resource, framework);
const apigwParams = getAPIGWRequestParams(resource, framework);

const apiName = resource.output.ApiName;

Expand All @@ -53,8 +54,8 @@ async function extractAPIModel(context: $TSContext, resource, framework) {
fs.removeSync(tempDir);
}

function copyFilesToSrc(context, apiName, framework) {
const backendDir = context.amplify.pathManager.getBackendDirPath();
function copyFilesToSrc(context: $TSContext, apiName: string, framework: string) {
const backendDir = pathManager.getBackendDirPath();
const tempDir = `${backendDir}/.temp`;

switch (framework) {
Expand Down Expand Up @@ -85,7 +86,7 @@ function copyFilesToSrc(context, apiName, framework) {
}
}

function getAPIGWRequestParams(_: $TSContext, resource, framework) {
function getAPIGWRequestParams(resource: $TSObject, framework: string) {
const apiUrl = resource.output.RootUrl;
const apiName = resource.output.ApiName;
const firstSplit = apiUrl.split('/');
Expand Down
25 changes: 13 additions & 12 deletions packages/amplify-provider-awscloudformation/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
"resources/overrides-resource/override.ts"
],
"references": [
{"path": "../amplify-cli-core"},
{"path": "../amplify-util-import"},
{"path": "../graphql-auth-transformer"},
{"path": "../graphql-connection-transformer"},
{"path": "../graphql-dynamodb-transformer"},
{"path": "../graphql-elasticsearch-transformer"},
{"path": "../graphql-function-transformer"},
{"path": "../graphql-http-transformer"},
{"path": "../graphql-key-transformer"},
{"path": "../graphql-predictions-transformer"},
{"path": "../graphql-transformer-core"},
{"path": "../graphql-versioned-transformer"},
{ "path": "../amplify-cli-core" },
{ "path": "../amplify-prompts" },
{ "path": "../amplify-util-import" },
{ "path": "../graphql-auth-transformer" },
{ "path": "../graphql-connection-transformer" },
{ "path": "../graphql-dynamodb-transformer" },
{ "path": "../graphql-elasticsearch-transformer" },
{ "path": "../graphql-function-transformer" },
{ "path": "../graphql-http-transformer" },
{ "path": "../graphql-key-transformer" },
{ "path": "../graphql-predictions-transformer" },
{ "path": "../graphql-transformer-core" },
{ "path": "../graphql-versioned-transformer" },
{ "path": "../amplify-graphql-transformer-core" },
{ "path": "../amplify-graphql-transformer-interfaces" },
]
Expand Down