The API must work like Terraform. If a value is not set, nothing is sent to the API.
For each attribute, we must think about three flows.
- The attribute is set to some value and sent to the API.
- The attribute is set to a zero value and sent to the API.
- The attribute is not set (
nil
) and sent to the API.
For each of these flows there is an associated read from the API. We need to have a clear understanding of what the API should return in each of these cases. The API should be designed to be consumed by Terraform.
For this example I will use the comment
attribute of the Network
object.
- The
comment
attribute is set to "test" in theNetwork
object and sent to the API. - The API will return the
Network
object with thecomment
attribute set to "test". - The
comment
attribute is not set (nil
) in theNetwork
object and sent to the API. - The API will return the
Network
object with thecomment
attribute set to "test". - The
comment
attribute is set to "" in theNetwork
object and sent to the API. - The API will return the
Network
object with thecomment
attribute set to "".
For attributes like these, we use a pointer, so we can differentiate between the zero value and nil
.
"test" --> create --> "test"
"test" <-- read <-- "test"
"" --> update --> ""
"" <-- read <-- ""
nil --> update --> ""
"" <-- read <-- ""