package resources_gen import ( "context" "strings" "terraform-provider-nubes/internal/core" "terraform-provider-nubes/internal/resources_core" "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/booldefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" ) // Code generated by tools/gen_v2. DO NOT EDIT. // Service: postgres // Service ID: 90 var _ resource.Resource = &PostgresResource{} var _ resource.ResourceWithModifyPlan = &PostgresResource{} var _ resource.ResourceWithImportState = &PostgresResource{} type PostgresResource struct { client *core.UniversalClient } type PostgresModel struct { ID types.String `tfsdk:"id"` ResourceName types.String `tfsdk:"resource_name"` OperationTimeout types.String `tfsdk:"operation_timeout"` LogLevel types.String `tfsdk:"log_level"` AccessConfiguration types.String `tfsdk:"access_configuration"` AutoscaleConfiguration types.String `tfsdk:"autoscale_configuration"` BackupConfiguration types.String `tfsdk:"backup_configuration"` ClusterConfiguration types.String `tfsdk:"cluster_configuration"` PostgresConf types.String `tfsdk:"postgres_conf"` PostgresConfiguration types.String `tfsdk:"postgres_configuration"` StartupConfiguration types.String `tfsdk:"startup_configuration"` SuspendOnDestroy types.Bool `tfsdk:"suspend_on_destroy"` AdoptExistingOnCreate types.Bool `tfsdk:"adopt_existing_on_create"` StateParams types.Map `tfsdk:"state_params"` StateOut types.Map `tfsdk:"state_out"` StateParamsFlat types.Map `tfsdk:"state_params_flat"` StateOutFlat types.Map `tfsdk:"state_out_flat"` VaultSecrets types.Map `tfsdk:"vault_secrets"` VaultUrl types.String `tfsdk:"vault_url"` VaultUserPath types.String `tfsdk:"vault_user_path"` VaultFields types.List `tfsdk:"vault_fields"` } func NewPostgresResource() resource.Resource { return &PostgresResource{} } func (r *PostgresResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_postgres" } func (r *PostgresResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { attrs := map[string]schema.Attribute{ "id": schema.StringAttribute{Computed: true, PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}}, "resource_name": schema.StringAttribute{Required: true}, "operation_timeout": schema.StringAttribute{Optional: true}, "log_level": schema.StringAttribute{Optional: true, MarkdownDescription: "Operation stages log level: none (default), info, debug. Overrides provider-level log_level."}, "access_configuration": schema.StringAttribute{Required: true}, "autoscale_configuration": schema.StringAttribute{Required: true}, "backup_configuration": schema.StringAttribute{Required: true}, "cluster_configuration": schema.StringAttribute{Required: true}, "postgres_conf": schema.StringAttribute{Optional: true}, "postgres_configuration": schema.StringAttribute{Required: true}, "startup_configuration": schema.StringAttribute{Required: true, MarkdownDescription: "Не изменяется после создания."}, "suspend_on_destroy": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(true)}, "adopt_existing_on_create": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(false)}, "state_params": schema.MapAttribute{Computed: true, ElementType: types.StringType}, "state_out": schema.MapAttribute{Computed: true, ElementType: types.StringType}, "state_params_flat": schema.MapAttribute{Computed: true, ElementType: types.StringType}, "state_out_flat": schema.MapAttribute{Computed: true, ElementType: types.StringType}, "vault_secrets": schema.MapAttribute{Computed: true, ElementType: types.StringType, Sensitive: true}, "vault_url": schema.StringAttribute{Computed: true}, "vault_user_path": schema.StringAttribute{Computed: true}, "vault_fields": schema.ListAttribute{Computed: true, ElementType: types.StringType}, } resp.Schema = schema.Schema{Attributes: attrs} } func (r *PostgresResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { if r.client == nil { return } var config *PostgresModel resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) if resp.Diagnostics.HasError() { return } if config == nil { return } var state *PostgresModel resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } if req.State.Raw.IsNull() && req.Plan.Raw.IsNull() { return } if state != nil && !state.ID.IsNull() && !state.ID.IsUnknown() { var plan PostgresModel resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) if resp.Diagnostics.HasError() { return } if !plan.ResourceName.IsNull() && !plan.ResourceName.IsUnknown() && !state.ResourceName.IsNull() && !state.ResourceName.IsUnknown() { if plan.ResourceName.ValueString() != state.ResourceName.ValueString() { resp.Diagnostics.AddError("Нельзя изменить resource_name", "Параметр resource_name задается при создании и не может быть изменен. Создайте новый ресурс с другим именем.") return } } if !plan.StartupConfiguration.IsNull() && !plan.StartupConfiguration.IsUnknown() && !state.StartupConfiguration.IsNull() && !state.StartupConfiguration.IsUnknown() { // FIX(uuid-case): сравниваем без учёта регистра — API возвращает UUID // в lowercase, пользователь мог написать upper/mixed. Это одно и то же // значение, менять его нельзя только если оно реально другое. if !strings.EqualFold(plan.StartupConfiguration.ValueString(), state.StartupConfiguration.ValueString()) { resp.Diagnostics.AddError("Нельзя изменить startup_configuration", "Параметр задается при создании и не может быть изменен.") return } } return } if config.AccessConfiguration.IsNull() { resp.Diagnostics.AddError("Missing required attribute", "access_configuration is required.") return } if config.AccessConfiguration.IsUnknown() { return } if config.AutoscaleConfiguration.IsNull() { resp.Diagnostics.AddError("Missing required attribute", "autoscale_configuration is required.") return } if config.AutoscaleConfiguration.IsUnknown() { return } if config.BackupConfiguration.IsNull() { resp.Diagnostics.AddError("Missing required attribute", "backup_configuration is required.") return } if config.BackupConfiguration.IsUnknown() { return } if config.ClusterConfiguration.IsNull() { resp.Diagnostics.AddError("Missing required attribute", "cluster_configuration is required.") return } if config.ClusterConfiguration.IsUnknown() { return } if config.PostgresConfiguration.IsNull() { resp.Diagnostics.AddError("Missing required attribute", "postgres_configuration is required.") return } if config.PostgresConfiguration.IsUnknown() { return } if config.StartupConfiguration.IsNull() { resp.Diagnostics.AddError("Missing required attribute", "startup_configuration is required.") return } if config.StartupConfiguration.IsUnknown() { return } if config.ResourceName.IsNull() || config.ResourceName.IsUnknown() { return } adoptExistingOnCreate := false if !config.AdoptExistingOnCreate.IsNull() && !config.AdoptExistingOnCreate.IsUnknown() { adoptExistingOnCreate = config.AdoptExistingOnCreate.ValueBool() } params := map[int]string{ 788: resources_core.FormatString(config.ClusterConfiguration), 789: resources_core.FormatString(config.StartupConfiguration), 790: resources_core.FormatString(config.AccessConfiguration), 791: resources_core.FormatString(config.PostgresConfiguration), 792: resources_core.FormatString(config.PostgresConf), 793: resources_core.FormatString(config.BackupConfiguration), 794: resources_core.FormatString(config.AutoscaleConfiguration), } desiredDomain := "" domainServiceIDs := []int{81, 82, 88, 96, 97, 98, 99, 119, 149} resp.Diagnostics.Append(resources_core.PlanExistingResourceDiagnosticsWithParamsAndDomainAndServices(ctx, r.client, 90, config.ResourceName.ValueString(), adoptExistingOnCreate, params, desiredDomain, domainServiceIDs)...) } func (r *PostgresResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { var data PostgresModel resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } if data.AccessConfiguration.IsNull() || data.AccessConfiguration.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "access_configuration is required.") return } if data.AutoscaleConfiguration.IsNull() || data.AutoscaleConfiguration.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "autoscale_configuration is required.") return } if data.BackupConfiguration.IsNull() || data.BackupConfiguration.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "backup_configuration is required.") return } if data.ClusterConfiguration.IsNull() || data.ClusterConfiguration.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "cluster_configuration is required.") return } if data.PostgresConfiguration.IsNull() || data.PostgresConfiguration.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "postgres_configuration is required.") return } if data.StartupConfiguration.IsNull() || data.StartupConfiguration.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "startup_configuration is required.") return } resourceName := data.ResourceName.ValueString() desiredDomain := "" domainServiceIDs := []int{81, 82, 88, 96, 97, 98, 99, 119, 149} resp.Diagnostics.Append(resources_core.CreateExistingResourceDiagnosticsWithDomainAndServices(ctx, r.client, 90, resourceName, data.AdoptExistingOnCreate.ValueBool(), desiredDomain, domainServiceIDs)...) params := map[int]string{ 788: resources_core.FormatString(data.ClusterConfiguration), 789: resources_core.FormatString(data.StartupConfiguration), 790: resources_core.FormatString(data.AccessConfiguration), 791: resources_core.FormatString(data.PostgresConfiguration), 792: resources_core.FormatString(data.PostgresConf), 793: resources_core.FormatString(data.BackupConfiguration), 794: resources_core.FormatString(data.AutoscaleConfiguration), } operationTimeout := "" if !data.OperationTimeout.IsNull() && !data.OperationTimeout.IsUnknown() { operationTimeout = data.OperationTimeout.ValueString() } if !data.LogLevel.IsNull() && !data.LogLevel.IsUnknown() { ctx = core.CtxWithLogLevel(ctx, data.LogLevel.ValueString()) } id, err := resources_core.CreateResourceWithTimeout(ctx, r.client, 90, resourceName, data.AdoptExistingOnCreate.ValueBool(), params, operationTimeout) if err != nil { resp.Diagnostics.AddError("Ошибка клиента", err.Error()) return } data.ID = types.StringValue(id) state, diags := resources_core.RefreshResourceState(ctx, r.client, id, 90, data, []resources_core.StateField{ {Code: "state_params"}, {Code: "state_out"}, {Code: "state_params_flat"}, {Code: "state_out_flat"}, {Code: "vault_secrets"}, {Code: "vault_url"}, {Code: "vault_user_path"}, {Code: "vault_fields"}, }, []resources_core.InputField{ {Code: "accessConfiguration", Field: "AccessConfiguration", Type: "string"}, {Code: "autoscaleConfiguration", Field: "AutoscaleConfiguration", Type: "string"}, {Code: "backupConfiguration", Field: "BackupConfiguration", Type: "string"}, {Code: "clusterConfiguration", Field: "ClusterConfiguration", Type: "string"}, {Code: "postgresConf", Field: "PostgresConf", Type: "string"}, {Code: "postgresConfiguration", Field: "PostgresConfiguration", Type: "string"}, {Code: "startupConfiguration", Field: "StartupConfiguration", Type: "string"}, }) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } func (r *PostgresResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { var state PostgresModel resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } if state.ID.IsNull() || state.ID.IsUnknown() { return } if r.client != nil { remove, err := resources_core.ShouldRemoveFromState(ctx, r.client, state.ID.ValueString()) if err != nil { resp.Diagnostics.AddError("Ошибка клиента", err.Error()) return } if remove { resp.State.RemoveResource(ctx) return } } newState, diags := resources_core.RefreshResourceState(ctx, r.client, state.ID.ValueString(), 90, state, []resources_core.StateField{ {Code: "state_params"}, {Code: "state_out"}, {Code: "state_params_flat"}, {Code: "state_out_flat"}, {Code: "vault_secrets"}, {Code: "vault_url"}, {Code: "vault_user_path"}, {Code: "vault_fields"}, }, []resources_core.InputField{ {Code: "accessConfiguration", Field: "AccessConfiguration", Type: "string"}, {Code: "autoscaleConfiguration", Field: "AutoscaleConfiguration", Type: "string"}, {Code: "backupConfiguration", Field: "BackupConfiguration", Type: "string"}, {Code: "clusterConfiguration", Field: "ClusterConfiguration", Type: "string"}, {Code: "postgresConf", Field: "PostgresConf", Type: "string"}, {Code: "postgresConfiguration", Field: "PostgresConfiguration", Type: "string"}, {Code: "startupConfiguration", Field: "StartupConfiguration", Type: "string"}, }) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } resp.Diagnostics.Append(resp.State.Set(ctx, &newState)...) } func (r *PostgresResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { var plan PostgresModel var state PostgresModel resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } instanceID := state.ID if instanceID.IsNull() || instanceID.IsUnknown() { instanceID = plan.ID } if instanceID.IsNull() || instanceID.IsUnknown() { resp.Diagnostics.AddError("Ошибка клиента", "отсутствует идентификатор экземпляра для modify") return } hasServiceParamChanges := false if !hasServiceParamChanges { if plan.ClusterConfiguration.IsNull() != state.ClusterConfiguration.IsNull() || plan.ClusterConfiguration.IsUnknown() != state.ClusterConfiguration.IsUnknown() { hasServiceParamChanges = true } else if !plan.ClusterConfiguration.IsNull() && !plan.ClusterConfiguration.IsUnknown() && !state.ClusterConfiguration.IsNull() && !state.ClusterConfiguration.IsUnknown() { if !strings.EqualFold(plan.ClusterConfiguration.ValueString(), state.ClusterConfiguration.ValueString()) { hasServiceParamChanges = true } } } if !hasServiceParamChanges { if plan.AccessConfiguration.IsNull() != state.AccessConfiguration.IsNull() || plan.AccessConfiguration.IsUnknown() != state.AccessConfiguration.IsUnknown() { hasServiceParamChanges = true } else if !plan.AccessConfiguration.IsNull() && !plan.AccessConfiguration.IsUnknown() && !state.AccessConfiguration.IsNull() && !state.AccessConfiguration.IsUnknown() { if !strings.EqualFold(plan.AccessConfiguration.ValueString(), state.AccessConfiguration.ValueString()) { hasServiceParamChanges = true } } } if !hasServiceParamChanges { if plan.PostgresConfiguration.IsNull() != state.PostgresConfiguration.IsNull() || plan.PostgresConfiguration.IsUnknown() != state.PostgresConfiguration.IsUnknown() { hasServiceParamChanges = true } else if !plan.PostgresConfiguration.IsNull() && !plan.PostgresConfiguration.IsUnknown() && !state.PostgresConfiguration.IsNull() && !state.PostgresConfiguration.IsUnknown() { if !strings.EqualFold(plan.PostgresConfiguration.ValueString(), state.PostgresConfiguration.ValueString()) { hasServiceParamChanges = true } } } if !hasServiceParamChanges { if plan.PostgresConf.IsNull() != state.PostgresConf.IsNull() || plan.PostgresConf.IsUnknown() != state.PostgresConf.IsUnknown() { hasServiceParamChanges = true } else if !plan.PostgresConf.IsNull() && !plan.PostgresConf.IsUnknown() && !state.PostgresConf.IsNull() && !state.PostgresConf.IsUnknown() { if !strings.EqualFold(plan.PostgresConf.ValueString(), state.PostgresConf.ValueString()) { hasServiceParamChanges = true } } } if !hasServiceParamChanges { if plan.BackupConfiguration.IsNull() != state.BackupConfiguration.IsNull() || plan.BackupConfiguration.IsUnknown() != state.BackupConfiguration.IsUnknown() { hasServiceParamChanges = true } else if !plan.BackupConfiguration.IsNull() && !plan.BackupConfiguration.IsUnknown() && !state.BackupConfiguration.IsNull() && !state.BackupConfiguration.IsUnknown() { if !strings.EqualFold(plan.BackupConfiguration.ValueString(), state.BackupConfiguration.ValueString()) { hasServiceParamChanges = true } } } if !hasServiceParamChanges { if plan.AutoscaleConfiguration.IsNull() != state.AutoscaleConfiguration.IsNull() || plan.AutoscaleConfiguration.IsUnknown() != state.AutoscaleConfiguration.IsUnknown() { hasServiceParamChanges = true } else if !plan.AutoscaleConfiguration.IsNull() && !plan.AutoscaleConfiguration.IsUnknown() && !state.AutoscaleConfiguration.IsNull() && !state.AutoscaleConfiguration.IsUnknown() { if !strings.EqualFold(plan.AutoscaleConfiguration.ValueString(), state.AutoscaleConfiguration.ValueString()) { hasServiceParamChanges = true } } } if !hasServiceParamChanges { plan.ID = instanceID plan.StateParams = state.StateParams plan.StateOut = state.StateOut plan.StateParamsFlat = state.StateParamsFlat plan.StateOutFlat = state.StateOutFlat plan.VaultSecrets = state.VaultSecrets plan.VaultUrl = state.VaultUrl plan.VaultUserPath = state.VaultUserPath plan.VaultFields = state.VaultFields resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...) return } operationTimeout := "" if !plan.OperationTimeout.IsNull() && !plan.OperationTimeout.IsUnknown() { operationTimeout = plan.OperationTimeout.ValueString() } if !plan.LogLevel.IsNull() && !plan.LogLevel.IsUnknown() { ctx = core.CtxWithLogLevel(ctx, plan.LogLevel.ValueString()) } params := map[int]string{ 795: resources_core.FormatString(plan.ClusterConfiguration), 796: resources_core.FormatString(plan.AccessConfiguration), 797: resources_core.FormatString(plan.PostgresConfiguration), 798: resources_core.FormatString(plan.PostgresConf), 799: resources_core.FormatString(plan.BackupConfiguration), 800: resources_core.FormatString(plan.AutoscaleConfiguration), } if err := resources_core.UpdateResourceWithTimeout(ctx, r.client, instanceID.ValueString(), params, operationTimeout); err != nil { resp.Diagnostics.AddError("Ошибка клиента", err.Error()) return } plan.ID = instanceID state, diags := resources_core.RefreshResourceState(ctx, r.client, instanceID.ValueString(), 90, plan, []resources_core.StateField{ {Code: "state_params"}, {Code: "state_out"}, {Code: "state_params_flat"}, {Code: "state_out_flat"}, {Code: "vault_secrets"}, {Code: "vault_url"}, {Code: "vault_user_path"}, {Code: "vault_fields"}, }, []resources_core.InputField{ {Code: "accessConfiguration", Field: "AccessConfiguration", Type: "string"}, {Code: "autoscaleConfiguration", Field: "AutoscaleConfiguration", Type: "string"}, {Code: "backupConfiguration", Field: "BackupConfiguration", Type: "string"}, {Code: "clusterConfiguration", Field: "ClusterConfiguration", Type: "string"}, {Code: "postgresConf", Field: "PostgresConf", Type: "string"}, {Code: "postgresConfiguration", Field: "PostgresConfiguration", Type: "string"}, {Code: "startupConfiguration", Field: "StartupConfiguration", Type: "string"}, }) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } func (r *PostgresResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { var state PostgresModel resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } if state.ID.IsNull() || state.ID.IsUnknown() { return } deleteMode := "state_only" if !state.SuspendOnDestroy.IsNull() && !state.SuspendOnDestroy.IsUnknown() && state.SuspendOnDestroy.ValueBool() { deleteMode = "suspend" } operationTimeout := "" if !state.OperationTimeout.IsNull() && !state.OperationTimeout.IsUnknown() { operationTimeout = state.OperationTimeout.ValueString() } if !state.LogLevel.IsNull() && !state.LogLevel.IsUnknown() { ctx = core.CtxWithLogLevel(ctx, state.LogLevel.ValueString()) } if err := resources_core.DeleteResourceWithTimeout(ctx, r.client, state.ID.ValueString(), deleteMode, operationTimeout); err != nil { resp.Diagnostics.AddError("Ошибка клиента", err.Error()) return } } func (r *PostgresResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) } func (r *PostgresResource) 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", "Invalid client type") return } r.client = client }