|
| 1 | +# filter_attribute_was_value |
| 2 | +This function filters a collection of resources, data sources, or blocks to those with an attribute that was equal to a given value. A policy would call it when it wants the attribute to not equal the given value, especially when trying to delete a resource. Note that this function passes `rc.change.before` instead of `rc` to the `evaluate_attribute()` function which converts `rc` to `rc.change.after`. |
| 3 | + |
| 4 | +## Sentinel Module |
| 5 | +This function is contained in the [tfplan-functions.sentinel](../tfplan-functions.sentinel) module. |
| 6 | + |
| 7 | +## Declaration |
| 8 | +`filter_attribute_was_value = func(resources, attr, value, prtmsg)` |
| 9 | + |
| 10 | +## Arguments |
| 11 | +* **resources**: a map of resources derived from [`tfplan.resource_changes`](https://www.terraform.io/docs/cloud/sentinel/import/tfplan-v2.html#the-resource_changes-collection) or a list of blocks returned by the `find_blocks` function. |
| 12 | +* **attr**: the name of a resource attribute given as a string that should not have previously equaled a given value. If the attribute is nested, the various blocks containing it should be delimited with periods (`.`). Indices of lists should not include brackets and should start with 0. So, you would use `boot_disk.0.initialize_params.0.image` rather than `boot_disk[0].initialize_params[0].image`. |
| 13 | +* **value**: the value the attribute should not have been equal to. This can be any primitive data type. If you want to match null, set value to "null". |
| 14 | +* **prtmsg**: a boolean indicating whether violation messages should be printed (if `true`) or not (if `false`). |
| 15 | + |
| 16 | +## Common Functions Used |
| 17 | +This function calls the [evaluate_attribute](./evaluate_attribute.md) and the [to_string](./to_string.md) functions. |
| 18 | + |
| 19 | +## What It Returns |
| 20 | +This function returns a map with two maps, `resources` and `messages`, both of which are indexed by the complete [addresses](https://www.terraform.io/docs/internals/resource-addressing.html) of the resources, data sources, or blocks that meet the condition of the filter function. The `resources` map contains the actual resource instances for which the attribute (`attr`) is equal to the given value, `value`, while the `messages` map contains the violation messages associated with those instances. |
| 21 | + |
| 22 | +## What It Prints |
| 23 | +This function prints the violation messages if the parameter, `prtmsg`, was set to `true`. Otherwise, it does not print anything. |
| 24 | + |
| 25 | +## Examples |
| 26 | +Here is an example of calling this function, assuming that the tfplan-functions.sentinel file that contains it has been imported with the alias `plan`: |
| 27 | +``` |
| 28 | +violatingRDSInstances = plan.filter_attribute_was_value(RDSInstancesBeingDeleted, |
| 29 | + "deletion_protection", true, false) |
| 30 | +
|
| 31 | +violatingAzureAppServices = plan.filter_attribute_was_value(allAzureAppServices, |
| 32 | + "https_only", true, true) |
| 33 | +``` |
| 34 | + |
| 35 | +This function is used by the [protect-against-rds-instance-deletion.sentinel (AWS)](../../../aws/protect-against-rds-instance-deletion.sentinel) policy. |
0 commit comments