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

feat(provider-generator): handle 'tuple' type for variable #2964

Merged

Conversation

kination
Copy link
Contributor

@kination kination commented Jun 24, 2023

Related issue

Fixes #1618

Description

Allow tuple for variable type

Checklist

  • I have updated the PR title to match CDKTF's style guide
  • I have run the linter on my code locally
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation if applicable
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works if applicable
  • New and existing unit tests pass locally with my changes

@kination kination requested a review from a team as a code owner June 24, 2023 04:24
@kination kination requested review from mutahhir and DanielMSchmidt and removed request for a team June 24, 2023 04:24
@kination kination changed the title chore: read tuple type for variable [WIP] chore: read tuple type for variable Jun 24, 2023
@kination kination mentioned this pull request Jun 24, 2023
Copy link
Collaborator

@jsteinich jsteinich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a shot at this. Need to make a few adjustments and then you should be good to add some test cases.

@kination kination changed the title [WIP] chore: read tuple type for variable chore: read tuple type for variable Jun 27, 2023
@kination
Copy link
Contributor Author

@jsteinich I've make few change, and trying to make test case in https://github.com/hashicorp/terraform-cdk/blob/main/packages/@cdktf/provider-generator/lib/get/__tests__/generator/module-generator.test.ts

(I've put test tf file inside fixtures/local-variable/variable.tf)

test("check valid data types", () => {
  const code = new CodeMaker();
  const module = new TerraformModuleConstraint({
    name: "local_module",
    fqn: "local_module",
    source: path.resolve(__dirname, "fixtures", "local-variable"),
  });
  const target = new ConstructsMakerModuleTarget(module, Language.TYPESCRIPT);
  new ModuleGenerator(code, [target])
...
})

I'm trying with following https://github.com/hashicorp/terraform-cdk/blob/main/packages/%40cdktf/provider-generator/package.json#L15
but test does not goes on...

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
      at Object.<anonymous> (../hcl2json/wasm/bridge_wasm_exec.js:14:28)
      at ../hcl2json/lib/bridge.ts:39:5
      at init (../hcl2json/lib/bridge.ts:39:5)


 RUNS  lib/get/__tests__/generator/module-generator.test.ts
 RUNS  lib/get/__tests__/read-schema.test.ts

Test Suites: 14 failed, 14 of 16 total
Tests:       14 failed, 40 passed, 54 total
Snapshots:   61 passed, 61 total
Time:        114 s

Could you give suggestion how to make testing for this?

@jsteinich
Copy link
Collaborator

Could you give suggestion how to make testing for this?

Might just need to reinstall dependencies (yarn install) or deleting a package lock and node_modules directory and trying that.

You can also likely simplify your test to just use expectModuleToMatchSnapshot.

@kination
Copy link
Contributor Author

kination commented Jul 1, 2023

@jsteinich thanks.
Also, could you give advise how to initialize module generator?
I'm trying as following:
(keeping test tf file inside fixtures/local-variable/variable.tf)

test("check valid data types", () => {
  const code = new CodeMaker();
  const module = new TerraformModuleConstraint({
    name: "local_module",
    fqn: "local_module",
    source: path.resolve(__dirname, "fixtures", "local-variable"),
  });
  const target = new ConstructsMakerModuleTarget(module, Language.TYPESCRIPT);
  new ModuleGenerator(code, [target])
...
})

but it failed as:

...
    missing spec for local_module

      24 |
      25 |     if (!spec) {
    > 26 |       throw new Error(`missing spec for ${target.fqn}`);
         |             ^
      27 |     }
      28 |
      29 |     this.code.openFile(target.fileName);

      at ModuleGenerator.emitSubmodule (lib/get/generator/module-generator.ts:26:13)
      at new ModuleGenerator (lib/get/generator/module-generator.ts:17:12)
      at Object.<anonymous> (lib/get/__tests__/generator/module-generator.test.ts:140:3)

Thanks.

@jsteinich
Copy link
Collaborator

Also, could you give advise how to initialize module generator?

ModuleGenerator is not something you're likely to ever directly create. The tests have some useful functions to simply module generation. For example:

expectModuleToMatchSnapshot("tuple type", "generator", [
  "local-variable/variable.tf",
]);

@kination
Copy link
Contributor Author

kination commented Jul 8, 2023

@jsteinich I've just added variable inside existing template.
But it shows following error:

...
   Snapshot name: `getX variables 1`

    - Snapshot  - 1
    + Received  + 1

    @@ -8,11 +8,11 @@
         * @default true
         */
        readonly fetchCallerIdentity?: boolean;
        /**
         * RDS DB subnet group resource
    -    * @default [["172.0.0.1", "172.0.0.2", 1],["172.0.10.1", "172.0.10.2", 2]]
    +    * @default 172.0.0.1,172.0.0.2,1,172.0.10.1,172.0.10.2,2
         */
        readonly fetchSubnetIds?: any[][];
...

What I've expect in comment part should be [["172.0.0.1", "172.0.0.2", 1],["172.0.10.1", "172.0.10.2", 2]], but result shows 172.0.0.1,172.0.0.2,1,172.0.10.1,172.0.10.2,2. Could you confirm this part?

@jsteinich
Copy link
Collaborator

What I've expect in comment part should be [["172.0.0.1", "172.0.0.2", 1],["172.0.10.1", "172.0.10.2", 2]], but result shows 172.0.0.1,172.0.0.2,1,172.0.10.1,172.0.10.2,2. Could you confirm this part?

That's certainly not what it should be, but I don't believe anything in the PR introduced that behavior. Can make another issue to keep track of that.

@kination
Copy link
Contributor Author

@jsteinich thanks for notice.

Anyway, this part makes test failure, so it couldn't pass the checks. So options I can think is...

How do you think?

@jsteinich
Copy link
Collaborator

@jsteinich thanks for notice.

Anyway, this part makes test failure, so it couldn't pass the checks. So options I can think is...

How do you think?

I think it makes sense to just update the snapshot so that it passes. Can leave a comment on the test case about it.
@DanielMSchmidt does that sound good to you?

@kination kination changed the title chore: read tuple type for variable feat(provider-generator): handle 'tuple' type for variable Jul 12, 2023
@ansgarm
Copy link
Member

ansgarm commented Jul 21, 2023

Updating the snapshot would be fine with me if we also create an issue to improve the generator to fix how these defaults are rendered 👍
@kination @jsteinich

@kination
Copy link
Contributor Author

@ansgarm @jsteinich thanks for suggestion. Than I'll go on with this.

https://github.com/hashicorp/terraform-cdk/actions/runs/5628083685/job/15251540738?pr=2964
=> I'm not sure why this cause failure. It works well when I run it locally. Could you check?

@jsteinich
Copy link
Collaborator

https://github.com/hashicorp/terraform-cdk/actions/runs/5628083685/job/15251540738?pr=2964
=> I'm not sure why this cause failure. It works well when I run it locally. Could you check?

Looks like this got broken with #2990. You might need to pull in latest main to see the issue locally. Can probably lump it into a follow-up issue to fix.

@kination
Copy link
Contributor Author

@jsteinich could you confirm following once more?
https://github.com/hashicorp/terraform-cdk/actions/runs/5640398393/job/15276937617?pr=2964
Seems template shows same result, but checker indicates as diff ... (some encoding issue?)

@jsteinich
Copy link
Collaborator

@jsteinich could you confirm following once more? https://github.com/hashicorp/terraform-cdk/actions/runs/5640398393/job/15276937617?pr=2964 Seems template shows same result, but checker indicates as diff ... (some encoding issue?)

I would guess a line ending issue.

@kination
Copy link
Contributor Author

@jsteinich I've tried to change, but git cannot find diff of this.
Could you give me suggestion how to go on with this?

@kination
Copy link
Contributor Author

@jsteinich thanks for update. But seems still there are failures on check. Could you confirm whether these are related with this update?

@ansgarm
Copy link
Member

ansgarm commented Jul 28, 2023

Hi @kination 👋

fyi, I just rebased your branch onto main as that should hopefully fix the CI errors you are seeing ✌️

@kination
Copy link
Contributor Author

@ansgarm thanks for your help!
Seems now 1 has left (Vercel authorization). Please let me know if I need to follow-up something for this ... 🤔

@ansgarm
Copy link
Member

ansgarm commented Jul 28, 2023

Oh, yeah, missed that. That's just something we have to click for third party PRs 😅

@DanielMSchmidt DanielMSchmidt added this pull request to the merge queue Aug 4, 2023
Merged via the queue into hashicorp:main with commit 21dcf29 Aug 4, 2023
44 checks passed
@mutahhir mutahhir mentioned this pull request Aug 17, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2023

I'm going to lock this pull request because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unknown type tuple
5 participants