Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions AGENTS.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (d *diskDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r
"size_gb": dschema.Int64Attribute{Computed: true},
"size_bytes": dschema.Int64Attribute{Computed: true},
"source": dschema.StringAttribute{Computed: true},
"protocol": dschema.StringAttribute{Computed: true},
"locator": dschema.StringAttribute{Computed: true},
"deletion_state": dschema.StringAttribute{Computed: true},
"deletion_error": dschema.StringAttribute{Computed: true},
"host_device": dschema.StringAttribute{Computed: true},
"storage_backend_id": dschema.Int64Attribute{Computed: true},
"storage_backend_name": dschema.StringAttribute{Computed: true},
Expand Down
120 changes: 120 additions & 0 deletions cmd/terraform-provider-rookery/internal/provider/kouja_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package provider

import (
"context"
"errors"
"testing"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

rookeryv1 "github.com/voidstarit/rookery/design/gen/rookery/v1"
)

type fakeDiskClient struct {
rookeryv1.DiskServiceClient
deleteResponse *rookeryv1.DeleteDiskResponse
deleteErr error
getResponse *rookeryv1.Disk
getErr error
}

func (f *fakeDiskClient) DeleteDisk(context.Context, *rookeryv1.DeleteDiskRequest, ...grpc.CallOption) (*rookeryv1.DeleteDiskResponse, error) {
return f.deleteResponse, f.deleteErr
}

func (f *fakeDiskClient) GetDisk(context.Context, *rookeryv1.GetDiskRequest, ...grpc.CallOption) (*rookeryv1.Disk, error) {
return f.getResponse, f.getErr
}

func TestDeleteDiskAsyncBranches(t *testing.T) {
ctx := context.Background()
if err := deleteDiskAsync(ctx, &fakeDiskClient{deleteResponse: &rookeryv1.DeleteDiskResponse{}}, 1, time.Second, time.Millisecond); err != nil {
t.Fatalf("completed delete = %v", err)
}
if err := deleteDiskAsync(ctx, &fakeDiskClient{deleteErr: status.Error(codes.NotFound, "gone")}, 1, time.Second, time.Millisecond); err != nil {
t.Fatalf("not-found delete = %v", err)
}
if err := deleteDiskAsync(ctx, &fakeDiskClient{deleteErr: errors.New("boom")}, 1, time.Second, time.Millisecond); err == nil {
t.Fatal("delete error was ignored")
}
if err := deleteDiskAsync(ctx, &fakeDiskClient{deleteResponse: &rookeryv1.DeleteDiskResponse{Accepted: true},
getErr: status.Error(codes.NotFound, "gone")}, 1, time.Second, time.Millisecond); err != nil {
t.Fatalf("accepted not-found = %v", err)
}
if err := deleteDiskAsync(ctx, &fakeDiskClient{deleteResponse: &rookeryv1.DeleteDiskResponse{Accepted: true},
getErr: errors.New("read failed")}, 1, time.Second, time.Millisecond); err == nil {
t.Fatal("accepted read error was ignored")
}
if err := deleteDiskAsync(ctx, &fakeDiskClient{deleteResponse: &rookeryv1.DeleteDiskResponse{Accepted: true},
getResponse: &rookeryv1.Disk{DeletionState: "delete_failed", DeletionError: "failed"}}, 1, time.Second, time.Millisecond); err == nil {
t.Fatal("delete_failed was ignored")
}
if err := deleteDiskAsync(ctx, &fakeDiskClient{deleteResponse: &rookeryv1.DeleteDiskResponse{Accepted: true},
getResponse: &rookeryv1.Disk{}}, 1, time.Second, time.Millisecond); err != nil {
t.Fatalf("completed polling = %v", err)
}
if err := deleteDiskAsync(ctx, &fakeDiskClient{deleteResponse: &rookeryv1.DeleteDiskResponse{Accepted: true},
getResponse: &rookeryv1.Disk{DeletionState: "deleting"}}, 1, time.Millisecond, time.Millisecond); err == nil {
t.Fatal("poll timeout was ignored")
}
}

func TestSetDiskStateRemoteFields(t *testing.T) {
var state diskModel
setDiskState(&state, &rookeryv1.Disk{
Id: 7, Name: "remote", SizeGb: 5, Source: "kouja", Protocol: "iscsi",
Locator: "10.0.0.1:3260 iqn.2026-11.io.example:disk", DeletionState: "deleting",
DeletionError: "slow", Device: "/dev/ignored",
})
if state.Protocol.ValueString() != "iscsi" || state.Locator.ValueString() == "" ||
state.DeletionState.ValueString() != "deleting" || state.DeletionError.ValueString() != "slow" {
t.Fatalf("state = %+v", state)
}
if !state.HostDevice.IsNull() {
t.Fatal("remote disk was treated as a host device")
}
setDiskState(&state, &rookeryv1.Disk{Id: 8, Source: "host-device", Device: "/dev/sdb"})
if state.HostDevice.ValueString() != "/dev/sdb" {
t.Fatalf("host device = %q", state.HostDevice.ValueString())
}
}

func TestStorageBackendKoujaRoundTrip(t *testing.T) {
ctx := context.Background()
var state storageBackendModel
backend := &rookeryv1.StorageBackend{
Id: 3, Name: "kouja", Driver: "kouja", DiskCount: 1,
Config: &rookeryv1.StorageBackendConfig{DriverConfig: &rookeryv1.StorageBackendConfig_Kouja{
Kouja: &rookeryv1.KoujaConfig{
Endpoint: "10.0.0.1:9443", Protocol: "iscsi", StorageClass: "nvme",
Initiator: "iqn.2026-11.io.example:rookery", HostNqn: "nqn.2026-11.io.example:rookery",
CaFile: "/etc/ca.crt", ClientCertFile: "/etc/client.crt", ClientKeyFile: "/etc/client.key",
ServerName: "kouja", Portal: "10.0.0.1:3260",
},
}},
}
if diags := setStorageBackendState(ctx, &state, backend); diags.HasError() {
t.Fatalf("setStorageBackendState: %v", diags)
}
config, diags := storageBackendConfigFromObject(ctx, state.Config)
if diags.HasError() {
t.Fatalf("storageBackendConfigFromObject: %v", diags)
}
if config.GetKouja().GetEndpoint() != "10.0.0.1:9443" || config.GetKouja().GetStorageClass() != "nvme" ||
config.GetKouja().GetClientKeyFile() != "/etc/client.key" {
t.Fatalf("config = %+v", config)
}
var lvmState storageBackendModel
lvm := &rookeryv1.StorageBackend{Id: 4, Name: "lvm", Driver: "lvm-thin",
Config: &rookeryv1.StorageBackendConfig{Vg: "vg0", ThinPool: "pool", SupportsTemplates: true}}
if diags := setStorageBackendState(ctx, &lvmState, lvm); diags.HasError() {
t.Fatalf("setStorageBackendState(lvm): %v", diags)
}
lvmConfig, diags := storageBackendConfigFromObject(ctx, lvmState.Config)
if diags.HasError() || lvmConfig.GetVg() != "vg0" || lvmConfig.GetKouja() != nil {
t.Fatalf("lvm config = %+v, %v", lvmConfig, diags)
}
}
56 changes: 52 additions & 4 deletions cmd/terraform-provider-rookery/internal/provider/resource_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -41,6 +42,10 @@ type diskModel struct {
ThinPool types.String `tfsdk:"thin_pool"`
Device types.String `tfsdk:"device"`
Source types.String `tfsdk:"source"`
Protocol types.String `tfsdk:"protocol"`
Locator types.String `tfsdk:"locator"`
DeletionState types.String `tfsdk:"deletion_state"`
DeletionError types.String `tfsdk:"deletion_error"`
HostDevice types.String `tfsdk:"host_device"`
AttachedVMID types.String `tfsdk:"attached_vm_id"`
AttachedVMName types.String `tfsdk:"attached_vm_name"`
Expand All @@ -59,7 +64,7 @@ func (r *diskResource) Metadata(_ context.Context, req resource.MetadataRequest,

func (r *diskResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "A thin-LVM disk or template clone in Rookery.",
Description: "A provisioned, remote, or passthrough disk in Rookery.",
Attributes: map[string]schema.Attribute{
"labels": labelsResourceAttribute(),
"id": schema.Int64Attribute{Computed: true, PlanModifiers: []planmodifier.Int64{int64planmodifier.UseStateForUnknown()}},
Expand All @@ -85,8 +90,12 @@ func (r *diskResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
},
"source": schema.StringAttribute{
Computed: true,
Description: "Disk source: lvm-thin or host-device.",
Description: "Disk source (lvm-thin, host-device, kouja, ...).",
},
"protocol": schema.StringAttribute{Computed: true, Description: "Attachment protocol."},
"locator": schema.StringAttribute{Computed: true, Description: "Display locator for remote disks."},
"deletion_state": schema.StringAttribute{Computed: true, Description: "Asynchronous deletion state."},
"deletion_error": schema.StringAttribute{Computed: true, Description: "Last asynchronous deletion error."},
"storage_backend_id": schema.Int64Attribute{
Optional: true,
Computed: true,
Expand Down Expand Up @@ -249,13 +258,48 @@ func (r *diskResource) Delete(ctx context.Context, req resource.DeleteRequest, r
resp.Diagnostics.AddError("Delete disk", err.Error())
return
}
callCtx, cancel := context.WithTimeout(ctx, client.Timeout())
callCtx, cancel := context.WithTimeout(ctx, 4*time.Hour)
defer cancel()
if _, err := client.Disks.DeleteDisk(callCtx, &rookeryv1.DeleteDiskRequest{Id: state.ID.ValueInt64()}); err != nil && !isNotFound(err) {
if err := deleteDiskAsync(callCtx, client.Disks, state.ID.ValueInt64(), 4*time.Hour, 2*time.Second); err != nil {
resp.Diagnostics.AddError("Delete disk", err.Error())
}
}

func deleteDiskAsync(ctx context.Context, client rookeryv1.DiskServiceClient, id int64, timeout, poll time.Duration) error {
deleted, err := client.DeleteDisk(ctx, &rookeryv1.DeleteDiskRequest{Id: id})
if err != nil {
if isNotFound(err) {
return nil
}
return err
}
if deleted == nil || !deleted.GetAccepted() {
return nil
}
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(poll):
}
disk, readErr := client.GetDisk(ctx, &rookeryv1.GetDiskRequest{Id: id})
if isNotFound(readErr) {
return nil
}
if readErr != nil {
return readErr
}
if disk.GetDeletionState() == "delete_failed" {
return fmt.Errorf("%s", disk.GetDeletionError())
}
if disk.GetDeletionState() == "" {
return nil
}
}
return fmt.Errorf("timed out waiting for asynchronous disk deletion")
}

func (r *diskResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
importInt64ID(ctx, req, resp)
}
Expand All @@ -271,6 +315,10 @@ func setDiskState(state *diskModel, disk *rookeryv1.Disk) {
state.ThinPool = types.StringValue(disk.GetThinPool())
state.Device = types.StringValue(disk.GetDevice())
state.Source = types.StringValue(disk.GetSource())
state.Protocol = optionalStringValue(disk.GetProtocol())
state.Locator = optionalStringValue(disk.GetLocator())
state.DeletionState = optionalStringValue(disk.GetDeletionState())
state.DeletionError = optionalStringValue(disk.GetDeletionError())
if disk.GetSource() == "host-device" {
state.HostDevice = types.StringValue(disk.GetDevice())
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ type storageBackendConfigModel struct {
PreparationScript types.String `tfsdk:"preparation_script"`
PreparationTimeout types.String `tfsdk:"preparation_timeout"`
AllowTemplateDeleteWithClones types.Bool `tfsdk:"allow_template_delete_with_clones"`
Kouja types.Object `tfsdk:"kouja"`
}

type koujaConfigModel struct {
Endpoint types.String `tfsdk:"endpoint"`
Protocol types.String `tfsdk:"protocol"`
StorageClass types.String `tfsdk:"storage_class"`
Initiator types.String `tfsdk:"initiator"`
HostNQN types.String `tfsdk:"host_nqn"`
CAFile types.String `tfsdk:"ca_file"`
ClientCertFile types.String `tfsdk:"client_cert_file"`
ClientKeyFile types.String `tfsdk:"client_key_file"`
ServerName types.String `tfsdk:"server_name"`
Portal types.String `tfsdk:"portal"`
}

var koujaConfigAttrTypes = map[string]attr.Type{
"endpoint": types.StringType,
"protocol": types.StringType,
"storage_class": types.StringType,
"initiator": types.StringType,
"host_nqn": types.StringType,
"ca_file": types.StringType,
"client_cert_file": types.StringType,
"client_key_file": types.StringType,
"server_name": types.StringType,
"portal": types.StringType,
}

var storageBackendConfigAttrTypes = map[string]attr.Type{
Expand All @@ -57,6 +84,7 @@ var storageBackendConfigAttrTypes = map[string]attr.Type{
"preparation_script": types.StringType,
"preparation_timeout": types.StringType,
"allow_template_delete_with_clones": types.BoolType,
"kouja": types.ObjectType{AttrTypes: koujaConfigAttrTypes},
}

func (r *storageBackendResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand All @@ -65,7 +93,7 @@ func (r *storageBackendResource) Metadata(_ context.Context, req resource.Metada

func (r *storageBackendResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "A Rookery storage backend. Only the lvm-thin driver is currently supported.",
Description: "A Rookery storage backend. Supports lvm-thin and kouja drivers.",
Attributes: map[string]schema.Attribute{
"labels": labelsResourceAttribute(),
"id": schema.Int64Attribute{
Expand All @@ -78,15 +106,15 @@ func (r *storageBackendResource) Schema(_ context.Context, _ resource.SchemaRequ
"driver": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "Storage driver. Defaults to lvm-thin.",
Description: "Storage driver: lvm-thin or kouja. Defaults to lvm-thin.",
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"config": schema.SingleNestedAttribute{
Required: true,
Description: "LVM thin-pool and template configuration.",
Description: "Driver-specific configuration. Use vg/thin_pool for LVM or kouja for a kouja backend.",
Attributes: map[string]schema.Attribute{
"vg": schema.StringAttribute{Required: true, Description: "Volume group."},
"thin_pool": schema.StringAttribute{Required: true, Description: "Thin pool name."},
"vg": schema.StringAttribute{Optional: true, Computed: true, Description: "Volume group for lvm-thin."},
"thin_pool": schema.StringAttribute{Optional: true, Computed: true, Description: "Thin pool for lvm-thin."},
"supports_templates": schema.BoolAttribute{Optional: true, Computed: true, Description: "Allow template disks on this backend."},
"preparation_script": schema.StringAttribute{Optional: true, Computed: true, Description: "Script used to clone template disks."},
"preparation_timeout": schema.StringAttribute{Optional: true, Computed: true, Description: "Preparation script timeout."},
Expand All @@ -95,6 +123,22 @@ func (r *storageBackendResource) Schema(_ context.Context, _ resource.SchemaRequ
Computed: true,
Description: "Allow deleting a template while clones exist.",
},
"kouja": schema.SingleNestedAttribute{
Optional: true,
Description: "Kouja endpoint, storage class, and mTLS client configuration.",
Attributes: map[string]schema.Attribute{
"endpoint": schema.StringAttribute{Optional: true, Computed: true},
"protocol": schema.StringAttribute{Optional: true, Computed: true},
"storage_class": schema.StringAttribute{Optional: true, Computed: true},
"initiator": schema.StringAttribute{Optional: true, Computed: true},
"host_nqn": schema.StringAttribute{Optional: true, Computed: true},
"ca_file": schema.StringAttribute{Optional: true, Computed: true},
"client_cert_file": schema.StringAttribute{Optional: true, Computed: true},
"client_key_file": schema.StringAttribute{Optional: true, Computed: true},
"server_name": schema.StringAttribute{Optional: true, Computed: true},
"portal": schema.StringAttribute{Optional: true, Computed: true},
},
},
},
},
"disk_count": schema.Int64Attribute{Computed: true, Description: "Number of disks on this backend."},
Expand Down Expand Up @@ -244,13 +288,27 @@ func setStorageBackendState(ctx context.Context, state *storageBackendModel, bac
state.Driver = types.StringValue(backend.GetDriver())
state.DiskCount = types.Int64Value(int64(backend.GetDiskCount()))
cfg := backend.GetConfig()
kouja := types.ObjectNull(koujaConfigAttrTypes)
if cfg.GetKouja() != nil {
value := koujaConfigModel{
Endpoint: types.StringValue(cfg.GetKouja().GetEndpoint()), Protocol: optionalStringValue(cfg.GetKouja().GetProtocol()),
StorageClass: types.StringValue(cfg.GetKouja().GetStorageClass()), Initiator: optionalStringValue(cfg.GetKouja().GetInitiator()),
HostNQN: optionalStringValue(cfg.GetKouja().GetHostNqn()), CAFile: types.StringValue(cfg.GetKouja().GetCaFile()),
ClientCertFile: types.StringValue(cfg.GetKouja().GetClientCertFile()), ClientKeyFile: types.StringValue(cfg.GetKouja().GetClientKeyFile()),
ServerName: optionalStringValue(cfg.GetKouja().GetServerName()), Portal: optionalStringValue(cfg.GetKouja().GetPortal()),
}
object, objectDiags := types.ObjectValueFrom(ctx, koujaConfigAttrTypes, value)
diags.Append(objectDiags...)
kouja = object
}
config := storageBackendConfigModel{
VG: types.StringValue(cfg.GetVg()),
ThinPool: types.StringValue(cfg.GetThinPool()),
SupportsTemplates: types.BoolValue(cfg.GetSupportsTemplates()),
PreparationScript: optionalStringValue(cfg.GetPreparationScript()),
PreparationTimeout: optionalStringValue(cfg.GetPreparationTimeout()),
AllowTemplateDeleteWithClones: types.BoolValue(cfg.GetAllowTemplateDeleteWithClones()),
Kouja: kouja,
}
object, objectDiags := types.ObjectValueFrom(ctx, storageBackendConfigAttrTypes, config)
diags.Append(objectDiags...)
Expand All @@ -264,12 +322,27 @@ func storageBackendConfigFromObject(ctx context.Context, object types.Object) (*
if diags.HasError() {
return nil, diags
}
return &rookeryv1.StorageBackendConfig{
result := &rookeryv1.StorageBackendConfig{
Vg: config.VG.ValueString(),
ThinPool: config.ThinPool.ValueString(),
SupportsTemplates: config.SupportsTemplates.ValueBool(),
PreparationScript: config.PreparationScript.ValueString(),
PreparationTimeout: config.PreparationTimeout.ValueString(),
AllowTemplateDeleteWithClones: config.AllowTemplateDeleteWithClones.ValueBool(),
}, diags
}
if !config.Kouja.IsNull() && !config.Kouja.IsUnknown() {
var kouja koujaConfigModel
diags.Append(config.Kouja.As(ctx, &kouja, basetypes.ObjectAsOptions{})...)
if diags.HasError() {
return nil, diags
}
result.DriverConfig = &rookeryv1.StorageBackendConfig_Kouja{Kouja: &rookeryv1.KoujaConfig{
Endpoint: kouja.Endpoint.ValueString(), Protocol: kouja.Protocol.ValueString(),
StorageClass: kouja.StorageClass.ValueString(), Initiator: kouja.Initiator.ValueString(),
HostNqn: kouja.HostNQN.ValueString(), CaFile: kouja.CAFile.ValueString(),
ClientCertFile: kouja.ClientCertFile.ValueString(), ClientKeyFile: kouja.ClientKeyFile.ValueString(),
ServerName: kouja.ServerName.ValueString(), Portal: kouja.Portal.ValueString(),
}}
}
return result, diags
}
Loading
Loading