feat: add artifactapi_local_terraform resource type
New resource for creating local terraform registries in ArtifactAPI (repo_type=local, package_type=terraform). These repos host providers directly rather than proxying an upstream registry. Schema is minimal: just name and description — no upstream-specific fields like base_url, caching TTLs, or auth.
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
resource "artifactapi_local_terraform" "internal" {
|
||||||
|
name = "tf-internal"
|
||||||
|
description = "Internal terraform provider registry"
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ 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"`
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res
|
|||||||
newRemoteResource("terraform"),
|
newRemoteResource("terraform"),
|
||||||
newRemoteResource("goproxy"),
|
newRemoteResource("goproxy"),
|
||||||
NewVirtualResource,
|
NewVirtualResource,
|
||||||
|
NewLocalTerraformResource,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 = 11
|
// 10 remote resource types + 1 virtual + 1 local_terraform = 12
|
||||||
expectedCount := 11
|
expectedCount := 12
|
||||||
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,6 +107,7 @@ 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 {
|
||||||
|
|||||||
@@ -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 = &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),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user