Understanding customized situation checks
On this article, I current a perspective on customized situation checks as a design by contracts strategy for writing Terraform modules.
In 2020, the Terraform staff introduced custom validation rules for input variables as an experimental feature and later released it as a secure function in v0.13. This yr, they prolonged customized validations by introducing preconditions and postconditions within the v1.2.0 launch, that are to be used in useful resource, knowledge supply, and output blocks.
Each the above introduce a method that allows Terraform authors to outline a exact contract for modules that specifies obligations and ensures for a module caller.
In software program design, that is known as Design by contract, a time period coined by Bertrand Meyer¹. Its central concept of it’s a metaphor for a way components of a software program system collaborate with one another on the idea of mutual obligations and benefits².
A software program element supplies a contract, for the companies that it’s going to present, and that through the use of the element a consumer agrees to the phrases of that contract³.
The contract is an inventory of specs. The specs could embrace:
- Preconditions: the consumer is obligated to satisfy a operate’s required preconditions earlier than calling a operate. If the preconditions are usually not met, then the operate could not function appropriately.
- Postconditions: the operate ensures that sure circumstances will probably be met after it has completed its work. If the postcondition isn’t met, then the operate didn’t full its work appropriately
- Acceptable and unacceptable enter values
The mechanisms for expressing obligations and ensures are assertions. Having assertions as a part of the code makes contracts a useful gizmo to test for correctness. Take into account the next operate for computing a sq. root:
assert x > 0 # precondition
/*a computation to get the sq. root of x as y*/
assert y*y == x # postcondition
Contracts in Terraform
By way of Terraform, the assertions are represented by a
situation argument inside a
postcondition block. The
situation takes an expression which should consider to
false . If the assertion is violated, Terraform will return an
error_message and halt the execution.
situation = self.private_dns != ""
error_message = "The occasion have to be in a VPC that has non-public DNS hostnames enabled."
The place Customized Situation Checks seem in numerous block sorts
Customized Situation Checks Seize Assumptions and Gaurantees
Discover the above makes use of the phrase “assumption”, as a substitute of “obligation”. This can be slightly complicated, so to clear it up contemplate the next factors of view:
The useful resource:
The useful resource should make sure the postconditon is met: makes a gaurantee for it’s caller. The useful resource could make an assumption about how the caller is utilizing it : defines a preconditions that have to be met by the caller.
The caller should make sure the precondition outlined by the useful resource is met and will assume some ensures made by the useful resource (postcondition).