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): better error for uninitialized provider #896

Merged
merged 3 commits into from
Aug 18, 2021
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
1 change: 1 addition & 0 deletions examples/go/google/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func NewMyStack(scope constructs.Construct, id string) cdktf.TerraformStack {
Zone: jsii.String("us-west1"),
Project: jsii.String("dschmidt-cdk-test"),
})
local.NewLocalProvider(stack, jsii.String("local"), &local.LocalProviderConfig{})

sa := google.NewServiceAccount(stack, jsii.String("sa"), &google.ServiceAccountConfig{
AccountId: jsii.String("cluster-admin"),
Expand Down
2 changes: 1 addition & 1 deletion examples/java/aws/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>3.0.0</version>
<version>3.3.75</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion examples/java/azure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>3.0.0</version>
<version>3.3.75</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion examples/java/google/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>3.0.0</version>
<version>3.3.75</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion examples/java/gradle-shared-module/common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ application {

dependencies {
api 'com.hashicorp:cdktf:0.1.0'
api 'software.constructs:constructs:3.0.0'
api 'software.constructs:constructs:3.3.75'
}
2 changes: 1 addition & 1 deletion examples/java/kubernetes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>3.0.0</version>
<version>3.3.75</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion examples/java/ucloud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>software.constructs</groupId>
<artifactId>constructs</artifactId>
<version>3.0.0</version>
<version>3.3.75</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/aws-cloudfront-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/aws-multiple-stacks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"cdktf-cli": "0.0.0"
},
"dependencies": {
"constructs": "^3.0.0",
"constructs": "^3.3.75",
"cdktf": "0.0.0"
}
}
2 changes: 1 addition & 1 deletion examples/typescript/aws-prebuilt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"dependencies": {
"@cdktf/provider-aws": "^0.0.19",
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
}
}
2 changes: 1 addition & 1 deletion examples/typescript/azure-app-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.27",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/azure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/backends/azurerm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/backends/gcs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/backends/remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
6 changes: 5 additions & 1 deletion examples/typescript/backends/s3/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import {
S3Backend,
DataTerraformRemoteStateS3,
} from "cdktf";
import { DataAwsS3BucketObject } from "./.gen/providers/aws";
import { AwsProvider, DataAwsS3BucketObject } from "./.gen/providers/aws";

class MyStack extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);

new AwsProvider(this, "aws", {
region: "eu-central-1",
});

// Only one backend is supported by Terraform

// S3 Backend - https://www.terraform.io/docs/backends/types/s3.html
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/backends/s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/docker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/google-cloudrun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.27",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/google/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/kubernetes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/ucloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript/vault/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"dependencies": {
"cdktf": "0.0.0",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
2 changes: 1 addition & 1 deletion packages/@cdktf/hcl2cdk/test/convertProject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ app.synth();`,
},
"dependencies": {
"cdktf": "*",
"constructs": "^3.0.0"
"constructs": "^3.3.75"
},
"devDependencies": {
"@types/node": "^14.0.26",
Expand Down
49 changes: 48 additions & 1 deletion packages/cdktf/lib/terraform-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Construct, IConstruct, ISynthesisSession, Node } from "constructs";
import { resolve } from "./_tokens";
import * as fs from "fs";
import * as path from "path";
import camelcase = require("camelcase"); // ES module interop
ansgarm marked this conversation as resolved.
Show resolved Hide resolved

import { TerraformElement } from "./terraform-element";
import { deepMerge } from "./util";
import { TerraformProvider } from "./terraform-provider";
Expand All @@ -14,6 +16,9 @@ import { Manifest } from "./manifest";

const STACK_SYMBOL = Symbol.for("ckdtf/TerraformStack");

const pascalCase = (str: string) =>
camelcase(str.replace(/[-/]/g, "_"), { pascalCase: true });

export interface TerraformStackMetadata {
readonly stackName: string;
readonly version: string;
Expand All @@ -28,6 +33,39 @@ export class TerraformStack extends Construct {
super(scope, id);

this.cdktfVersion = Node.of(this).tryGetContext("cdktfVersion");
Node.of(this).addValidation({
validate: () => {
const errors = [];
const tfConfig = this.toTerraform();

// validate provider has been initialized
const requiredProviders = [
...new Set(
Object.keys({ ...tfConfig?.resource, ...tfConfig?.data }).map(
(providerName) => providerName.split("_")[0]
)
),
];

const providers = Object.keys(tfConfig?.provider || {});

const uninitializedProviders = requiredProviders.filter(
(reqProvider) =>
!providers.includes(reqProvider) && reqProvider !== "terraform" // e.g. terraform_remote_state
);
ansgarm marked this conversation as resolved.
Show resolved Hide resolved

if (uninitializedProviders.length > 0) {
errors.push(
`Could not find provider initialization for provider ${uninitializedProviders.join(
" & "
)}. Please initialize the provider(s) as ${uninitializedProviders
.map((p) => pascalCase(p + "Provider"))
.join(", ")}`
);
}
return errors;
},
});

Object.defineProperty(this, STACK_SYMBOL, { value: true });
}
Expand Down Expand Up @@ -172,6 +210,13 @@ export class TerraformStack extends Construct {
}

protected onSynthesize(session: ISynthesisSession) {
const errors = Node.of(this).validate();
if (errors.length > 0) {
throw new Error(`${errors.length} Error found in stack:

${errors.map((err) => `${err.source}: ${err.message}`).join("\n")}`);
}

const manifest = session.manifest as Manifest;
const stackManifest = manifest.forStack(this);

Expand All @@ -181,9 +226,11 @@ export class TerraformStack extends Construct {
);
if (!fs.existsSync(workingDirectory)) fs.mkdirSync(workingDirectory);

const tfConfig = this.toTerraform();

fs.writeFileSync(
path.join(session.outdir, stackManifest.synthesizedStackPath),
JSON.stringify(this.toTerraform(), undefined, 2)
JSON.stringify(tfConfig, undefined, 2)
);
}
}
Expand Down
10 changes: 9 additions & 1 deletion packages/cdktf/lib/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ export class Testing {
* Returns the Terraform synthesized JSON.
*/
public static synth(stack: TerraformStack) {
return JSON.stringify(stack.toTerraform(), null, 2);
const errors = Node.of(stack).validate();
if (errors.length > 0) {
throw new Error(`${errors.length} Error found in stack:

${errors.map((err) => `${err.source}: ${err.message}`).join("\n")}`);
}
const tfConfig = stack.toTerraform();

return JSON.stringify(tfConfig, null, 2);
}

/* istanbul ignore next */
Expand Down
13 changes: 9 additions & 4 deletions packages/cdktf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@
},
"license": "MPL-2.0",
"devDependencies": {
"@types/camelcase": "^5.2.0",
"@types/jest": "^25.1.2",
"@types/node": "^14.0.26",
"@typescript-eslint/eslint-plugin": "^4.28.1",
"@typescript-eslint/parser": "^4.28.1",
"constructs": "^3.3.75",
"eslint": "^7.29.0",
"jest": "^26.6.3",
"jsii": "^1.29.0",
"jsii-pacmak": "^1.29.0",
"jest": "^26.6.3",
"json-schema-to-typescript": "^8.0.1",
"typescript": "^3.9.7"
},
Expand All @@ -98,11 +99,15 @@
]
},
"dependencies": {
"archiver": "5.3.0"
"archiver": "5.3.0",
"camelcase": "^6.2.0"
},
"bundledDependencies": ["archiver"],
"bundledDependencies": [
"archiver",
"camelcase"
],
"stability": "experimental",
"peerDependencies": {
"constructs": "^3.3.75"
}
}
}
Loading