From d1f0b62ed2a2c45a614c82c2900d6a7b1281c716 Mon Sep 17 00:00:00 2001 From: jimarek Date: Mon, 13 Nov 2023 13:03:15 +0100 Subject: [PATCH] fix: expose conditional --- packages/cdktf/lib/terraform-functions.ts | 16 +++++++++++++++- packages/cdktf/test/functions.test.ts | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/cdktf/lib/terraform-functions.ts b/packages/cdktf/lib/terraform-functions.ts index e7eecaafb4..00c0ad46d9 100644 --- a/packages/cdktf/lib/terraform-functions.ts +++ b/packages/cdktf/lib/terraform-functions.ts @@ -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"; @@ -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 diff --git a/packages/cdktf/test/functions.test.ts b/packages/cdktf/test/functions.test.ts index b62aeb55cf..e3369cf6d8 100644 --- a/packages/cdktf/test/functions.test.ts +++ b/packages/cdktf/test/functions.test.ts @@ -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");