Compare commits

..

2 Commits

Author SHA1 Message Date
benvin c7a2416518 Merge pull request 'Add artifactapi_local_generic resource' (#12) from benvin/local-generic into main
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #12
2026-07-29 23:52:46 +10:00
unkinben 14b7c4bdcd Add artifactapi_local_generic resource
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
Adds a local generic (raw-file) repository resource so arbitrary binaries -- the
bootapi node rootfs tarballs -- can be hosted on artifactapi and managed in
Terraform. The backend already serves generic locals; only the provider surface
was missing.

- resource_local_generic.go: local repo with package_type=generic (mirrors
  local_rpm), registered in the provider.
- Tests + example.

Claude-Session: https://claude.ai/code/session_015ur3i7D2azsMAWTSVABApv
2026-07-29 21:48:47 +10:00
5 changed files with 297 additions and 2 deletions
@@ -0,0 +1,4 @@
resource "artifactapi_local_generic" "rootfs_images" {
name = "rootfs-images"
description = "Prebuilt node rootfs tarballs (bootapi image provisioning)"
}
+1
View File
@@ -72,6 +72,7 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res
NewLocalPyPIResource,
NewLocalRPMResource,
NewLocalDockerResource,
NewLocalGenericResource,
}
}
+3 -2
View File
@@ -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 + 1 local_pypi + 1 local_rpm + 1 local_docker = 15
expectedCount := 15
// 10 remote resource types + 1 virtual + local_terraform/pypi/rpm/docker/generic = 16
expectedCount := 16
if len(resources) != expectedCount {
t.Fatalf("expected %d resources, got %d", expectedCount, len(resources))
}
@@ -111,6 +111,7 @@ func TestProvider_Resources_ContainsExpectedTypes(t *testing.T) {
"artifactapi_local_pypi",
"artifactapi_local_rpm",
"artifactapi_local_docker",
"artifactapi_local_generic",
}
for _, name := range expected {
+164
View File
@@ -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 = &localGenericResource{}
_ resource.ResourceWithImportState = &localGenericResource{}
)
type localGenericResource struct {
client *apiClient
}
type localGenericResourceModel struct {
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
}
func NewLocalGenericResource() resource.Resource {
return &localGenericResource{}
}
func (r *localGenericResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_local_generic"
}
func (r *localGenericResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages a local ArtifactAPI generic repository for hosting arbitrary files (e.g. rootfs tarballs).",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Description: "Unique name of the local Generic repository.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Description: "Human-readable description.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString(""),
},
},
}
}
func (r *localGenericResource) 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 *localGenericResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan localGenericResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localGenericModelToAPI(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 := localGenericAPIToModel(created)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localGenericResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state localGenericResourceModel
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 := localGenericAPIToModel(remote)
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
}
func (r *localGenericResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan localGenericResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
api := localGenericModelToAPI(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 := localGenericAPIToModel(updated)
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
func (r *localGenericResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state localGenericResourceModel
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 *localGenericResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}
func localGenericModelToAPI(m localGenericResourceModel) remoteAPI {
return remoteAPI{
Name: m.Name.ValueString(),
PackageType: "generic",
RepoType: "local",
Description: m.Description.ValueString(),
}
}
func localGenericAPIToModel(api remoteAPI) localGenericResourceModel {
return localGenericResourceModel{
Name: types.StringValue(api.Name),
Description: types.StringValue(api.Description),
}
}
@@ -0,0 +1,125 @@
package provider
import (
"context"
"testing"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func TestLocalGenericModelToAPI(t *testing.T) {
model := localGenericResourceModel{
Name: types.StringValue("rootfs-images"),
Description: types.StringValue("Internal Generic repository"),
}
api := localGenericModelToAPI(model)
if api.Name != "rootfs-images" {
t.Errorf("Name: expected rootfs-images, got %s", api.Name)
}
if api.PackageType != "generic" {
t.Errorf("PackageType: expected generic, got %s", api.PackageType)
}
if api.RepoType != "local" {
t.Errorf("RepoType: expected local, got %s", api.RepoType)
}
if api.Description != "Internal Generic repository" {
t.Errorf("Description: expected 'Internal Generic repository', got %s", api.Description)
}
}
func TestLocalGenericModelToAPI_EmptyDescription(t *testing.T) {
model := localGenericResourceModel{
Name: types.StringValue("generic-empty"),
Description: types.StringValue(""),
}
api := localGenericModelToAPI(model)
if api.Name != "generic-empty" {
t.Errorf("Name: expected generic-empty, got %s", api.Name)
}
if api.Description != "" {
t.Errorf("Description: expected empty string, got %s", api.Description)
}
if api.PackageType != "generic" {
t.Errorf("PackageType: expected generic, got %s", api.PackageType)
}
if api.RepoType != "local" {
t.Errorf("RepoType: expected local, got %s", api.RepoType)
}
}
func TestLocalGenericAPIToModel(t *testing.T) {
api := remoteAPI{
Name: "rootfs-images",
PackageType: "generic",
RepoType: "local",
Description: "Internal Generic repository",
ManagedBy: "terraform",
}
model := localGenericAPIToModel(api)
if model.Name.ValueString() != "rootfs-images" {
t.Errorf("Name: expected rootfs-images, got %s", model.Name.ValueString())
}
if model.Description.ValueString() != "Internal Generic repository" {
t.Errorf("Description: expected 'Internal Generic repository', got %s", model.Description.ValueString())
}
}
func TestLocalGenericRoundTrip(t *testing.T) {
original := localGenericResourceModel{
Name: types.StringValue("roundtrip-generic"),
Description: types.StringValue("Round trip test"),
}
api := localGenericModelToAPI(original)
result := localGenericAPIToModel(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 TestLocalGenericResource_Metadata(t *testing.T) {
r := NewLocalGenericResource()
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
var resp resource.MetadataResponse
r.Metadata(context.Background(), req, &resp)
if resp.TypeName != "artifactapi_local_generic" {
t.Errorf("expected artifactapi_local_generic, got %s", resp.TypeName)
}
}
func TestLocalGenericResource_Schema(t *testing.T) {
r := NewLocalGenericResource()
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 TestNewLocalGenericResource_Type(t *testing.T) {
r := NewLocalGenericResource()
_, ok := r.(*localGenericResource)
if !ok {
t.Error("expected *localGenericResource")
}
}