365 lines
15 KiB
Go
365 lines
15 KiB
Go
package resources_gen
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"terraform-provider-nubes/internal/core"
|
|
"terraform-provider-nubes/internal/resources_core"
|
|
|
|
"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: kafka
|
|
// Subresource: topic
|
|
|
|
var _ resource.Resource = &KafkaTopicResource{}
|
|
|
|
// KafkaTopicResource manages a subresource via operations.
|
|
type KafkaTopicResource struct {
|
|
client *core.UniversalClient
|
|
}
|
|
|
|
type KafkaTopicModel struct {
|
|
ID types.String `tfsdk:"id"`
|
|
KafkaID types.String `tfsdk:"kafka_id"`
|
|
AdoptExistingOnCreate types.Bool `tfsdk:"adopt_existing_on_create"`
|
|
SkipMissingOnDelete types.Bool `tfsdk:"skip_missing_on_delete"`
|
|
OperationTimeout types.String `tfsdk:"operation_timeout"`
|
|
LogLevel types.String `tfsdk:"log_level"`
|
|
NameTopic types.String `tfsdk:"name_topic"`
|
|
Partitions types.Int64 `tfsdk:"partitions"`
|
|
Replicas types.Int64 `tfsdk:"replicas"`
|
|
}
|
|
|
|
func NewKafkaTopicResource() resource.Resource {
|
|
return &KafkaTopicResource{}
|
|
}
|
|
|
|
func (r *KafkaTopicResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_kafka_topic"
|
|
}
|
|
|
|
func (r *KafkaTopicResource) 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()}},
|
|
"kafka_id": schema.StringAttribute{
|
|
Required: true,
|
|
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
|
|
},
|
|
"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)},
|
|
"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."},
|
|
"name_topic": schema.StringAttribute{Required: true, MarkdownDescription: "Пример: `orders-events` Изменение требует пересоздания.", PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}},
|
|
"partitions": schema.Int64Attribute{Required: true, MarkdownDescription: "Число партиций можно увеличить, но уменьшить их нельзя."},
|
|
"replicas": schema.Int64Attribute{Required: true, MarkdownDescription: "Количество реплик не может привышать количество реплик брокера"},
|
|
}
|
|
resp.Schema = schema.Schema{Attributes: attrs}
|
|
}
|
|
|
|
func (r *KafkaTopicResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
var plan KafkaTopicModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
if plan.NameTopic.IsNull() || plan.NameTopic.IsUnknown() {
|
|
resp.Diagnostics.AddError("Missing required attribute", "name_topic is required.")
|
|
return
|
|
}
|
|
if plan.Partitions.IsNull() || plan.Partitions.IsUnknown() {
|
|
resp.Diagnostics.AddError("Missing required attribute", "partitions is required.")
|
|
return
|
|
}
|
|
if plan.Replicas.IsNull() || plan.Replicas.IsUnknown() {
|
|
resp.Diagnostics.AddError("Missing required attribute", "replicas is required.")
|
|
return
|
|
}
|
|
|
|
instanceUID := strings.TrimSpace(plan.KafkaID.ValueString())
|
|
if instanceUID == "" {
|
|
resp.Diagnostics.AddError("Ошибка клиента", "отсутствует идентификатор экземпляра")
|
|
return
|
|
}
|
|
adoptExistingOnCreate := !plan.AdoptExistingOnCreate.IsNull() && !plan.AdoptExistingOnCreate.IsUnknown() && plan.AdoptExistingOnCreate.ValueBool()
|
|
|
|
idParams := map[string]string{
|
|
"nameTopic": resources_core.FormatString(plan.NameTopic),
|
|
}
|
|
identityCodes := []string{
|
|
"nameTopic",
|
|
}
|
|
listKey, idKey := resources_core.ResolveSubresourceStateKeys("topic", identityCodes)
|
|
targetValue := strings.TrimSpace(idParams[idKey])
|
|
if targetValue == "" {
|
|
for _, code := range identityCodes {
|
|
candidate := strings.TrimSpace(idParams[code])
|
|
if candidate == "" {
|
|
continue
|
|
}
|
|
targetValue = candidate
|
|
idKey = code
|
|
break
|
|
}
|
|
}
|
|
|
|
if targetValue != "" {
|
|
found, known, err := resources_core.FindSubresourceInStateOut(ctx, r.client, instanceUID, listKey, idKey, targetValue)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Ошибка клиента", err.Error())
|
|
return
|
|
}
|
|
if known && found {
|
|
if adoptExistingOnCreate {
|
|
plan.ID = types.StringValue(resources_core.BuildSubresourceID(instanceUID, "topic", idParams))
|
|
resp.Diagnostics.AddWarning("Подресурс уже существует", "Объект уже есть, выполняется усыновление: "+targetValue)
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
|
|
return
|
|
}
|
|
resp.Diagnostics.AddError("Подресурс уже существует", "Найден объект с таким именем: "+targetValue)
|
|
return
|
|
}
|
|
}
|
|
|
|
params := resources_core.CompactParams(map[string]string{
|
|
"nameTopic": resources_core.FormatString(plan.NameTopic),
|
|
"partitions": resources_core.FormatInt64(plan.Partitions),
|
|
"replicas": resources_core.FormatInt64(plan.Replicas),
|
|
})
|
|
|
|
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())
|
|
}
|
|
createErr := resources_core.RunOperationByCodeWithTimeout(ctx, r.client, instanceUID, "create_topic", params, operationTimeout)
|
|
if createErr != nil && resources_core.IsSubresourceTransientDependencyError(createErr) {
|
|
createErr = resources_core.RunOperationByCodeWithTimeout(ctx, r.client, instanceUID, "create_topic", params, operationTimeout)
|
|
}
|
|
if createErr != nil {
|
|
if targetValue != "" {
|
|
found, known, checkErr := resources_core.FindSubresourceInStateOut(ctx, r.client, instanceUID, listKey, idKey, targetValue)
|
|
if checkErr != nil {
|
|
resp.Diagnostics.AddError("Ошибка клиента", checkErr.Error())
|
|
return
|
|
}
|
|
if known && found {
|
|
plan.ID = types.StringValue(resources_core.BuildSubresourceID(instanceUID, "topic", idParams))
|
|
resp.Diagnostics.AddWarning("Подресурс подтверждён в state_out", "Операция create вернула ошибку, но объект найден в state_out и принят в state: "+targetValue)
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
|
|
return
|
|
}
|
|
}
|
|
if adoptExistingOnCreate && resources_core.IsSubresourceAlreadyExistsError(createErr) {
|
|
if targetValue != "" {
|
|
found, known, checkErr := resources_core.FindSubresourceInStateOut(ctx, r.client, instanceUID, listKey, idKey, targetValue)
|
|
if checkErr != nil {
|
|
resp.Diagnostics.AddError("Ошибка клиента", checkErr.Error())
|
|
return
|
|
}
|
|
if found || !known {
|
|
plan.ID = types.StringValue(resources_core.BuildSubresourceID(instanceUID, "topic", idParams))
|
|
resp.Diagnostics.AddWarning("Подресурс уже существует", "Операция вернула duplicate/exist, объект принят в state")
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
|
|
return
|
|
}
|
|
}
|
|
resp.Diagnostics.AddError("Нарушена консистентность", "Операция вернула duplicate/exist, но объект не найден в state_out")
|
|
return
|
|
}
|
|
resp.Diagnostics.AddError("Ошибка клиента", createErr.Error())
|
|
return
|
|
}
|
|
if targetValue != "" {
|
|
found, known, err := resources_core.FindSubresourceInStateOut(ctx, r.client, instanceUID, listKey, idKey, targetValue)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Ошибка клиента", err.Error())
|
|
return
|
|
}
|
|
if known && !found {
|
|
resp.Diagnostics.AddError("Нарушена консистентность", "Операция create завершилась успешно, но объект не найден в state_out: "+targetValue)
|
|
return
|
|
}
|
|
}
|
|
|
|
plan.ID = types.StringValue(resources_core.BuildSubresourceID(instanceUID, "topic", idParams))
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
|
|
}
|
|
|
|
func (r *KafkaTopicResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
var state KafkaTopicModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
|
|
}
|
|
|
|
func (r *KafkaTopicResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
var plan KafkaTopicModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
if "modify_topic" == "" {
|
|
var state KafkaTopicModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
if plan.ID.IsNull() || plan.ID.IsUnknown() {
|
|
plan.ID = state.ID
|
|
}
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
|
|
return
|
|
}
|
|
|
|
instanceUID := strings.TrimSpace(plan.KafkaID.ValueString())
|
|
if instanceUID == "" {
|
|
resp.Diagnostics.AddError("Ошибка клиента", "отсутствует идентификатор экземпляра")
|
|
return
|
|
}
|
|
|
|
params := resources_core.CompactParams(map[string]string{
|
|
"nameTopic": resources_core.FormatString(plan.NameTopic),
|
|
"partitions": resources_core.FormatInt64(plan.Partitions),
|
|
"replicas": resources_core.FormatInt64(plan.Replicas),
|
|
})
|
|
|
|
idParams := map[string]string{
|
|
"nameTopic": resources_core.FormatString(plan.NameTopic),
|
|
}
|
|
|
|
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())
|
|
}
|
|
if err := resources_core.RunOperationByCodeWithTimeout(ctx, r.client, instanceUID, "modify_topic", params, operationTimeout); err != nil {
|
|
resp.Diagnostics.AddError("Ошибка клиента", err.Error())
|
|
return
|
|
}
|
|
|
|
plan.ID = types.StringValue(resources_core.BuildSubresourceID(instanceUID, "topic", idParams))
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
|
|
}
|
|
|
|
func (r *KafkaTopicResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
var state KafkaTopicModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
if "delete_topic" == "" {
|
|
return
|
|
}
|
|
|
|
instanceUID := strings.TrimSpace(state.KafkaID.ValueString())
|
|
if instanceUID == "" {
|
|
return
|
|
}
|
|
skipMissingOnDelete := !state.SkipMissingOnDelete.IsNull() && !state.SkipMissingOnDelete.IsUnknown() && state.SkipMissingOnDelete.ValueBool()
|
|
|
|
idParams := map[string]string{
|
|
"nameTopic": resources_core.FormatString(state.NameTopic),
|
|
}
|
|
identityCodes := []string{
|
|
"nameTopic",
|
|
}
|
|
listKey, idKey := resources_core.ResolveSubresourceStateKeys("topic", identityCodes)
|
|
targetValue := strings.TrimSpace(idParams[idKey])
|
|
if targetValue == "" {
|
|
for _, code := range identityCodes {
|
|
candidate := strings.TrimSpace(idParams[code])
|
|
if candidate == "" {
|
|
continue
|
|
}
|
|
targetValue = candidate
|
|
idKey = code
|
|
break
|
|
}
|
|
}
|
|
|
|
if targetValue != "" {
|
|
found, known, err := resources_core.FindSubresourceInStateOut(ctx, r.client, instanceUID, listKey, idKey, targetValue)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Ошибка клиента", err.Error())
|
|
return
|
|
}
|
|
if known && !found {
|
|
resp.Diagnostics.AddWarning("Подресурс не найден", "Объект уже удалён, пропускаем: "+targetValue)
|
|
return
|
|
}
|
|
}
|
|
|
|
params := resources_core.CompactParams(map[string]string{
|
|
"nameTopic": resources_core.FormatString(state.NameTopic),
|
|
})
|
|
|
|
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.RunOperationByCodeWithTimeout(ctx, r.client, instanceUID, "delete_topic", params, operationTimeout); err != nil {
|
|
if resources_core.IsSubresourceMissingError(err) {
|
|
if targetValue != "" {
|
|
found, known, checkErr := resources_core.FindSubresourceInStateOut(ctx, r.client, instanceUID, listKey, idKey, targetValue)
|
|
if checkErr != nil {
|
|
resp.Diagnostics.AddError("Ошибка клиента", checkErr.Error())
|
|
return
|
|
}
|
|
if known && found {
|
|
resp.Diagnostics.AddError("Нарушена консистентность", "Операция delete вернула not found, но объект всё ещё присутствует в state_out: "+targetValue)
|
|
return
|
|
}
|
|
}
|
|
if skipMissingOnDelete {
|
|
resp.Diagnostics.AddWarning("Подресурс не найден", "Операция delete вернула not found, объект отсутствует в state_out, удаление пропущено")
|
|
return
|
|
}
|
|
}
|
|
resp.Diagnostics.AddError("Ошибка клиента", err.Error())
|
|
return
|
|
}
|
|
|
|
if targetValue != "" {
|
|
found, known, err := resources_core.FindSubresourceInStateOut(ctx, r.client, instanceUID, listKey, idKey, targetValue)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Ошибка клиента", err.Error())
|
|
return
|
|
}
|
|
if known && found {
|
|
resp.Diagnostics.AddError("Нарушена консистентность", "Операция delete завершилась успешно, но объект всё ещё присутствует в state_out: "+targetValue)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *KafkaTopicResource) 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
|
|
}
|