Skip to content

Commit

Permalink
add docs for Fn.lookup and Fn.lookupNested
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarm committed Aug 7, 2023
1 parent 5bf5147 commit 367c8a2
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 43 deletions.
64 changes: 64 additions & 0 deletions examples/csharp/documentation/FunctionsOther.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0


using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Constructs;
using HashiCorp.Cdktf;
using aws.Provider;
using aws.DataAwsAvailabilityZones;

namespace Examples
{
class FunctionsOtherStack : TerraformStack
{
public FunctionsOtherStack(Construct scope, string name) : base(scope, name)
{
new AwsProvider(this, "aws", new AwsProviderConfig
{
Region = "eu-central-1"
});


// DOCS_BLOCK_START:functions-raw
DataAwsAvailabilityZones zones = new DataAwsAvailabilityZones(this, "zones", new DataAwsAvailabilityZonesConfig
{
State = "available"
});

new TerraformOutput(this, "half-of-the-zone", new TerraformOutputConfig
{
Value = $"${{length({zones.Fqn}.names) / 2}}"
});
// DOCS_BLOCK_END:functions-raw

// DOCS_BLOCK_START:functions-lookup
TerraformVariable v = new TerraformVariable(this, "complex_object", TerraformVariableConfig.builder()
.type("object({users: list(object({name: string}))})")
.build());
new TerraformOutput(this, "users", new TerraformOutputConfig
{
Value = Fn.Lookup(v.Value, "users")
});
new TerraformOutput(this, "first-user-name", new TerraformOutputConfig
{
Value = Fn.LookupNested(v.Value, new() { "users", "0", "name" })
});
// DOCS_BLOCK_END:functions-lookup

// DOCS_BLOCK_START:functions-raw-string
new TerraformOutput(this, "quotes", new TerraformOutputConfig
{
Value = Fn.RawString("\"b\"")
});
new TerraformOutput(this, "template", new TerraformOutputConfig
{
Value = Fn.RawString("${TEMPLATE}")
});
// DOCS_BLOCK_END:functions-raw-string
}
}
}
39 changes: 0 additions & 39 deletions examples/csharp/documentation/FunctionsRawStack.cs

This file was deleted.

2 changes: 1 addition & 1 deletion examples/csharp/documentation/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void Main(string[] args)
new Examples.Producer(app, "cdktf-producer");
new Examples.Consumer(app, "cdktf-consumer");
new Examples.OperatorsStack(app, "operators");
new Examples.FunctionsRawStack(app, "functions-raw");
new Examples.FunctionsOtherStack(app, "functions-other");

TerraformStack stack = new TerraformStack(app, "stack-escape-hatches");
// DOCS_BLOCK_START:stack-escape-hatches
Expand Down
37 changes: 37 additions & 0 deletions examples/go/documentation/functions-other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package main

import (
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
"github.com/hashicorp/terraform-cdk-go/cdktf"
)

func NewFunctionsOtherStack(scope constructs.Construct, name string) cdktf.TerraformStack {
stack := cdktf.NewTerraformStack(scope, &name)

// DOCS_BLOCK_START:functions-lookup
v := cdktf.NewTerraformVariable(stack, jsii.String("complex-object"), &cdktf.TerraformVariableConfig{
Type: jsii.String("object({users: list(object({name: string}))})"),
})
cdktf.NewTerraformOutput(stack, jsii.String("users"), &cdktf.TerraformOutputConfig{
Value: cdktf.Fn_Lookup(v.Value(), jsii.String("users"), nil),
})
cdktf.NewTerraformOutput(stack, jsii.String("first-user-name"), &cdktf.TerraformOutputConfig{
Value: cdktf.Fn_LookupNested(v.Value(), &[]interface{}{"users", 0, "name"}),
})
// DOCS_BLOCK_END:functions-lookup

// DOCS_BLOCK_START:functions-raw-string
cdktf.NewTerraformOutput(stack, jsii.String("quotes"), &cdktf.TerraformOutputConfig{
Value: cdktf.Fn_RawString(jsii.String("\"b\"")),
})
cdktf.NewTerraformOutput(stack, jsii.String("template"), &cdktf.TerraformOutputConfig{
Value: cdktf.Fn_RawString(jsii.String("${TEMPLATE}")),
})
// DOCS_BLOCK_END:functions-raw-string

return stack
}
1 change: 1 addition & 0 deletions examples/go/documentation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func main() {
NewAspectsStack(app, "aspects")
NewPrefixAspectsStack(app, "aspects-validation")
NewFunctionsStack(app, "functions")
NewFunctionsOtherStack(app, "functions-other")
NewOperatorsAndFunctionsRawStack(app, "operators-functions-raw")
NewHclInteropStack(app, "hcl-interop")
NewProvidersStack(app, "providers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

import software.constructs.Construct;

import java.util.Arrays;

// DOCS_BLOCK_START:functions-usage-example
import com.hashicorp.cdktf.Fn;
import com.hashicorp.cdktf.TerraformVariable;
import com.hashicorp.cdktf.TerraformOutput;
import com.hashicorp.cdktf.TerraformOutputConfig;
import imports.aws.data_aws_availability_zones.DataAwsAvailabilityZones;
Expand Down Expand Up @@ -41,5 +44,26 @@ public MainFunction(Construct scope, String id) {
.build());
// DOCS_BLOCK_END:functions-usage-example

// DOCS_BLOCK_START:functions-lookup
TerraformVariable v = new TerraformVariable(this, "complex_object", TerraformVariableConfig.builder()
.type("object({users: list(object({name: string}))})")
.build());
new TerraformOutput(this, "users", TerraformOutputConfig.builder()
.value(Fn.lookup(v.getValue(), "users"))
.build());
new TerraformOutput(this, "first-user-name", TerraformOutputConfig.builder()
.value(Fn.lookupNested(v.getValue(), Arrays.asList("users", "0", "name")))
.build());
// DOCS_BLOCK_END:functions-lookup

// DOCS_BLOCK_START:functions-raw-string
new TerraformOutput(this, "quotes", TerraformOutputConfig.builder()
.value(Fn.rawString("\"b\""))
.build());
new TerraformOutput(this, "template", TerraformOutputConfig.builder()
.value(Fn.rawString("${TEMPLATE}"))
.build());
// DOCS_BLOCK_END:functions-raw-string

}
}
23 changes: 22 additions & 1 deletion examples/python/documentation/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: MPL-2.0

from constructs import Construct
from cdktf import TerraformStack, App
from cdktf import TerraformStack, App, TerraformVariable
# DOCS_BLOCK_START:functions-usage-example
from cdktf import Fn, TerraformOutput
from imports.aws.provider import AwsProvider
Expand All @@ -28,3 +28,24 @@ def __init__(self, scope: Construct, id: str):

# DOCS_BLOCK_END:functions-usage-example

# DOCS_BLOCK_START:functions-lookup
v = TerraformVariable(self, "complex-object",
type = 'object({users: list(object({name: string}))})',
)
TerraformOutput(self, 'users',
value=Fn.lookup(v.value, "users")
)
TerraformOutput(self, 'first_user_name',
value=Fn.lookup_nested(v.value, ["users", 0, "name"])
)
# DOCS_BLOCK_END:functions-lookup

# DOCS_BLOCK_START:functions-raw-string
TerraformOutput(self, 'quotes',
value=Fn.raw_string('"b"')
)
TerraformOutput(self, 'users',
value=Fn.raw_string('${TEMPLATE}')
)
# DOCS_BLOCK_END:functions-raw-string

21 changes: 20 additions & 1 deletion examples/typescript/documentation/functions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc
// SPDX-License-Identifier: MPL-2.0
// DOCS_BLOCK_START:functions
import { TerraformStack } from "cdktf";
import { TerraformStack, TerraformVariable } from "cdktf";
import { Construct } from "constructs";
import { AwsProvider } from "@cdktf/provider-aws/lib/aws-provider";
// DOCS_BLOCK_END:functions
Expand Down Expand Up @@ -37,6 +37,25 @@ export class FunctionsStack extends TerraformStack {
});
// DOCS_BLOCK_END:functions

// DOCS_BLOCK_START:functions-lookup
const v = new TerraformVariable(this, "complex_object", {
type: "object({users: list(object({name: string}))})",
});
new TerraformOutput(this, "users", { value: Fn.lookup(v.value, "users") });
new TerraformOutput(this, "first_user_name", {
value: Fn.lookupNested(v.value, ["users", 0, "name"]),
});
// DOCS_BLOCK_END:functions-lookup

// DOCS_BLOCK_START:functions-raw-string
new TerraformOutput(this, "quotes", {
value: Fn.rawString(`"b"`),
});
new TerraformOutput(this, "template", {
value: Fn.rawString("${TEMPLATE}"),
});
// DOCS_BLOCK_END:functions-raw-string

// DOCS_BLOCK_START:operators

// ...
Expand Down
Loading

0 comments on commit 367c8a2

Please sign in to comment.