Assuming that we have a field of a certain type (int
, boolean
, string
etc..), that field can either be nullable or not and also can have various defaults. We need our modifiers to work with all these scenarios.
nullable | null default | empty default1 | known default | random default |
---|---|---|---|---|
no | X2 | DefaultType |
DefaultType |
UseStateForUnknown |
yes | NullableType |
DefaultType 3 |
DefaultType 3 |
UseStateForUnknown 3 |
We also have more modifiers that helps with ergonomics.
Use the appropriate function for type whose name starts with Default
.
modifiers.DefaultBool(true)
modifiers.DefaultString("")
modifiers.DefaultFloat(3)
Use the appropriate function for type whose name starts with Nullable
.
modifiers.NullableBool()
modifiers.NullableString()
modifiers.NullableFloat()
Use UseStateForUnknown
from Terraform plugin framework.
resource.UseStateForUnknown()
NOTE: Make sure that you omit this property when sending payloads during both creation and updation.
Use this when you have a nested block of SingleNestedAttributes
and the block is fully optional.
modifiers.UnknownAttributesOnUnknown()
Validates that the number is one of certain values.
validators.FloatInSlice(1, 4, 6)
Validates that the string is one of certain values.
validators.StringInSlice(true, "one", "two", "three")
validators.StringInSlice(false, "OnE", "tWo", "tHrEe")
Validates that the string matches a certain regex.
validators.Match(regexp.MustCompile("^[0-9a-fA-F]{6}$"))
Validates that the string does not match a certain regex.
validators.NotMatch(regexp.MustCompile("\\s"))
Validates that the string's length is at least of a certain value.
validators.MinLength(1)
Validates that the string's length is at most of a certain value.
validators.MaxLength(5)
Footnotes
-
empty default means that the default value of the field on the server is the empty value for that type in golang. e.g. boolean
false
, string""
, int0
etc.. ↩ -
This scenairo is impossible. ↩
-
End users will not be able to set the value of the field as
null
in the server. This is a limitation on terraform itself. ↩ ↩2 ↩3