From 23d298a87242f793eaa2203f3e39fe1c3048dcc5 Mon Sep 17 00:00:00 2001 From: eladcon Date: Wed, 19 Jun 2024 11:55:02 +0300 Subject: [PATCH] feat(python): custom liftable resource (#269) --- python/function.test.w | 14 +++++++++++++- python/lib.w | 4 ++++ python/package-lock.json | 28 ++++++++++++++-------------- python/package.json | 8 ++++---- python/test-assets/main.py | 7 +++++-- python/test-assets/requirements.txt | 2 +- python/tfaws/function.js | 9 +++++++++ python/util.js | 12 ++++++++++++ 8 files changed, 62 insertions(+), 22 deletions(-) diff --git a/python/function.test.w b/python/function.test.w index 1535fb24..1a5a59ed 100644 --- a/python/function.test.w +++ b/python/function.test.w @@ -8,6 +8,14 @@ bring ses; bring fs; bring "./lib.w" as python; +class CustomLiftable impl python.ILiftable { + pub liftData(): Json { + return { + "info": "CustomData", + }; + } +} + let table = new dynamodb.Table( attributes: [ { @@ -42,6 +50,10 @@ let func = new cloud.Function(new python.InflightFunction( obj: emailClient, allow: ["sendEmail"], }, + "custom": { + obj: new CustomLiftable(), + allow: [], + } }, ), { env: { "FOO": "bar" } }); @@ -51,5 +63,5 @@ new std.Test(inflight () => { let res = func.invoke("function1"); log("res: {res ?? "null"}"); expect.equal(Json.parse(res!).get("body"), "Hello!"); - expect.equal(bucket.get("test.txt"), "Hello, world!function1bardynamoDbValue"); + expect.equal(bucket.get("test.txt"), "Hello, world!function1bardynamoDbValueCustomData"); }, timeout: 3m) as "invokes the function"; diff --git a/python/lib.w b/python/lib.w index 2036bd7d..f13b6a22 100644 --- a/python/lib.w +++ b/python/lib.w @@ -7,6 +7,10 @@ bring "./sim/api_onrequest_inflight.w" as simapi; bring "./tfaws/inflight.w" as aws; bring "./tfaws/api_onrequest_inflight.w" as tfawsapi; +pub interface ILiftable { + liftData(): Json; +} + pub class InflightFunction impl cloud.IFunctionHandler { _inflightType: str; inner: types.IInflight; diff --git a/python/package-lock.json b/python/package-lock.json index ef83cdc3..a26b791a 100644 --- a/python/package-lock.json +++ b/python/package-lock.json @@ -1,20 +1,20 @@ { "name": "@winglibs/python", - "version": "0.0.11", + "version": "0.0.12", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@winglibs/python", - "version": "0.0.11", + "version": "0.0.12", "license": "MIT", "peerDependencies": { "@aws-sdk/client-lambda": "^3.549.0", "@cdktf/provider-aws": "^19.13.0", "@smithy/util-utf8": "^2.3.0", - "@winglibs/dynamodb": "^0.1.10", - "@winglibs/ses": "^0.0.2", - "@winglibs/sns": "^0.1.0", + "@winglibs/dynamodb": "^0.1.13", + "@winglibs/ses": "^0.0.4", + "@winglibs/sns": "^0.1.3", "cdktf": "^0.20.6", "glob": "^10.3.12" } @@ -6075,9 +6075,9 @@ } }, "node_modules/@winglibs/dynamodb": { - "version": "0.1.10", - "resolved": "https://registry.npmjs.org/@winglibs/dynamodb/-/dynamodb-0.1.10.tgz", - "integrity": "sha512-8xbGBF6jSyCKBTSBreyNlGaPCHjskyee/bWlFFi1kAn857b0HS/oxvaw6MakikHdwcfrnJW+lSnBKdoEpUNTOA==", + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/@winglibs/dynamodb/-/dynamodb-0.1.13.tgz", + "integrity": "sha512-z4nbfPk3iT3EjnIfPvrW/C7XrBh+7nRExpWOlnY8dgYgdZF2CyXS25XyuC3gG6IfpbHgY0neaMQp/OuazYzjhA==", "peer": true, "engines": { "wing": "*" @@ -6094,9 +6094,9 @@ } }, "node_modules/@winglibs/ses": { - "version": "0.0.2", - "resolved": "https://registry.npmjs.org/@winglibs/ses/-/ses-0.0.2.tgz", - "integrity": "sha512-ErjgbG+y0WFTN1b/+SrfhrkWPfmM11AByR9ZdhkRfwRHXmFv6Bx+CUeWfjAgLAoiFj720w4ZyZdqkgTmkyLr6A==", + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/@winglibs/ses/-/ses-0.0.4.tgz", + "integrity": "sha512-UC/noX/b9wJ5wmCi0DeTyxoBRCfdnMuUQeBOgs5nCYkHJDyYu+8ZxqiI2m72Me0O90bsOAyBj0VCJ6YY+AVlqg==", "peer": true, "engines": { "wing": "*" @@ -6107,9 +6107,9 @@ } }, "node_modules/@winglibs/sns": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/@winglibs/sns/-/sns-0.1.0.tgz", - "integrity": "sha512-5mAfxrTMSYZb702seTrsBhUul3uqoGCIx7xpP6QXaQsOLp1QN4Ubr+zEo1ehzDie0QRQ666zSE04HziBfcL4iQ==", + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/@winglibs/sns/-/sns-0.1.3.tgz", + "integrity": "sha512-qY4tMge6LAHVZ9LG0wtUVBKqaXLpwLubwh5AhERw5Pbcod4bTft5pcYc/1YD2qM4rCV1MizpAbNnO6FRqBJNmQ==", "peer": true, "engines": { "wing": "*" diff --git a/python/package.json b/python/package.json index 94ec1be1..8b461599 100644 --- a/python/package.json +++ b/python/package.json @@ -1,7 +1,7 @@ { "name": "@winglibs/python", "description": "python library for Wing", - "version": "0.0.11", + "version": "0.0.12", "repository": { "type": "git", "url": "https://github.com/winglang/winglibs.git", @@ -22,9 +22,9 @@ "@aws-sdk/client-lambda": "^3.549.0", "@cdktf/provider-aws": "^19.13.0", "@smithy/util-utf8": "^2.3.0", - "@winglibs/dynamodb": "^0.1.10", - "@winglibs/ses": "^0.0.3", - "@winglibs/sns": "^0.1.1", + "@winglibs/dynamodb": "^0.1.13", + "@winglibs/ses": "^0.0.4", + "@winglibs/sns": "^0.1.3", "cdktf": "^0.20.6", "glob": "^10.3.12" } diff --git a/python/test-assets/main.py b/python/test-assets/main.py index 836c3642..c62619cd 100644 --- a/python/test-assets/main.py +++ b/python/test-assets/main.py @@ -21,11 +21,14 @@ def handler(event, context): table = lifted("table") response = table.get(Key={"id":"test"}) table_value = response["Item"]["body"] + + custom = lifted("custom") + custom_data = custom["liftData"]["info"] bucket = lifted("bucket") value = bucket.get("test.txt") - bucket.put("test.txt", value + payload + foo_env + table_value) - + bucket.put("test.txt", value + payload + foo_env + table_value + custom_data) + return { "statusCode": 200, "body": "Hello!" diff --git a/python/test-assets/requirements.txt b/python/test-assets/requirements.txt index f74c2c75..e72933c0 100644 --- a/python/test-assets/requirements.txt +++ b/python/test-assets/requirements.txt @@ -1,4 +1,4 @@ -wingsdk == 0.0.4 +wingsdk == 0.0.6 requests boto3 Faker diff --git a/python/tfaws/function.js b/python/tfaws/function.js index d339586a..bb638ad5 100644 --- a/python/tfaws/function.js +++ b/python/tfaws/function.js @@ -96,6 +96,15 @@ module.exports.Function = class Function extends Construct { target: "aws", props: {}, } + } else if (typeof client.liftData === "function") { + client.onLift(this.dummy, allow); + clients[clientId] = { + type: "@winglibs.python.ILiftable", + target: "aws", + props: { + liftData: client.liftData(), + }, + } } } diff --git a/python/util.js b/python/util.js index 8679031a..fee27baf 100644 --- a/python/util.js +++ b/python/util.js @@ -227,6 +227,18 @@ const getLifted = (resource, id) => { props: {}, } } + } else if (typeof resource.liftData === "function") { + lifted = { + id, + path: resource.node.path, + handle: makeEnvVarName("LIFT_DATA", resource), + type: "@winglibs.python.ILiftable", + target: "sim", + resource, + props: { + liftData: resource.liftData(), + }, + }; } return lifted;