1 Commits

Author SHA1 Message Date
unkinben be28507b92 feat: add zip packaging target and woodpecker release pipeline
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
2026-06-22 23:36:03 +10:00
14 changed files with 13 additions and 408 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
when: when:
- event: pull_request - event: [push, pull_request]
steps: steps:
- name: build - name: build
-18
View File
@@ -1,18 +0,0 @@
when:
- event: pull_request
steps:
- name: pre-commit
image: git.unkin.net/unkin/almalinux9-gobuilder:20260606
commands:
- uvx pre-commit run --all-files
backend_options:
kubernetes:
serviceAccountName: default
resources:
requests:
memory: 512Mi
cpu: 1
limits:
memory: 2Gi
cpu: 2
+4 -3
View File
@@ -3,18 +3,19 @@ when:
steps: steps:
- name: package - name: package
image: git.unkin.net/unkin/almalinux9-gobuilder:20260606 image: golang:1.25
commands: commands:
- apt-get update && apt-get install -y zip - apt-get update && apt-get install -y zip
- make package VERSION=${CI_COMMIT_TAG} - make package VERSION=${CI_COMMIT_TAG}
- name: upload - name: upload
image: git.unkin.net/unkin/almalinux9-base:20260606 image: curlimages/curl:latest
secrets: [artifactapi_url, artifactapi_repo]
commands: commands:
- | - |
VERSION=$(echo ${CI_COMMIT_TAG} | sed 's/^v//') VERSION=$(echo ${CI_COMMIT_TAG} | sed 's/^v//')
FILE="terraform-provider-artifactapi_${VERSION}_linux_amd64.zip" FILE="terraform-provider-artifactapi_${VERSION}_linux_amd64.zip"
curl -f -X PUT \ curl -f -X PUT \
"https://artifactapi3.k8s.syd1.au.unkin.net/api/v2/remotes/terraform-unkin/files/unkin/artifactapi/$${FILE}" \ "$${ARTIFACTAPI_URL}/api/v2/remotes/$${ARTIFACTAPI_REPO}/files/unkin/artifactapi/$${FILE}" \
-H "Content-Type: application/zip" \ -H "Content-Type: application/zip" \
--data-binary @"$${FILE}" --data-binary @"$${FILE}"
+1 -1
View File
@@ -1,5 +1,5 @@
when: when:
- event: pull_request - event: [push, pull_request]
steps: steps:
- name: lint - name: lint
+2 -20
View File
@@ -1,7 +1,7 @@
.PHONY: build install test lint fmt clean tidy patch minor major package .PHONY: build install test lint fmt clean tidy package
BINARY := terraform-provider-artifactapi BINARY := terraform-provider-artifactapi
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "0.0.0-dev") VERSION ?= 0.0.1
OS_ARCH := linux_amd64 OS_ARCH := linux_amd64
INSTALL_VERSION := $(shell echo $(VERSION) | sed 's/^v//') INSTALL_VERSION := $(shell echo $(VERSION) | sed 's/^v//')
INSTALL_DIR := ~/.terraform.d/plugins/git.unkin.net/unkin/artifactapi/$(INSTALL_VERSION)/$(OS_ARCH) INSTALL_DIR := ~/.terraform.d/plugins/git.unkin.net/unkin/artifactapi/$(INSTALL_VERSION)/$(OS_ARCH)
@@ -41,21 +41,3 @@ clean:
tidy: tidy:
go mod tidy go mod tidy
_LATEST := $(shell git tag --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$$' | head -1)
_BASE := $(if $(_LATEST),$(_LATEST),v0.0.0)
_MAJ := $(shell echo $(_BASE) | sed 's/^v//' | cut -d. -f1)
_MIN := $(shell echo $(_BASE) | sed 's/^v//' | cut -d. -f2)
_PAT := $(shell echo $(_BASE) | sed 's/^v//' | cut -d. -f3)
patch:
@NEW=v$(_MAJ).$(_MIN).$(shell expr $(_PAT) + 1); \
git tag $$NEW && echo "Tagged $$NEW" && git push origin $$NEW
minor:
@NEW=v$(_MAJ).$(shell expr $(_MIN) + 1).0; \
git tag $$NEW && echo "Tagged $$NEW" && git push origin $$NEW
major:
@NEW=v$(shell expr $(_MAJ) + 1).0.0; \
git tag $$NEW && echo "Tagged $$NEW" && git push origin $$NEW
@@ -1,4 +0,0 @@
resource "artifactapi_local_terraform" "internal" {
name = "tf-internal"
description = "Internal terraform provider registry"
}
+1 -12
View File
@@ -16,7 +16,7 @@ func listToStrings(ctx context.Context, l types.List) []string {
} }
func stringsToList(ctx context.Context, ss []string) types.List { func stringsToList(ctx context.Context, ss []string) types.List {
if ss == nil { if len(ss) == 0 {
return types.ListNull(types.StringType) return types.ListNull(types.StringType)
} }
elems := make([]types.String, len(ss)) elems := make([]types.String, len(ss))
@@ -26,14 +26,3 @@ func stringsToList(ctx context.Context, ss []string) types.List {
list, _ := types.ListValueFrom(ctx, types.StringType, elems) list, _ := types.ListValueFrom(ctx, types.StringType, elems)
return list return list
} }
// preserveListNullEmptySemantics keeps the prior null/empty distinction when
// the API returns null for a field that was previously an empty list.
// Without this, OpenTofu reports "inconsistent result after apply" because
// the plan/state had [] but the provider returned null.
func preserveListNullEmptySemantics(prior, current types.List) types.List {
if current.IsNull() && !prior.IsNull() && len(prior.Elements()) == 0 {
return prior
}
return current
}
+2 -44
View File
@@ -63,11 +63,8 @@ func TestListToStrings_WithValues(t *testing.T) {
func TestStringsToList_EmptySlice(t *testing.T) { func TestStringsToList_EmptySlice(t *testing.T) {
ctx := context.Background() ctx := context.Background()
result := stringsToList(ctx, []string{}) result := stringsToList(ctx, []string{})
if result.IsNull() { if !result.IsNull() {
t.Fatalf("expected empty list for empty slice, got null") t.Fatalf("expected null list for empty slice, got %v", result)
}
if len(result.Elements()) != 0 {
t.Fatalf("expected 0 elements, got %d", len(result.Elements()))
} }
} }
@@ -106,42 +103,3 @@ func TestStringsToList_SingleValue(t *testing.T) {
t.Fatalf("expected [\"solo\"], got %v", back) t.Fatalf("expected [\"solo\"], got %v", back)
} }
} }
func TestPreserveListNullEmptySemantics(t *testing.T) {
ctx := context.Background()
emptyList, _ := types.ListValueFrom(ctx, types.StringType, []types.String{})
nullList := types.ListNull(types.StringType)
populatedList := stringsToList(ctx, []string{"a"})
t.Run("prior empty + current null → preserves empty", func(t *testing.T) {
result := preserveListNullEmptySemantics(emptyList, nullList)
if result.IsNull() {
t.Fatal("expected empty list, got null")
}
if len(result.Elements()) != 0 {
t.Fatalf("expected 0 elements, got %d", len(result.Elements()))
}
})
t.Run("prior null + current null → stays null", func(t *testing.T) {
result := preserveListNullEmptySemantics(nullList, nullList)
if !result.IsNull() {
t.Fatal("expected null, got non-null")
}
})
t.Run("prior empty + current populated → uses current", func(t *testing.T) {
result := preserveListNullEmptySemantics(emptyList, populatedList)
elems := listToStrings(ctx, result)
if len(elems) != 1 || elems[0] != "a" {
t.Fatalf("expected [a], got %v", elems)
}
})
t.Run("prior populated + current null → stays null", func(t *testing.T) {
result := preserveListNullEmptySemantics(populatedList, nullList)
if !result.IsNull() {
t.Fatal("expected null (prior was populated, not empty — no preservation)")
}
})
}
-1
View File
@@ -3,7 +3,6 @@ package provider
type remoteAPI struct { type remoteAPI struct {
Name string `json:"name"` Name string `json:"name"`
PackageType string `json:"package_type"` PackageType string `json:"package_type"`
RepoType string `json:"repo_type,omitempty"`
BaseURL string `json:"base_url"` BaseURL string `json:"base_url"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
Username string `json:"username,omitempty"` Username string `json:"username,omitempty"`
-1
View File
@@ -68,7 +68,6 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res
newRemoteResource("terraform"), newRemoteResource("terraform"),
newRemoteResource("goproxy"), newRemoteResource("goproxy"),
NewVirtualResource, NewVirtualResource,
NewLocalTerraformResource,
} }
} }
+2 -3
View File
@@ -66,8 +66,8 @@ func TestProvider_Resources(t *testing.T) {
p := &ArtifactAPIProvider{version: "1.0.0"} p := &ArtifactAPIProvider{version: "1.0.0"}
resources := p.Resources(context.Background()) resources := p.Resources(context.Background())
// 10 remote resource types + 1 virtual + 1 local_terraform = 12 // 10 remote resource types + 1 virtual = 11
expectedCount := 12 expectedCount := 11
if len(resources) != expectedCount { if len(resources) != expectedCount {
t.Fatalf("expected %d resources, got %d", expectedCount, len(resources)) t.Fatalf("expected %d resources, got %d", expectedCount, len(resources))
} }
@@ -107,7 +107,6 @@ func TestProvider_Resources_ContainsExpectedTypes(t *testing.T) {
"artifactapi_remote_terraform", "artifactapi_remote_terraform",
"artifactapi_remote_goproxy", "artifactapi_remote_goproxy",
"artifactapi_virtual", "artifactapi_virtual",
"artifactapi_local_terraform",
} }
for _, name := range expected { for _, name := range expected {
@@ -1,164 +0,0 @@
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 = &localTerraformResource{}
_ resource.ResourceWithImportState = &localTerraformResource{}
)
type localTerraformResource struct {
client *apiClient
}
type localTerraformResourceModel struct {
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}
func NewLocalTerraformResource() resource.Resource {
return &localTerraformResource{}
}
func (r *localTerraformResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_local_terraform"
}
func (r *localTerraformResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages a local ArtifactAPI terraform registry for hosting providers directly.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Unique name of the local terraform repository.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Description: "Human-readable description.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
},
}
}
func (r *localTerraformResource) 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 *localTerraformResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan localTerraformResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localTerraformModelToAPI(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 terraform failed", err.Error())
return
}
state := localTerraformAPIToModel(created)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localTerraformResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state localTerraformResourceModel
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 terraform failed", err.Error())
return
}
newState := localTerraformAPIToModel(remote)
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
}
func (r *localTerraformResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan localTerraformResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localTerraformModelToAPI(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 terraform failed", err.Error())
return
}
state := localTerraformAPIToModel(updated)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localTerraformResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state localTerraformResourceModel
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 terraform failed", err.Error())
return
}
}
func (r *localTerraformResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}
func localTerraformModelToAPI(m localTerraformResourceModel) remoteAPI {
return remoteAPI{
Name: m.Name.ValueString(),
PackageType: "terraform",
RepoType: "local",
Description: m.Description.ValueString(),
}
}
func localTerraformAPIToModel(api remoteAPI) localTerraformResourceModel {
return localTerraformResourceModel{
Name: types.StringValue(api.Name),
Description: types.StringValue(api.Description),
}
}
@@ -1,125 +0,0 @@
package provider
import (
"context"
"testing"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func TestLocalTerraformModelToAPI(t *testing.T) {
model := localTerraformResourceModel{
Name: types.StringValue("tf-internal"),
Description: types.StringValue("Internal terraform registry"),
}
api := localTerraformModelToAPI(model)
if api.Name != "tf-internal" {
t.Errorf("Name: expected tf-internal, got %s", api.Name)
}
if api.PackageType != "terraform" {
t.Errorf("PackageType: expected terraform, got %s", api.PackageType)
}
if api.RepoType != "local" {
t.Errorf("RepoType: expected local, got %s", api.RepoType)
}
if api.Description != "Internal terraform registry" {
t.Errorf("Description: expected 'Internal terraform registry', got %s", api.Description)
}
}
func TestLocalTerraformModelToAPI_EmptyDescription(t *testing.T) {
model := localTerraformResourceModel{
Name: types.StringValue("tf-empty"),
Description: types.StringValue(""),
}
api := localTerraformModelToAPI(model)
if api.Name != "tf-empty" {
t.Errorf("Name: expected tf-empty, got %s", api.Name)
}
if api.Description != "" {
t.Errorf("Description: expected empty string, got %s", api.Description)
}
if api.PackageType != "terraform" {
t.Errorf("PackageType: expected terraform, got %s", api.PackageType)
}
if api.RepoType != "local" {
t.Errorf("RepoType: expected local, got %s", api.RepoType)
}
}
func TestLocalTerraformAPIToModel(t *testing.T) {
api := remoteAPI{
Name: "tf-internal",
PackageType: "terraform",
RepoType: "local",
Description: "Internal terraform registry",
ManagedBy: "terraform",
}
model := localTerraformAPIToModel(api)
if model.Name.ValueString() != "tf-internal" {
t.Errorf("Name: expected tf-internal, got %s", model.Name.ValueString())
}
if model.Description.ValueString() != "Internal terraform registry" {
t.Errorf("Description: expected 'Internal terraform registry', got %s", model.Description.ValueString())
}
}
func TestLocalTerraformRoundTrip(t *testing.T) {
original := localTerraformResourceModel{
Name: types.StringValue("roundtrip-local"),
Description: types.StringValue("Round trip test"),
}
api := localTerraformModelToAPI(original)
result := localTerraformAPIToModel(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 TestLocalTerraformResource_Metadata(t *testing.T) {
r := NewLocalTerraformResource()
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
var resp resource.MetadataResponse
r.Metadata(context.Background(), req, &resp)
if resp.TypeName != "artifactapi_local_terraform" {
t.Errorf("expected artifactapi_local_terraform, got %s", resp.TypeName)
}
}
func TestLocalTerraformResource_Schema(t *testing.T) {
r := NewLocalTerraformResource()
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 TestNewLocalTerraformResource_Type(t *testing.T) {
r := NewLocalTerraformResource()
_, ok := r.(*localTerraformResource)
if !ok {
t.Error("expected *localTerraformResource")
}
}
-11
View File
@@ -181,7 +181,6 @@ func (r *remoteResource) Create(ctx context.Context, req resource.CreateRequest,
} }
state := r.apiToModel(ctx, created) state := r.apiToModel(ctx, created)
reconcileOptionalLists(&plan, &state)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...) resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
} }
@@ -204,7 +203,6 @@ func (r *remoteResource) Read(ctx context.Context, req resource.ReadRequest, res
} }
newState := r.apiToModel(ctx, remote) newState := r.apiToModel(ctx, remote)
reconcileOptionalLists(&state, &newState)
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...) resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
} }
@@ -225,7 +223,6 @@ func (r *remoteResource) Update(ctx context.Context, req resource.UpdateRequest,
} }
state := r.apiToModel(ctx, updated) state := r.apiToModel(ctx, updated)
reconcileOptionalLists(&plan, &state)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...) resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
} }
@@ -246,14 +243,6 @@ func (r *remoteResource) ImportState(ctx context.Context, req resource.ImportSta
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
} }
func reconcileOptionalLists(prior, current *remoteResourceModel) {
current.Patterns = preserveListNullEmptySemantics(prior.Patterns, current.Patterns)
current.Blocklist = preserveListNullEmptySemantics(prior.Blocklist, current.Blocklist)
current.MutablePatterns = preserveListNullEmptySemantics(prior.MutablePatterns, current.MutablePatterns)
current.ImmutablePatterns = preserveListNullEmptySemantics(prior.ImmutablePatterns, current.ImmutablePatterns)
current.BanTags = preserveListNullEmptySemantics(prior.BanTags, current.BanTags)
}
func (r *remoteResource) modelToAPI(ctx context.Context, m remoteResourceModel) remoteAPI { func (r *remoteResource) modelToAPI(ctx context.Context, m remoteResourceModel) remoteAPI {
api := remoteAPI{ api := remoteAPI{
Name: m.Name.ValueString(), Name: m.Name.ValueString(),