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

Enable preview for create and update #1891

Merged
merged 9 commits into from
Dec 12, 2024
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
22 changes: 22 additions & 0 deletions examples/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ func TestRefreshChanges(t *testing.T) {
assert.Equal(t, 1, len(*updateSummary))
}

func TestPreviewStableProperties(t *testing.T) {
cwd := getCwd(t)
options := []opttest.Option{
opttest.LocalProviderPath("aws-native", filepath.Join(cwd, "..", "bin")),
opttest.YarnLink("@pulumi/aws-native"),
}
test := pulumitest.NewPulumiTest(t, filepath.Join(cwd, "stable-outputs-preview"), options...)
test.SetConfig(t, "lambdaDescription", "Lambda 1")
defer func() {
test.Destroy(t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't pulumitest auto destroy by default?

}()

upResult := test.Up(t)
t.Logf("#%v", upResult.Summary)

// updating a non-replaceOnChanges property to ensure
// that the downstream resources doesn't show a replacement
test.SetConfig(t, "lambdaDescription", "Lambda 2")
previewResult := test.Preview(t)
assertpreview.HasNoReplacements(t, previewResult)
}

func TestCustomResourceEmulator(t *testing.T) {
crossTest := func(t *testing.T, outputs auto.OutputMap) {
require.Contains(t, outputs, "cloudformationAmiId")
Expand Down
3 changes: 3 additions & 0 deletions examples/stable-outputs-preview/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/bin/
/node_modules/
handler.zip
3 changes: 3 additions & 0 deletions examples/stable-outputs-preview/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: aws-native-stable-outputs-preview
runtime: nodejs
description: An example program to test stable outputs during preview
8 changes: 8 additions & 0 deletions examples/stable-outputs-preview/app/index.handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const handler = async () => {
return {
statusCode: 200,
body: JSON.stringify({
message: "Hello World!",
}),
};
};
49 changes: 49 additions & 0 deletions examples/stable-outputs-preview/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as pulumi from '@pulumi/pulumi';
import * as path from 'path';
import * as ccapi from "@pulumi/aws-native";
import * as aws from '@pulumi/aws';
import { zipDirectory } from './zip';

const config = new pulumi.Config();
const desc = config.get('lambdaDescription') ?? 'test lambda';
const role = new ccapi.iam.Role('lambda-role', {
assumeRolePolicyDocument: {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: {
Service: "lambda.amazonaws.com",
},
Action: "sts:AssumeRole",
},
],
},
});


const bucket = new ccapi.s3.Bucket('bucket');
const object = new aws.s3.BucketObjectv2(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meta: Kind of frustrating that many rather basic use cases require layering aws classic on top of aws native in order to make it usable
:(

'object',
{
source: zipDirectory(path.join(__dirname, 'app'), 'handler.zip'),
bucket: bucket.bucketName.apply(bucket => bucket!),
key: 'handler.zip',
},
);
const handler = new ccapi.lambda.Function('my-function', {
code: {
s3Bucket: bucket.bucketName.apply(bucket => bucket!),
s3Key: object.key,
},
description: desc,
runtime: 'nodejs20.x',
handler: 'index.handler',
role: role.arn,
});

new ccapi.lambda.Permission('chall-permission', {
functionName: handler.arn,
action: 'lambda:InvokeFunction',
principal: 's3.amazonaws.com',
});
13 changes: 13 additions & 0 deletions examples/stable-outputs-preview/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "aws-native-naming-conventions",
"main": "index.ts",
"devDependencies": {
"@types/node": "^16"
},
"dependencies": {
"@pulumi/aws": "^6.63.0",
"@pulumi/aws-native": "^0.8.0",
"@pulumi/pulumi": "^3.74.0",
"archiver": "^7.0.1"
}
}
18 changes: 18 additions & 0 deletions examples/stable-outputs-preview/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}
48 changes: 48 additions & 0 deletions examples/stable-outputs-preview/zip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createWriteStream, promises as fs } from 'fs';
import * as path from 'path';
import * as glob from 'glob';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const archiver = require('archiver');

export function zipDirectory(directory: string, outputFile: string): Promise<string> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we do that by using an AssetArchive?

// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
// The below options are needed to support following symlinks when building zip files:
// - nodir: This will prevent symlinks themselves from being copied into the zip.
// - follow: This will follow symlinks and copy the files within.
const globOptions = {
dot: true,
nodir: true,
follow: true,
cwd: directory,
};
const files = glob.sync('**', globOptions); // The output here is already sorted

const output = createWriteStream(outputFile);

const archive = archiver('zip');
archive.on('warning', reject);
archive.on('error', reject);

// archive has been finalized and the output file descriptor has closed, resolve promise
// this has to be done before calling `finalize` since the events may fire immediately after.
// see https://www.npmjs.com/package/archiver
output.once('close', () => resolve(outputFile));

archive.pipe(output);

// Append files serially to ensure file order
for (const file of files) {
const fullPath = path.resolve(directory, file);
const [data, stat] = await Promise.all([fs.readFile(fullPath), fs.stat(fullPath)]);
archive.append(data, {
name: file,
date: new Date('1980-01-01T00:00:00.000Z'), // reset dates to get the same hash for the same content
mode: stat.mode,
});
}

await archive.finalize();
});
}
Loading
Loading