-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sdk): custom bind method on awscdk targets (#3769)
Following @eladb's recommendation in PR #3398, I removed the import of `target-awscdk` and `target-tf-aws`, and checked if the `host` implements the methods of the `IAwsFunction` interface. This way, the error that was occurring due to not having `aws-cdk-lib` globally installed no longer happens. ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
- Loading branch information
1 parent
f416d4b
commit 8b517b8
Showing
4 changed files
with
146 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/*\ | ||
skip: true | ||
\*/ | ||
// this example only works on AWS (intentionally) | ||
|
||
bring "aws-cdk-lib" as awscdk; | ||
bring aws; | ||
bring cloud; | ||
bring util; | ||
|
||
enum AttributeType { | ||
String, | ||
Number, | ||
Binary, | ||
} | ||
|
||
struct Attribute { | ||
type: AttributeType; | ||
value: Json; | ||
} | ||
|
||
class DynamoTable { | ||
table: awscdk.aws_dynamodb.Table; | ||
tableName: str; | ||
init() { | ||
let target = util.env("WING_TARGET"); | ||
if target != "awscdk" { | ||
throw("Unsupported target: ${target} (expected 'awscdk')"); | ||
} | ||
|
||
this.table = new awscdk.aws_dynamodb.Table( | ||
tableName: this.node.addr, | ||
billingMode: awscdk.aws_dynamodb.BillingMode.PAY_PER_REQUEST, | ||
removalPolicy: awscdk.RemovalPolicy.DESTROY, | ||
partitionKey: awscdk.aws_dynamodb.Attribute { | ||
name: "Flavor", | ||
type: awscdk.aws_dynamodb.AttributeType.STRING, | ||
}, | ||
); | ||
this.tableName = this.table.tableName; | ||
} | ||
|
||
bind(host: std.IInflightHost, ops: Array<str>) { | ||
if let host = aws.Function.from(host) { | ||
if ops.contains("putItem") { | ||
host.addPolicyStatements([aws.PolicyStatement { | ||
actions: ["dynamodb:PutItem"], | ||
resources: [this.table.tableArn], | ||
effect: aws.Effect.ALLOW, | ||
}]); | ||
} | ||
|
||
if ops.contains("getItem") { | ||
host.addPolicyStatements([aws.PolicyStatement { | ||
actions: ["dynamodb:GetItem"], | ||
resources: [this.table.tableArn], | ||
effect: aws.Effect.ALLOW, | ||
}]); | ||
} | ||
} | ||
} | ||
|
||
extern "./dynamo.js" inflight _putItem(tableName: str, item: Json): void; | ||
inflight putItem(item: Map<Attribute>) { | ||
let json = this._itemToJson(item); | ||
this._putItem(this.tableName, json); | ||
} | ||
|
||
extern "./dynamo.js" inflight _getItem(tableName: str, key: Json): Json; | ||
inflight getItem(key: Map<Attribute>): Json { | ||
let json = this._itemToJson(key); | ||
return this._getItem(this.tableName, json); | ||
} | ||
|
||
inflight _itemToJson(item: Map<Attribute>): Json { | ||
let json = MutJson {}; | ||
for key in item.keys() { | ||
let attribute = item.get(key); | ||
let attributeTypeStr = this._attributeTypeToString(attribute.type); | ||
|
||
let innerJson = MutJson {}; | ||
innerJson.set(attributeTypeStr, attribute.value); | ||
json.set(key, innerJson); | ||
} | ||
return json; | ||
} | ||
|
||
inflight _attributeTypeToString(type: AttributeType): str { | ||
if type == AttributeType.String { | ||
return "S"; | ||
} elif type == AttributeType.Number { | ||
return "N"; | ||
} elif type == AttributeType.Binary { | ||
return "B"; | ||
} | ||
} | ||
} | ||
|
||
// --- tests --- | ||
|
||
let table = new DynamoTable(); | ||
|
||
test "cdk table" { | ||
table.putItem({ | ||
"Flavor" => Attribute { | ||
type: AttributeType.String, | ||
value: "Chocolate", | ||
}, | ||
"Quantity" => Attribute { | ||
type: AttributeType.String, | ||
value: "20Kg" | ||
} | ||
}); | ||
|
||
let c = table.getItem({ | ||
"Flavor" => Attribute { | ||
type: AttributeType.String, | ||
value: "Chocolate", | ||
} | ||
}); | ||
assert(c.get("Item").get("Flavor").get("S").asStr() == "Chocolate"); | ||
assert(c.get("Item").get("Quantity").get("S").asStr() == "20Kg"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters