Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix: Issue #286] Do not populate terraform schema Default field if property type is list #287

Merged
merged 1 commit into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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