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(lib): expose conditional #3264

Merged
merged 2 commits into from
Nov 29, 2023
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
16 changes: 15 additions & 1 deletion packages/cdktf/lib/terraform-functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) HashiCorp, Inc
// SPDX-License-Identifier: MPL-2.0
import { propertyAccess, rawString, Token } from ".";
import { conditional, Expression, propertyAccess, rawString, Token } from ".";
import { asAny } from "./functions/helpers";
import { FnGenerated } from "./functions/terraform-functions.generated";

Expand All @@ -18,6 +18,20 @@ export class Fn extends FnGenerated {
return Fn._bcrypt(str, cost ? [cost] : []);
}

/**
* {@link https://developer.hashicorp.com/terraform/language/expressions/conditionals} A conditional expression uses the value of a boolean expression to select one of two values.
* @param {Expression} condition
* @param {Expression} trueValue
* @param {Expression} falseValue
*/
static conditional(
condition: Expression,
trueValue: Expression,
falseValue: Expression
): any {
return conditional(condition, trueValue, falseValue);
}

/**
* {@link /terraform/docs/language/functions/lookup.html lookup} retrieves the value of a single element from a map, given its key. If the given key does not exist, the given default value is returned instead.
* @param {any} inputMap
Expand Down
19 changes: 19 additions & 0 deletions packages/cdktf/test/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,25 @@ test("functions with object inputs", () => {
`);
});

test("conditional", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");

new TerraformOutput(stack, "test-output", {
value: Fn.conditional(false, 0, 5),
});

expect(Testing.synth(stack)).toMatchInlineSnapshot(`
"{
"output": {
"test-output": {
"value": "\${false ? 0 : 5}"
}
}
}"
`);
});

test("quoted primitives in list", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");
Expand Down
Loading