From fefc2006b1da71f41dfd501736e3b4479b6f2a57 Mon Sep 17 00:00:00 2001 From: Repinoid Date: Thu, 1 Oct 2026 15:44:14 +0300 Subject: [PATCH] =?UTF-8?q?gen(subresource):=20keep=5Fon=5Fdestroy=20?= =?UTF-8?q?=E2=80=94=20=D0=BF=D0=BE=D0=B4=D1=80=D0=B5=D1=81=D1=83=D1=80?= =?UTF-8?q?=D1=81=20=D0=BF=D1=80=D0=B8=20destroy=20=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D1=91=D1=82=D1=81=D1=8F=20=D0=B2=20=D0=BE=D0=B1=D0=BB?= =?UTF-8?q?=D0=B0=D0=BA=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Подресурсы (nubes_postgres_user/database и остальные 20) удалялись всегда, даже когда родительский инстанс при destroy только приостанавливается. Из-за этого пользователь БД удалялся, а следующий apply создавал его заново с НОВЫМ паролем. Добавлен атрибут keep_on_destroy (как у инстансовых ресурсов, Default=false): - поле KeepOnDestroy в модели; - атрибут схемы (Optional+Computed, Default=false); - ранний выход в Delete с предупреждением (режим state_only). Проверено: 22 подресурса получили атрибут; сборка провайдера с перегенерённым кодом — BUILD_OK. Дефолт false → поведение существующих конфигураций не меняется. --- .../resource-generator/internal/templates/subresource.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/TOOLS/resource-generator/internal/templates/subresource.go b/TOOLS/resource-generator/internal/templates/subresource.go index 6092242..c2456c2 100644 --- a/TOOLS/resource-generator/internal/templates/subresource.go +++ b/TOOLS/resource-generator/internal/templates/subresource.go @@ -45,6 +45,7 @@ type {{ToCamel (printf "%s_%s" .ServiceName .SubName)}}Model struct { {{ToCamel .ServiceName}}ID types.String ` + "`" + `tfsdk:"{{ToSnake .ServiceName}}_id"` + "`" + ` AdoptExistingOnCreate types.Bool ` + "`" + `tfsdk:"adopt_existing_on_create"` + "`" + ` SkipMissingOnDelete types.Bool ` + "`" + `tfsdk:"skip_missing_on_delete"` + "`" + ` + KeepOnDestroy types.Bool ` + "`" + `tfsdk:"keep_on_destroy"` + "`" + ` OperationTimeout types.String ` + "`" + `tfsdk:"operation_timeout"` + "`" + ` LogLevel types.String ` + "`" + `tfsdk:"log_level"` + "`" + ` {{- range .SchemaParams }} @@ -69,6 +70,7 @@ func (r *{{ToCamel (printf "%s_%s" .ServiceName .SubName)}}Resource) Schema(ctx }, "adopt_existing_on_create": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(false)}, "skip_missing_on_delete": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(true)}, + "keep_on_destroy": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(false), MarkdownDescription: "Режим state_only при destroy: подресурс не удаляется и не меняется в облаке, только убирается из состояния. Нужен там, где родительский инстанс при destroy не удаляется (например suspend_on_destroy = true) и объект должен остаться внутри него. false = обычное удаление."}, "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."}, {{- range .SchemaParams }} @@ -327,6 +329,13 @@ func (r *{{ToCamel (printf "%s_%s" .ServiceName .SubName)}}Resource) Delete(ctx if resp.Diagnostics.HasError() { return } + if !state.KeepOnDestroy.IsNull() && !state.KeepOnDestroy.IsUnknown() && state.KeepOnDestroy.ValueBool() { + resp.Diagnostics.AddWarning( + "Подресурс оставлен как есть, а не удалён", + "destroy: {{.ServiceName}}_{{.SubName}} (service_id={{.ServiceID}}) не изменялся в облаке и только убран из состояния (keep_on_destroy = true). Для полного удаления выставьте keep_on_destroy = false.", + ) + return + } {{- if .HasRefSvcParams }} {{- range .SchemaParams }} {{- if and (gt .RefSvcId 0) (eq (ParamType .) "types.String") }}