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: lambda layer change handling #7537

Merged
merged 3 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export async function updateLayerWalkthrough(

// select layer version
if (layerHasDeployed) {
const layerCloudState = LayerCloudState.getInstance();
const layerCloudState = LayerCloudState.getInstance(parameters.layerName);
const layerVersions = await layerCloudState.getLayerVersionsFromCloud(context, parameters.layerName);
const latestVersionText = 'Future layer versions';
const layerVersionChoices = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { updateLayerArtifacts } from '../utils/storeResources';
const removeLayerQuestion = 'Choose the Layer versions you want to remove.';

export async function removeWalkthrough(context: $TSContext, layerName: string): Promise<string | undefined> {
const layerCloudState = LayerCloudState.getInstance();
const layerCloudState = LayerCloudState.getInstance(layerName);
const layerVersionList = await layerCloudState.getLayerVersionsFromCloud(context, layerName);

// if the layer hasn't been pushed return and remove it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const askLayerSelection = async (
layerSelections = layerSelections.filter(selection => selection !== provideExistingARNsPrompt);

for (const layerName of layerSelections) {
const layerCloudState = LayerCloudState.getInstance();
const layerCloudState = LayerCloudState.getInstance(layerName);
const layerVersions = await layerCloudState.getLayerVersionsFromCloud(context, layerName);
const layerVersionChoices = layerVersions.map(mapVersionNumberToChoice);

Expand All @@ -83,10 +83,16 @@ export const askLayerSelection = async (
const previousLayerSelection = _.first(filterProjectLayers(previousSelections).filter(prev => prev.resourceName === layerName));

let defaultLayerSelection: string;

if (previousLayerSelection === undefined || previousLayerSelection.isLatestVersionSelected) {
defaultLayerSelection = defaultLayerVersionPrompt;
} else {
defaultLayerSelection = mapVersionNumberToChoice(_.first(layerVersions.filter(v => v.Version === previousLayerSelection.version)));
const previouslySelectedLayerVersion = _.first(layerVersions.filter(v => v.Version === previousLayerSelection.version));

// Fallback to defaultLayerVersionPrompt as it is possible that a function is associated with a non-existent layer version
defaultLayerSelection = previouslySelectedLayerVersion
? mapVersionNumberToChoice(previouslySelectedLayerVersion)
: defaultLayerVersionPrompt;
}

const versionSelection = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function generateLayerCfnObj(isNewVersion: boolean, parameters: LayerPara
if (isNewVersion) {
const [shortId] = uuid().split('-');
logicalName = `${LayerCfnLogicalNamePrefix.LambdaLayerVersion}${shortId}`;
const layerCloudState = LayerCloudState.getInstance();
layerCloudState.latestVersionLogicalId = logicalName; // Store in singleton so it can be used in zipfile name
const layerCloudState = LayerCloudState.getInstance(parameters.layerName);
layerCloudState.latestVersionLogicalId = logicalName; // Store in the given layer's layerCloudState instance so it can be used in zipfile name
versionList.unshift({ LogicalName: logicalName, legacyLayer: false });
} else {
logicalName = _.first(versionList).LogicalName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import { LegacyPermissionEnum } from './layerMigrationUtils';
import { LayerVersionMetadata, PermissionEnum } from './layerParams';

export class LayerCloudState {
private static instance: LayerCloudState;
private static instances: Record<string, LayerCloudState> = {};
private layerVersionsMetadata: LayerVersionMetadata[];
public latestVersionLogicalId: string;

private constructor() {}

static getInstance(): LayerCloudState {
if (!LayerCloudState.instance) {
LayerCloudState.instance = new LayerCloudState();
static getInstance(layerName: string): LayerCloudState {
if (!LayerCloudState.instances[layerName]) {
LayerCloudState.instances[layerName] = new LayerCloudState();
}
return LayerCloudState.instance;
return LayerCloudState.instances[layerName];
}

private async loadLayerDataFromCloud(context: $TSContext, layerName: string): Promise<LayerVersionMetadata[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export const packageLayer: Packager = async (context, resource) => {
await zipPackage(packageResult.zipEntries, destination);
}

const layerCloudState = LayerCloudState.getInstance();
const layerCloudState = LayerCloudState.getInstance(resource.resourceName);
if (!layerCloudState.latestVersionLogicalId) {
// "Should" never be reachable, but sanity check just in case
throw new Error('LogicalId missing for new layer version.');
throw new Error(`LogicalId missing for new layer version: ${resource.resourceName}.`);
}

const zipFilename = createLayerZipFilename(resource.resourceName, layerCloudState.latestVersionLogicalId);
Expand Down Expand Up @@ -112,6 +112,9 @@ export async function checkContentChanges(context: $TSContext, layerResources: A
for (const layer of changedLayerResources) {
let { parameters } = layer;
if (!accepted) {
context.print.info('');
context.print.info(`Change options layer: ${layer.resourceName}`);
context.print.info('');
parameters = await lambdaLayerNewVersionWalkthrough(parameters, timestampString);
} else {
parameters.description = `Updated layer version ${timestampString}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,17 @@ function ensureLayerRuntimeFolder(layerDirPath: string, runtime: LayerRuntime) {
}

function createLayerCfnFile(parameters: LayerParameters, layerDirPath: string) {
JSONUtilities.writeJson(path.join(layerDirPath, getCfnTemplateFileName(parameters.layerName)), generateLayerCfnObj(true, parameters));
const layerCfnObj = generateLayerCfnObj(true, parameters);
const layerCfnFilePath = path.join(layerDirPath, getCfnTemplateFileName(parameters.layerName));

JSONUtilities.writeJson(layerCfnFilePath, layerCfnObj);
}

async function updateLayerCfnFile(context: $TSContext, parameters: LayerParameters, layerDirPath: string): Promise<$TSObject> {
let layerVersionList: LayerVersionMetadata[] = [];

if (loadPreviousLayerHash(parameters.layerName)) {
const layerCloudState = LayerCloudState.getInstance();
const layerCloudState = LayerCloudState.getInstance(parameters.layerName);

layerVersionList = await layerCloudState.getLayerVersionsFromCloud(context, parameters.layerName);
}
Expand Down