Skip to main content

Resource schemas

Terraform resources must have schemas. liblab can automatically infer the schema from your API spec.

liblab will infer the schemas from any endpoints annotated with x-liblab-resource. The schema will be inferred by merging the request schema, response schema, and all query/path parameter schemas by respecting the following order:

  1. Path/query parameters
  2. Request body schema
  3. Response body schema

Fields with matching names will be overridden.

For example:

"post": {
"x-liblab-resource": "Soda#Create", // marked as a resource
"summary": "Create a new soda",
"operationId": "createSoda",
"tags": [
"soda"
],
"requestBody": {
"description": "Soda to add to the store",
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NewSoda" // request body schema
}
}
}
},
"responses": {
"201": {
"description": "Expected response to a valid request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Soda" // response body schema
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
"schemas": {
"NewSoda": {
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string",
"example": "Flower Power",
"description": "The name of the soda"
},
"flavor": {
"type": "string",
"example": "rose and hibiscus",
"description": "The flavor of the soda"
}
},
"Soda": {
"type": "object",
"required": [
"id",
"name",
"flavor"
],
"properties": {
"id": {
"type": "string",
"example": "1"
},
"updatedAt": {
"type": "integer",
"example": 1231313
},
"name": {
"type": "string",
"example": "Flower Power",
"description": "The name of the soda"
},
"flavor": {
"type": "string",
"example": "rose and hibiscus",
"description": "The flavor of the soda"
}
}
}
}
}

This will give the following Terraform model:

type PetResourceModel struct {
Race types.String `tfsdk:"flavor"`
Id types.String `tfsdk:"id"`
UpdatedAt types.Int64 `tfsdk:"updated_at"`
Name types.String `tfsdk:"name"`
}

And the following Terraform schema:

Attributes: map[string]schema.Attribute{
"flavor": schema.StringAttribute {
Description: "The flavor of the soda",
Optional: true,
},
"id": schema.StringAttribute {
Description: "The id of the soda to retrieve",
Computed: true,
Optional: true,
},
"updated_at": schema.Int64Attribute {
Description: "updated_at",
Computed: true,
Optional: true,
},
"name": schema.StringAttribute {
Description: "The name of the soda",
Required: true,
},
},

The following rules are applied to the schema:

  • If the property is marked as required in the request schema - the schema attribute is marked as Required.
  • If the property exists in the response schema but not in the request schema - the schema attribute is marked as Computed and Optional.
  • If the property is not Required it is marked as Optional.