From a83eab24493d16d9b47d74023242ac3a712f9367 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Fri, 16 Oct 2026 23:17:09 +1100 Subject: [PATCH 1/2] Add artifactapi_local_helm resource --- README.md | 1 + .../resources/artifactapi_local_helm/main.tf | 4 + internal/provider/provider.go | 1 + internal/provider/provider_test.go | 3 +- internal/provider/resource_local_helm.go | 164 ++++++++++++++++++ internal/provider/resource_local_helm_test.go | 125 +++++++++++++ 6 files changed, 297 insertions(+), 1 deletion(-) create mode 100644 examples/resources/artifactapi_local_helm/main.tf create mode 100644 internal/provider/resource_local_helm.go create mode 100644 internal/provider/resource_local_helm_test.go diff --git a/README.md b/README.md index 4d9266e..ae0d657 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,7 @@ Available resource types: - `artifactapi_local_rpm` - `artifactapi_local_deb` - `artifactapi_local_alpine` +- `artifactapi_local_helm` — a chart repository (index.yaml generated from uploaded charts) - `artifactapi_local_terraform` Each takes just `name` (required, forces replacement) and an optional diff --git a/examples/resources/artifactapi_local_helm/main.tf b/examples/resources/artifactapi_local_helm/main.tf new file mode 100644 index 0000000..d4f4e97 --- /dev/null +++ b/examples/resources/artifactapi_local_helm/main.tf @@ -0,0 +1,4 @@ +resource "artifactapi_local_helm" "internal" { + name = "helm-internal" + description = "Internal Helm chart repository" +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 16e4a24..fd3040d 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -89,6 +89,7 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res NewLocalRPMResource, NewLocalDebResource, NewLocalAlpineResource, + NewLocalHelmResource, NewLocalDockerResource, NewLocalGenericResource, } diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 2a6f93d..3226ac2 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -67,7 +67,7 @@ func TestProvider_Resources(t *testing.T) { resources := p.Resources(context.Background()) // 15 remote resource types + 1 virtual + 7 local = 23 - expectedCount := 23 + expectedCount := 24 if len(resources) != expectedCount { t.Fatalf("expected %d resources, got %d", expectedCount, len(resources)) } @@ -117,6 +117,7 @@ func TestProvider_Resources_ContainsExpectedTypes(t *testing.T) { "artifactapi_local_rpm", "artifactapi_local_deb", "artifactapi_local_alpine", + "artifactapi_local_helm", "artifactapi_local_docker", "artifactapi_local_generic", } diff --git a/internal/provider/resource_local_helm.go b/internal/provider/resource_local_helm.go new file mode 100644 index 0000000..5c0eacf --- /dev/null +++ b/internal/provider/resource_local_helm.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 = &localHelmResource{} + _ resource.ResourceWithImportState = &localHelmResource{} +) + +type localHelmResource struct { + client *apiClient +} + +type localHelmResourceModel struct { + Name types.String `tfsdk:"name"` + Description types.String `tfsdk:"description"` +} + +func NewLocalHelmResource() resource.Resource { + return &localHelmResource{} +} + +func (r *localHelmResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_local_helm" +} + +func (r *localHelmResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "Manages a local ArtifactAPI Helm repository for hosting Helm charts directly.", + Attributes: map[string]schema.Attribute{ + "name": schema.StringAttribute{ + Description: "Unique name of the local Helm repository.", + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "description": schema.StringAttribute{ + Description: "Human-readable description.", + Optional: true, + Computed: true, + Default: stringdefault.StaticString(""), + }, + }, + } +} + +func (r *localHelmResource) 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 *localHelmResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan localHelmResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localHelmModelToAPI(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 helm failed", err.Error()) + return + } + + state := localHelmAPIToModel(created) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localHelmResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state localHelmResourceModel + 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 helm failed", err.Error()) + return + } + + newState := localHelmAPIToModel(remote) + resp.Diagnostics.Append(resp.State.Set(ctx, newState)...) +} + +func (r *localHelmResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan localHelmResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localHelmModelToAPI(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 helm failed", err.Error()) + return + } + + state := localHelmAPIToModel(updated) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localHelmResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state localHelmResourceModel + 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 helm failed", err.Error()) + return + } +} + +func (r *localHelmResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) +} + +func localHelmModelToAPI(m localHelmResourceModel) remoteAPI { + return remoteAPI{ + Name: m.Name.ValueString(), + PackageType: "helm", + RepoType: "local", + Description: m.Description.ValueString(), + } +} + +func localHelmAPIToModel(api remoteAPI) localHelmResourceModel { + return localHelmResourceModel{ + Name: types.StringValue(api.Name), + Description: types.StringValue(api.Description), + } +} diff --git a/internal/provider/resource_local_helm_test.go b/internal/provider/resource_local_helm_test.go new file mode 100644 index 0000000..0927986 --- /dev/null +++ b/internal/provider/resource_local_helm_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 TestLocalHelmModelToAPI(t *testing.T) { + model := localHelmResourceModel{ + Name: types.StringValue("helm-internal"), + Description: types.StringValue("Internal Helm repository"), + } + + api := localHelmModelToAPI(model) + + if api.Name != "helm-internal" { + t.Errorf("Name: expected helm-internal, got %s", api.Name) + } + if api.PackageType != "helm" { + t.Errorf("PackageType: expected helm, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } + if api.Description != "Internal Helm repository" { + t.Errorf("Description: expected 'Internal Helm repository', got %s", api.Description) + } +} + +func TestLocalHelmModelToAPI_EmptyDescription(t *testing.T) { + model := localHelmResourceModel{ + Name: types.StringValue("helm-empty"), + Description: types.StringValue(""), + } + + api := localHelmModelToAPI(model) + + if api.Name != "helm-empty" { + t.Errorf("Name: expected helm-empty, got %s", api.Name) + } + if api.Description != "" { + t.Errorf("Description: expected empty string, got %s", api.Description) + } + if api.PackageType != "helm" { + t.Errorf("PackageType: expected helm, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } +} + +func TestLocalHelmAPIToModel(t *testing.T) { + api := remoteAPI{ + Name: "helm-internal", + PackageType: "helm", + RepoType: "local", + Description: "Internal Helm repository", + ManagedBy: "terraform", + } + + model := localHelmAPIToModel(api) + + if model.Name.ValueString() != "helm-internal" { + t.Errorf("Name: expected helm-internal, got %s", model.Name.ValueString()) + } + if model.Description.ValueString() != "Internal Helm repository" { + t.Errorf("Description: expected 'Internal Helm repository', got %s", model.Description.ValueString()) + } +} + +func TestLocalHelmRoundTrip(t *testing.T) { + original := localHelmResourceModel{ + Name: types.StringValue("roundtrip-helm"), + Description: types.StringValue("Round trip test"), + } + + api := localHelmModelToAPI(original) + result := localHelmAPIToModel(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 TestLocalHelmResource_Metadata(t *testing.T) { + r := NewLocalHelmResource() + req := resource.MetadataRequest{ProviderTypeName: "artifactapi"} + var resp resource.MetadataResponse + r.Metadata(context.Background(), req, &resp) + if resp.TypeName != "artifactapi_local_helm" { + t.Errorf("expected artifactapi_local_helm, got %s", resp.TypeName) + } +} + +func TestLocalHelmResource_Schema(t *testing.T) { + r := NewLocalHelmResource() + 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 TestNewLocalHelmResource_Type(t *testing.T) { + r := NewLocalHelmResource() + _, ok := r.(*localHelmResource) + if !ok { + t.Error("expected *localHelmResource") + } +} -- 2.47.3 From 3b65a3ca928afe2f615de690f5a98a211deac56d Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sat, 17 Oct 2026 12:40:36 +1100 Subject: [PATCH 2/2] Fix resource count comment --- internal/provider/provider_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 3226ac2..de85705 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -66,7 +66,7 @@ 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 + // 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)) -- 2.47.3