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

Add list-remove module #14

Merged
merged 2 commits into from Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/list-remove/README.md
@@ -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`.
9 changes: 9 additions & 0 deletions examples/list-remove/main.tf
@@ -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}"]
}
7 changes: 7 additions & 0 deletions examples/list-remove/outputs.tf
@@ -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)}"
}
9 changes: 9 additions & 0 deletions examples/list-remove/variables.tf
@@ -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"
}
31 changes: 31 additions & 0 deletions modules/list-remove/README.md
@@ -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.
10 changes: 10 additions & 0 deletions modules/list-remove/main.tf
@@ -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
Copy link
Member

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 😁

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))}"
}
3 changes: 3 additions & 0 deletions modules/list-remove/outputs.tf
@@ -0,0 +1,3 @@
output "output_list" {
value = "${local.list_without_items}"
}
14 changes: 14 additions & 0 deletions modules/list-remove/variables.tf
@@ -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"
}
122 changes: 120 additions & 2 deletions test/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.