-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(terraform): Parse and auto detect TF module dependencies (#298)
It could be tedious to manually manage all terraform module calls as dependencies for larger projects. This parses the HCL source to identify and add direct static dependencies to the NX dependency graph.
- Loading branch information
Showing
5 changed files
with
298 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
CreateDependencies, | ||
logger, | ||
RawProjectGraphDependency, | ||
workspaceRoot | ||
} from '@nx/devkit' | ||
import * as hcl2JsonParser from 'hcl2-json-parser' | ||
import * as fs from 'node:fs/promises' | ||
import * as path from 'node:path' | ||
import { DependencyType } from 'nx/src/config/project-graph' | ||
|
||
const isLocalPath = (path: string) => { | ||
return path.startsWith('./') || path.startsWith('../') | ||
} | ||
|
||
export const createDependencies: CreateDependencies = async (_, ctx) => { | ||
const results: RawProjectGraphDependency[] = [] | ||
|
||
const projectRootsToProject: [projectRoot: string, name: string][] = | ||
Object.entries(ctx.projects).map(([name, project]) => [project.root, name]) | ||
|
||
for (const project of Object.keys(ctx.projects)) { | ||
// find tf files to process in the project | ||
const tfFilesToProcess = | ||
ctx.filesToProcess.projectFileMap[project]?.filter((file) => | ||
file.file.endsWith('.tf') | ||
) ?? [] | ||
|
||
for (const file of tfFilesToProcess) { | ||
const data = await fs.readFile(file.file) | ||
|
||
let parsed: hcl2JsonParser.HclDef | ||
|
||
try { | ||
parsed = await hcl2JsonParser.parseToObject(data.toString()) | ||
} catch (e) { | ||
logger.warn( | ||
`Failed to parse .tf file ${file.file}. Error: ${e.message}` | ||
) | ||
continue | ||
} | ||
|
||
for (const moduleCall of Object.values(parsed.module ?? [])) { | ||
const depSourcePathRel = moduleCall[0]?.source | ||
|
||
if (!isLocalPath(depSourcePathRel)) { | ||
continue | ||
} | ||
|
||
const depSourceAbs = path.resolve(file.file, depSourcePathRel) | ||
|
||
const depSourceRelativeToWorkspace = path.relative( | ||
workspaceRoot, | ||
depSourceAbs | ||
) | ||
|
||
const targetProject = projectRootsToProject.find(([root]) => | ||
depSourceRelativeToWorkspace.startsWith(root) | ||
)?.[1] | ||
|
||
if (!targetProject || targetProject === project) { | ||
continue | ||
} | ||
|
||
results.push({ | ||
type: DependencyType.static, | ||
source: project, | ||
target: targetProject, | ||
sourceFile: file.file | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return results | ||
} |
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,27 @@ | ||
declare module 'hcl2-json-parser' { | ||
export type HclDef = { | ||
data?: { | ||
[data: string]: { | ||
[dataName: string]: [{ [propertyName: string]: any }] | ||
} | ||
} | ||
|
||
resource?: { | ||
[resource: string]: { | ||
[resourceName: string]: [{ [propertyName: string]: any }] | ||
} | ||
} | ||
|
||
terraform?: [ | ||
{ | ||
backend?: [{ [propertyName: string]: any }] | ||
} | ||
] | ||
|
||
module?: { | ||
[moduleName: string]: [{ source: string }] | ||
} | ||
} | ||
|
||
export function parseToObject(data: string): Promise<HclDef> | ||
} |
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 @@ | ||
export * from './graph' |
Oops, something went wrong.