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: Add terraform_graph hook #589

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

jrdnbradford
Copy link

Put an x into the box if that apply:

  • This PR introduces breaking change.
  • This PR fixes a bug.
  • This PR adds new functionality.
  • This PR enhances existing functionality.

Description of your changes

This adds a terraform_graph hook that generates charts using terraform graph and GraphViz. My org finds something like this helpful to upkeep documentation.

Test results

200 runs 'terraform_graph v0.0.1:'

time command max min mean median
users seconds 14.71 9.51 13.6592 13.84
system seconds 1.48 0.95 1.28115 1.29
CPU % 415 149 371.545 377
Total time 8.71 3.16 4.054 3.99
Run details
  • Test Start: Mon 11 Dec 2023 04:41:22 PM UTC
  • Test End: Mon 11 Dec 2023 04:54:54 PM UTC
Variable name Value
TEST_NUM 200
TEST_COMMAND pre-commit try-repo -a /home/$user/Desktop/pre-commit-terraform terraform_graph
TEST_DIR /home/$user/$repo
TEST_DESCRIPTION 200 runs 'terraform_graph v0.0.1:'
RAW_TEST_RESULTS_FILE_NAME terraform_graph_results

Memory info (head -n 6 /proc/meminfo):

MemTotal:       32519148 kB
MemFree:         8189176 kB
MemAvailable:   27378808 kB
Buffers:          571396 kB
Cached:         14010628 kB
SwapCached:          120 kB

CPU info:

Real procs: 4
Virtual (hyper-threading) procs: 8
processor	: 7
vendor_id	: GenuineIntel
cpu family	: 6
model		: 142
model name	: Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
stepping	: 12
microcode	: 0xf8
cpu MHz		: 3400.063
cache size	: 8192 KB
physical id	: 0
siblings	: 8
core id		: 3
cpu cores	: 4
apicid		: 7
initial apicid	: 7
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d arch_capabilities
vmx flags	: vnmi preemption_timer invvpid ept_x_only ept_ad ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple shadow_vmcs pml ept_mode_based_exec
bugs		: spectre_v1 spectre_v2 spec_store_bypass swapgs taa itlb_multihit srbds mmio_stale_data retbleed eibrs_pbrsb gds
bogomips	: 4599.93
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:

How can we test changes

Testing can be performed by running against a repo with *.tf files and ensuring that the correct *.svg files are output in each directory:

- repo: https://github.com/jrdnbradford/pre-commit-terraform
  rev: add-graph-hook
  hooks:
    - id: terraform_graph
      args:
      - --args=-type=plan-destroy
      - --hook-config=--path-to-file=test-config.svg

The most important configs for the hook are -type and a hook config --path-to-file that sets the name of the output *.svg.

I cannot say that I'm a particularly good shell scripter, so if there's interest in adding this please feel free to edit.

@jrdnbradford jrdnbradford changed the title Add terraform_graph hook feat: Add terraform_graph hook Dec 11, 2023
@MaxymVlasov MaxymVlasov added the feature New feature or request label Dec 11, 2023
Copy link
Collaborator

@MaxymVlasov MaxymVlasov left a comment

Choose a reason for hiding this comment

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

Please add hook and it deps to Dockerfile

And double-check https://github.com/antonbabenko/pre-commit-terraform/blob/master/.github/CONTRIBUTING.md#add-new-hook - you could miss something else

local -a -r args=("$@")

if [[ ! $(command -v dot) ]]; then
echo "ERROR: dot is required by terraform_graph pre-commit hook but is not installed or in the system's PATH."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add some colors

Suggested change
echo "ERROR: dot is required by terraform_graph pre-commit hook but is not installed or in the system's PATH."
common::colorify "red" "ERROR: dot is required by terraform_graph pre-commit hook but is not installed or in the system's PATH."

@@ -926,6 +930,16 @@ If the generated name is incorrect, set them by providing the `module-repo-short
Check [`tfupdate` usage instructions](https://github.com/minamijoyo/tfupdate#usage) for other available options and usage examples.
No need to pass `--recursive .` as it is added automatically.

### terraform_graph
`terraform_graph` utilizes Terraform's built-in [`graph` command](https://developer.hashicorp.com/terraform/cli/commands/graph) and `dot` from [GraphViz](https://www.graphviz.org/) to generate charts of your configuration.

Copy link
Collaborator

@MaxymVlasov MaxymVlasov Dec 11, 2023

Choose a reason for hiding this comment

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

Would be nice to have an example image of resulting graph here

You can populate it inside assets/ dir

@jrdnbradford
Copy link
Author

@MaxymVlasov, awesome, I'll work on this. Thanks for your help.

@MaxymVlasov
Copy link
Collaborator

@jrdnbradford howdy. Do you plan to continue working on this PR? Do you need any help from our side?

@leandroscardua
Copy link

I’m really crossing my fingers that @jrdnbradford pulls off this new feature! . I'm looking forward to using it.
and share the new feature with my team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants