question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Add back classes and interfaces published in earlier `cdktf-cli` versions

See original GitHub issue

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

Description

After upgrading from 7.0.0 to 8.0.0 Iā€™ve noticed that the published cdktf-cli NPM package no longer contains the cdktf-cli/bin/cmds/ui/models/terraform directory where classes like AbstractTerraformPlan lived.

Is it possible that you can add them back?

The reason why I need this is that Iā€™ve written a policy framework for Terraform CDK that includes the ability to validate changes to Terraform plans.

I have a very specific use case where I want a differentiation between the resource changes from before & after.


import { promises as fs } from 'fs';
import { Delta, diff } from '@n1ru4l/json-patch-plus';
import { TerraformResource } from 'cdktf';
import is from '@sindresorhus/is';
// These are the interfaces and classes I need
import {
  PlannedResourceAction,
  AbstractTerraformPlan,
  PlannedResource,
  ResourceChanges,
} from 'cdktf-cli/bin/cmds/ui/models/terraform';

const camelCaseObjectDeep = require('camelcase-object-deep');

export interface PlanFile {
  readonly resource_changes: ResourceChanges[];
}

export { PlannedResourceAction };

export interface TerraformPlanResourceChange<R extends TerraformResource>
  extends ResourceChanges,
    PlannedResource {
  readonly difference: Delta;
}

export class TerraformPlan extends AbstractTerraformPlan {
  readonly #resourceChanges: readonly ResourceChanges[];

  static async create(planFile: string | PlanFile): Promise<TerraformPlan> {
    const plan = is.string(planFile)
      ? (JSON.parse(await fs.readFile(planFile, 'utf8')) as PlanFile)
      : planFile;
    return new TerraformPlan(plan);
  }

  constructor(plan: PlanFile) {
    super('', plan.resource_changes as ResourceChanges[], {});
    this.#resourceChanges = plan.resource_changes;
  }

  getApplicableResource(id: string): PlannedResource | undefined {
    return this.applyableResources.find(r => r.id === id);
  }

  hasApplicableResource(id: string): boolean {
    return this.applyableResources.some(r => r.id === id);
  }

  getResourceChanges(): readonly TerraformPlanResourceChange<any>[] {
    return this.#resourceChanges
      .filter(({ address }) => this.hasApplicableResource(address))
      .map(resource => {
        const before = camelCaseObjectDeep(resource.change.before);
        const after = camelCaseObjectDeep(resource.change.after);

        return {
          ...resource,
          difference: diff(
            { left: before, right: after },
            { includePreviousValue: true },
          ),
          change: {
            ...resource.change,
            before,
            after,
          },
          ...this.getApplicableResource(resource.address)!,
        };
      });
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
marcus-sacommented, Dec 3, 2021

@skorfmann if you or anyone else is interested, Iā€™ve made the code open source. Iā€™ve extracted most of it for now into a public repository https://github.com/marcus-sa/terraform-cdk-extras Will most likely wrap it up and publish them as separate NPM packages sometime next week.

1reaction
skorfmanncommented, Dec 2, 2021

@marcus-sa thanks for creating this issue, please have a look at https://github.com/hashicorp/terraform-cdk/pull/1379 which I believe would solve your issue. Could you confirm, please?

As mentioned there, the mid term fix is more along the lines https://github.com/hashicorp/terraform-cdk/pull/804 šŸ˜ƒ

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Your Preferred Programming Language with CDKTF
HashiCorp Terraform now supports writing configurations in HCL or your choice of C#, Python, TypeScript, Java, or Go with Cloud DevelopmentĀ ...
Read more >
HashiCorp Live: CDK for Terraform - YouTube
Join Taylor Dolezal, Anubhav Mishra, and CDK for Terraform contributor Sebastian Korfmann as they provision infrastructure resources inĀ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found