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: allow using terratag as a validation tool - add dryrun #179

Closed
wants to merge 6 commits into from

Conversation

TomerHeber
Copy link
Collaborator

@TomerHeber TomerHeber commented Nov 13, 2023

Added a new dryrun mode that checks if all resources are tagged.
If a resource isn't tagged, it returns 1 exit code.

resolves #178

@TomerHeber TomerHeber self-assigned this Nov 13, 2023
@@ -59,6 +59,19 @@ func HasResourceTagFn(resourceType string) bool {
return resourceTypeToFnMap[resourceType] != nil
}

func HasTags(args TagBlockArgs) (bool, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Setting to beta because this function may need to handle more use-cases.
Basically just checks that the resource has tags.

}

func validate(args Args) error {
if args.Tags == "" {
if args.Tags == "" && !args.DryRun {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

adding tags is irrelevant for a dryrun.

taggedResources uint32
totalFiles uint32
taggedFiles uint32
untaggedResources uint32 // For dryrun.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

counts the number of untagged resources (if after dryrun there are still untagged resources - exit 1 is returned).

}

counters := tagDirectoryResources(taggingArgs)
log.Print("[INFO] Summary:")
log.Print("[INFO] Tagged ", counters.taggedResources, " resource/s (out of ", counters.totalResources, " resource/s processed)")
log.Print("[INFO] In ", counters.taggedFiles, " file/s (out of ", counters.totalFiles, " file/s processed)")
if args.DryRun {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

different messages for each mode.

}

if !args.DryRun {
hclMap, err := toHclMap(args.Tags)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

only valid when there are tags... not relevant for dryrun.

@@ -95,6 +95,7 @@ func TestTerragruntWithCache(t *testing.T) {
itShouldRunTerratagTerragruntMode(out, g)
itShouldRunTerragruntValidate(out, g)
itShouldGenerateExpectedTerragruntTerratagFiles(entryDir, g)
itShouldRunTerratagTerragruntModeDryRun(out, g)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updating the tests to make run a dry run at the end and verify it's resolved.
I'm not running a dry run at the beginning because some tests are not expected to be tagged.

@avnerenv0
Copy link
Contributor

@TomerHeber could you please add some screenshots here or something like that so I could understand the flow and it how it looks like to the user?
Thanks

@TomerHeber
Copy link
Collaborator Author

@TomerHeber could you please add some screenshots here or something like that so I could understand the flow and it how it looks like to the user? Thanks

@avnerenv0

➜  terratag git:(feat-add-dryrun-#178) ✗ ./terratag -dryrun -dir ./tf2
2023/11/20 07:49:31 [INFO] Processing file tf2/main.tf
2023/11/20 07:49:31 [INFO] Processing resource [azurerm_resource_group example]
2023/11/20 07:49:32 [INFO] Resource taggable, processing...[azurerm_resource_group example]
2023/11/20 07:49:32 [INFO] Resource [azurerm_resource_group example] is taggable and tagged
2023/11/20 07:49:32 [INFO] Processing resource [azurerm_virtual_network example]
2023/11/20 07:49:32 [INFO] Resource taggable, processing...[azurerm_virtual_network example]
2023/11/20 07:49:32 [INFO] Resource [azurerm_virtual_network example] is taggable and tagged
2023/11/20 07:49:32 [INFO] Summary:
2023/11/20 07:49:32 [INFO] Scanned 1 files
2023/11/20 07:49:32 [INFO] Detected 0 untagged resources (out of 2 resource/s)
➜  terratag git:(feat-add-dryrun-#178) ✗ echo $?
0
➜  terratag git:(feat-add-dryrun-#178) ✗ ./terratag -dryrun -dir ./tf2
2023/11/20 07:51:09 [INFO] Processing file tf2/main.tf
2023/11/20 07:51:09 [INFO] Processing resource [azurerm_resource_group example]
2023/11/20 07:51:10 [INFO] Resource taggable, processing...[azurerm_resource_group example]
2023/11/20 07:51:10 [WARN] Resource [azurerm_resource_group example] is taggable but does not have tags
2023/11/20 07:51:10 [INFO] Processing resource [azurerm_virtual_network example]
2023/11/20 07:51:10 [INFO] Resource taggable, processing...[azurerm_virtual_network example]
2023/11/20 07:51:10 [INFO] Resource [azurerm_virtual_network example] is taggable and tagged
2023/11/20 07:51:10 [INFO] Summary:
2023/11/20 07:51:10 [INFO] Scanned 1 files
2023/11/20 07:51:10 [INFO] Detected 1 untagged resources (out of 2 resource/s)
2023/11/20 07:51:10 [ERROR] execution failed due to an error
dry run detected untagged resources
➜  terratag git:(feat-add-dryrun-#178) ✗ echo $?
1

@avnerenv0
Copy link
Contributor

talked in Slack - will change to verifing for specific tags - If tags are passed it will check for these tags... otherwise just in general...

@avnerenv0
Copy link
Contributor

@TomerHeber what's the status here?

@TomerHeber
Copy link
Collaborator Author

@avnerenv0 - I have been busy with the provider. I will get back to it when things cool down on the provider side.

@avnerenv0
Copy link
Contributor

@TomerHeber it's Passover soon I have to close this :)

@avnerenv0 avnerenv0 closed this Apr 2, 2024
@avnerenv0
Copy link
Contributor

not deleting the branch though

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

Successfully merging this pull request may close these issues.

Allow using terratag as a validation tool
2 participants