diff --git a/README.md b/README.md index 23674e0..7be7fa0 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,7 @@ Available resource types: - `artifactapi_local_deb` - `artifactapi_local_alpine` - `artifactapi_local_terraform` +- `artifactapi_local_cargo` — a private crate registry (sparse index, `cargo publish`/`yank`) Each takes just `name` (required, forces replacement) and an optional `description`. diff --git a/examples/resources/artifactapi_local_cargo/main.tf b/examples/resources/artifactapi_local_cargo/main.tf new file mode 100644 index 0000000..681d074 --- /dev/null +++ b/examples/resources/artifactapi_local_cargo/main.tf @@ -0,0 +1,4 @@ +resource "artifactapi_local_cargo" "internal" { + name = "crates-internal" + description = "Private crate registry" +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 73d9706..4d95426 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -93,6 +93,7 @@ func (p *ArtifactAPIProvider) Resources(_ context.Context) []func() resource.Res NewLocalAlpineResource, NewLocalDockerResource, NewLocalGenericResource, + NewLocalCargoResource, } } diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index a5a18f6..ef7e1de 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()) - // 16 remote resource types + 1 virtual + 8 local = 25 - expectedCount := 25 + // 16 remote resource types + 1 virtual + 9 local = 26 + expectedCount := 26 if len(resources) != expectedCount { t.Fatalf("expected %d resources, got %d", expectedCount, len(resources)) } @@ -121,6 +121,7 @@ func TestProvider_Resources_ContainsExpectedTypes(t *testing.T) { "artifactapi_local_alpine", "artifactapi_local_docker", "artifactapi_local_generic", + "artifactapi_local_cargo", } for _, name := range expected { diff --git a/internal/provider/resource_local_cargo.go b/internal/provider/resource_local_cargo.go new file mode 100644 index 0000000..a367b81 --- /dev/null +++ b/internal/provider/resource_local_cargo.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 = &localCargoResource{} + _ resource.ResourceWithImportState = &localCargoResource{} +) + +type localCargoResource struct { + client *apiClient +} + +type localCargoResourceModel struct { + Name types.String `tfsdk:"name"` + Description types.String `tfsdk:"description"` +} + +func NewLocalCargoResource() resource.Resource { + return &localCargoResource{} +} + +func (r *localCargoResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_local_cargo" +} + +func (r *localCargoResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schema.Schema{ + Description: "Manages a local ArtifactAPI Cargo repository for hosting crates as a private cargo registry.", + Attributes: map[string]schema.Attribute{ + "name": schema.StringAttribute{ + Description: "Unique name of the local Cargo repository.", + Required: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, + "description": schema.StringAttribute{ + Description: "Human-readable description.", + Optional: true, + Computed: true, + Default: stringdefault.StaticString(""), + }, + }, + } +} + +func (r *localCargoResource) 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 *localCargoResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var plan localCargoResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localCargoModelToAPI(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 cargo failed", err.Error()) + return + } + + state := localCargoAPIToModel(created) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localCargoResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var state localCargoResourceModel + 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 cargo failed", err.Error()) + return + } + + newState := localCargoAPIToModel(remote) + resp.Diagnostics.Append(resp.State.Set(ctx, newState)...) +} + +func (r *localCargoResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan localCargoResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + if resp.Diagnostics.HasError() { + return + } + + api := localCargoModelToAPI(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 cargo failed", err.Error()) + return + } + + state := localCargoAPIToModel(updated) + resp.Diagnostics.Append(resp.State.Set(ctx, state)...) +} + +func (r *localCargoResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var state localCargoResourceModel + 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 cargo failed", err.Error()) + return + } +} + +func (r *localCargoResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp) +} + +func localCargoModelToAPI(m localCargoResourceModel) remoteAPI { + return remoteAPI{ + Name: m.Name.ValueString(), + PackageType: "cargo", + RepoType: "local", + Description: m.Description.ValueString(), + } +} + +func localCargoAPIToModel(api remoteAPI) localCargoResourceModel { + return localCargoResourceModel{ + Name: types.StringValue(api.Name), + Description: types.StringValue(api.Description), + } +} diff --git a/internal/provider/resource_local_cargo_test.go b/internal/provider/resource_local_cargo_test.go new file mode 100644 index 0000000..488884e --- /dev/null +++ b/internal/provider/resource_local_cargo_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 TestLocalCargoModelToAPI(t *testing.T) { + model := localCargoResourceModel{ + Name: types.StringValue("cargo-internal"), + Description: types.StringValue("Internal Cargo repository"), + } + + api := localCargoModelToAPI(model) + + if api.Name != "cargo-internal" { + t.Errorf("Name: expected cargo-internal, got %s", api.Name) + } + if api.PackageType != "cargo" { + t.Errorf("PackageType: expected cargo, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } + if api.Description != "Internal Cargo repository" { + t.Errorf("Description: expected 'Internal Cargo repository', got %s", api.Description) + } +} + +func TestLocalCargoModelToAPI_EmptyDescription(t *testing.T) { + model := localCargoResourceModel{ + Name: types.StringValue("cargo-empty"), + Description: types.StringValue(""), + } + + api := localCargoModelToAPI(model) + + if api.Name != "cargo-empty" { + t.Errorf("Name: expected cargo-empty, got %s", api.Name) + } + if api.Description != "" { + t.Errorf("Description: expected empty string, got %s", api.Description) + } + if api.PackageType != "cargo" { + t.Errorf("PackageType: expected cargo, got %s", api.PackageType) + } + if api.RepoType != "local" { + t.Errorf("RepoType: expected local, got %s", api.RepoType) + } +} + +func TestLocalCargoAPIToModel(t *testing.T) { + api := remoteAPI{ + Name: "cargo-internal", + PackageType: "cargo", + RepoType: "local", + Description: "Internal Cargo repository", + ManagedBy: "terraform", + } + + model := localCargoAPIToModel(api) + + if model.Name.ValueString() != "cargo-internal" { + t.Errorf("Name: expected cargo-internal, got %s", model.Name.ValueString()) + } + if model.Description.ValueString() != "Internal Cargo repository" { + t.Errorf("Description: expected 'Internal Cargo repository', got %s", model.Description.ValueString()) + } +} + +func TestLocalCargoRoundTrip(t *testing.T) { + original := localCargoResourceModel{ + Name: types.StringValue("roundtrip-cargo"), + Description: types.StringValue("Round trip test"), + } + + api := localCargoModelToAPI(original) + result := localCargoAPIToModel(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 TestLocalCargoResource_Metadata(t *testing.T) { + r := NewLocalCargoResource() + req := resource.MetadataRequest{ProviderTypeName: "artifactapi"} + var resp resource.MetadataResponse + r.Metadata(context.Background(), req, &resp) + if resp.TypeName != "artifactapi_local_cargo" { + t.Errorf("expected artifactapi_local_cargo, got %s", resp.TypeName) + } +} + +func TestLocalCargoResource_Schema(t *testing.T) { + r := NewLocalCargoResource() + 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 TestNewLocalCargoResource_Type(t *testing.T) { + r := NewLocalCargoResource() + _, ok := r.(*localCargoResource) + if !ok { + t.Error("expected *localCargoResource") + } +}