Skip to content

Commit

Permalink
feat(amplify-provider-awscloudformation): match env directive field f…
Browse files Browse the repository at this point in the history
…or sandbox mode (#3)
  • Loading branch information
danielleadams committed Oct 4, 2021
1 parent 2619975 commit 6aa7e53
Show file tree
Hide file tree
Showing 22 changed files with 210 additions and 252 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { AddApiRequest, UpdateApiRequest } from 'amplify-headless-interface';

export interface ApiArtifactHandlerOptions {
skipCompile?: boolean;
}

export interface ApiArtifactHandler {
createArtifacts(request: AddApiRequest): Promise<string>;
updateArtifacts(request: UpdateApiRequest): Promise<void>;
updateArtifacts(request: UpdateApiRequest, opts?: ApiArtifactHandlerOptions): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isResourceNameUnique, FeatureFlags } from 'amplify-cli-core';
import { isResourceNameUnique } from 'amplify-cli-core';
import {
AddApiRequest,
AppSyncServiceConfiguration,
Expand All @@ -12,7 +12,7 @@ import _ from 'lodash';
import * as path from 'path';
import uuid from 'uuid';
import { category } from '../../category-constants';
import { ApiArtifactHandler } from '../api-artifact-handler';
import { ApiArtifactHandler, ApiArtifactHandlerOptions } from '../api-artifact-handler';
import { cfnParametersFilename, gqlSchemaFilename, provider, rootAssetDir } from './aws-constants';
import { authConfigHasApiKey, checkIfAuthExists, getAppSyncAuthConfig, getAppSyncResourceName } from './utils/amplify-meta-utils';
import { appSyncAuthTypeToAuthConfig } from './utils/auth-config-to-app-sync-auth-type-bi-di-mapper';
Expand Down Expand Up @@ -96,7 +96,7 @@ class CfnApiArtifactHandler implements ApiArtifactHandler {

// TODO once the AddApiRequest contains multiple services this class should depend on an ApiArtifactHandler
// for each service and delegate to the correct one
updateArtifacts = async (request: UpdateApiRequest): Promise<void> => {
updateArtifacts = async (request: UpdateApiRequest, opts?: ApiArtifactHandlerOptions): Promise<void> => {
const updates = request.serviceModification;
const apiName = getAppSyncResourceName(this.context.amplify.getProjectMeta());
if (!apiName) {
Expand All @@ -118,11 +118,14 @@ class CfnApiArtifactHandler implements ApiArtifactHandler {
if (updates.additionalAuthTypes) {
authConfig.additionalAuthenticationProviders = updates.additionalAuthTypes.map(appSyncAuthTypeToAuthConfig);
}
await this.context.amplify.executeProviderUtils(this.context, 'awscloudformation', 'compileSchema', {
resourceDir,
parameters: this.getCfnParameters(apiName, authConfig, resourceDir),
authConfig,
});

if (!opts?.skipCompile) {
await this.context.amplify.executeProviderUtils(this.context, 'awscloudformation', 'compileSchema', {
resourceDir,
parameters: this.getCfnParameters(apiName, authConfig, resourceDir),
authConfig,
});
}

this.context.amplify.updateamplifyMetaAfterResourceUpdate(category, apiName, 'output', { authConfig });
this.context.amplify.updateBackendConfigAfterResourceUpdate(category, apiName, 'output', { authConfig });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import { authConfigToAppSyncAuthType } from './utils/auth-config-to-app-sync-aut
import { getCfnApiArtifactHandler } from './cfn-api-artifact-handler';
import { prompter } from 'amplify-prompts';

export async function promptToAddApiKey(context: $TSContext): Promise<void> {
export async function promptToAddApiKey(context: $TSContext): Promise<any> {
if (await prompter.confirmContinue('Would you like to create an API Key?')) {
const apiKeyConfig = await askApiKeyQuestions();
const authConfig = [apiKeyConfig];

await getCfnApiArtifactHandler(context).updateArtifacts({
version: 1,
serviceModification: {
serviceName: 'AppSync',
additionalAuthTypes: authConfig.map(authConfigToAppSyncAuthType),
await getCfnApiArtifactHandler(context).updateArtifacts(
{
version: 1,
serviceModification: {
serviceName: 'AppSync',
additionalAuthTypes: authConfig.map(authConfigToAppSyncAuthType),
},
},
});
{
skipCompile: true,
},
);

return apiKeyConfig;
}
}
1 change: 0 additions & 1 deletion packages/amplify-cli-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ interface AmplifyToolkit {
) => $TSAny;
sharedQuestions: () => $TSAny;
showAllHelp: () => $TSAny;
showGlobalSandboxModeWarning: (context: $TSContext) => $TSAny;
showHelp: (header: string, commands: { name: string; description: string }[]) => $TSAny;
showHelpfulProviderLinks: (context: $TSContext) => $TSAny;
showResourceTable: () => $TSAny;
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion packages/amplify-cli/src/commands/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const run = async (context: $TSContext) => {
} else {
try {
await context.amplify.showStatusTable(view);
await context.amplify.showGlobalSandboxModeWarning(context);
await context.amplify.showHelpfulProviderLinks(context);
await showAmplifyConsoleHostingStatus(context);
} catch (e) {
Expand Down
7 changes: 0 additions & 7 deletions packages/amplify-cli/src/domain/amplify-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class AmplifyToolkit {
private _removeResource: any;
private _sharedQuestions: any;
private _showAllHelp: any;
private _showGlobalSandboxModeWarning: any;
private _showHelp: any;
private _showHelpfulProviderLinks: any;
private _showResourceTable: any;
Expand Down Expand Up @@ -241,12 +240,6 @@ export class AmplifyToolkit {
this._sharedQuestions = this._sharedQuestions || require(path.join(this._amplifyHelpersDirPath, 'shared-questions')).sharedQuestions;
return this._sharedQuestions;
}
get showGlobalSandboxModeWarning(): any {
this._showGlobalSandboxModeWarning =
this._showGlobalSandboxModeWarning ||
require(path.join(this._amplifyHelpersDirPath, 'show-global-sandbox-mode-warning')).showGlobalSandboxModeWarning;
return this._showGlobalSandboxModeWarning;
}
get showHelp(): any {
this._showHelp = this._showHelp || require(path.join(this._amplifyHelpersDirPath, 'show-help')).showHelp;
return this._showHelp;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { initializeEnv } from '../../initialize-env';
import { getProviderPlugins } from './get-provider-plugins';
import { getEnvInfo } from './get-env-info';
import { EnvironmentDoesNotExistError, exitOnNextTick, stateManager, $TSAny, $TSContext } from 'amplify-cli-core';
import { promptSandboxModeApiKey } from './prompt-sandbox-mode-api-key';

export async function pushResources(
context: $TSContext,
Expand Down Expand Up @@ -68,8 +67,6 @@ export async function pushResources(
continueToPush = await context.amplify.confirmPrompt('Are you sure you want to continue?');
}

await promptSandboxModeApiKey(context);

if (continueToPush) {
try {
// Get current-cloud-backend's amplify-meta
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const generateResolverKey = (typeName: string, fieldName: string): string
* @param ctx context to get sandbox mode
*/
export const generateAuthExpressionForSandboxMode = (ctx: any): string => {
let enabled = ctx.resourceHelper.api.globalSandboxModeEnabled;
const enabled = ctx.resourceHelper.api.sandboxModeEnabled;
let exp;

if (enabled) exp = iff(notEquals(methodCall(ref('util.authType')), str(API_KEY)), methodCall(ref('util.unauthorized')));
Expand Down
6 changes: 3 additions & 3 deletions packages/amplify-graphql-transformer-core/src/graphql-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class IamResource implements APIIAMResourceProvider {
export type TransformerAPIProps = GraphqlApiProps & {
readonly createApiKey?: boolean;
readonly host?: TransformHostProvider;
readonly globalSandboxModeEnv?: string;
readonly sandboxModeEnabled?: boolean;
};
export class GraphQLApi extends GraphqlApiBase implements GraphQLAPIProvider {
/**
Expand Down Expand Up @@ -165,7 +165,7 @@ export class GraphQLApi extends GraphqlApiBase implements GraphQLAPIProvider {
/**
* Global Sandbox Mode for GraphQL API
*/
public readonly globalSandboxModeEnabled?: boolean;
public readonly sandboxModeEnabled?: boolean;

private schemaResource: CfnGraphQLSchema;
private api: CfnGraphQLApi;
Expand Down Expand Up @@ -215,7 +215,7 @@ export class GraphQLApi extends GraphqlApiBase implements GraphQLAPIProvider {
this.apiKey = this.apiKeyResource.attrApiKey;
}

if (hasApiKey && !!props.globalSandboxModeEnv) this.globalSandboxModeEnabled = true;
if (hasApiKey && props.sandboxModeEnabled) this.sandboxModeEnabled = true;

if (props.host) {
this.host = props.host;
Expand Down
2 changes: 0 additions & 2 deletions packages/amplify-graphql-transformer-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export {
collectDirectives,
collectDirectivesByTypeNames,
DirectiveWrapper,
getSandboxModeEnvNameFromDirectiveSet,
getSandboxModeEnvNameFromNodeMap,
IAM_AUTH_ROLE_PARAMETER,
IAM_UNAUTH_ROLE_PARAMETER,
} from './utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import {
sortTransformerPlugins,
} from './utils';
import { validateModelSchema, validateAuthModes } from './validation';
import { getSandboxModeEnvNameFromNodeMap } from '../utils/sandbox-mode';

// eslint-disable-next-line @typescript-eslint/ban-types
function isFunction(obj: any): obj is Function {
Expand Down Expand Up @@ -72,6 +71,7 @@ export interface GraphQLTransformOptions {
readonly stacks?: Record<string, Template>;
readonly featureFlags?: FeatureFlagProvider;
readonly host?: TransformHostProvider;
readonly sandboxModeEnabled?: boolean;
}
export type StackMapping = { [resourceId: string]: string };
export class GraphQLTransform {
Expand Down Expand Up @@ -261,13 +261,12 @@ export class GraphQLTransform {
type: 'String',
}).valueAsString;
const envName = stackManager.getParameter('env');
const globalSandboxModeEnv = getSandboxModeEnvNameFromNodeMap(output.nodeMap);
assert(envName);
const api = new GraphQLApi(rootStack, 'GraphQLAPI', {
name: `${apiName}-${envName.valueAsString}`,
authorizationConfig,
host: this.options.host,
globalSandboxModeEnv,
sandboxModeEnabled: this.options.sandboxModeEnabled,
});
const authModes = [authorizationConfig.defaultAuthorization, ...(authorizationConfig.additionalAuthorizationModes || [])].map(
mode => mode?.authorizationType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export { DirectiveWrapper } from './directive-wrapper';
export { collectDirectives, collectDirectivesByTypeNames } from './type-map-utils';
export { stripDirectives } from './strip-directives';
export { DEFAULT_SCHEMA_DEFINITION } from './defaultSchema';
export { getSandboxModeEnvNameFromNodeMap, getSandboxModeEnvNameFromDirectiveSet } from './sandbox-mode';
export { IAM_AUTH_ROLE_PARAMETER, IAM_UNAUTH_ROLE_PARAMETER } from './authType';

This file was deleted.

Loading

0 comments on commit 6aa7e53

Please sign in to comment.