Files
terraform-provider-artifactapi/internal/provider/resource_local_docker.go
T
unkinben 30b414141a
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
feat: add artifactapi_local_docker resource
The artifactapi server now serves local docker repos as real container
registries, but the provider had no resource to declare one — only remote
docker proxies and local terraform/pypi/rpm repos.

- Add the artifactapi_local_docker resource (package_type=docker,
  repo_type=local), mirroring the other local resources: name + description,
  managed via /api/v2/remotes.
- Register it in the provider and update the resource-count/type tests.
- Add unit tests, an example, and a Local Resources section to the README.
2026-07-04 22:37:10 +10:00

165 lines
5.1 KiB
Go

package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var (
_ resource.Resource = &localDockerResource{}
_ resource.ResourceWithImportState = &localDockerResource{}
)
type localDockerResource struct {
client *apiClient
}
type localDockerResourceModel struct {
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}
func NewLocalDockerResource() resource.Resource {
return &localDockerResource{}
}
func (r *localDockerResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_local_docker"
}
func (r *localDockerResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages a local ArtifactAPI Docker repository — a real container registry serving the Docker Registry HTTP API V2 for push and pull.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Unique name of the local Docker repository. Becomes the first path segment of pushed image references (e.g. <endpoint>/<name>/<image>:<tag>).",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Description: "Human-readable description.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
},
}
}
func (r *localDockerResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*apiClient)
if !ok {
resp.Diagnostics.AddError("unexpected provider data type", fmt.Sprintf("got %T", req.ProviderData))
return
}
r.client = client
}
func (r *localDockerResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan localDockerResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localDockerModelToAPI(plan)
api.ManagedBy = "terraform"
var created remoteAPI
if err := r.client.post(ctx, "/api/v2/remotes", api, &created); err != nil {
resp.Diagnostics.AddError("create local docker failed", err.Error())
return
}
state := localDockerAPIToModel(created)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localDockerResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state localDockerResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
var remote remoteAPI
err := r.client.get(ctx, "/api/v2/remotes/"+state.Name.ValueString(), &remote)
if err != nil {
if isNotFound(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("read local docker failed", err.Error())
return
}
newState := localDockerAPIToModel(remote)
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
}
func (r *localDockerResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan localDockerResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localDockerModelToAPI(plan)
api.ManagedBy = "terraform"
var updated remoteAPI
if err := r.client.put(ctx, "/api/v2/remotes/"+plan.Name.ValueString(), api, &updated); err != nil {
resp.Diagnostics.AddError("update local docker failed", err.Error())
return
}
state := localDockerAPIToModel(updated)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localDockerResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state localDockerResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
if err := r.client.del(ctx, "/api/v2/remotes/"+state.Name.ValueString()); err != nil {
resp.Diagnostics.AddError("delete local docker failed", err.Error())
return
}
}
func (r *localDockerResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}
func localDockerModelToAPI(m localDockerResourceModel) remoteAPI {
return remoteAPI{
Name: m.Name.ValueString(),
PackageType: "docker",
RepoType: "local",
Description: m.Description.ValueString(),
}
}
func localDockerAPIToModel(api remoteAPI) localDockerResourceModel {
return localDockerResourceModel{
Name: types.StringValue(api.Name),
Description: types.StringValue(api.Description),
}
}