feat: add import support and unit tests for provider resources

This commit is contained in:
Naeel
2026-04-14 23:00:46 +03:00
parent a2688e5f2c
commit 81852c28b1
12 changed files with 366 additions and 0 deletions
@@ -4,6 +4,7 @@ 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/types"
@@ -13,6 +14,7 @@ import (
)
var _ resource.Resource = &FunctionResource{}
var _ resource.ResourceWithImportState = &FunctionResource{}
// Изменено: 2026-04-14 20:00 UTC.
// Resource для управления Fission Function через Kubernetes CRD API.
@@ -184,6 +186,19 @@ func (r *FunctionResource) Delete(ctx context.Context, req resource.DeleteReques
}
}
// ImportState поддерживает импорт ресурса в формате namespace/name.
func (r *FunctionResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
namespace, name, err := parseImportID(req.ID)
if err != nil {
resp.Diagnostics.AddError("Некорректный формат import ID", err.Error())
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), req.ID)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("namespace"), namespace)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), name)...)
}
// functionToUnstructured преобразует Terraform model в Kubernetes CRD payload.
func functionToUnstructured(model functionResourceModel, namespace string) *unstructured.Unstructured {
return &unstructured.Unstructured{Object: map[string]interface{}{