fix: ModifyPlan — terraform plan теперь видит изменения в source_dir (provider v0.1.16)

This commit is contained in:
“Naeel”
2026-03-11 18:27:46 +04:00
parent d3c54eb521
commit 57193536f3
7 changed files with 40 additions and 10 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ terraform {
required_providers { required_providers {
sless = { sless = {
source = "terra.k8c.ru/naeel/sless" source = "terra.k8c.ru/naeel/sless"
version = "~> 0.1.14" version = "~> 0.1.16"
} }
} }
} }
+1 -1
View File
@@ -11,7 +11,7 @@ terraform {
required_providers { required_providers {
sless = { sless = {
source = "terra.k8c.ru/naeel/sless" source = "terra.k8c.ru/naeel/sless"
version = "~> 0.1.13" version = "~> 0.1.16"
} }
} }
} }
+1 -1
View File
@@ -14,7 +14,7 @@ terraform {
# Провайдер для управления serverless функциями через sless API # Провайдер для управления serverless функциями через sless API
sless = { sless = {
source = "terra.k8c.ru/naeel/sless" source = "terra.k8c.ru/naeel/sless"
version = "~> 0.1.13" version = "~> 0.1.16"
} }
} }
} }
+1 -1
View File
@@ -5,7 +5,7 @@ terraform {
required_providers { required_providers {
sless = { sless = {
source = "terra.k8c.ru/naeel/sless" source = "terra.k8c.ru/naeel/sless"
version = "~> 0.1.14" version = "~> 0.1.16"
} }
} }
} }
+1 -1
View File
@@ -19,7 +19,7 @@ terraform {
required_providers { required_providers {
sless = { sless = {
source = "terra.k8c.ru/naeel/sless" source = "terra.k8c.ru/naeel/sless"
version = "~> 0.1.13" version = "~> 0.1.16"
} }
} }
} }
+1 -1
View File
@@ -18,7 +18,7 @@ terraform {
required_providers { required_providers {
sless = { sless = {
source = "terra.k8c.ru/naeel/sless" source = "terra.k8c.ru/naeel/sless"
version = "~> 0.1.13" version = "~> 0.1.16"
} }
} }
} }
@@ -3,10 +3,11 @@
// //
// Lifecycle: // Lifecycle:
// //
// Create: POST /functions → если code_path задан: upload zip → WaitReady (5 мин) // Create: POST /functions → если code_path задан: upload zip → WaitReady (5 мин)
// Read: GET /functions/{name} → sync state // Read: GET /functions/{name} → sync state
// Update: PUT /functions/{name} → если code_hash изменился: upload zip → WaitReady // Update: PUT /functions/{name} → если code_hash изменился: upload zip → WaitReady
// Delete: DELETE /functions/{name} // Delete: DELETE /functions/{name}
// ModifyPlan: вычисляет hash source_dir на фазе plan → terraform видит diff при изменении кода
// //
// code_hash — пользователь задаёт сам (например filemd5("./handler.zip")). // code_hash — пользователь задаёт сам (например filemd5("./handler.zip")).
// Изменение hash → провайдер перезагружает zip и ждёт новой сборки. // Изменение hash → провайдер перезагружает zip и ждёт новой сборки.
@@ -47,6 +48,7 @@ import (
const defaultBuildTimeoutSec = 300 const defaultBuildTimeoutSec = 300
var _ resource.Resource = &FunctionResource{} var _ resource.Resource = &FunctionResource{}
var _ resource.ResourceWithModifyPlan = &FunctionResource{}
type FunctionResource struct { type FunctionResource struct {
client *client.Client client *client.Client
@@ -344,6 +346,34 @@ func (r *FunctionResource) Delete(ctx context.Context, req resource.DeleteReques
} }
} }
// ModifyPlan вычисляет hash директории source_dir на фазе terraform plan.
// Без этого terraform не видит изменения файлов кода между apply — потому что
// code_hash Computed и при plan берётся из state. Здесь мы явно пересчитываем
// и записываем актуальный hash в plan → если отличается от state, terraform
// показывает diff и вызывает Update.
func (r *FunctionResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
// При destroy plan пустой — ничего не делаем
if req.Plan.Raw.IsNull() {
return
}
var plan FunctionModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
// source_dir не задан — нечего считать
if plan.SourceDir.IsNull() || plan.SourceDir.ValueString() == "" {
return
}
_, hash, err := zipDir(plan.SourceDir.ValueString())
if err != nil {
// Директория может не существовать при первом init — не фейлим plan
return
}
plan.CodeHash = types.StringValue(hash)
resp.Diagnostics.Append(resp.Plan.Set(ctx, plan)...)
}
// fnToModel конвертирует API-ответ + plan (для локальных полей) → state модель. // fnToModel конвертирует API-ответ + plan (для локальных полей) → state модель.
// plan используется для code_path, code_hash, build_timeout_sec — API их не хранит. // plan используется для code_path, code_hash, build_timeout_sec — API их не хранит.
func fnToModel(plan FunctionModel, fn *client.FunctionResponse) FunctionModel { func fnToModel(plan FunctionModel, fn *client.FunctionResponse) FunctionModel {