add: internal package (provider, core, registrykeys)
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package generated
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"terraform-provider-mycloud/internal/core"
|
||||
// "terraform-provider-mycloud/internal/provider" REMOVED CYCLE
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
// ========= RESOURCE DEFINITION (Auto-Generated) =========
|
||||
// Service: Bolvanka (Dummy)
|
||||
// Service ID: 1
|
||||
|
||||
var _ resource.Resource = &BolvankaResource{}
|
||||
|
||||
func NewBolvankaResource() resource.Resource {
|
||||
return &BolvankaResource{}
|
||||
}
|
||||
|
||||
type BolvankaResource struct {
|
||||
client *core.UniversalClient
|
||||
}
|
||||
|
||||
type BolvankaResourceModel struct {
|
||||
ID types.String `tfsdk:"id"`
|
||||
DisplayName types.String `tfsdk:"display_name"`
|
||||
|
||||
// Params
|
||||
DurationMs types.Int64 `tfsdk:"duration_ms"` // ID 198
|
||||
FailAtStart types.Bool `tfsdk:"fail_at_start"` // ID 199
|
||||
}
|
||||
|
||||
func (r *BolvankaResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_bolvanka"
|
||||
}
|
||||
|
||||
func (r *BolvankaResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{Computed: true},
|
||||
"display_name": schema.StringAttribute{Required: true},
|
||||
"duration_ms": schema.Int64Attribute{
|
||||
Required: true,
|
||||
MarkdownDescription: "Sleep duration in ms (Param 198)",
|
||||
},
|
||||
"fail_at_start": schema.BoolAttribute{
|
||||
Optional: true,
|
||||
MarkdownDescription: "Fail immediately (Param 199)",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *BolvankaResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data BolvankaResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// === MAP PARAMETERS (The Stringifier Logic) ===
|
||||
params := make(map[int]string)
|
||||
|
||||
// 198: durationMs (int -> string)
|
||||
params[198] = strconv.FormatInt(data.DurationMs.ValueInt64(), 10)
|
||||
|
||||
// 199: failAtStart (bool -> string)
|
||||
valRef := "false"
|
||||
if data.FailAtStart.ValueBool() {
|
||||
valRef = "true"
|
||||
}
|
||||
params[199] = valRef
|
||||
|
||||
// Call Core
|
||||
id, err := r.client.CreateGenericInstance(ctx, 1, data.DisplayName.ValueString(), params)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Client Error", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
data.ID = types.StringValue(id)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *BolvankaResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
// Implemented as shim for now
|
||||
}
|
||||
|
||||
func (r *BolvankaResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
}
|
||||
func (r *BolvankaResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
}
|
||||
|
||||
func (r *BolvankaResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||
if req.ProviderData == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Assuming Provider passes the correct *UniversalClient or compatible interface
|
||||
client, ok := req.ProviderData.(*core.UniversalClient)
|
||||
if !ok {
|
||||
resp.Diagnostics.AddError("Error", "Wrong client type expected *core.UniversalClient")
|
||||
return
|
||||
}
|
||||
|
||||
r.client = client
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package generated
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"terraform-provider-mycloud/internal/core"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
// ========= RESOURCE DEFINITION (Auto-Generated, Universal Flow) =========
|
||||
// Service: Bolvanka (Dummy)
|
||||
// Service ID: 1
|
||||
|
||||
var _ resource.Resource = &BolvankaUniversalResource{}
|
||||
|
||||
func NewBolvankaUniversalResource() resource.Resource {
|
||||
return &BolvankaUniversalResource{}
|
||||
}
|
||||
|
||||
type BolvankaUniversalResource struct {
|
||||
client *core.UniversalClient
|
||||
}
|
||||
|
||||
type BolvankaUniversalResourceModel struct {
|
||||
ID types.String `tfsdk:"id"`
|
||||
DisplayName types.String `tfsdk:"display_name"`
|
||||
|
||||
// Params
|
||||
DurationMs types.Int64 `tfsdk:"duration_ms"` // ID 198
|
||||
FailAtStart types.Bool `tfsdk:"fail_at_start"` // ID 199
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_bolvanka_universal"
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{Computed: true},
|
||||
"display_name": schema.StringAttribute{Required: true},
|
||||
"duration_ms": schema.Int64Attribute{
|
||||
Required: true,
|
||||
MarkdownDescription: "Sleep duration in ms (Param 198)",
|
||||
},
|
||||
"fail_at_start": schema.BoolAttribute{
|
||||
Optional: true,
|
||||
MarkdownDescription: "Fail immediately (Param 199)",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data BolvankaUniversalResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// === MAP PARAMETERS (The Stringifier Logic) ===
|
||||
params := make(map[int]string)
|
||||
|
||||
// 198: durationMs (int -> string)
|
||||
params[198] = strconv.FormatInt(data.DurationMs.ValueInt64(), 10)
|
||||
|
||||
// 199: failAtStart (bool -> string)
|
||||
valRef := "false"
|
||||
if data.FailAtStart.ValueBool() {
|
||||
valRef = "true"
|
||||
}
|
||||
params[199] = valRef
|
||||
|
||||
// Call Core (Universal Flow V6)
|
||||
id, err := r.client.CreateGenericInstanceUniversalV6(ctx, 1, data.DisplayName.ValueString(), params)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Client Error", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
data.ID = types.StringValue(id)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
// Implemented as shim for now
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
}
|
||||
func (r *BolvankaUniversalResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||
if req.ProviderData == nil {
|
||||
return
|
||||
}
|
||||
|
||||
client, ok := req.ProviderData.(*core.UniversalClient)
|
||||
if !ok {
|
||||
resp.Diagnostics.AddError("Error", "Wrong client type expected *core.UniversalClient")
|
||||
return
|
||||
}
|
||||
|
||||
r.client = client
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package generated
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"terraform-provider-mycloud/internal/core"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
// ========= RESOURCE DEFINITION (Auto-Generated, Universal Flow + Lifecycle) =========
|
||||
// Service: Bolvanka (Dummy)
|
||||
// Service ID: 1
|
||||
|
||||
var _ resource.Resource = &BolvankaUniversalLifecycleResource{}
|
||||
|
||||
func NewBolvankaUniversalLifecycleResource() resource.Resource {
|
||||
return &BolvankaUniversalLifecycleResource{}
|
||||
}
|
||||
|
||||
type BolvankaUniversalLifecycleResource struct {
|
||||
client *core.UniversalClient
|
||||
}
|
||||
|
||||
type BolvankaUniversalLifecycleModel struct {
|
||||
ID types.String `tfsdk:"id"`
|
||||
DisplayName types.String `tfsdk:"display_name"`
|
||||
DurationMs types.Int64 `tfsdk:"duration_ms"`
|
||||
FailAtStart types.Bool `tfsdk:"fail_at_start"`
|
||||
DeleteMode types.String `tfsdk:"delete_mode"`
|
||||
ResumeIfExists types.Bool `tfsdk:"resume_if_exists"`
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalLifecycleResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_bolvanka_universal_lifecycle"
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalLifecycleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"display_name": schema.StringAttribute{
|
||||
Required: true,
|
||||
},
|
||||
"duration_ms": schema.Int64Attribute{
|
||||
Required: true,
|
||||
MarkdownDescription: "Sleep duration in ms (Param 198)",
|
||||
},
|
||||
"fail_at_start": schema.BoolAttribute{
|
||||
Optional: true,
|
||||
MarkdownDescription: "Fail immediately (Param 199)",
|
||||
},
|
||||
"delete_mode": schema.StringAttribute{
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Default: stringdefault.StaticString("state_only"),
|
||||
MarkdownDescription: "Deletion mode: delete | suspend | state_only",
|
||||
},
|
||||
"resume_if_exists": schema.BoolAttribute{
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
Default: booldefault.StaticBool(true),
|
||||
MarkdownDescription: "If true, attempt resume/adopt when instance already exists",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalLifecycleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data BolvankaUniversalLifecycleModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
displayName := data.DisplayName.ValueString()
|
||||
|
||||
// If resume_if_exists: try to find existing instance by display_name
|
||||
if data.ResumeIfExists.ValueBool() {
|
||||
existing, err := r.client.FindInstanceByDisplayName(ctx, 1, displayName)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Client Error", err.Error())
|
||||
return
|
||||
}
|
||||
if existing != nil {
|
||||
status := strings.ToLower(existing.ExplainedStatus)
|
||||
if strings.Contains(status, "suspend") || strings.Contains(status, "suspended") {
|
||||
if err := r.client.RunInstanceOperationUniversal(ctx, existing.InstanceUid, "resume", nil); err != nil {
|
||||
resp.Diagnostics.AddError("Client Error", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
data.ID = types.StringValue(existing.InstanceUid)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
params := map[int]string{
|
||||
198: strconv.FormatInt(data.DurationMs.ValueInt64(), 10),
|
||||
199: boolToString(data.FailAtStart.ValueBool()),
|
||||
}
|
||||
|
||||
id, err := r.client.CreateGenericInstanceUniversalV6(ctx, 1, displayName, params)
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Client Error", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
data.ID = types.StringValue(id)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalLifecycleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
// Implemented as shim for now
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalLifecycleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var data BolvankaUniversalLifecycleModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
params := map[int]string{
|
||||
198: strconv.FormatInt(data.DurationMs.ValueInt64(), 10),
|
||||
199: boolToString(data.FailAtStart.ValueBool()),
|
||||
}
|
||||
|
||||
if err := r.client.RunInstanceOperationUniversal(ctx, data.ID.ValueString(), "modify", params); err != nil {
|
||||
resp.Diagnostics.AddError("Client Error", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalLifecycleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var state BolvankaUniversalLifecycleModel
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
mode := strings.ToLower(state.DeleteMode.ValueString())
|
||||
if mode == "" {
|
||||
mode = "state_only"
|
||||
}
|
||||
|
||||
switch mode {
|
||||
case "state_only":
|
||||
return
|
||||
case "suspend", "delete":
|
||||
if err := r.client.RunInstanceOperationUniversal(ctx, state.ID.ValueString(), mode, nil); err != nil {
|
||||
resp.Diagnostics.AddError("Client Error", err.Error())
|
||||
return
|
||||
}
|
||||
default:
|
||||
resp.Diagnostics.AddError("Invalid delete_mode", "Use: delete | suspend | state_only")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (r *BolvankaUniversalLifecycleResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||
if req.ProviderData == nil {
|
||||
return
|
||||
}
|
||||
|
||||
client, ok := req.ProviderData.(*core.UniversalClient)
|
||||
if !ok {
|
||||
resp.Diagnostics.AddError("Error", "Wrong client type expected *core.UniversalClient")
|
||||
return
|
||||
}
|
||||
|
||||
r.client = client
|
||||
}
|
||||
|
||||
func boolToString(v bool) string {
|
||||
if v {
|
||||
return "true"
|
||||
}
|
||||
return "false"
|
||||
}
|
||||
Reference in New Issue
Block a user