-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: upgrade to cdktf 0.17 and tsx (#199)
Closes hashicorp/terraform-cdk#2549
- Loading branch information
1 parent
e3dbc75
commit 81132b2
Showing
11 changed files
with
2,461 additions
and
795 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"language": "typescript", | ||
"app": "npm run --silent compile && node main.js", | ||
"app": "npx tsx main.ts", | ||
"projectId": "46fb4da5-e0c7-486d-aba8-24e91527e550" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
import { TerraformElement } from "cdktf"; | ||
import { Node } from "constructs"; | ||
import { makeUniqueId } from "./unique-id-override"; | ||
|
||
function allocateLogicalId(tfElement: TerraformElement | Node): string { | ||
const node = TerraformElement.isTerraformElement(tfElement) | ||
? tfElement.node | ||
: tfElement; | ||
|
||
// This is the previous behavior, which we want for now. | ||
const stackIndex = 0; | ||
|
||
const components = node.scopes.slice(stackIndex + 1).map((c) => c.node.id); | ||
return components.length > 0 ? makeUniqueId(components, false) : ""; | ||
} | ||
|
||
export function setOldId(tfElement: TerraformElement): void { | ||
tfElement.overrideLogicalId(allocateLogicalId(tfElement)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright (c) HashiCorp, Inc | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
/** | ||
* We have to copy this from the old CDKTF version for now, so that we can | ||
* calculate the old logical IDs for now and for later for the migration. | ||
*/ | ||
|
||
// tslint:disable-next-line:no-var-requires | ||
import * as crypto from "crypto"; | ||
|
||
/** | ||
* Resources with this ID are hidden from humans | ||
* | ||
* They do not appear in the human-readable part of the logical ID, | ||
* but they are included in the hash calculation. | ||
*/ | ||
const HIDDEN_FROM_HUMAN_ID = "Resource"; | ||
|
||
/** | ||
* Resources with this ID are complete hidden from the logical ID calculation. | ||
*/ | ||
const HIDDEN_ID = "Default"; | ||
|
||
const PATH_SEP = "/"; | ||
const UNIQUE_SEP = "_"; | ||
|
||
const HASH_LEN = 8; | ||
const MAX_HUMAN_LEN = 240; // max ID len is 255 | ||
const MAX_ID_LEN = 255; | ||
|
||
/** | ||
* Calculates a unique ID for a set of textual components. | ||
* | ||
* This is done by calculating a hash on the full path and using it as a suffix | ||
* of a length-limited "human" rendition of the path components. | ||
* | ||
* @param components The path components | ||
* @returns a unique alpha-numeric identifier with a maximum length of 255 | ||
*/ | ||
export function makeUniqueId(components: string[], allowSepChars: boolean) { | ||
components = components.filter((x) => x !== HIDDEN_ID); | ||
|
||
if (components.length === 0) { | ||
throw new Error( | ||
"Unable to calculate a unique id for an empty set of components" | ||
); | ||
} | ||
|
||
// top-level resources will simply use the `name` as-is in order to support | ||
// transparent migration of cloudformation templates to the CDK without the | ||
// need to rename all resources. | ||
if (components.length === 1) { | ||
// we filter out non-alpha characters but that is actually a bad idea | ||
// because it could create conflicts ("A-B" and "AB" will render the same | ||
// logical ID). sadly, changing it in the 1.x version line is impossible | ||
// because it will be a breaking change. we should consider for v2.0. | ||
// https://github.com/aws/aws-cdk/issues/6421 | ||
const candidate = removeDisallowedCharacters(components[0], allowSepChars); | ||
|
||
// if our candidate is short enough, use it as is. otherwise, fall back to | ||
// the normal mode. | ||
if (candidate.length <= MAX_ID_LEN) { | ||
return candidate; | ||
} | ||
} | ||
|
||
const hash = pathHash(components); | ||
const human = removeDupes(components) | ||
.filter((x) => x !== HIDDEN_FROM_HUMAN_ID) | ||
.map((s) => removeDisallowedCharacters(s, allowSepChars)) | ||
.join(UNIQUE_SEP) | ||
.slice(0, MAX_HUMAN_LEN); | ||
|
||
return human + UNIQUE_SEP + hash; | ||
} | ||
|
||
/** | ||
* Take a hash of the given path. | ||
* | ||
* The hash is limited in size. | ||
*/ | ||
function pathHash(path: string[]): string { | ||
const md5 = crypto | ||
.createHash("md5") | ||
.update(path.join(PATH_SEP)) | ||
.digest("hex"); | ||
return md5.slice(0, HASH_LEN).toUpperCase(); | ||
} | ||
|
||
function removeDisallowedCharacters(s: string, allowSepChars: boolean) { | ||
if (allowSepChars) { | ||
return removeNonAlphanumericSep(s); | ||
} else { | ||
return removeNonAlphanumeric(s); | ||
} | ||
} | ||
|
||
/** | ||
* Removes all non-alphanumeric characters in a string. | ||
*/ | ||
function removeNonAlphanumeric(s: string) { | ||
return s.replace(/[^A-Za-z0-9]/g, ""); | ||
} | ||
|
||
function removeNonAlphanumericSep(s: string) { | ||
return s.replace(/[^A-Za-z0-9_-]/g, ""); | ||
} | ||
|
||
/** | ||
* Remove duplicate "terms" from the path list | ||
* | ||
* If the previous path component name ends with this component name, skip the | ||
* current component. | ||
*/ | ||
function removeDupes(path: string[]): string[] { | ||
const ret = new Array<string>(); | ||
|
||
for (const component of path) { | ||
if (ret.length === 0 || !ret[ret.length - 1].endsWith(component)) { | ||
ret.push(component); | ||
} | ||
} | ||
|
||
return ret; | ||
} |
Oops, something went wrong.