A Terraform provider for netcup
A Terraform provider in Go that manages DNS, CloudDNS and servers at netcup through three APIs, and how it is tested without touching servers in production.
Table of Contents
My servers and domains are at netcup. I was curious how a Terraform provider is built, and I wanted to manage my own setup with it. That became terraform-provider-netcup, published on the Terraform Registry. Since then other people download it, and netcup keeps extending its APIs. Both make it fun to keep going.
What it manages
The provider manages DNS records and zones, domains on CloudDNS, and servers with what belongs to them, such as disks, snapshots, firewalls and reverse DNS. It is written in Go on the Terraform Plugin Framework and follows HashiCorp’s provider design principles. Each resource represents one API object, and the schema stays close to the API. Registering domains needs reseller functions and is not part of it.
Three APIs, three logins
netcup spreads its functions over three APIs. The CCP API belongs to the Customer Control Panel (CCP). It is a JSON interface with a login session and serves the classic DNS. The netcup API takes an API key and serves CloudDNS, to which netcup is moving domains. The SCP REST API belongs to the Server Control Panel (SCP), uses OAuth 2.0 and serves the servers. Each API has credentials of its own, and the provider only sets up the ones it is given. Someone who only manages DNS never needs access to the servers.
The SCP REST API issues short-lived access tokens. The provider takes an offline
refresh token, obtained once through the device authorization grant
(RFC 8628), and requests new access
tokens with it. After a 401 it renews the token, and it repeats idempotent
requests after a transport error or a 5xx.
A new record in a whole zone
The CCP API writes DNS records with updateDnsRecords and answers with the
whole zone. It does not say which record is new. Terraform needs that record’s
id to manage it later. The provider therefore reads the ids of the zone first,
writes the record and takes the new one with the same hostname, type and
destination.
for i := range after {
r := after[i]
if !had[r.ID] && r.Hostname == rec.Hostname && r.Type == rec.Type && r.Destination == rec.Destination {
return &after[i], nil
}
}
Terraform creates resources in parallel. If two records of the same zone were written at the same time, each could pick up the other’s id. The provider therefore writes to one zone at a time, with a lock per domain.
Beyond create, read, update and delete
For a long time Terraform knew two kinds of objects. A resource has a lifecycle, a data source is only read. Many server operations fit neither, such as a restart, reverting a snapshot or installing a new system. They are actions, which Terraform runs once on request.
action "netcup_server_state" "restart" {
config {
server_id = 12345
state = "ON"
state_option = "RESET"
}
}
terraform apply -invoke=action.netcup_server_state.restart restarts the server
without anything landing in the state. Secrets stay out of the state as well.
Passwords are write-only attributes, and the password of the rescue system or a
download link comes from an
ephemeral resource.
Testing without touching production
My servers are in production. A test against them would restart them, revert snapshots or install a new system. The acceptance tests therefore run real Terraform against a mock of the netcup APIs. The mock keeps state and checks every request and response against netcup’s published OpenAPI specifications with kin-openapi. It checks CCP requests against the list of CCP functions.
A mock proves that the provider follows the specification, not that netcup does. Live tests against the real APIs exist as well and run in a netcup account of their own with a VPS for that purpose. Every test that can change a server refuses to run unless all servers of the account carry a test nickname, so a wrong token cannot change a server in production. A scheduled workflow downloads the specifications every day. When they change, it runs the mock tests against them and reports added and removed operations in an issue.
Quirks of the APIs
Where netcup does something its documentation does not say, the provider follows netcup. netcup activates the rescue system only on a server that is shut down, so the provider shuts a running server down first and starts it again into the rescue system.
The CCP API answers an empty zone with an error. An unknown domain gets the same status code, and only the message tells the two apart. A domain that moved to CloudDNS also looks like an empty zone. The provider then warns, because Terraform would plan to create its records again in the CCP.
if errors.As(err, &apiError) && apiError.StatusCode == statusDNSRecordsFailed &&
strings.Contains(apiError.LongMessage, emptyZoneMessage) {
return nil, nil
}
The netcup API pages its lists, and on the last page nextPage points at the
current page. Only lastPage marks the end of a list.
From 1.0.0 to 1.2.0
Version 1.0.0 covered the DNS of the CCP and the first server objects, such as reverse DNS, SSH keys, firewall policies, snapshots and failover routes. Version 1.1.0 came from using it on real servers. Errors now name their cause instead of only “Validation failed”, offline snapshots select their disk, failover routes work without IPv6, and the settings of a server became a resource. Version 1.2.0 added CloudDNS as a third API, mapped the rest of the SCP REST API with actions and ephemeral resources, and brought the mock and the live tests. The changelog lists every change.
Limits
Actions need Terraform 1.14 or newer, and OpenTofu does not support them. Objects that need a failover IP, a VLAN or a GPU server, and most CloudDNS objects, have only run against the mock so far. Where the mock and netcup disagree, only a live test shows it.
Conclusion
What started as curiosity now manages DNS, CloudDNS and servers at netcup from one configuration. The mock lets the provider grow without risk to servers in production, and the live tests keep it honest about how netcup really behaves. The source is on GitHub.