Skip to content

Commit

Permalink
chore: integrated feedback from ansgar, made clear workflow for confi…
Browse files Browse the repository at this point in the history
…g generation, updated docs
  • Loading branch information
Maed223 committed Sep 26, 2023
1 parent 2e84867 commit afdd7e8
Show file tree
Hide file tree
Showing 20 changed files with 1,387 additions and 126 deletions.
2 changes: 1 addition & 1 deletion examples/typescript/aws-import/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class StackWithImportAndConfigurationGeneration extends TerraformStack {
});

// Step 2: Create import block
S3Bucket.importGenerateConfig(this, "bucket", bucketId);
S3Bucket.generateConfigForImport(this, "bucket", bucketId);

// Step 3: Run `cdktf plan` and get the configuration to put in below
// Step 4: Remove the `import` call, the resource is now imported
Expand Down
17 changes: 13 additions & 4 deletions packages/@cdktf/cli-core/src/lib/cdktf-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { extractJsonLogIfPresent } from "./server/terraform-logs";
import {
TerraformCli,
OutputFilter,
findGeneratedConfigurationFile,
tryReadGeneratedConfigurationFile,
tryRemoveGeneratedConfigurationFile,
} from "./models/terraform-cli";
import { ProviderConstraint } from "./dependencies/dependency-manager";
import { terraformJsonSchema, TerraformStack } from "./terraform-json";
Expand Down Expand Up @@ -379,7 +380,7 @@ export class CdktfStack {
this.updateState({ type: "planned", stackName: this.stack.name });

// Find generated file
const configFile = await findGeneratedConfigurationFile(
const configFile = await tryReadGeneratedConfigurationFile(
this.stack.workingDirectory
);
if (configFile) {
Expand All @@ -389,7 +390,10 @@ export class CdktfStack {
configuration: configFile,
});

const convertedCode = await convertConfigurationFile(configFile);
const convertedCode = await convertConfigurationFile(
configFile,
this.stack.workingDirectory
);
this.updateState({
type: "import with configuration converted",
stackName: this.stack.name,
Expand All @@ -406,10 +410,15 @@ CDKTF has translated the code to the following:
${convertedCode}
Please review the code and make any necessary changes before adding it to your codebase.
Make sure to only copy the code within the construct's constructor.`,
Make sure to only copy the code within the construct's constructor.
NOTE: Your resource has not yet become managed by CDKTF.
To finish the import remove the call "generateConfigForImport", add the above code within the construct's constructor, and then append the call importFrom({resource_id_to_import_from}) to the generated code.
`,
isError: false,
});
}
await tryRemoveGeneratedConfigurationFile(this.stack.workingDirectory);
}
});
}
Expand Down
7 changes: 5 additions & 2 deletions packages/@cdktf/cli-core/src/lib/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import {
ConstructsMakerProviderTarget,
} from "@cdktf/commons";

export async function convertConfigurationFile(configuration: string) {
const cfg = CdktfConfig.read(process.cwd()); // TODO: make this the project directory instead of cwd
export async function convertConfigurationFile(
configuration: string,
stackWorkingDirectory: string
) {
const cfg = CdktfConfig.read(stackWorkingDirectory); // TODO: make this the project directory instead of cwd
const targets = cfg.terraformProviders.map((constraint) =>
ConstructsMakerProviderTarget.from(
new TerraformProviderConstraint(constraint),
Expand Down
24 changes: 20 additions & 4 deletions packages/@cdktf/cli-core/src/lib/models/terraform-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class VariableRequiredFilter extends AbstractOutputFilter {

export class TerraformCli implements Terraform {
public readonly workdir: string;
static readonly generatedImportConfigFile: string = "generated_resources.tf";
private readonly onStdout: (
stateName: string,
filter?: OutputFilter[]
Expand Down Expand Up @@ -227,13 +228,15 @@ export class TerraformCli implements Terraform {

const generatedConfigFile = path.join(
this.workdir,
"generated_resources.tf"
TerraformCli.generatedImportConfigFile
);
if (fs.existsSync(generatedConfigFile)) {
fs.remove(generatedConfigFile);
}
if (this.hasImports) {
options.push("-generate-config-out=generated_resources.tf");
options.push(
`-generate-config-out=${TerraformCli.generatedImportConfigFile}`
);
}
if (!this.isCloudStack) {
const planFile = "plan";
Expand Down Expand Up @@ -481,12 +484,25 @@ export class TerraformCli implements Terraform {
}
}

export async function findGeneratedConfigurationFile(
export async function tryReadGeneratedConfigurationFile(
workingDir: string
): Promise<string | null> {
const generatedConfigPath = path.join(workingDir, "generated_resources.tf");
const generatedConfigPath = path.join(
workingDir,
TerraformCli.generatedImportConfigFile
);
if (!fs.existsSync(generatedConfigPath)) {
return null;
}
return fs.readFileSync(generatedConfigPath, "utf-8");
}

export async function tryRemoveGeneratedConfigurationFile(workingDir: string) {
const generatedConfigPath = path.join(
workingDir,
TerraformCli.generatedImportConfigFile
);
if (fs.existsSync(generatedConfigPath)) {
fs.unlinkSync(generatedConfigPath);
}
}
Loading

0 comments on commit afdd7e8

Please sign in to comment.