Skip to content

Commit

Permalink
Do not populate terraform schema Default field if property type is list
Browse files Browse the repository at this point in the history
- Terraform currently does not support setting up Default values for TypeList. Otherwise
an error will be thrown by Terraform on runtime like this: Default is not valid for lists
  • Loading branch information
dikhan committed Apr 7, 2021
1 parent 1cefe94 commit a469e4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/how_to.md
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,9 @@ if input is not given, that's the purpose of ```Optional computed``` properties.
**NOTE**:
- Object properties containing optional computed child properties will also need to include the extension ```x-terraform-computed```. Otherwise
the Terraform schema for the object will not be marked as computed and any non expected value change in the child properties will result into diffs.
- Optional properties that are of type `array` that contain a default value are not supported at the moment and the provider
will ignore the Default value when creating the schema for the property. This is due to Terraform not supporting at the moment
[default values to be set in the schema's Default field for TypeList properties](https://github.com/hashicorp/terraform-plugin-sdk/blob/v2.5.0/helper/schema/schema.go#L763).

- Computed properties: These properties must contain the ```readOnly``` attribute set. These properties are included
in responses but not in requests, and the value is automatically assigned by the API. See example below **computed**
Expand Down
5 changes: 4 additions & 1 deletion openapi/openapi_spec_resource_schema_definition_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ func (s *SpecSchemaDefinitionProperty) terraformSchema() (*schema.Schema, error)
// not allow properties with Computed = true having the Default field populated, otherwise the following error will be
// thrown at runtime: Default must be nil if computed
if !s.isComputed() {
terraformSchema.Default = s.Default
// Terraform does not allow defaults to be set on type list properties, an error (Default is not valid for lists) would be thrown otherwise (https://www.terraform.io/docs/extend/schemas/schema-behaviors.html#default)
if !s.isArrayProperty() {
terraformSchema.Default = s.Default
}
}

return terraformSchema, nil
Expand Down
18 changes: 18 additions & 0 deletions openapi/openapi_spec_resource_schema_definition_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,24 @@ func TestTerraformSchema(t *testing.T) {
})
})

Convey("Given a list type schemaDefinitionProperty that is optional and does have a default value (meaning the value is known at plan time)", t, func() {
s := newListSchemaDefinitionPropertyWithDefaults("slice_property", "", false, false, false, []interface{}{"value1"}, TypeString, nil)
Convey("When terraformSchema is called with a schema definition property that is optional computed", func() {
terraformPropertySchema, err := s.terraformSchema()
Convey("Then the result returned should be the expected one", func() {
So(err, ShouldBeNil)
So(terraformPropertySchema.Optional, ShouldBeTrue)
So(terraformPropertySchema.Required, ShouldBeFalse)
So(terraformPropertySchema.Computed, ShouldBeFalse)
// the schema returned should be configured with no default value since Terraform does not allow defaults to be set on type list properties, an error (Default is not valid for lists) would be thrown otherwise:
// Ref: https://www.terraform.io/docs/extend/schemas/schema-behaviors.html#default
So(terraformPropertySchema.Default, ShouldBeNil)
// ValidateFunc is not yet supported on lists or sets
So(terraformPropertySchema.ValidateDiagFunc, ShouldBeNil)
})
})
})

Convey("Given a schemaDefinitionProperty that is forceNew and immutable ", t, func() {
s := newStringSchemaDefinitionProperty("propertyName", "", false, false, false, true, false, true, false, false, "")
Convey("When terraformSchema is called with a schema definition property that validation fails due to immutable and forceNew set", func() {
Expand Down

0 comments on commit a469e4e

Please sign in to comment.