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
Add list-remove module #14
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# List Remove example | ||
|
||
This folder shows examples of how to use the [list-remove module](/modules/list-remove) to remove items from a list | ||
based on a lookup list. | ||
|
||
|
||
## How do you run these examples? | ||
|
||
1. Install [Terraform](https://www.terraform.io/). | ||
1. `terraform init`. | ||
1. `terraform apply`. |
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,9 @@ | ||
module "list_remove" { | ||
# When using these modules in your own templates, you will need to use a Git URL with a ref attribute that pins you | ||
# to a specific version of the modules, such as the following example: | ||
# source = "git::git@github.com:gruntwork-io/package-terraform-utilities.git//modules/list-remove?ref=v0.0.8" | ||
source = "../../modules/list-remove" | ||
|
||
original_list = ["${var.input_list}"] | ||
items_to_remove = ["${var.items_to_remove}"] | ||
} |
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,7 @@ | ||
output "output_list" { | ||
value = "${module.list_remove.output_list}" | ||
} | ||
|
||
output "output_list_as_csv" { | ||
value = "${join(",", module.list_remove.output_list)}" | ||
} |
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,9 @@ | ||
variable "input_list" { | ||
description = "The list of items from which you wish to remove items." | ||
type = "list" | ||
} | ||
|
||
variable "items_to_remove" { | ||
description = "The list of items you wish to remove from the input_list." | ||
type = "list" | ||
} |
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,31 @@ | ||
# List Remove Module | ||
|
||
This is a module that can be used to remove items in a given list from another list. This functionality is not yet | ||
available as an interpolation function. | ||
|
||
For example, suppose you have a list of availability zones (`["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d", | ||
"us-east-1e"]`) and you want to remove specific zones that don't support the features you need (`["us-east-1b", | ||
"us-east-1c"]`). You can use this module: | ||
|
||
```hcl | ||
module "list_remove" { | ||
source = "git::git@github.com:gruntwork-io/package-terraform-utilities.git//modules/list-remove?ref=v0.0.8" | ||
|
||
original_list = ["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d", "us-east-1e"] | ||
items_to_remove = ["us-east-1b", "us-east-1c"] | ||
} | ||
|
||
output "output_list" { | ||
value = "${module.list_remove.output_list}" | ||
} | ||
``` | ||
|
||
The output `new_list` should be the list `["us-east-1a", "us-east-1d", "us-east-1e"]`. | ||
|
||
|
||
**NOTE**: This will dedup the input list due to the way it is implemented. This module will not work if you are expecting duplicate items to remain. | ||
|
||
|
||
## Example code | ||
|
||
See the [list-remove example](/examples/list-remove) for working sample code. |
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,10 @@ | ||
# Remove the items in items_to_remove from original_list. This works because: | ||
# a) concat will create a list in the order of the arguments it's given | ||
# b) distinct will discard duplicate items in the order they are found | ||
# => If you slice the new list from the point of the length of the first list in the concat, then you will get the | ||
# second list of concat with all the elements from the first list removed. | ||
# Inspired by https://github.com/hashicorp/terraform/issues/16044#issuecomment-392269246 | ||
locals { | ||
combined_list = "${distinct(concat(var.items_to_remove, var.original_list))}" | ||
list_without_items = "${slice(local.combined_list, length(var.items_to_remove), length(local.combined_list))}" | ||
} |
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,3 @@ | ||
output "output_list" { | ||
value = "${local.list_without_items}" | ||
} |
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,14 @@ | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
# REQUIRED MODULE PARAMETERS | ||
# These variables must be passed in by the operator. | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
variable "original_list" { | ||
description = "The list of items where you want to remove items from." | ||
type = "list" | ||
} | ||
|
||
variable "items_to_remove" { | ||
description = "The list of items that you want to remove from the original list." | ||
type = "list" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, that's super clever 😁