Skip to content

Commit

Permalink
fix: expose conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
jimarek authored and DanielMSchmidt committed Nov 29, 2023
1 parent a5956b0 commit d1f0b62
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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

0 comments on commit d1f0b62

Please sign in to comment.