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/resource/schema/int64default"
@@ -14,6 +15,7 @@ import (
)
var _ resource.Resource = &EnvironmentResource{}
var _ resource.ResourceWithImportState = &EnvironmentResource{}
// Изменено: 2026-04-14 19:15 UTC.
// Resource для управления Fission Environment через Kubernetes CRD API.
@@ -199,6 +201,19 @@ func (r *EnvironmentResource) Delete(ctx context.Context, req resource.DeleteReq
}
}
// ImportState поддерживает импорт ресурса в формате namespace/name.
func (r *EnvironmentResource) 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)...)
}
// environmentToUnstructured преобразует Terraform model в Kubernetes CRD payload.
func environmentToUnstructured(model environmentResourceModel, namespace string) *unstructured.Unstructured {
return &unstructured.Unstructured{Object: map[string]interface{}{