Merge pull request 'Add artifactapi_local_ansible resource' (#34) from benvin/local-ansible into main
ci/woodpecker/tag/release Pipeline was successful
ci/woodpecker/tag/release Pipeline was successful
Reviewed-on: #34
This commit was merged in pull request #34.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# `artifactapi_local_ansible`
|
||||
|
||||
Local resources manage repositories that ArtifactAPI hosts directly (rather than
|
||||
proxying an upstream) — each is a real registry for its package type.
|
||||
|
||||
A Galaxy v3 server for collections (`ansible-galaxy collection publish` and `install`).
|
||||
|
||||
Each takes just `name` (required, forces replacement) and an optional
|
||||
`description`.
|
||||
|
||||
## Example
|
||||
|
||||
See `examples/resources/artifactapi_local_ansible/main.tf`.
|
||||
@@ -0,0 +1,4 @@
|
||||
resource "artifactapi_local_ansible" "internal" {
|
||||
name = "ansible-internal"
|
||||
description = "Internal Ansible Galaxy collections"
|
||||
}
|
||||
@@ -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 = &localAnsibleResource{}
|
||||
_ resource.ResourceWithImportState = &localAnsibleResource{}
|
||||
)
|
||||
|
||||
type localAnsibleResource struct {
|
||||
client *apiClient
|
||||
}
|
||||
|
||||
type localAnsibleResourceModel struct {
|
||||
Name types.String `tfsdk:"name"`
|
||||
Description types.String `tfsdk:"description"`
|
||||
}
|
||||
|
||||
func init() { registerResource(NewLocalAnsibleResource) }
|
||||
|
||||
func NewLocalAnsibleResource() resource.Resource {
|
||||
return &localAnsibleResource{}
|
||||
}
|
||||
|
||||
func (r *localAnsibleResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_local_ansible"
|
||||
}
|
||||
|
||||
func (r *localAnsibleResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Description: "Manages a local ArtifactAPI Ansible Galaxy repository for hosting collections directly.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"name": schema.StringAttribute{
|
||||
Description: "Unique name of the local ansible repository.",
|
||||
Required: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"description": schema.StringAttribute{
|
||||
Description: "Human-readable description.",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Default: stringdefault.StaticString(""),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *localAnsibleResource) 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 *localAnsibleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var plan localAnsibleResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
api := localAnsibleModelToAPI(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 ansible failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state := localAnsibleAPIToModel(created)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
func (r *localAnsibleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var state localAnsibleResourceModel
|
||||
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 ansible failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
newState := localAnsibleAPIToModel(remote)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
|
||||
}
|
||||
|
||||
func (r *localAnsibleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var plan localAnsibleResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
api := localAnsibleModelToAPI(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 ansible failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
state := localAnsibleAPIToModel(updated)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
|
||||
}
|
||||
|
||||
func (r *localAnsibleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var state localAnsibleResourceModel
|
||||
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 ansible failed", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (r *localAnsibleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
|
||||
}
|
||||
|
||||
func localAnsibleModelToAPI(m localAnsibleResourceModel) remoteAPI {
|
||||
return remoteAPI{
|
||||
Name: m.Name.ValueString(),
|
||||
PackageType: "ansible",
|
||||
RepoType: "local",
|
||||
Description: m.Description.ValueString(),
|
||||
}
|
||||
}
|
||||
|
||||
func localAnsibleAPIToModel(api remoteAPI) localAnsibleResourceModel {
|
||||
return localAnsibleResourceModel{
|
||||
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 TestLocalAnsibleModelToAPI(t *testing.T) {
|
||||
model := localAnsibleResourceModel{
|
||||
Name: types.StringValue("ansible-internal"),
|
||||
Description: types.StringValue("Internal ansible repository"),
|
||||
}
|
||||
|
||||
api := localAnsibleModelToAPI(model)
|
||||
|
||||
if api.Name != "ansible-internal" {
|
||||
t.Errorf("Name: expected ansible-internal, got %s", api.Name)
|
||||
}
|
||||
if api.PackageType != "ansible" {
|
||||
t.Errorf("PackageType: expected ansible, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
if api.Description != "Internal ansible repository" {
|
||||
t.Errorf("Description: expected 'Internal ansible repository', got %s", api.Description)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalAnsibleModelToAPI_EmptyDescription(t *testing.T) {
|
||||
model := localAnsibleResourceModel{
|
||||
Name: types.StringValue("ansible-empty"),
|
||||
Description: types.StringValue(""),
|
||||
}
|
||||
|
||||
api := localAnsibleModelToAPI(model)
|
||||
|
||||
if api.Name != "ansible-empty" {
|
||||
t.Errorf("Name: expected ansible-empty, got %s", api.Name)
|
||||
}
|
||||
if api.Description != "" {
|
||||
t.Errorf("Description: expected empty string, got %s", api.Description)
|
||||
}
|
||||
if api.PackageType != "ansible" {
|
||||
t.Errorf("PackageType: expected ansible, got %s", api.PackageType)
|
||||
}
|
||||
if api.RepoType != "local" {
|
||||
t.Errorf("RepoType: expected local, got %s", api.RepoType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalAnsibleAPIToModel(t *testing.T) {
|
||||
api := remoteAPI{
|
||||
Name: "ansible-internal",
|
||||
PackageType: "ansible",
|
||||
RepoType: "local",
|
||||
Description: "Internal ansible repository",
|
||||
ManagedBy: "terraform",
|
||||
}
|
||||
|
||||
model := localAnsibleAPIToModel(api)
|
||||
|
||||
if model.Name.ValueString() != "ansible-internal" {
|
||||
t.Errorf("Name: expected ansible-internal, got %s", model.Name.ValueString())
|
||||
}
|
||||
if model.Description.ValueString() != "Internal ansible repository" {
|
||||
t.Errorf("Description: expected 'Internal ansible repository', got %s", model.Description.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalAnsibleRoundTrip(t *testing.T) {
|
||||
original := localAnsibleResourceModel{
|
||||
Name: types.StringValue("roundtrip-ansible"),
|
||||
Description: types.StringValue("Round trip test"),
|
||||
}
|
||||
|
||||
api := localAnsibleModelToAPI(original)
|
||||
result := localAnsibleAPIToModel(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 TestLocalAnsibleResource_Metadata(t *testing.T) {
|
||||
r := NewLocalAnsibleResource()
|
||||
req := resource.MetadataRequest{ProviderTypeName: "artifactapi"}
|
||||
var resp resource.MetadataResponse
|
||||
r.Metadata(context.Background(), req, &resp)
|
||||
if resp.TypeName != "artifactapi_local_ansible" {
|
||||
t.Errorf("expected artifactapi_local_ansible, got %s", resp.TypeName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocalAnsibleResource_Schema(t *testing.T) {
|
||||
r := NewLocalAnsibleResource()
|
||||
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 TestNewLocalAnsibleResource_Type(t *testing.T) {
|
||||
r := NewLocalAnsibleResource()
|
||||
_, ok := r.(*localAnsibleResource)
|
||||
if !ok {
|
||||
t.Error("expected *localAnsibleResource")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user