Carina

Your First Provider

Build a minimal Carina provider with carina-plugin-sdk, compile it to a WebAssembly component, and use it from a configuration through a file:// source.

This page builds hello, a provider with one resource type, hello.greeting. It calls no API: a greeting’s message is derived from its name. That keeps the example small enough to read in full while still going through every call the host makes, so you can run plan, apply and destroy against it.

The SDK supplies the WIT bindings and their generator from the Carina revision you pin. Use a Carina revision that includes carina-rs/carina#4191 (SDK-owned provider bindings) and the PhasedToken associated operation-payload API. Your provider needs no WIT checkout or wit-bindgen dependency.

Prerequisites

  • Rust with the wasm32-wasip2 target: rustup target add wasm32-wasip2
  • The carina CLI, at a version that speaks the same protocol version as the SDK you build against (see Protocol version)

Project layout

carina-provider-hello/          # repository root
├── Cargo.toml                  # workspace
└── carina-provider-hello/
    ├── Cargo.toml
    └── src/main.rs

This example uses a workspace with one provider crate. A standalone crate works too: the SDK resolves its own WIT files, so your provider can use any directory layout.

Create the repository and the provider’s source directory:

mkdir carina-provider-hello && cd carina-provider-hello
git init
mkdir -p carina-provider-hello/src

Cargo manifests

The workspace manifest at the repository root:

[workspace]
members = ["carina-provider-hello"]
resolver = "2"

The crate manifest, carina-provider-hello/Cargo.toml:

[package]
name = "carina-provider-hello"
version = "0.1.0"
edition = "2024"

[dependencies]
carina-plugin-sdk = { git = "https://github.com/carina-rs/carina", rev = "<carina-rev>" }
serde = "1"
serde_json = "1"

The SDK is not on crates.io; depend on it from git. Replace <carina-rev> with the full commit SHA of a Carina revision that includes SDK-owned provider bindings and the PhasedToken associated operation-payload API. serde and serde_json are needed because the adapter that export_provider! generates uses them from your crate. The SDK chooses the binding generator and WIT revision for you.

The provider

carina-provider-hello/src/main.rs:

// The provider only runs as a WebAssembly component; on the host target
// nothing calls it.
#![cfg_attr(not(target_arch = "wasm32"), allow(dead_code))]

use std::collections::BTreeMap;

use carina_plugin_sdk::core::schema::{
    AttributeSchema, AttributeType, ReferenceForms, ResourceSchema, ValidatedConfigSchema,
    ValidatedResourceSchema,
};
use carina_plugin_sdk::types::*;
use carina_plugin_sdk::core::provider_text::{ProviderDiagnostic, ProviderText};
use carina_plugin_sdk::{
    CarinaProvider, CreateAttempt, CreateStep, CreateVerdict, DeleteOutcome, Never, Pending,
    PendingDelete, Poll, PollVerdict, Resume, ResumeStep, ResumeVerdict, Sent, UpdateOutcome,
};

/// A provider with one resource, `hello.greeting`. It talks to no API:
/// the greeting is derived from its name, so it keeps no state of its own.
#[derive(Default)]
struct HelloProvider;

fn error(kind: ProviderErrorKind, message: impl Into<String>) -> ProviderError {
    let message = message.into();
    let error = ProviderError::from_diagnostic(
        kind,
        ProviderDiagnostic::message(ProviderText::new(message)),
    );
    error.for_provider("hello")
}

fn greeting(name: &str) -> BTreeMap<String, Value> {
    BTreeMap::from([
        ("name".to_string(), Value::String(name.to_string())),
        ("message".to_string(), Value::String(format!("Hello, {name}!"))),
    ])
}

impl CarinaProvider for HelloProvider {
    // This provider finishes every operation in one call, so it never
    // hands the host a continuation token.
    type CreateToken = Never;
    type UpdateToken = Never;
    type DeleteToken = Never;

    fn info(&self) -> ProviderInfo {
        ProviderInfo {
            name: "hello".to_string(),
            display_name: "Hello".to_string(),
            capabilities: vec![],
            version: env!("CARGO_PKG_VERSION").to_string(),
        }
    }

    fn schemas(&self) -> Vec<ValidatedResourceSchema> {
        vec![ResourceSchema::new("greeting")
            .with_description("A greeting derived from a name")
            .attribute(
                AttributeSchema::required("name", AttributeType::string())
                    .create_only()
                    .identity()
                    .build(),
            )
            .attribute(
                AttributeSchema::provider_populated("message", AttributeType::string()).build(),
            )
            .validate()
            .expect("the greeting schema is valid")]
    }

    fn reference_forms(&self) -> ReferenceForms {
        ReferenceForms::new()
    }

    fn assignability_edges(&self) -> Vec<carina_plugin_sdk::core::schema::AssignabilityEdge> {
        Vec::new()
    }

    fn provider_config_attribute_types(&self) -> ValidatedConfigSchema {
        ValidatedConfigSchema::default()
    }

    fn validate_config(&self, _attrs: &BTreeMap<String, Value>) -> Result<(), ProviderError> {
        Ok(())
    }

    fn initialize(
        &mut self,
        _attrs: &BTreeMap<String, Value>,
    ) -> Result<ReferenceScope, ProviderError> {
        Ok(ReferenceScope::Unavailable)
    }

    fn read(
        &self,
        id: &ResourceId,
        identifier: &AppliedIdentifier,
        _request: ReadRequest,
    ) -> Result<ReadOutcome, ProviderError> {
        Ok(ReadOutcome::Found {
            state: AppliedState {
                id: id.clone(),
                identifier: identifier.clone(),
                attributes: greeting(identifier.as_str()),
            },
        })
    }

    fn read_data_source(&self, _resource: &Resource) -> Result<State, ProviderError> {
        Err(error(ProviderErrorKind::InvalidInput, "hello has no data sources"))
    }

    fn create(
        &self,
        _id: &ResourceId,
        request: CreateRequest,
        attempt: CreateAttempt,
    ) -> Result<CreateStep<Never>, CreateVerdict<Never>> {
        let name = match request.resource.attributes.get("name") {
            Some(Value::String(name)) if !name.is_empty() => name.clone(),
            _ => {
                return Err(attempt.not_created(error(
                    ProviderErrorKind::InvalidInput,
                    "name must be a non-empty string",
                )));
            }
        };
        let identifier = AppliedIdentifier::new(name.clone()).expect("name is non-empty");
        Ok(attempt
            .issued(identifier, Sent::AllRequestAttributes)
            .succeeded(greeting(&name)))
    }

    fn poll_create(
        &self,
        _id: &ResourceId,
        _request: CreateRequest,
        poll: Poll<Never>,
        _poll_request: PollRequest,
    ) -> Result<CreateStep<Never>, PollVerdict<Never>> {
        match poll {
            Poll::Unissued(_, token) => match token {},
            Poll::Issued(_, token) => match token {},
        }
    }

    fn resume_create(
        &self,
        _id: &ResourceId,
        _request: CreateRequest,
        resume: Resume<Never>,
        _poll_request: PollRequest,
    ) -> Result<ResumeStep<Never>, ResumeVerdict<Never>> {
        match resume {
            Resume::Unissued(_, token) => match token {},
            Resume::Issued(_, token) => match token {},
        }
    }

    fn update(
        &self,
        _id: &ResourceId,
        _identifier: &AppliedIdentifier,
        _request: UpdateRequest,
    ) -> Result<UpdateOutcome<Never>, ProviderError> {
        // `name` is create-only and `message` is read-only, so the host
        // replaces the resource instead of updating it.
        Err(error(ProviderErrorKind::Internal, "greeting cannot be updated"))
    }

    fn poll_update(
        &self,
        _id: &ResourceId,
        _identifier: &AppliedIdentifier,
        _request: UpdateRequest,
        pending: Pending<Never>,
        _poll_request: PollRequest,
    ) -> Result<UpdateOutcome<Never>, ProviderError> {
        match pending.token {}
    }

    fn delete(
        &self,
        _id: &ResourceId,
        _identifier: &AppliedIdentifier,
        _request: DeleteRequest,
    ) -> Result<DeleteOutcome<Never>, ProviderError> {
        Ok(DeleteOutcome::Deleted)
    }

    fn poll_delete(
        &self,
        _id: &ResourceId,
        _identifier: &AppliedIdentifier,
        _request: DeleteRequest,
        pending: PendingDelete<Never>,
    ) -> Result<DeleteOutcome<Never>, ProviderError> {
        match pending.token {}
    }

    fn required_permissions(
        &self,
        _id: &ResourceId,
        _op: carina_plugin_sdk::PlanOp,
    ) -> Result<Vec<String>, ProviderError> {
        Ok(vec![])
    }
}

#[cfg(target_arch = "wasm32")]
carina_plugin_sdk::export_provider!(HelloProvider);

fn main() {}

The parts to notice:

  • info returns the name the configuration uses in provider hello { ... } and in resource types. A resource type greeting becomes hello.greeting in the DSL.
  • name is required and create-only, so changing it replaces the resource instead of updating it. It is also marked identity, so an anonymous hello.greeting (one without let) gets a stable address from its name.
  • message is read-only: the provider sets it, and the configuration cannot.
  • create returns the identifier through the create typestate. issued records the identifier the provider reported; succeeded finishes the create with its attributes. Sent says which top-level attributes the accepted API request carried; Sent::AllRequestAttributes is right here because nothing is sent separately. A provider that sets some attributes through a later call should name only the ones it sent, with Sent::only([...]).
  • read rebuilds the attributes from the identifier. A provider for a real API would call the API here and return ReadOutcome::NotFound when the resource is gone.
  • The token types are Never, an empty type, because every operation finishes in one call. The poll and resume methods still have to exist, but they can never be called, and match token {} proves that to the compiler.
  • export_provider! builds the provider with Default, so the type must implement it. main is empty; the host calls the exported functions, not main.
  • The macro only expands for wasm32, so on the host target (cargo check, rust-analyzer) nothing uses the provider. The cfg_attr line at the top keeps that from producing dead-code warnings.

Build

cargo build --target wasm32-wasip2 --release

The component is at target/wasm32-wasip2/release/carina-provider-hello.wasm.

Use it from a configuration

In a separate directory, create main.crn and point a provider block at the component with a file:// URL. Use an absolute path so the configuration works from any invocation directory. With no backend block, state goes to carina.state.json in that directory.

provider hello {
  source = 'file:///path/to/carina-provider-hello/target/wasm32-wasip2/release/carina-provider-hello.wasm'
}

let greeting = hello.greeting {
  name = 'world'
}

Install the provider, then plan and apply:

carina init
carina plan
carina apply

carina plan shows the greeting to be created, with message as (known after apply). After apply, another plan reports no changes, because read returns the same attributes. Change name to 'carina' and plan shows the greeting must be replaced, because name is create-only. carina destroy deletes it.

carina init installs an atomic copy of the component under .carina/providers/ and records its checksum in carina-providers.lock. Later commands use that copy, so after you rebuild the provider, run carina init again to pick up the new build and update the checksum. For an HTTP provider, that init also reads the new egress declaration: an unchanged declaration carries consent forward without a prompt, while a changed declaration shows the diff and asks again.

Use it from GitHub

Once the provider is in a GitHub repository, a configuration can install it from there:

provider hello {
  source  = 'github.com/your-org/carina-provider-hello'
  version = '0.1.0'
}

version is a semver constraint, as for any provider. carina init considers all of the repository’s releases, picks the newest matching version, and preserves the release’s exact tag. Tags may be either v0.1.0 or 0.1.0. The release must contain exactly one asset whose name ends in .wasm; the asset may otherwise have any name. The lookup uses GITHUB_TOKEN when set or the token from a logged-in gh when available, so it also works with private repositories. carina-providers.lock records the exact tag and GitHub asset ID, alongside the semver version and checksum, so later installs fetch and verify the same asset.

GitHub lock entries without the exact release tag and asset ID are rejected when the lock file is loaded. Use Carina v0.15.0 to rewrite older entries before using this version of Carina.

With revision instead of version, carina init installs the CI build for a branch, tag or commit. It resolves the revision to a commit and looks at the successful GitHub Actions runs for that commit. Only runs triggered by a push to the repository itself count; pull request runs, including those from forks, are ignored. Across the artifacts those runs uploaded, the .wasm files must all have the same content, so have one workflow upload the built component. The artifact may have any name; the file inside it must end in .wasm. Byte-identical copies count as one build, and the newest run’s copy is used. Every artifact of those runs is downloaded and inspected, so an artifact larger than 512 MiB, or a .wasm file larger than 256 MiB once unpacked, makes the install fail. When no build or more than one is found, the error lists the candidates. This call does need a token, GITHUB_TOKEN or a logged-in gh.

carina-providers.lock records the commit, the artifact ID and the checksum, and later installs download that artifact without choosing again. GitHub deletes artifacts after their retention period. When the recorded artifact is gone, carina init looks for a build at the same commit and accepts it only if its checksum matches the lock. In lock-writing modes it records the new artifact ID. With --locked it leaves the lock unchanged, installs the build and suggests running carina init, which then records the new ID. If every artifact for the commit has expired, re-run CI for that commit or point revision at a newer one. Unless your builds are reproducible, the rebuilt .wasm will not match the recorded checksum; Carina refuses it, and you accept the new build by removing the provider’s entry from carina-providers.lock and running carina init.

Revision lock entries without an artifact ID are rejected when the lock file is loaded. Use Carina v0.15.0 to rewrite older entries while preserving their recorded commit and checksum.

A GitHub source is not signature-checked, but carina init records the checksum of what it downloaded in the lock file.

Next steps

Implementing Operations covers a provider for a real, asynchronous API: pending creates, polling, updates from a patch, and errors. The mock provider in the carina repository, carina-provider-mock, is a test fixture that exercises every create outcome.