Skip to main content

Crafting Custom Resource Types

Crafting Custom Resource Types

Create a resource type when no existing type describes the data your bundles exchange. See Resources & Resource Types for the concepts.

A resource type is a massdriver.yaml in its own directory. It is published to a repository in your organization as a versioned OCI artifact, and bundles reference it by name and version.

Step 1: Check whether one already exists​

Check these first:

  • Massdriver resource types: Massdriver service accounts, Kubernetes clusters, WireGuard tunnels, and event destinations.
  • The Massdriver Catalog: networks, databases, object storage, and workloads, to customize for your organization.
  • The cloud resource types, such as aws-iam-role, in raw JSON schema format. Convert them with mass resource-type convert.

Step 2: Create the resource type​

Create the repository with the Massdriver CLI:

shell
mass resource-type create my-resource-type

A resource type lives in a directory:

my-resource-type/
├── massdriver.yaml
├── instructions/
│ └── console.md
└── exports/
└── config.yaml.liquid

Start from this template:

yaml
name: my-resource-type
version: 0.1.0
label: My Resource Type

schema:
type: object
title: My Resource Type
additionalProperties: false
properties:
authentication:
title: Authentication
type: object
properties: {}
infrastructure:
title: Infrastructure
type: object
properties: {}

If you already have a resource type as a raw JSON schema, convert it rather than rewriting it by hand:

shell
mass resource-type convert ./my-resource-type.json

convert writes a massdriver.yaml next to the schema and moves inlined instruction and export content into referenced files. It writes a placeholder version; set a real one before you publish.

Step 3: Shape the schema​

Group related properties, and mark secrets with $md.sensitive: true so their values are masked in API responses and the UI.

yaml
name: my-resource-type
version: 1.0.0
label: My Resource Type
icon: https://example.com/my-icon.svg

schema:
type: object
title: My Resource Type
additionalProperties: false
required:
- infrastructure
- authentication
properties:
infrastructure:
title: Infrastructure configuration
type: object
required:
- foo
- bar
properties:
foo:
type: string
title: Foo
description: Foo description
pattern: "^.*+$"
message:
pattern: Must be a valid format for foo.
bar:
type: string
title: Bar
description: Bar description

authentication:
title: Authentication configuration
type: object
required:
- token
properties:
token:
title: Token
type: string
$md.sensitive: true

iam:
title: IAM
description: IAM Roles And Scopes
additionalProperties: false
patternProperties:
"^[a-z]+[a-z_]*[a-z]$":
type: object
required:
- role
- scope
properties:
role:
title: Role
description: Cloud Role
pattern: "^[a-zA-Z ]+$"
message:
pattern: Must be a valid Cloud Role (uppercase, lowercase letters and spaces)
examples:
- Data Reader
scope:
title: Scope
description: Cloud IAM Scope (cloud resource identifier)
type: string

cloud:
type: object
properties:
region:
type: string
title: Cloud Region
description: Select the cloud region you'd like to provision your resources in.
note

$md.sensitive is a single key with a dot in it, written at the same level as type and title. It is not a nested $md object.

Step 4: Publish​

shell
mass resource-type publish ./my-resource-type

A published version cannot be overwritten. Once 1.0.0 exists, anything pinned to it keeps resolving to the same document. Bump version in massdriver.yaml for each change, following semantic versioning. Adding a required field to the payload breaks every bundle that consumes the type, so it needs a major version. To iterate without a new version, publish a development release with --development.

The published artifact holds the massdriver.yaml, the readme, the changelog, the icon, and the instruction and export files that massdriver.yaml references. A referenced file that does not exist, or that resolves outside the directory, fails the publish.

Step 5: Confirm it published​

shell
mass resource-type get my-resource-type
mass resource-type pull my-resource-type@1.0.0

Step 6: Use it in a bundle​

Name the resource type and the versions you accept in the bundle's massdriver.yaml. resources is what the bundle produces; dependencies is what it consumes. Resource types resolve within your organization, so omit the organization prefix.

yaml
resources:
my_resource:
resource_type: my-resource-type@1.0.0
required: true

A bundle that consumes it declares a range instead of a pinned version:

yaml
dependencies:
my_resource:
resource_type: my-resource-type@~1
required: true

See Version Resolution for the accepted range forms and how a range is matched at deploy time.

Produce the resource from your OpenTofu module with the provider's massdriver_resource:

hcl
resource "massdriver_resource" "my_resource" {
field = "my_resource"
name = "My Resource ${var.md_metadata.name_prefix}"
resource = jsonencode(
{
infrastructure = {
foo = my_dummy_resource.main.foo
bar = my_dummy_resource.main.bar
}
authentication = {
token = my_dummy_resource.main.token
}
iam = {
"read" = {
role = "Data Reader"
scope = my_dummy_resource.main.id
}
}
cloud = {
region = my_dummy_resource.main.region
}
}
)
}

Run mass bundle lint and mass bundle build to check the bundle, then mass bundle publish to publish it.

Customizing Massdriver​

Customizing Onboarding​

The instructions panel in the Import Resource dialog, shown below for aws-iam-role, is rendered from ui.instructions. Each instruction has a label and a path to a markdown file.

Relevant fields:

  • label: The display name for your resource type in the UI.
  • icon: A custom icon for your resource type.
  • ui.instructions: Onboarding steps, each with a label and a path to a markdown file.
yaml
name: my-cloud-credential
version: 1.0.0
label: My Cloud Credential
icon: https://example.com/my-icon.svg

ui:
connectionOrientation: environmentDefault
instructions:
- label: "Step 1: Create a Service Account"
path: ./instructions/create-service-account.md
- label: "Step 2: Grant Access"
path: ./instructions/grant-access.md

schema:
type: object
title: My Cloud Credential
properties: {}

mass resource-type publish packages the instruction files into the published artifact. For a full example, see the massdriver-kubernetes-cluster resource type.

Customizing the import form​

The fields in the Import Resource dialog are generated from the resource type's schema. ui.schema takes a React JSON Schema Form uiSchema to control field order and widgets, the same way the ui block does for a bundle's configuration form:

yaml
name: gcp-service-account
version: 1.0.0
label: GCP Service Account

ui:
connectionOrientation: environmentDefault
schema:
ui:order: [project_id, client_email, private_key, "*"]
private_key:
ui:widget: textarea
ui:options:
rows: 8

schema:
type: object
properties:
project_id:
type: string
client_email:
type: string
private_key:
type: string

Massdriver stores ui.schema as-is and returns it from the uiSchema field on ResourceType in the GraphQL API. A value that is not an object is dropped at publish.

Offering a downloadable file​

exports adds a download button to the resource. The file is a Liquid template rendered with artifact.id and artifact.payload, the resource's data:

yaml
exports:
- downloadButtonText: Download .env
fileFormat: env
templatePath: ./exports/dotenv.liquid
templateLang: liquid
liquid
DATABASE_HOST={{ artifact.payload.authentication.hostname }}
DATABASE_PORT={{ artifact.payload.authentication.port }}
DATABASE_USER={{ artifact.payload.authentication.username }}
DATABASE_PASSWORD={{ artifact.payload.authentication.password }}

Environment defaults and canvas display​

An environment default fills a resource type for every instance in an environment. Any resource type can be chosen in the Environment Defaults dialog. The recording shows a Kubernetes cluster default being set.

Relevant fields:

  • ui.connectionOrientation: link lets users draw links to the resource on the canvas. environmentDefault shows it only as an environment default.
  • ui.environmentDefaultGroup: Groups the type with others in the environment defaults panel. Types in the credentials group are cloud credentials, which the UI lists separately.

References:

Publishing a raw JSON schema​

mass resource-type publish still accepts a raw JSON or YAML schema file, the format before massdriver.yaml, and prints a deprecation warning. Support will be removed.

A raw schema carries no version of its own. It is stored as the resource type's unversioned 0.0.0 document, cannot be pinned by version, and cannot be pulled back down. Run mass resource-type convert to move one to massdriver.yaml.