-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): detect Terraform resource name
- Loading branch information
1 parent
9ef7d9e
commit 58327d6
Showing
13 changed files
with
166 additions
and
23 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
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,12 @@ | ||
import { register } from '../../rules.js'; | ||
|
||
register({ | ||
tech: 'gcp.cloudrun', | ||
dependencies: [ | ||
{ type: 'npm', name: '@google-cloud/run' }, | ||
{ | ||
type: 'terraform.resource', | ||
name: 'google_cloud_run_v2_service', | ||
}, | ||
], | ||
}); |
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,12 @@ | ||
import { register } from '../../rules.js'; | ||
|
||
register({ | ||
tech: 'gcp.pubsub', | ||
dependencies: [ | ||
{ type: 'npm', name: '@google-cloud/pubsub' }, | ||
{ | ||
type: 'terraform.resource', | ||
name: 'google_pubsub_topic', | ||
}, | ||
], | ||
}); |
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 +1,2 @@ | ||
import './rabbitmq.js'; | ||
import './gcp.pubsub.js'; |
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,10 +1,11 @@ | ||
import { register } from '../../../rules.js'; | ||
|
||
import { detectTerraformComponent } from './component.js'; | ||
import { detectTerraformLockfile } from './lockfile.js'; | ||
import { detectTerraformResource } from './resource.js'; | ||
|
||
register({ | ||
tech: 'terraform', | ||
files: ['.terraform', '.terraform.lock.hcl', 'main.tf', 'variables.tf'], | ||
dependencies: [], | ||
detect: detectTerraformComponent, | ||
detect: [detectTerraformLockfile, detectTerraformResource], | ||
}); |
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,67 @@ | ||
import path from 'node:path'; | ||
|
||
import { parse } from '@cdktf/hcl2json'; | ||
|
||
import { listIndexed } from '../../../common/techs.js'; | ||
import { Payload } from '../../../payload/index.js'; | ||
import { detect } from '../../../rules.js'; | ||
import type { ComponentMatcher } from '../../../types/rule.js'; | ||
|
||
const FILE = /.tf$/; | ||
|
||
export const detectTerraformResource: ComponentMatcher = async ( | ||
files, | ||
provider | ||
) => { | ||
for (const file of files) { | ||
if (!FILE.test(file.name)) { | ||
continue; | ||
} | ||
|
||
const content = await provider.open(file.fp); | ||
if (!content) { | ||
continue; | ||
} | ||
|
||
let json: Record<string, any>; | ||
try { | ||
json = await parse(file.fp, content); | ||
} catch (err) /* istanbul ignore next */ { | ||
console.warn('Failed to parse HCL', err); | ||
return false; | ||
} | ||
|
||
if (!('resource' in json)) { | ||
return false; | ||
} | ||
|
||
const pl = new Payload({ | ||
name: 'virtual', | ||
folderPath: path.dirname(file.fp), | ||
}); | ||
|
||
// We only register docker service with image and that we know | ||
for (const name of Object.keys(json.resource)) { | ||
const matched = [...detect([name], 'terraform.resource')]; | ||
if (!matched.length) { | ||
continue; | ||
} | ||
|
||
const tech = matched[0]; | ||
|
||
pl.addChild( | ||
new Payload({ | ||
name: listIndexed[tech].name, | ||
folderPath: file.fp, | ||
tech, | ||
parent: pl, | ||
dependencies: [['terraform.resource', name, 'unknown']], | ||
}) | ||
); | ||
} | ||
|
||
return pl; | ||
} | ||
|
||
return false; | ||
}; |
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