How marshallford/terraform-provider-pfsense works
Purpose and Entry Point
This is a Go Terraform provider that configures pfSense CE firewalls/routers (validated for pfSense 2.6.0–2.8.1). Terraform loads the provider binary through main.go, which serves the provider implementation in internal/provider/provider.go. The provider exposes managed resources, read-only data sources, and imperative-style actions for DHCPv4, DNS Resolver, firewall aliases/filter reload, PHP execution, and system version queries.
Terraform Provider Boundary
internal/provider/provider.go defines provider schema, configuration, and registration of resource/data-source/action implementations. Provider-specific Terraform type handling, plan semantics, and validation are centralized in:
• internal/provider/provider_custom_types.go
• internal/provider/provider_plan_modifiers.go
• internal/provider/validators.go
Terraform configuration is translated into provider models and then delegated to the pfSense client layer; Terraform state is reconstructed from pfSense responses during reads.
Resource and Data-Source Domains
The main declarative domains are:
• DHCP static mappings and DHCP apply: internal/provider/dhcpv4_staticmapping_resource.go, internal/provider/dhcpv4_staticmappings_data_source.go, internal/provider/dhcpv4_apply_resource.go
• DNS Resolver config file, host/domain overrides, and apply: internal/provider/dnsresolver_configfile_resource.go, internal/provider/dnsresolver_hostoverride_resource.go, internal/provider/dnsresolver_domainoverride_resource.go
• Firewall IP/port aliases and filter reload: internal/provider/firewall_ip_alias_resource.go, internal/provider/firewall_port_alias_resource.go, internal/provider/firewall_filter_reload_resource.go
Common files within each domain hold shared schema/model and CRUD translation logic, separating Terraform-facing lifecycle code from shared operations.
Actions and Command Execution
Actions provide explicit operations that are not necessarily persistent objects, including applying DHCP/DNS changes, reloading firewall filters, and executing pfSense PHP:
• internal/provider/dhcpv4_apply_action.go
• internal/provider/dnsresolver_apply_action.go
• internal/provider/execute_php_command_action.go
PHP execution is also available as a resource and data source (internal/provider/execute_php_command_resource.go, internal/provider/execute_php_command_data_source.go), making it an escape hatch for operations not covered by typed provider features.
pfSense Integration Layer
pkg/pfsense is the infrastructure boundary between Terraform semantics and pfSense. pkg/pfsense/client.go owns client construction/session behavior; pkg/pfsense/http.go and pkg/pfsense/html.go handle HTTP and pfSense web-interface response processing. Domain APIs encapsulate pfSense operations:
• pkg/pfsense/dhcpv4_staticmapping.go
• pkg/pfsense/dnsresolver_hostoverride.go
• pkg/pfsense/firewall_ip_alias.go
This layer also owns privilege handling (pkg/pfsense/privileges.go), validation, and pfSense-specific error mapping, preventing web/UI protocol concerns from leaking into Terraform resource implementations.
Primary Flow and Operational Boundaries
Terraform plan/apply invokes provider resource or action handlers; handlers validate and map Terraform values, call pkg/pfsense, then persist IDs and observed values into Terraform state. Reads/data sources retrieve and normalize current pfSense configuration. Mutating configuration and activating it are intentionally separable: DNS/DHCP “apply” and firewall filter reload endpoints trigger runtime adoption after related configuration changes.
The external runtime boundary is the pfSense administration interface, reached over HTTP(S) and using pfSense’s web/PHP mechanisms rather than a separate documented API.
Build, Distribution, and Documentation
The provider is built with Go modules (go.mod) and released as a Terraform provider using terraform-registry-manifest.json and .goreleaser.yml. CI/release automation resides in .github/workflows/ci.yml and .github/workflows/release.yml. Generated registry documentation is organized under docs/, with template support in templates/index.md.tmpl.
Main entry points: provider.go Provider registration · client.go pfSense client · main.go Provider binary · .goreleaser.yml Provider release packaging
Components 18
Terraform provider
- Provider binary
main.go - Provider registration · Registers schemas, resources, data sources, and actions.
internal/provider/provider.go - Types, planning & validation
internal/provider/provider_custom_types.go
Provider domains
- DHCPv4 mappings · Static mapping CRUD, lookup, and runtime apply support.
internal/provider/dhcpv4_staticmapping_resource.go - DNS Resolver · Manages resolver config, host overrides, and domain overrides.
internal/provider/dnsresolver_configfile_resource.go - Firewall aliases · Manages IP and port aliases.
internal/provider/firewall_ip_alias_resource.go - Apply & filter reload · Separately activates DHCP/DNS changes and reloads firewall filters.
internal/provider/dhcpv4_apply_action.go - PHP command escape hatch · Executes pfSense PHP for operations outside typed domains.
internal/provider/execute_php_command_action.go
pfSense integration
- pfSense client · Owns session construction and pfSense operation access.
pkg/pfsense/client.go - pfSense domain APIs · Encapsulates DHCP, DNS, and firewall web-interface operations.
pkg/pfsense/dhcpv4_staticmapping.go - HTTP & HTML processing · Processes pfSense administration-interface requests and responses.
pkg/pfsense/http.go - Privileges & error mapping
pkg/pfsense/privileges.go
Build & release
- Go module build
go.mod - Provider release packaging · Builds distributable Terraform provider releases.
.goreleaser.yml - Terraform Registry metadata
terraform-registry-manifest.json - Generated documentation
docs/index.md
Other components
- Terraform CLI · Loads the provider and invokes plan, read, and apply operations.
- pfSense administration interface · HTTP(S) web and PHP mechanisms, not a separate documented API.
Connections 21
- Terraform CLI → Provider binary: loads
- Provider binary → Provider registration: serves
- Provider registration → Types, planning & validation: uses
- Provider registration → DHCPv4 mappings: registers
- Provider registration → DNS Resolver: registers
- Provider registration → Firewall aliases: registers
- Provider registration → Apply & filter reload: registers
- Provider registration → PHP command escape hatch: registers
- DHCPv4 mappings → pfSense client: maps state and calls
- DNS Resolver → pfSense client: maps state and calls
- Firewall aliases → pfSense client: maps state and calls
- Apply & filter reload → pfSense client: triggers via
- PHP command escape hatch → pfSense client: executes via
- pfSense client → pfSense domain APIs: provides
- pfSense client → HTTP & HTML processing: uses
- pfSense client → Privileges & error mapping: enforces
- HTTP & HTML processing → pfSense administration interface: HTTP(S)
- Go module build → Provider binary: builds
- Provider release packaging → Go module build: packages
- Provider release packaging → Terraform Registry metadata: publishes with
- Provider registration → Generated documentation: documented as