From 7783b571b71f6e6808a10ba42adb9abe554900f9 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Fri, 16 Oct 2026 23:33:59 +1100 Subject: [PATCH 1/2] Add artifactapi_local_goproxy resource --- README.md | 1 + .../artifactapi_local_goproxy/main.tf | 4 + internal/provider/provider.go | 1 + internal/provider/provider_test.go | 5 +- internal/provider/resource_local_goproxy.go | 164 ++++++++++++++++++ .../provider/resource_local_goproxy_test.go | 125 +++++++++++++ 6 files changed, 298 insertions(+), 2 deletions(-) create mode 100644 examples/resources/artifactapi_local_goproxy/main.tf create mode 100644 internal/provider/resource_local_goproxy.go create mode 100644 internal/provider/resource_local_goproxy_test.go diff --git a/README.md b/README.md index 4d9266e..bac480a 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,7 @@ Available resource types: - `artifactapi_local_deb` - `artifactapi_local_alpine` - `artifactapi_local_terraform` +- `artifactapi_local_goproxy` — a Go module proxy (GOPROXY protocol) for private modules Each takes just `name` (required, forces replacement) and an optional `description`. diff --git a/examples/resources/artifactapi_local_goproxy/main.tf b/examples/resources/artifactapi_local_goproxy/main.tf new file mode 100644 index 0000000..24291d4 --- /dev/null +++ b/examples/resources/artifactapi_local_goproxy/main.tf @@ -0,0 +1,4 @@ +resource "artifactapi_local_goproxy" "internal" { + name = "go-internal" + description = "Internal Go module proxy" +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 16e4a24..293d97c 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -91,6 +91,7 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res NewLocalAlpineResource, NewLocalDockerResource, NewLocalGenericResource, + NewLocalGoProxyResource, } } diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 2a6f93d..a396dcf 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -66,8 +66,8 @@ func TestProvider_Resources(t *testing.T) { p := &ArtifactAPIProvider{version: "1.0.0"} resources := p.Resources(context.Background()) - // 15 remote resource types + 1 virtual + 7 local = 23 - expectedCount := 23 + // 15 remote resource types + 1 virtual + 8 local = 24 + expectedCount := 24 if len(resources) != expectedCount { t.Fatalf("expected %d resources, got %d", expectedCount, len(resources)) } @@ -119,6 +119,7 @@ func TestProvider_Resources_ContainsExpectedTypes(t *testing.T) { "artifactapi_local_alpine", "artifactapi_local_docker", "artifactapi_local_generic", + "artifactapi_local_goproxy", } for _, name := range expected { diff --git a/internal/provider/resource_local_goproxy.go b/internal/provider/resource_local_goproxy.go new file mode 100644 index 0000000..6f35176 --- /dev/null +++ b/internal/provider/resource_local_goproxy.go @@ -0,0 +1,164 @@ +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 = &localGoProxyResource{} + _ resource.ResourceWithImportState = &localGoProxyResource{} +) + +type localGoProxyResource struct { + client *apiClient +} + +type localGoProxyResourceModel struct { + Name types.String `tfsdk:"name"` + Description types.String `tfsdk:"description"` +} + +func NewLocalGoProxyResource() resource.Resource { + return &localGoProxyResource{} +} + +func (r *localGoProxyResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_local_goproxy" +} + +func (r *localGoProxyResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "Manages a local ArtifactAPI Go module proxy for hosting private Go modules directly.", + Attributes: map[string]schema.Attribute{ + "name": schema.StringAttribute{ + Description: "Unique name of the local Go module proxy.", + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "description": schema.StringAttribute{ + Description: "Human-readable description.", + Optional: true, + Computed: true, + Default: stringdefault.StaticString(""), + }, + }, + } +} + +func (r *localGoProxyResource) 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 *localGoProxyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan localGoProxyResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localGoProxyModelToAPI(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 goproxy failed", err.Error()) + return + } + + state := localGoProxyAPIToModel(created) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localGoProxyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state localGoProxyResourceModel + 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 goproxy failed", err.Error()) + return + } + + newState := localGoProxyAPIToModel(remote) + resp.Diagnostics.Append(resp.State.Set(ctx, newState)...) +} + +func (r *localGoProxyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan localGoProxyResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localGoProxyModelToAPI(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 goproxy failed", err.Error()) + return + } + + state := localGoProxyAPIToModel(updated) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localGoProxyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state localGoProxyResourceModel + 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 goproxy failed", err.Error()) + return + } +} + +func (r *localGoProxyResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) +} + +func localGoProxyModelToAPI(m localGoProxyResourceModel) remoteAPI { + return remoteAPI{ + Name: m.Name.ValueString(), + PackageType: "goproxy", + RepoType: "local", + Description: m.Description.ValueString(), + } +} + +func localGoProxyAPIToModel(api remoteAPI) localGoProxyResourceModel { + return localGoProxyResourceModel{ + Name: types.StringValue(api.Name), + Description: types.StringValue(api.Description), + } +} diff --git a/internal/provider/resource_local_goproxy_test.go b/internal/provider/resource_local_goproxy_test.go new file mode 100644 index 0000000..3d50702 --- /dev/null +++ b/internal/provider/resource_local_goproxy_test.go @@ -0,0 +1,125 @@ +package provider + +import ( + "context" + "testing" + + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +func TestLocalGoProxyModelToAPI(t *testing.T) { + model := localGoProxyResourceModel{ + Name: types.StringValue("go-internal"), + Description: types.StringValue("Internal Go modules"), + } + + api := localGoProxyModelToAPI(model) + + if api.Name != "go-internal" { + t.Errorf("Name: expected go-internal, got %s", api.Name) + } + if api.PackageType != "goproxy" { + t.Errorf("PackageType: expected goproxy, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } + if api.Description != "Internal Go modules" { + t.Errorf("Description: expected 'Internal Go modules', got %s", api.Description) + } +} + +func TestLocalGoProxyModelToAPI_EmptyDescription(t *testing.T) { + model := localGoProxyResourceModel{ + Name: types.StringValue("go-empty"), + Description: types.StringValue(""), + } + + api := localGoProxyModelToAPI(model) + + if api.Name != "go-empty" { + t.Errorf("Name: expected go-empty, got %s", api.Name) + } + if api.Description != "" { + t.Errorf("Description: expected empty string, got %s", api.Description) + } + if api.PackageType != "goproxy" { + t.Errorf("PackageType: expected goproxy, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } +} + +func TestLocalGoProxyAPIToModel(t *testing.T) { + api := remoteAPI{ + Name: "go-internal", + PackageType: "goproxy", + RepoType: "local", + Description: "Internal Go modules", + ManagedBy: "terraform", + } + + model := localGoProxyAPIToModel(api) + + if model.Name.ValueString() != "go-internal" { + t.Errorf("Name: expected go-internal, got %s", model.Name.ValueString()) + } + if model.Description.ValueString() != "Internal Go modules" { + t.Errorf("Description: expected 'Internal Go modules', got %s", model.Description.ValueString()) + } +} + +func TestLocalGoProxyRoundTrip(t *testing.T) { + original := localGoProxyResourceModel{ + Name: types.StringValue("roundtrip-go"), + Description: types.StringValue("Round trip test"), + } + + api := localGoProxyModelToAPI(original) + result := localGoProxyAPIToModel(api) + + if result.Name.ValueString() != original.Name.ValueString() { + t.Errorf("Name: expected %s, got %s", original.Name.ValueString(), result.Name.ValueString()) + } + if result.Description.ValueString() != original.Description.ValueString() { + t.Errorf("Description: expected %s, got %s", original.Description.ValueString(), result.Description.ValueString()) + } +} + +func TestLocalGoProxyResource_Metadata(t *testing.T) { + r := NewLocalGoProxyResource() + req := resource.MetadataRequest{ProviderTypeName: "artifactapi"} + var resp resource.MetadataResponse + r.Metadata(context.Background(), req, &resp) + if resp.TypeName != "artifactapi_local_goproxy" { + t.Errorf("expected artifactapi_local_goproxy, got %s", resp.TypeName) + } +} + +func TestLocalGoProxyResource_Schema(t *testing.T) { + r := NewLocalGoProxyResource() + req := resource.SchemaRequest{} + var resp resource.SchemaResponse + r.Schema(context.Background(), req, &resp) + + expectedAttrs := []string{"name", "description"} + for _, attr := range expectedAttrs { + if _, ok := resp.Schema.Attributes[attr]; !ok { + t.Errorf("missing expected attribute: %s", attr) + } + } + + if len(resp.Schema.Attributes) != len(expectedAttrs) { + t.Errorf("expected %d attributes, got %d", len(expectedAttrs), len(resp.Schema.Attributes)) + } +} + +func TestNewLocalGoProxyResource_Type(t *testing.T) { + r := NewLocalGoProxyResource() + _, ok := r.(*localGoProxyResource) + if !ok { + t.Error("expected *localGoProxyResource") + } +} -- 2.47.3 From 81c2bc1e4ac2713ea836ba293e9bdac004287c28 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Fri, 16 Oct 2026 23:35:45 +1100 Subject: [PATCH 2/2] Retrigger CI -- 2.47.3