terraform-cdk icon indicating copy to clipboard operation
terraform-cdk copied to clipboard

hcl2cdk: example is broken

Open wandergeek opened this issue 2 years ago • 1 comments

Description

The example provided in the npm docs is broken: https://www.npmjs.com/package/@cdktf/hcl2cdk.

I got the simple inline example working like so. I'm unsure if this can be further simplified.

import * as hcl2cdk from "@cdktf/hcl2cdk";
import {readSchema} from "@cdktf/provider-schema";
import { ConstructsMakerProviderTarget, LANGUAGES, TerraformProviderConstraint } from "@cdktf/commons";

const hcl = `
  variable "name" {
    description = "Name to be used on all the resources as identifier"
    type        = string
    default     = ""
  }
`;

(async () => {
    const providerRequirements = await hcl2cdk.parseProviderRequirements(hcl);
    const targets = Object.entries(providerRequirements).map(([name, version]) =>
    ConstructsMakerProviderTarget.from(
      new TerraformProviderConstraint(`${name}@ ${version}`),
      LANGUAGES[0]
    )
  );
  const { providerSchema } = await readSchema(targets);
  const ts = await hcl2cdk.convert(hcl, { 
    language: "typescript",
    providerSchema: providerSchema
   });
  console.log(ts.imports); // just the necessary imports
  console.log(ts.code); // just the constructs
  console.log(ts.all); // code with imports
})();

It'd be excellent to update the docs indicating the split/rename of some of these fields and exports. I'm unsure how the project conversion works at this time, would appreciate some guidance here.

Links

https://www.npmjs.com/package/@cdktf/hcl2cdk

Help Wanted

  • [x] I'm interested in contributing a fix myself

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

wandergeek avatar Nov 27 '23 00:11 wandergeek

Hey, good point, if you got the capacity we'd love to receive a PR on this topic. This section is what is used for the project conversion :)

DanielMSchmidt avatar Dec 08 '23 09:12 DanielMSchmidt