How wouldgo/terraform-provider-openwrt works
Project type and purpose
• This is a Terraform provider plugin written in Go.
• Purpose: Expose OpenWrt-specific resources (files, UCI config, opkg packages, system services) as Terraform resources, so users can declaratively manage an OpenWrt device.
File structure
• Top-level files:
• main.go – entry point for the Terraform plugin.
• go.mod/go.sum – Go module definitions.
• GNUmakefile, goreleaser.yml – build & release scripts.
• docs/, examples/ – user documentation & Terraform usage examples.
• internal/ – divides the code into three subpackages:
• internal/provider/ – Terraform provider setup (schema, configuration, resource registration).
• internal/resources/ – implementations of CRUD logic for each resource type (fs/configfile, fs/file, opkg, system services).
• internal/api/ – low-level client abstractions to perform operations on an OpenWrt device (UCI, filesystem, opkg).
• internal/types/ – helpers for type conversion and validation.
• Tests are co-located alongside their target packages (*_test.go).
Extract architecture from README
• Confirms the stack: Terraform CLI ↔ provider plugin ↔ OpenWrt device.
• No built-in UI or database; all state is managed by Terraform and remote device.
Main components
A. Terraform CLI
– Reads .tf configuration & state files.
– Loads the provider plugin binary.
B. Terraform Registry (optional)
– Hosts official release of foxboron/openwrt.
C. Provider Plugin (Go binary)
1. Provider Core (internal/provider)
• Registers resources (fs, file, opkg, system).
• Validates provider-level config (e.g. device address, credentials).
2. Resource Layer (internal/resources)
• Implements Create/Read/Update/Delete for each Terraform resource.
3. API Client (internal/api)
• Abstracts SSH or RPC calls:
– UCI command invocations (uci.go)
– File operations (fs.go)
– Package management via opkg (opkg.go)
4. Types (internal/types)
• Common type conversions and schema helpers.
D. OpenWrt Device
– Remote target.
– Provides SSH or API access for UCI, filesystem, opkg, system services.
Relationships & interactions
• Optionally: Terraform CLI ↔ Terraform Registry (when fetching the plugin)
Architectural patterns & principles
• Plugin pattern (Terraform Plugin SDK)
• Layered architecture:
• API layer (abstract OS calls)
• Resource layer (map Terraform operations to API calls)
• Provider layer (schema & orchestration)
• Single binary, no external server.
Relevant technologies & frameworks
• Go modules (go.mod)
• Terraform Plugin SDK v2
• SSH/UCI/opkg CLI on OpenWrt
• GNU Make & GoReleaser for CI/CD