Hi everyone
I am working on a terraform provider using the terraform plugin framework.
I am using ConflictsWith to make these two attributes image
and source_instance
mutually exclusive.
"image": schema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.ConflictsWith(
path.Expressions{
path.MatchRoot("source_instance"),
}...
),
stringvalidator.AtLeastOneOf(
path.Expressions{
path.MatchRoot("image"),
path.MatchRoot("source_instance"),
}...
),
},
},
"source_instance": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"project": schema.StringAttribute{
Required: true,
},
"name": schema.StringAttribute{
Required: true,
},
"snapshot": schema.StringAttribute{
Optional: true,
},
},
PlanModifiers: []planmodifier.Object{
objectplanmodifier.RequiresReplace(),
},
Validators: []validator.Object{
objectvalidator.ConflictsWith(
path.Expressions{
path.MatchRoot("image"),
}...),
objectvalidator.AtLeastOneOf(
path.Expressions{
path.MatchRoot("image"),
path.MatchRoot("source_instance"),
}...
),
},
},
This works perfectly when I set the values of the attributes to literal values inside the resource or where I use variables with defaults for both values.
But when I use a mix of both the plan fails with
Attribute "image" cannot be specified when "source_instance" is specified
Here is an example hcl
# This works
resource "my_resource"{
image = "my_image"
source_instance = null
# This works
variable "image"{
default = "my_image"
}
variable "source_instance"{
default = null
}
resource "my_resource"{
image = var.image
source_instance = var.source_instance
}
# This Does not work
variable "source_instance"{
default = null
}
resource "my_resource"{
image = "my_image"
source_instance = var.source_instance
}
From the source code of of SchemaValidator, it says that it waits for the resource value to be known before the evaluation. In this case the value of source_instance will be null not unknown and the plan will succeed
Can you please help me understand what going on? What is the recommendations in this case?