From 1bddc48e5af178fd80190e0c741bd077387ce9c9 Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Tue, 23 Jun 2026 23:16:17 +1000 Subject: [PATCH] feat: add artifactapi_local_pypi and artifactapi_local_rpm resource types --- .../resources/artifactapi_local_pypi/main.tf | 4 + .../resources/artifactapi_local_rpm/main.tf | 4 + internal/provider/provider.go | 2 + internal/provider/provider_test.go | 6 +- internal/provider/resource_local_pypi.go | 164 ++++++++++++++++++ internal/provider/resource_local_pypi_test.go | 125 +++++++++++++ internal/provider/resource_local_rpm.go | 164 ++++++++++++++++++ internal/provider/resource_local_rpm_test.go | 125 +++++++++++++ 8 files changed, 592 insertions(+), 2 deletions(-) create mode 100644 examples/resources/artifactapi_local_pypi/main.tf create mode 100644 examples/resources/artifactapi_local_rpm/main.tf create mode 100644 internal/provider/resource_local_pypi.go create mode 100644 internal/provider/resource_local_pypi_test.go create mode 100644 internal/provider/resource_local_rpm.go create mode 100644 internal/provider/resource_local_rpm_test.go diff --git a/examples/resources/artifactapi_local_pypi/main.tf b/examples/resources/artifactapi_local_pypi/main.tf new file mode 100644 index 0000000..3d138cd --- /dev/null +++ b/examples/resources/artifactapi_local_pypi/main.tf @@ -0,0 +1,4 @@ +resource "artifactapi_local_pypi" "internal" { + name = "pypi-internal" + description = "Internal PyPI package registry" +} diff --git a/examples/resources/artifactapi_local_rpm/main.tf b/examples/resources/artifactapi_local_rpm/main.tf new file mode 100644 index 0000000..402873a --- /dev/null +++ b/examples/resources/artifactapi_local_rpm/main.tf @@ -0,0 +1,4 @@ +resource "artifactapi_local_rpm" "internal" { + name = "rpm-internal" + description = "Internal RPM package repository" +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index e4709e2..5cbe343 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -69,6 +69,8 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res newRemoteResource("goproxy"), NewVirtualResource, NewLocalTerraformResource, + NewLocalPyPIResource, + NewLocalRPMResource, } } diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index be864d2..bd72bc9 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()) - // 10 remote resource types + 1 virtual + 1 local_terraform = 12 - expectedCount := 12 + // 10 remote resource types + 1 virtual + 1 local_terraform + 1 local_pypi + 1 local_rpm = 14 + expectedCount := 14 if len(resources) != expectedCount { t.Fatalf("expected %d resources, got %d", expectedCount, len(resources)) } @@ -108,6 +108,8 @@ func TestProvider_Resources_ContainsExpectedTypes(t *testing.T) { "artifactapi_remote_goproxy", "artifactapi_virtual", "artifactapi_local_terraform", + "artifactapi_local_pypi", + "artifactapi_local_rpm", } for _, name := range expected { diff --git a/internal/provider/resource_local_pypi.go b/internal/provider/resource_local_pypi.go new file mode 100644 index 0000000..7109d92 --- /dev/null +++ b/internal/provider/resource_local_pypi.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 = &localPyPIResource{} + _ resource.ResourceWithImportState = &localPyPIResource{} +) + +type localPyPIResource struct { + client *apiClient +} + +type localPyPIResourceModel struct { + Name types.String `tfsdk:"name"` + Description types.String `tfsdk:"description"` +} + +func NewLocalPyPIResource() resource.Resource { + return &localPyPIResource{} +} + +func (r *localPyPIResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_local_pypi" +} + +func (r *localPyPIResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "Manages a local ArtifactAPI PyPI repository for hosting Python packages directly.", + Attributes: map[string]schema.Attribute{ + "name": schema.StringAttribute{ + Description: "Unique name of the local PyPI repository.", + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "description": schema.StringAttribute{ + Description: "Human-readable description.", + Optional: true, + Computed: true, + Default: stringdefault.StaticString(""), + }, + }, + } +} + +func (r *localPyPIResource) 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 *localPyPIResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan localPyPIResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localPyPIModelToAPI(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 pypi failed", err.Error()) + return + } + + state := localPyPIAPIToModel(created) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localPyPIResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state localPyPIResourceModel + 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 pypi failed", err.Error()) + return + } + + newState := localPyPIAPIToModel(remote) + resp.Diagnostics.Append(resp.State.Set(ctx, newState)...) +} + +func (r *localPyPIResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan localPyPIResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localPyPIModelToAPI(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 pypi failed", err.Error()) + return + } + + state := localPyPIAPIToModel(updated) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localPyPIResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state localPyPIResourceModel + 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 pypi failed", err.Error()) + return + } +} + +func (r *localPyPIResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) +} + +func localPyPIModelToAPI(m localPyPIResourceModel) remoteAPI { + return remoteAPI{ + Name: m.Name.ValueString(), + PackageType: "pypi", + RepoType: "local", + Description: m.Description.ValueString(), + } +} + +func localPyPIAPIToModel(api remoteAPI) localPyPIResourceModel { + return localPyPIResourceModel{ + Name: types.StringValue(api.Name), + Description: types.StringValue(api.Description), + } +} diff --git a/internal/provider/resource_local_pypi_test.go b/internal/provider/resource_local_pypi_test.go new file mode 100644 index 0000000..8da79f6 --- /dev/null +++ b/internal/provider/resource_local_pypi_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 TestLocalPyPIModelToAPI(t *testing.T) { + model := localPyPIResourceModel{ + Name: types.StringValue("pypi-internal"), + Description: types.StringValue("Internal PyPI registry"), + } + + api := localPyPIModelToAPI(model) + + if api.Name != "pypi-internal" { + t.Errorf("Name: expected pypi-internal, got %s", api.Name) + } + if api.PackageType != "pypi" { + t.Errorf("PackageType: expected pypi, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } + if api.Description != "Internal PyPI registry" { + t.Errorf("Description: expected 'Internal PyPI registry', got %s", api.Description) + } +} + +func TestLocalPyPIModelToAPI_EmptyDescription(t *testing.T) { + model := localPyPIResourceModel{ + Name: types.StringValue("pypi-empty"), + Description: types.StringValue(""), + } + + api := localPyPIModelToAPI(model) + + if api.Name != "pypi-empty" { + t.Errorf("Name: expected pypi-empty, got %s", api.Name) + } + if api.Description != "" { + t.Errorf("Description: expected empty string, got %s", api.Description) + } + if api.PackageType != "pypi" { + t.Errorf("PackageType: expected pypi, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } +} + +func TestLocalPyPIAPIToModel(t *testing.T) { + api := remoteAPI{ + Name: "pypi-internal", + PackageType: "pypi", + RepoType: "local", + Description: "Internal PyPI registry", + ManagedBy: "terraform", + } + + model := localPyPIAPIToModel(api) + + if model.Name.ValueString() != "pypi-internal" { + t.Errorf("Name: expected pypi-internal, got %s", model.Name.ValueString()) + } + if model.Description.ValueString() != "Internal PyPI registry" { + t.Errorf("Description: expected 'Internal PyPI registry', got %s", model.Description.ValueString()) + } +} + +func TestLocalPyPIRoundTrip(t *testing.T) { + original := localPyPIResourceModel{ + Name: types.StringValue("roundtrip-pypi"), + Description: types.StringValue("Round trip test"), + } + + api := localPyPIModelToAPI(original) + result := localPyPIAPIToModel(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 TestLocalPyPIResource_Metadata(t *testing.T) { + r := NewLocalPyPIResource() + req := resource.MetadataRequest{ProviderTypeName: "artifactapi"} + var resp resource.MetadataResponse + r.Metadata(context.Background(), req, &resp) + if resp.TypeName != "artifactapi_local_pypi" { + t.Errorf("expected artifactapi_local_pypi, got %s", resp.TypeName) + } +} + +func TestLocalPyPIResource_Schema(t *testing.T) { + r := NewLocalPyPIResource() + 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 TestNewLocalPyPIResource_Type(t *testing.T) { + r := NewLocalPyPIResource() + _, ok := r.(*localPyPIResource) + if !ok { + t.Error("expected *localPyPIResource") + } +} diff --git a/internal/provider/resource_local_rpm.go b/internal/provider/resource_local_rpm.go new file mode 100644 index 0000000..bcd8172 --- /dev/null +++ b/internal/provider/resource_local_rpm.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 = &localRPMResource{} + _ resource.ResourceWithImportState = &localRPMResource{} +) + +type localRPMResource struct { + client *apiClient +} + +type localRPMResourceModel struct { + Name types.String `tfsdk:"name"` + Description types.String `tfsdk:"description"` +} + +func NewLocalRPMResource() resource.Resource { + return &localRPMResource{} +} + +func (r *localRPMResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_local_rpm" +} + +func (r *localRPMResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "Manages a local ArtifactAPI RPM repository for hosting RPM packages directly.", + Attributes: map[string]schema.Attribute{ + "name": schema.StringAttribute{ + Description: "Unique name of the local RPM repository.", + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "description": schema.StringAttribute{ + Description: "Human-readable description.", + Optional: true, + Computed: true, + Default: stringdefault.StaticString(""), + }, + }, + } +} + +func (r *localRPMResource) 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 *localRPMResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan localRPMResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localRPMModelToAPI(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 rpm failed", err.Error()) + return + } + + state := localRPMAPIToModel(created) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localRPMResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state localRPMResourceModel + 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 rpm failed", err.Error()) + return + } + + newState := localRPMAPIToModel(remote) + resp.Diagnostics.Append(resp.State.Set(ctx, newState)...) +} + +func (r *localRPMResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan localRPMResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localRPMModelToAPI(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 rpm failed", err.Error()) + return + } + + state := localRPMAPIToModel(updated) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localRPMResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state localRPMResourceModel + 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 rpm failed", err.Error()) + return + } +} + +func (r *localRPMResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) +} + +func localRPMModelToAPI(m localRPMResourceModel) remoteAPI { + return remoteAPI{ + Name: m.Name.ValueString(), + PackageType: "rpm", + RepoType: "local", + Description: m.Description.ValueString(), + } +} + +func localRPMAPIToModel(api remoteAPI) localRPMResourceModel { + return localRPMResourceModel{ + Name: types.StringValue(api.Name), + Description: types.StringValue(api.Description), + } +} diff --git a/internal/provider/resource_local_rpm_test.go b/internal/provider/resource_local_rpm_test.go new file mode 100644 index 0000000..4081ade --- /dev/null +++ b/internal/provider/resource_local_rpm_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 TestLocalRPMModelToAPI(t *testing.T) { + model := localRPMResourceModel{ + Name: types.StringValue("rpm-internal"), + Description: types.StringValue("Internal RPM repository"), + } + + api := localRPMModelToAPI(model) + + if api.Name != "rpm-internal" { + t.Errorf("Name: expected rpm-internal, got %s", api.Name) + } + if api.PackageType != "rpm" { + t.Errorf("PackageType: expected rpm, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } + if api.Description != "Internal RPM repository" { + t.Errorf("Description: expected 'Internal RPM repository', got %s", api.Description) + } +} + +func TestLocalRPMModelToAPI_EmptyDescription(t *testing.T) { + model := localRPMResourceModel{ + Name: types.StringValue("rpm-empty"), + Description: types.StringValue(""), + } + + api := localRPMModelToAPI(model) + + if api.Name != "rpm-empty" { + t.Errorf("Name: expected rpm-empty, got %s", api.Name) + } + if api.Description != "" { + t.Errorf("Description: expected empty string, got %s", api.Description) + } + if api.PackageType != "rpm" { + t.Errorf("PackageType: expected rpm, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } +} + +func TestLocalRPMAPIToModel(t *testing.T) { + api := remoteAPI{ + Name: "rpm-internal", + PackageType: "rpm", + RepoType: "local", + Description: "Internal RPM repository", + ManagedBy: "terraform", + } + + model := localRPMAPIToModel(api) + + if model.Name.ValueString() != "rpm-internal" { + t.Errorf("Name: expected rpm-internal, got %s", model.Name.ValueString()) + } + if model.Description.ValueString() != "Internal RPM repository" { + t.Errorf("Description: expected 'Internal RPM repository', got %s", model.Description.ValueString()) + } +} + +func TestLocalRPMRoundTrip(t *testing.T) { + original := localRPMResourceModel{ + Name: types.StringValue("roundtrip-rpm"), + Description: types.StringValue("Round trip test"), + } + + api := localRPMModelToAPI(original) + result := localRPMAPIToModel(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 TestLocalRPMResource_Metadata(t *testing.T) { + r := NewLocalRPMResource() + req := resource.MetadataRequest{ProviderTypeName: "artifactapi"} + var resp resource.MetadataResponse + r.Metadata(context.Background(), req, &resp) + if resp.TypeName != "artifactapi_local_rpm" { + t.Errorf("expected artifactapi_local_rpm, got %s", resp.TypeName) + } +} + +func TestLocalRPMResource_Schema(t *testing.T) { + r := NewLocalRPMResource() + 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 TestNewLocalRPMResource_Type(t *testing.T) { + r := NewLocalRPMResource() + _, ok := r.(*localRPMResource) + if !ok { + t.Error("expected *localRPMResource") + } +}