From e097d0dae927e51cd1e14eeb644ec5b14bcbdfcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Thu, 16 Jul 2026 15:09:54 +0400 Subject: [PATCH] =?UTF-8?q?docs:=20=D0=B8=D1=81=D1=82=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D1=8F=20=E2=80=94=20SubParams,=20=D1=80=D0=B0=D0=B7=D0=BC?= =?UTF-8?q?=D0=BE=D0=BD=D0=BE=D0=BB=D0=B8=D1=87=D0=B8=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5,=20=D0=B1=D0=B0=D0=B3=D1=84=D0=B8=D0=BA=D1=81?= =?UTF-8?q?=D1=8B=20lifecycle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6_subparams_nested_terraform_attributes.md | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 HISTORY/2026-07-16_subparams_nested_terraform_attributes.md diff --git a/HISTORY/2026-07-16_subparams_nested_terraform_attributes.md b/HISTORY/2026-07-16_subparams_nested_terraform_attributes.md new file mode 100644 index 0000000..9ae01b4 --- /dev/null +++ b/HISTORY/2026-07-16_subparams_nested_terraform_attributes.md @@ -0,0 +1,89 @@ +# 2026-07-16 — SubParams: map-fixed → nested Terraform attributes + +## Контекст +Генератор ресурсов не обрабатывал `map-fixed`/`array-map-fixed` параметры — +они шли как `types.String`, пользователь был вынужден писать JSON руками. + +Теперь YAML содержит `sub_params` (получены через метод Виталия — +`/instanceOperations/default/{id}` с `dataDescriptor`), и генератор +раскрывает их во вложенные Terraform-блоки. + +## Изменения (2026-07-16, ветка svc-api) + +### Подготовка +- Переход на Gateway API (`lk-api-gateway`) для всех стендов +- Метод Виталия: `/instanceOperations/default/{id}` вместо `/serviceOperation/{id}` +- YAML сгенерированы заново: dev=50, test=48, prod=46 +- Списки сервисов обновлены из Gateway +- DDoS-Guard: User-Agent + Referer для всех запросов +- LEGACY-пометки на всех старых `deck-api` + +### Размоноличивание templates.go +- `templates.go` (1224 строки) → 3 файла: `instance.go`, `subresource.go`, `action.go` +- Имена констант (`Instance`, `Subresource`, `Action`) не менялись + +### Багфиксы lifecycle (Соннет) +- **Баг A**: нет `HasError()` guard после диагностик в Create — сайд-эффект выполнялся вопреки ошибкам +- **Баг B**: `not created` → авто-delete+create в crud.go противоречил философии (должен быть hard error) + +### SubParams — вложенные Terraform-блоки + +**Изменённые файлы:** + +| Файл | Что | +|------|-----| +| `TOOLS/resource-generator/internal/types/types.go` | `SubParams []Param`, `IsNested bool` в `Param` | +| `TOOLS/resource-generator/internal/loader/loader.go` | `ConvertParams` — рекурсивная конвертация SubParams; `NormalizeParamType` → `map-fixed`/`array-map-fixed` | +| `TOOLS/resource-generator/internal/helpers/helpers.go` | 8 новых функций: `IsNested`, `IsNestedList`, `NestedModelName`, `NestedTfType`, `NestedSchemaType`, `NestedSchemaBlock`, `NestedSchemaEnd`, `NestedJSONExpr`, `SubSchemaType`, `SubDefaultExpr` | +| `TOOLS/resource-generator/internal/templates/instance.go` | nested struct'ы перед Model, `SingleNestedAttribute`/`ListNestedAttribute` в Schema, `BuildJSON` в Create/Modify | +| `TOOLS/resource-generator/internal/writers/writers.go` | 10 новых template-функций зарегистрировано | +| `provider/internal/resources_core/helpers.go` | `BuildJSON(map[string]string) string` — строит JSON из map | + +**Что генерируется (пример postgres):** + +```go +// Вложенный struct +type PostgresClusterConfigurationModel struct { + Cpu types.Int64 `tfsdk:"cpu" json:"cpu"` + Memory types.Int64 `tfsdk:"memory" json:"memory"` + Replicas types.Int64 `tfsdk:"replicas" json:"replicas"` + Disk types.Int64 `tfsdk:"disk" json:"disk"` +} + +// В Model — указатель на nested struct +ClusterConfiguration *PostgresClusterConfigurationModel `tfsdk:"cluster_configuration"` + +// В Schema — SingleNestedAttribute +"cluster_configuration": schema.SingleNestedAttribute{Required: true, + Attributes: map[string]schema.Attribute{ + "cpu": schema.Int64Attribute{Optional: true, Computed: true, Default: int64default.StaticInt64(500)}, + ... + }, +}, + +// В Create/Modify — JSON через BuildJSON +params[788] = resources_core.BuildJSON(map[string]string{ + "cpu": fmt.Sprintf("%d", data.ClusterConfiguration.Cpu.ValueInt64()), + ... +}) +``` + +**Для array-map-fixed** (postgresConf) — `ListNestedAttribute` + `[]Model`. + +### Результаты генерации + +| Стенд | Go-файлов | +|-------|-----------| +| dev | 73 | +| test | 70 | +| prod | 65 | + +### Подводные камни (учтены) +- **Required + Default**: подполя с default → `Optional + Computed + Default` +- **JSON-ключи**: `json:"cpu"` теги = оригинальный code (camelCase) +- **types.* в JSON**: `BuildJSON` вместо `json.Marshal` (types.Int64 не маршалится как число) +- **value_list**: enum-валидаторы — out of scope +- **SubParams только для Instance**: subresource/action используют плоские параметры + +### Версия +5.0.68 → 5.0.73