Skip to content

Commit

Permalink
Add support for Field-Level Descriptions
Browse files Browse the repository at this point in the history
  - Terraform Plugin SDK v2 Upgrade Guide: The helper/schema.Resource and helper/schema.Schema types both now have Description properties that accept strings. These properties are laying the groundwork for future improvements to Terraform, and will have no visible effect to the Terraform CLI at the moment. If you’d like to build in support for your provider starting now, it’s recommended that you set these properties to whatever you’d document the resource or field as in your terraform.io docs for the resource. You can globally set the format of the text in these fields by setting the global variable helper/schema.DescriptionKind. Its acceptable values are helper/schema.StringPlain (plain text, the default) or helper/schema.StringMarkdown (markdown formatting).
    https://www.terraform.io/docs/extend/guides/v2-upgrade-guide.html#support-for-resource-level-and-field-level-descriptions
  • Loading branch information
dikhan committed Jan 18, 2021
1 parent 71182f8 commit 4c50370
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/how_to.md
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ The following is a list of attributes that can be added to each property to defi
Attribute Name | Type | Description
---|:---:|---
readOnly | boolean | A property with this attribute enabled will be considered a computed property. readOnly properties are included in responses but not in requests. Hence, it will not be expected from the consumer of the API when posting the resource. However; it will be expected that the API will return tthe property with the computed value in the response payload.
description | string | A description for property.
default | primitive (int, bool, string) | Documents what will be the default value generated by the API for the given property
x-terraform-immutable | boolean | The field will be used to create a brand new resource; however it can not be updated. Attempts to update this value will result into terraform aborting the update. This applies also to properties of type object and also list of objects. If an object property contains this attribute, any update to its child properties will result terraform aborting the update too. Also, if an object property is does not contain this flag, but any of its child properties, the same principle applies and updates to the values of those properties will not be allowed.
x-terraform-force-new | boolean | If the value of this property is updated; terraform will delete the previously created resource and create a new one with this value
Expand Down Expand Up @@ -1097,6 +1098,7 @@ definitions:
properties:
id: # the value of this computed property is not known at plan time (e,g: uuid, etc)
type: string
description: "some description for the property..."
readOnly: true

mandatory_property: # this property is required
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,45 @@ As part of the update of the CRUD functions to support better diagnosis via the
upgrade guide, the OpenAPI Terraform provider now supports timeouts not only for async resource operations but also synchronous. The timeouts can
be specified in the OpenAPI document per resource operation using the [x-terraform-resource-timeout](https://github.com/dikhan/terraform-provider-openapi/blob/master/docs/how_to.md#xTerraformResourceTimeout)
extension.


### Support for Field-Level Descriptions

The [Terraform helper/schema.Schema](https://www.terraform.io/docs/extend/guides/v2-upgrade-guide.html#support-for-resource-level-and-field-level-descriptions) for OpenAPI Terraform
compatible resources and data sources now contains a field Description which is populated based on the corresponding OpenAPI definition property's description.

For instance, the following OpenAPI object definition would be translated into the below helper/schema.Schema
containing the `property_with_description` and its corresponding description as specified in the OpenAPI document.

```yml
definitions:
ContentDeliveryNetworkV1:
type: object
properties:
...
property_with_description:
type: string
description: "some description for the property..."
...
```

````
&schema.Resource{
# This will be the Terraform schema of the resource using the ContentDeliveryNetworkV1 model definition
Schema: map[string]*schema.Schema {
...
"property_with_description": *schema.Schema {
Type:TypeString
Description: "some description"
Optional:true
Required:false
...
}
},
...
}
````


### To be removed

- Multi-region on resource name level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func (s *SpecSchemaDefinitionProperty) terraformSchema() (*schema.Schema, error)
return nil, err
}
terraformSchema.Type = schemaType
terraformSchema.Description = s.Description

// complex data structures
switch s.Type {
Expand Down
15 changes: 15 additions & 0 deletions openapi/openapi_spec_resource_schema_definition_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,21 @@ func TestTerraformSchema(t *testing.T) {
})
})

Convey("Given a swagger schema definition that has a property with a description", t, func() {
s := &SpecSchemaDefinitionProperty{
Name: "string_prop",
Type: TypeString,
Description: "string_prop description",
}
Convey("When terraformSchema method is called", func() {
tfPropSchema, err := s.terraformSchema()
Convey("Then the resulted terraform property schema should contain the description too", func() {
So(err, ShouldBeNil)
So(tfPropSchema.Description, ShouldEqual, s.Description)
})
})
})

Convey("Given a swagger schema definition that has a property of type 'array' and the elems are type string", t, func() {
s := &SpecSchemaDefinitionProperty{
Name: "array_prop",
Expand Down

0 comments on commit 4c50370

Please sign in to comment.