I’m working with an API which accepts aliases for some string values.
For example, if the API attribute were a color, it might accept crimson or raspberry, but when the value is retrieved, it will return red.
I can retrieve the full list of possible aliases in Create()
, Read()
, and Update()
, but I’m not able to predict them.
I thought I’d be able to solve this problem with a custom string type which knows about the aliases and implements semantic equality:
type StringWithAlts struct {
basetypes.StringValue
altValues []basetypes.StringValue // alt values are aliases for the string value
}
The tests for this custom type work fine. But the altValues
element doesn’t survive when used in a provider.
My intent was to populate the altValues
struct element in Read()
, so that no changes would be detected when the user’s config says crimson and the API responds with red (alts: crimson, raspberry, scarlet)
I’m setting the altValues element in Read()
, but find them missing shortly afterward when the StringSemanticEquals()
function is invoked.
Maybe I should be encoding the values in such a way that they survive the trip across the RPC API?
I’m a little fuzzy which of these functions should be doing the encoding and decoding if I was to adopt such a strategy:
StringWithAltsType.ValueFromTerraform()
StringWithAltsType.ValueFromString()
StringWithAlts.ToStringValue()
StringWithAlts.ToTerraformValue()
StringWithAlts.String()
I don’t want to impose strange encoding requirements (JSON, etc…) on the practitioner, and I don’t care if the values in the state file are encoded with alts. I just want to collect a value with alts during Read()
and make use of its alts in StringSemanticEquals()
.
There’s a skeleton provider here. The following configuration applies okay…
terraform {
required_providers {
altstrings = {
source = "chrismarget/altstrings"
}
}
}
resource "altstrings_thing" "test" {
color = "salmon"
}
…but without modifying the configuration, it generates a plan because of the alias thing:
# altstrings_thing.test will be updated in-place
~ resource "altstrings_thing" "test" {
~ color = "red" -> "salmon"
id = "50bbfbfb-aa39-4f45-886e-dab9950fe2e0"
}
So, does encoding the alt values into the string payload make sense? I’m imagining the string value could be something like ["red", "crimson", "raspberry", "scarlet"]