Skip to content

Commit

Permalink
locals
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteinich committed Jul 11, 2020
1 parent b1f9467 commit 7969da3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
6 changes: 5 additions & 1 deletion examples/simple/stacks/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ class MyBucketStack extends tsk.Stack {
region: aModule.getString("region")
});

const aLocal = new tsk.Local(this, 'local_val', {
expression: bucket.arn
});

new tsk.Output(this, 'bucket_arn', {
value: bucket.arn,
value: aLocal.value,
description: "A bucket arn"
})

Expand Down
3 changes: 2 additions & 1 deletion packages/@terrastack/core/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export * from './backends';
export * from './remote-state';
export * from './output';
export * from './variable';
export * from './module';
export * from './module';
export * from './local';
40 changes: 40 additions & 0 deletions packages/@terrastack/core/lib/local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ResourceObject, TerraformSchemaType } from './resource-object';
import { Construct } from 'constructs';

export class Local extends ResourceObject {
value: any;

constructor(scope: Construct, ns: string, options: LocalProps) {
super(scope, ns, { ...options }, {
name: "local",
schemaType: TerraformSchemaType.LOCALS,
providerVersion: "1.0",
providerName: "terraform",
rawSchema: ""
});

this.value = `\$\{local.${this._name}\}`
}

public get stringValue(): string {
return this.value;
}

public get numberValue(): number {
return this.value;
}

/**
* Renders the object to Terraform config.
* @internal
*/
public _render(): any {
const obj: {[k: string]: any} = {};
obj[this._name] = this.tfProperties.expression
return obj;
}
}

export interface LocalProps {
readonly expression: any;
}
3 changes: 2 additions & 1 deletion packages/@terrastack/core/lib/resource-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export enum TerraformSchemaType {
TERRAFORM = 'terraform',
OUTPUT = 'output',
VARIABLE = 'variable',
MODULE = 'module'
MODULE = 'module',
LOCALS = 'locals'
}

export interface TerraformMetadata {
Expand Down
3 changes: 2 additions & 1 deletion packages/@terrastack/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export class Stack extends Construct {
case TerraformSchemaType.TERRAFORM:
case TerraformSchemaType.OUTPUT:
case TerraformSchemaType.VARIABLE:
case TerraformSchemaType.MODULE: {
case TerraformSchemaType.MODULE:
case TerraformSchemaType.LOCALS: {
const manifest = removeEmpty(resolve(this, resource._render()));
const merged = {...doc[type], ...manifest}
doc[type] = merged
Expand Down

0 comments on commit 7969da3

Please sign in to comment.