Files
tf_provider/TOOLS/resource-generator/internal/writers/writers.go
T

154 lines
5.7 KiB
Go

// Package writers — генерация Go-файлов ресурсов и registry.go.
//
// Генерирует три типа файлов в internal/resources_gen/:
// - {id}_{service}_resource.go — основной CRUD инстанса
// - {id}_{service}_{sub}_resource.go — подресурсы
// - {id}_{service}_{action}_action.go — action-ресурсы (редко)
// - registry.go — AllResources() со списком всех ресурсов
//
// Каждый файл компилируется через text/template и форматируется через gofmt.
package writers
import (
"bytes"
"fmt"
"os"
"path/filepath"
"text/template"
"resource-generator/internal/helpers"
"resource-generator/internal/templates"
"resource-generator/internal/types"
)
// WriteInstanceResource генерирует nubes_{service} (CRUD инстанса).
func WriteInstanceResource(outDir string, svc types.GenResource) error {
fileName := fmt.Sprintf("%d_%s_resource.go", svc.ServiceID, svc.Name)
filePath := filepath.Join(outDir, fileName)
tpl, err := template.New("resource").Funcs(template.FuncMap{
"ToCamel": helpers.ToCamel,
"ToSnake": helpers.ToSnake,
"ParamType": helpers.ParamType,
"ParamDefaultExpr": helpers.ParamDefaultExpr,
"ParamFormat": helpers.ParamFormat,
"ParamParse": helpers.ParamParse,
"ParamDescription": helpers.ParamDescription,
"OutputType": helpers.OutputParamType,
"OutputIsMap": helpers.OutputParamIsMap,
"OutputIsList": helpers.OutputParamIsList,
"OutputSensitive": helpers.OutputParamSensitive,
"IsNested": helpers.IsNested,
"IsNestedList": helpers.IsNestedList,
"NestedModelName": helpers.NestedModelName,
"NestedTfType": helpers.NestedTfType,
"NestedSchemaType": helpers.NestedSchemaType,
"NestedSchemaBlock": helpers.NestedSchemaBlock,
"NestedSchemaEnd": helpers.NestedSchemaEnd,
"NestedJSONExpr": helpers.NestedJSONExpr,
"SubSchemaType": helpers.SubSchemaType,
"SubDefaultExpr": helpers.SubDefaultExpr,
"bt": func() string { return "`" },
}).Parse(templates.Instance)
if err != nil {
return err
}
var buf bytes.Buffer
if err := tpl.Execute(&buf, svc); err != nil {
return err
}
formatted := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
return os.WriteFile(filePath, formatted, 0644)
}
// WriteSubresource генерирует nubes_{service}_{sub} (пользователи, базы, ...).
func WriteSubresource(outDir string, sr types.GenSubresource) error {
name := fmt.Sprintf("%s_%s", sr.ServiceName, sr.SubName)
fileName := fmt.Sprintf("%d_%s_resource.go", sr.ServiceID, name)
filePath := filepath.Join(outDir, fileName)
tpl, err := template.New("subresource").Funcs(template.FuncMap{
"ToCamel": helpers.ToCamel,
"ToSnake": helpers.ToSnake,
"ParamType": helpers.ParamType,
"ParamDefaultExpr": helpers.ParamDefaultExpr,
"ParamFormat": helpers.ParamFormat,
"ParamParse": helpers.ParamParse,
"ParamDescription": helpers.ParamDescription,
"PlanModifierType": helpers.PlanModifierType,
"PlanModifierExpr": helpers.PlanModifierExpr,
"bt": func() string { return "`" },
}).Parse(templates.Subresource)
if err != nil {
return err
}
var buf bytes.Buffer
if err := tpl.Execute(&buf, sr); err != nil {
return err
}
formatted := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
return os.WriteFile(filePath, formatted, 0644)
}
// WriteActionResource генерирует action-ресурс (почти не используется).
func WriteActionResource(outDir string, act types.GenAction) error {
name := fmt.Sprintf("%s_%s", act.ServiceName, act.ActionName)
fileName := fmt.Sprintf("%d_%s_action.go", act.ServiceID, name)
filePath := filepath.Join(outDir, fileName)
tpl, err := template.New("action").Funcs(template.FuncMap{
"ToCamel": helpers.ToCamel,
"ToSnake": helpers.ToSnake,
"ParamType": helpers.ParamType,
"ParamDefaultExpr": helpers.ParamDefaultExpr,
"ParamFormat": helpers.ParamFormat,
"ParamParse": helpers.ParamParse,
"ParamDescription": helpers.ParamDescription,
"bt": func() string { return "`" },
}).Parse(templates.Action)
if err != nil {
return err
}
var buf bytes.Buffer
if err := tpl.Execute(&buf, act); err != nil {
return err
}
formatted := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
return os.WriteFile(filePath, formatted, 0644)
}
// WriteRegistry генерирует registry.go со списком всех ресурсов.
func WriteRegistry(outDir string, services []types.GenResource, subs []types.GenSubresource, actions []types.GenAction) error {
var buf bytes.Buffer
buf.WriteString("package resources_gen\n\n")
buf.WriteString("import \"github.com/hashicorp/terraform-plugin-framework/resource\"\n\n")
buf.WriteString("// Code generated by tools/gen_v2. DO NOT EDIT.\n")
buf.WriteString("func AllResources() []func() resource.Resource {\n")
buf.WriteString("\treturn []func() resource.Resource{\n")
for _, svc := range services {
buf.WriteString(fmt.Sprintf("\t\tNew%[1]sResource,\n", helpers.ToCamel(svc.Name)))
}
for _, sr := range subs {
buf.WriteString(fmt.Sprintf("\t\tNew%[1]sResource,\n", helpers.ToCamel(sr.ServiceName+"_"+sr.SubName)))
}
for _, act := range actions {
buf.WriteString(fmt.Sprintf("\t\tNew%[1]sResource,\n", helpers.ToCamel(act.ServiceName+"_"+act.ActionName)))
}
buf.WriteString("\t}\n")
buf.WriteString("}\n")
regPath := filepath.Join(outDir, "registry.go")
formatted := helpers.FormatSourceOrWarn(regPath, buf.Bytes())
return os.WriteFile(regPath, formatted, 0644)
}