Add artifactapi_local_gems resource #33
@@ -0,0 +1,13 @@
|
||||
# `artifactapi_local_gems`
|
||||
|
||||
Local resources manage repositories that ArtifactAPI hosts directly (rather than
|
||||
proxying an upstream) — each is a real registry for its package type.
|
||||
|
||||
A RubyGems source (`gem push`, `gem yank`, `gem install` and `bundle install`).
|
||||
|
||||
Each takes just `name` (required, forces replacement) and an optional
|
||||
`description`.
|
||||
|
||||
## Example
|
||||
|
||||
See `examples/resources/artifactapi_local_gems/main.tf`.
|
||||
@@ -0,0 +1,4 @@
|
||||
resource "artifactapi_local_gems" "internal" {
|
||||
name = "gems-internal"
|
||||
description = "Internal RubyGems repository"
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
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 = &localGemsResource{}
|
||||
_ resource.ResourceWithImportState = &localGemsResource{}
|
||||
)
|
||||
|
||||
type localGemsResource struct {
|
||||
client *apiClient
|
||||
}
|
||||
|
||||
type localGemsResourceModel struct {
|
||||
Name types.String `tfsdk:"name"`
|
||||
Description types.String `tfsdk:"description"`
|
||||
}
|
||||
|
||||
func init() { registerResource(NewLocalGemsResource) }
|
||||
|
||||
func NewLocalGemsResource() resource.Resource {
|
||||
return &localGemsResource{}
|
||||
}
|
||||
|
||||
func (r *localGemsResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_local_gems"
|
||||
}
|
||||
|
||||
func (r *localGemsResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Description: "Manages a local ArtifactAPI RubyGems repository for hosting gems directly.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"name": schema.StringAttribute{
|
||||
Description: "Unique name of the local gems repository.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"description": schema.StringAttribute{
|
||||
Description: "Human-readable description.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Default: stringdefault.StaticString(""),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *localGemsResource) 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 *localGemsResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var plan localGemsResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
api := localGemsModelToAPI(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 gems failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state := localGemsAPIToModel(created)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
func (r *localGemsResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var state localGemsResourceModel
|
||||
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 gems failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
newState := localGemsAPIToModel(remote)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
|
||||
}
|
||||
|
||||
func (r *localGemsResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var plan localGemsResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
api := localGemsModelToAPI(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 gems failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state := localGemsAPIToModel(updated)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
func (r *localGemsResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var state localGemsResourceModel
|
||||
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 gems failed", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (r *localGemsResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
|
||||
}
|
||||
|
||||
func localGemsModelToAPI(m localGemsResourceModel) remoteAPI {
|
||||
return remoteAPI{
|
||||
Name: m.Name.ValueString(),
|
||||
PackageType: "gems",
|
||||
RepoType: "local",
|
||||
Description: m.Description.ValueString(),
|
||||
}
|
||||
}
|
||||
|
||||
func localGemsAPIToModel(api remoteAPI) localGemsResourceModel {
|
||||
return localGemsResourceModel{
|
||||
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 TestLocalGemsModelToAPI(t *testing.T) {
|
||||
model := localGemsResourceModel{
|
||||
Name: types.StringValue("gems-internal"),
|
||||
Description: types.StringValue("Internal gems repository"),
|
||||
}
|
||||
|
||||
api := localGemsModelToAPI(model)
|
||||
|
||||
if api.Name != "gems-internal" {
|
||||
t.Errorf("Name: expected gems-internal, got %s", api.Name)
|
||||
}
|
||||
if api.PackageType != "gems" {
|
||||
t.Errorf("PackageType: expected gems, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
if api.Description != "Internal gems repository" {
|
||||
t.Errorf("Description: expected 'Internal gems repository', got %s", api.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalGemsModelToAPI_EmptyDescription(t *testing.T) {
|
||||
model := localGemsResourceModel{
|
||||
Name: types.StringValue("gems-empty"),
|
||||
Description: types.StringValue(""),
|
||||
}
|
||||
|
||||
api := localGemsModelToAPI(model)
|
||||
|
||||
if api.Name != "gems-empty" {
|
||||
t.Errorf("Name: expected gems-empty, got %s", api.Name)
|
||||
}
|
||||
if api.Description != "" {
|
||||
t.Errorf("Description: expected empty string, got %s", api.Description)
|
||||
}
|
||||
if api.PackageType != "gems" {
|
||||
t.Errorf("PackageType: expected gems, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalGemsAPIToModel(t *testing.T) {
|
||||
api := remoteAPI{
|
||||
Name: "gems-internal",
|
||||
PackageType: "gems",
|
||||
RepoType: "local",
|
||||
Description: "Internal gems repository",
|
||||
ManagedBy: "terraform",
|
||||
}
|
||||
|
||||
model := localGemsAPIToModel(api)
|
||||
|
||||
if model.Name.ValueString() != "gems-internal" {
|
||||
t.Errorf("Name: expected gems-internal, got %s", model.Name.ValueString())
|
||||
}
|
||||
if model.Description.ValueString() != "Internal gems repository" {
|
||||
t.Errorf("Description: expected 'Internal gems repository', got %s", model.Description.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalGemsRoundTrip(t *testing.T) {
|
||||
original := localGemsResourceModel{
|
||||
Name: types.StringValue("roundtrip-gems"),
|
||||
Description: types.StringValue("Round trip test"),
|
||||
}
|
||||
|
||||
api := localGemsModelToAPI(original)
|
||||
result := localGemsAPIToModel(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 TestLocalGemsResource_Metadata(t *testing.T) {
|
||||
r := NewLocalGemsResource()
|
||||
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
|
||||
var resp resource.MetadataResponse
|
||||
r.Metadata(context.Background(), req, &resp)
|
||||
if resp.TypeName != "artifactapi_local_gems" {
|
||||
t.Errorf("expected artifactapi_local_gems, got %s", resp.TypeName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalGemsResource_Schema(t *testing.T) {
|
||||
r := NewLocalGemsResource()
|
||||
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 TestNewLocalGemsResource_Type(t *testing.T) {
|
||||
r := NewLocalGemsResource()
|
||||
_, ok := r.(*localGemsResource)
|
||||
if !ok {
|
||||
t.Error("expected *localGemsResource")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user