From 1c6320a74703e5e1d25697574bece13c2207682b Mon Sep 17 00:00:00 2001 From: Elad Cohen Date: Tue, 18 Jun 2024 13:07:28 +0300 Subject: [PATCH] wip --- dynamodb/dynamodb-types.w | 2 +- dynamodb/dynamodb.w | 4 ++++ dynamodb/package-lock.json | 4 ++-- dynamodb/package.json | 2 +- python/inflight.js | 15 +++++++++++++++ python/test-assets/main.py | 4 ++-- python/tfaws/api.js | 5 ++++- python/tfaws/bucket.js | 13 +++++++++---- python/tfaws/function.js | 9 ++++++--- python/tfaws/queue.js | 6 ++++-- python/tfaws/topic.js | 5 ++++- python/wplatform.js | 18 ++---------------- ses/lib.w | 4 ++++ ses/package-lock.json | 4 ++-- ses/package.json | 2 +- ses/types.w | 2 +- sns/lib.w | 4 ++++ sns/package-lock.json | 4 ++-- sns/package.json | 2 +- sns/types.w | 2 +- 20 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 python/inflight.js diff --git a/dynamodb/dynamodb-types.w b/dynamodb/dynamodb-types.w index 08e57c0c..a3d2428a 100644 --- a/dynamodb/dynamodb-types.w +++ b/dynamodb/dynamodb-types.w @@ -206,7 +206,7 @@ pub inflight interface IClient { inflight transactWrite(options: TransactWriteOptions): TransactWriteOutput; } -pub interface ITable extends IClient { +pub interface ITable extends IClient, std.IResource { setStreamConsumer(handler: inflight (StreamRecord): void, options: StreamConsumerOptions?): void; inflight readWriteConnection(): Connection; } diff --git a/dynamodb/dynamodb.w b/dynamodb/dynamodb.w index 6b5e7def..deba8fbd 100644 --- a/dynamodb/dynamodb.w +++ b/dynamodb/dynamodb.w @@ -76,5 +76,9 @@ pub class Table impl dynamodb_types.ITable { pub inflight readWriteConnection(): dynamodb_types.Connection { return this.implementation.readWriteConnection(); } + + pub onLift(host: std.IInflightHost, ops: Array) { + this.implementation.onLift(host, ops); + } } diff --git a/dynamodb/package-lock.json b/dynamodb/package-lock.json index 5b0e9c87..d564d731 100644 --- a/dynamodb/package-lock.json +++ b/dynamodb/package-lock.json @@ -1,12 +1,12 @@ { "name": "@winglibs/dynamodb", - "version": "0.1.11", + "version": "0.1.12", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@winglibs/dynamodb", - "version": "0.1.11", + "version": "0.1.12", "license": "MIT", "peerDependencies": { "@aws-sdk/client-dynamodb": "^3.461.0", diff --git a/dynamodb/package.json b/dynamodb/package.json index 192f2be7..6d84dbd1 100644 --- a/dynamodb/package.json +++ b/dynamodb/package.json @@ -1,6 +1,6 @@ { "name": "@winglibs/dynamodb", - "version": "0.1.11", + "version": "0.1.12", "description": "DynamoDB library for Wing", "author": { "name": "Cristian Pallarés", diff --git a/python/inflight.js b/python/inflight.js new file mode 100644 index 00000000..1160e8e6 --- /dev/null +++ b/python/inflight.js @@ -0,0 +1,15 @@ +const tryGetPythonInflight = (inflight) => { + if (inflight._inflightType === "_inflightPython") { + return inflight; + } else { + // inflight was lifted to another inflight + for (let l of inflight._liftMap?.handle || []) { + const lifted = tryGetPythonInflight(l[0]); + if (lifted) { + return lifted; + } + } + } +}; + +exports.tryGetPythonInflight = tryGetPythonInflight; diff --git a/python/test-assets/main.py b/python/test-assets/main.py index f6a24976..836c3642 100644 --- a/python/test-assets/main.py +++ b/python/test-assets/main.py @@ -9,13 +9,13 @@ def handler(event, context): foo_env = os.getenv("FOO") email_client = lifted("email") - email_client.send_email(Source="bot@wing.cloud", Destination={'ToAddresses': ['bot@monada.co',],},Message={'Subject': {'Data': 'Winglang Test Email!',},'Body': {'Text': {'Data': 'Hello from Python!',},}},) + email_client.send_email(Source="eladc@wing.cloud", Destination={'ToAddresses': ['eladc@monada.co',],},Message={'Subject': {'Data': 'Winglang Test Email!',},'Body': {'Text': {'Data': 'Hello from Python!',},}},) mobile_client = lifted("sms") mobile_client.publish( Message="Hello from Python!", Subject="Test Subject", - PhoneNumber="1234567890", + PhoneNumber="+972503292946", ) table = lifted("table") diff --git a/python/tfaws/api.js b/python/tfaws/api.js index bf4a6d9e..4c657144 100644 --- a/python/tfaws/api.js +++ b/python/tfaws/api.js @@ -2,10 +2,12 @@ const { Api: TfAwsApi } = require("@winglang/sdk/lib/target-tf-aws/api.js"); const { App } = require("@winglang/sdk/lib/target-tf-aws/app.js"); const { Node } = require("@winglang/sdk/lib/std/node.js"); const { Function } = require("./function.js"); +const { tryGetPythonInflight } = require("../inflight.js"); module.exports.Api = class Api extends TfAwsApi { addHandler(inflight, method, path, props) { - if (inflight._inflightType !== "_inflightPython") { + const pythonInflight = tryGetPythonInflight(inflight); + if (!pythonInflight) { return super.addHandler(inflight, method, path); } @@ -16,6 +18,7 @@ module.exports.Api = class Api extends TfAwsApi { App.of(this).makeId(this, `${this.node.id}-OnMessage`), inflight, props, + pythonInflight, ); Node.of(handler).hidden = true; this.handlers[inflight._id] = handler; diff --git a/python/tfaws/bucket.js b/python/tfaws/bucket.js index 9f7030bc..5ddd0402 100644 --- a/python/tfaws/bucket.js +++ b/python/tfaws/bucket.js @@ -2,6 +2,7 @@ const { Bucket: TfAwsBucket } = require("@winglang/sdk/lib/target-tf-aws/bucket. const { BucketEventType } = require("@winglang/sdk/lib/cloud/bucket.js"); const { Node } = require("@winglang/sdk/lib/std/node.js"); const { Topic } = require("./topic.js"); +const { tryGetPythonInflight } = require("../inflight.js"); const EVENTS = { [BucketEventType.DELETE]: ["s3:ObjectRemoved:*"], @@ -20,7 +21,8 @@ module.exports.Bucket = class Bucket extends TfAwsBucket { } onCreate(inflight, opts) { - if (inflight._inflightType !== "_inflightPython") { + const pythonInflight = tryGetPythonInflight(inflight); + if (!pythonInflight) { return super.onCreate(inflight, props); } @@ -35,7 +37,8 @@ module.exports.Bucket = class Bucket extends TfAwsBucket { } onUpdate(inflight, opts) { - if (inflight._inflightType !== "_inflightPython") { + const pythonInflight = tryGetPythonInflight(inflight); + if (!pythonInflight) { return super.onUpdate(inflight, props); } @@ -50,7 +53,8 @@ module.exports.Bucket = class Bucket extends TfAwsBucket { } onDelete(inflight, opts) { - if (inflight._inflightType !== "_inflightPython") { + const pythonInflight = tryGetPythonInflight(inflight); + if (!pythonInflight) { return super.onDelete(inflight, props); } @@ -65,7 +69,8 @@ module.exports.Bucket = class Bucket extends TfAwsBucket { } onEvent(inflight, opts) { - if (inflight._inflightType !== "_inflightPython") { + const pythonInflight = tryGetPythonInflight(inflight); + if (!pythonInflight) { return super.onEvent(inflight, props); } diff --git a/python/tfaws/function.js b/python/tfaws/function.js index 0499634b..d339586a 100644 --- a/python/tfaws/function.js +++ b/python/tfaws/function.js @@ -33,7 +33,7 @@ module.exports.Function = class Function extends Construct { const homeEnv = process.env["HOME"] || ""; const outdir = buildAws({ - nodePath: Node.of(handler).path, + nodePath: Node.of(this).path, path: pythonInflight.inner.props.path, handler: pythonInflight.inner.props.handler, homeEnv: homeEnv, @@ -76,18 +76,21 @@ module.exports.Function = class Function extends Construct { // Custom resources if (typeof client.tableName === "string" && typeof client.connection === "object" ) { + client.onLift(this.dummy, allow); clients[clientId] = { type: "@winglibs.dyanmodb.Table", target: "aws", props: { connection: client.connection }, } - } else if (client.constructor?.name === "MobileClient") { + } else if (client.constructor?.name === "MobileNotifications") { + client.onLift(this.dummy, allow); clients[clientId] = { - type: "@winglibs.sns.MobileClient", + type: "@winglibs.sns.MobileNotifications", target: "aws", props: {}, } } else if (client.constructor?.name === "EmailService") { + client.onLift(this.dummy, allow); clients[clientId] = { type: "@winglibs.ses.EmailService", target: "aws", diff --git a/python/tfaws/queue.js b/python/tfaws/queue.js index 5e0a095d..7eb62e9b 100644 --- a/python/tfaws/queue.js +++ b/python/tfaws/queue.js @@ -3,6 +3,7 @@ const { Node } = require("@winglang/sdk/lib/std/node.js"); const { Duration } = require("@winglang/sdk/lib/std/duration.js"); const awsProvider = require("@cdktf/provider-aws"); const { Function } = require("./function.js"); +const { tryGetPythonInflight } = require("../inflight.js"); module.exports.Queue = class Queue extends TfAwsQueue { constructor( @@ -17,7 +18,8 @@ module.exports.Queue = class Queue extends TfAwsQueue { inflight, props = {}, ) { - if (inflight._inflightType !== "_inflightPython") { + const pythonInflight = tryGetPythonInflight(inflight); + if (!pythonInflight) { return super.setConsumer(inflight, props); } @@ -26,7 +28,7 @@ module.exports.Queue = class Queue extends TfAwsQueue { timeout: Duration.fromSeconds( this.queue.visibilityTimeoutSeconds ?? 30 ), - }); + }, pythonInflight); consumer.dummy.addPolicyStatements({ actions: [ diff --git a/python/tfaws/topic.js b/python/tfaws/topic.js index 321a6bbb..b1b29600 100644 --- a/python/tfaws/topic.js +++ b/python/tfaws/topic.js @@ -3,6 +3,7 @@ const { App } = require("@winglang/sdk/lib/target-tf-aws/app.js"); const { Node } = require("@winglang/sdk/lib/std/node.js"); const awsProvider = require("@cdktf/provider-aws"); const { Function } = require("./function.js"); +const { tryGetPythonInflight } = require("../inflight.js"); module.exports.Topic = class Topic extends TfAwsTopic { constructor( @@ -17,7 +18,8 @@ module.exports.Topic = class Topic extends TfAwsTopic { inflight, props = {} ) { - if (inflight._inflightType !== "_inflightPython") { + const pythonInflight = tryGetPythonInflight(inflight); + if (!pythonInflight) { return super.setConsumer(inflight, props); } @@ -31,6 +33,7 @@ module.exports.Topic = class Topic extends TfAwsTopic { App.of(this).makeId(this, `${this.node.id}-OnMessage`), inflight, props, + pythonInflight, ); this.handlers[inflight._id] = consumer; diff --git a/python/wplatform.js b/python/wplatform.js index 61f19174..09a9f648 100644 --- a/python/wplatform.js +++ b/python/wplatform.js @@ -4,32 +4,18 @@ const { Queue: TfAwsQueue } = require("./tfaws/queue.js"); const { Topic: TfAwsTopic } = require("./tfaws/topic.js"); const { Bucket: TfAwsBucket } = require("./tfaws/bucket.js"); const { Api: TfAwsApi } = require("./tfaws/api.js"); - +const { tryGetPythonInflight } = require("./inflight.js"); const FUNCTION_FQN = "@winglang/sdk.cloud.Function"; const QUEUE_FQN = "@winglang/sdk.cloud.Queue"; const TOPIC_FQN = "@winglang/sdk.cloud.Topic"; const BUCKET_FQN = "@winglang/sdk.cloud.Bucket"; const API_FQN = "@winglang/sdk.cloud.Api"; -const tryGetPythonInflight = (inflight) => { - if (inflight._inflightType === "_inflightPython") { - return inflight; - } else { - // inflight was lifted to another inflight - for (let l of inflight._liftMap?.handle || []) { - const lifted = tryGetPythonInflight(l[0]); - if (lifted) { - return lifted; - } - } - } -}; - const createFunction = (target, scope, id, inflight, props) => { const pythonInflight = tryGetPythonInflight(inflight); if (pythonInflight) { if (target === "tf-aws") { - return new TfAwsFunction(scope, id, inflight, props); + return new TfAwsFunction(scope, id, inflight, props, pythonInflight); } else if (target === "sim") { return new SimFunction(scope, id, inflight, props, pythonInflight); } diff --git a/ses/lib.w b/ses/lib.w index e2cb4ec3..04de9677 100644 --- a/ses/lib.w +++ b/ses/lib.w @@ -34,4 +34,8 @@ pub class EmailService impl types.IEmailService { pub inflight sendRawEmail(options: types.SendRawEmailOptions): str? { return this.inner.sendRawEmail(options); } + + pub onLift(host: std.IInflightHost, ops: Array) { + this.inner.onLift(host, ops); + } } diff --git a/ses/package-lock.json b/ses/package-lock.json index f6ffab5a..72f96138 100644 --- a/ses/package-lock.json +++ b/ses/package-lock.json @@ -1,12 +1,12 @@ { "name": "@winglibs/ses", - "version": "0.0.2", + "version": "0.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@winglibs/ses", - "version": "0.0.2", + "version": "0.0.3", "license": "MIT", "peerDependencies": { "@aws-sdk/client-ses": "^3.572.0", diff --git a/ses/package.json b/ses/package.json index 2c0ed309..5af06760 100644 --- a/ses/package.json +++ b/ses/package.json @@ -1,7 +1,7 @@ { "name": "@winglibs/ses", "description": "Wing library for interacting with AWS SES", - "version": "0.0.2", + "version": "0.0.3", "repository": { "type": "git", "url": "https://github.com/winglang/winglibs.git", diff --git a/ses/types.w b/ses/types.w index d816bc7c..c69cfce8 100644 --- a/ses/types.w +++ b/ses/types.w @@ -56,7 +56,7 @@ pub struct SendRawEmailOptions { RawMessage: RawMessage; } -pub interface IEmailService { +pub interface IEmailService extends std.IResource { inflight sendEmail(options: SendEmailOptions): str?; inflight sendRawEmail(options: SendRawEmailOptions): str?; } diff --git a/sns/lib.w b/sns/lib.w index e193925c..d9e99bf5 100644 --- a/sns/lib.w +++ b/sns/lib.w @@ -33,4 +33,8 @@ pub class MobileNotifications impl types.IMobileNotifications { pub inflight publish(options: types.PublishOptions): types.PublishResult { return this.inner.publish(options); } + + pub onLift(host: std.IInflightHost, ops: Array) { + this.inner.onLift(host, ops); + } } diff --git a/sns/package-lock.json b/sns/package-lock.json index 460a19b8..06095eb7 100644 --- a/sns/package-lock.json +++ b/sns/package-lock.json @@ -1,12 +1,12 @@ { "name": "@winglibs/sns", - "version": "0.1.0", + "version": "0.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@winglibs/sns", - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "peerDependencies": { "@aws-sdk/client-sns": "^3.577.0" diff --git a/sns/package.json b/sns/package.json index b2f21aa1..f3506d2b 100644 --- a/sns/package.json +++ b/sns/package.json @@ -1,7 +1,7 @@ { "name": "@winglibs/sns", "description": "Wing library for interacting with AWS SNS", - "version": "0.1.0", + "version": "0.1.1", "repository": { "type": "git", "url": "https://github.com/winglang/winglibs.git", diff --git a/sns/types.w b/sns/types.w index 2fe2aa9d..3f782d08 100644 --- a/sns/types.w +++ b/sns/types.w @@ -16,6 +16,6 @@ pub struct PublishResult { SequenceNumber: str?; } -pub interface IMobileNotifications { +pub interface IMobileNotifications extends std.IResource { inflight publish(options: PublishOptions): PublishResult; }