From 924ca78104f36992c9e241e65216bec4728a7059 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Fri, 16 Oct 2026 23:10:39 +1100 Subject: [PATCH] Add artifactapi_local_npm resource --- README.md | 1 + .../resources/artifactapi_local_npm/main.tf | 4 + internal/provider/provider.go | 1 + internal/provider/provider_test.go | 5 +- internal/provider/resource_local_npm.go | 164 ++++++++++++++++++ internal/provider/resource_local_npm_test.go | 125 +++++++++++++ 6 files changed, 298 insertions(+), 2 deletions(-) create mode 100644 examples/resources/artifactapi_local_npm/main.tf create mode 100644 internal/provider/resource_local_npm.go create mode 100644 internal/provider/resource_local_npm_test.go diff --git a/README.md b/README.md index 4d9266e..1a6b5cb 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,7 @@ Available resource types: - `artifactapi_local_docker` — a container registry (Docker Registry HTTP API V2, push and pull) - `artifactapi_local_pypi` +- `artifactapi_local_npm` — an npm registry (`npm publish` and `npm install`) - `artifactapi_local_rpm` - `artifactapi_local_deb` - `artifactapi_local_alpine` diff --git a/examples/resources/artifactapi_local_npm/main.tf b/examples/resources/artifactapi_local_npm/main.tf new file mode 100644 index 0000000..05b64d9 --- /dev/null +++ b/examples/resources/artifactapi_local_npm/main.tf @@ -0,0 +1,4 @@ +resource "artifactapi_local_npm" "internal" { + name = "npm-internal" + description = "Internal npm package registry" +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 16e4a24..e9d70d6 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -86,6 +86,7 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res NewVirtualResource, NewLocalTerraformResource, NewLocalPyPIResource, + NewLocalNPMResource, NewLocalRPMResource, NewLocalDebResource, NewLocalAlpineResource, diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 2a6f93d..a279f3b 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)) } @@ -114,6 +114,7 @@ func TestProvider_Resources_ContainsExpectedTypes(t *testing.T) { "artifactapi_virtual", "artifactapi_local_terraform", "artifactapi_local_pypi", + "artifactapi_local_npm", "artifactapi_local_rpm", "artifactapi_local_deb", "artifactapi_local_alpine", diff --git a/internal/provider/resource_local_npm.go b/internal/provider/resource_local_npm.go new file mode 100644 index 0000000..3d23acb --- /dev/null +++ b/internal/provider/resource_local_npm.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 = &localNPMResource{} + _ resource.ResourceWithImportState = &localNPMResource{} +) + +type localNPMResource struct { + client *apiClient +} + +type localNPMResourceModel struct { + Name types.String `tfsdk:"name"` + Description types.String `tfsdk:"description"` +} + +func NewLocalNPMResource() resource.Resource { + return &localNPMResource{} +} + +func (r *localNPMResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_local_npm" +} + +func (r *localNPMResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "Manages a local ArtifactAPI NPM repository for hosting npm packages directly.", + Attributes: map[string]schema.Attribute{ + "name": schema.StringAttribute{ + Description: "Unique name of the local NPM repository.", + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "description": schema.StringAttribute{ + Description: "Human-readable description.", + Optional: true, + Computed: true, + Default: stringdefault.StaticString(""), + }, + }, + } +} + +func (r *localNPMResource) 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 *localNPMResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan localNPMResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localNPMModelToAPI(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 npm failed", err.Error()) + return + } + + state := localNPMAPIToModel(created) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localNPMResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state localNPMResourceModel + 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 npm failed", err.Error()) + return + } + + newState := localNPMAPIToModel(remote) + resp.Diagnostics.Append(resp.State.Set(ctx, newState)...) +} + +func (r *localNPMResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan localNPMResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localNPMModelToAPI(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 npm failed", err.Error()) + return + } + + state := localNPMAPIToModel(updated) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localNPMResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state localNPMResourceModel + 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 npm failed", err.Error()) + return + } +} + +func (r *localNPMResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) +} + +func localNPMModelToAPI(m localNPMResourceModel) remoteAPI { + return remoteAPI{ + Name: m.Name.ValueString(), + PackageType: "npm", + RepoType: "local", + Description: m.Description.ValueString(), + } +} + +func localNPMAPIToModel(api remoteAPI) localNPMResourceModel { + return localNPMResourceModel{ + Name: types.StringValue(api.Name), + Description: types.StringValue(api.Description), + } +} diff --git a/internal/provider/resource_local_npm_test.go b/internal/provider/resource_local_npm_test.go new file mode 100644 index 0000000..c8bf382 --- /dev/null +++ b/internal/provider/resource_local_npm_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 TestLocalNPMModelToAPI(t *testing.T) { + model := localNPMResourceModel{ + Name: types.StringValue("npm-internal"), + Description: types.StringValue("Internal NPM registry"), + } + + api := localNPMModelToAPI(model) + + if api.Name != "npm-internal" { + t.Errorf("Name: expected npm-internal, got %s", api.Name) + } + if api.PackageType != "npm" { + t.Errorf("PackageType: expected npm, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } + if api.Description != "Internal NPM registry" { + t.Errorf("Description: expected 'Internal NPM registry', got %s", api.Description) + } +} + +func TestLocalNPMModelToAPI_EmptyDescription(t *testing.T) { + model := localNPMResourceModel{ + Name: types.StringValue("npm-empty"), + Description: types.StringValue(""), + } + + api := localNPMModelToAPI(model) + + if api.Name != "npm-empty" { + t.Errorf("Name: expected npm-empty, got %s", api.Name) + } + if api.Description != "" { + t.Errorf("Description: expected empty string, got %s", api.Description) + } + if api.PackageType != "npm" { + t.Errorf("PackageType: expected npm, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } +} + +func TestLocalNPMAPIToModel(t *testing.T) { + api := remoteAPI{ + Name: "npm-internal", + PackageType: "npm", + RepoType: "local", + Description: "Internal NPM registry", + ManagedBy: "terraform", + } + + model := localNPMAPIToModel(api) + + if model.Name.ValueString() != "npm-internal" { + t.Errorf("Name: expected npm-internal, got %s", model.Name.ValueString()) + } + if model.Description.ValueString() != "Internal NPM registry" { + t.Errorf("Description: expected 'Internal NPM registry', got %s", model.Description.ValueString()) + } +} + +func TestLocalNPMRoundTrip(t *testing.T) { + original := localNPMResourceModel{ + Name: types.StringValue("roundtrip-npm"), + Description: types.StringValue("Round trip test"), + } + + api := localNPMModelToAPI(original) + result := localNPMAPIToModel(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 TestLocalNPMResource_Metadata(t *testing.T) { + r := NewLocalNPMResource() + req := resource.MetadataRequest{ProviderTypeName: "artifactapi"} + var resp resource.MetadataResponse + r.Metadata(context.Background(), req, &resp) + if resp.TypeName != "artifactapi_local_npm" { + t.Errorf("expected artifactapi_local_npm, got %s", resp.TypeName) + } +} + +func TestLocalNPMResource_Schema(t *testing.T) { + r := NewLocalNPMResource() + 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 TestNewLocalNPMResource_Type(t *testing.T) { + r := NewLocalNPMResource() + _, ok := r.(*localNPMResource) + if !ok { + t.Error("expected *localNPMResource") + } +}