package resources_gen import ( "context" "terraform-provider-nubes/internal/core" "terraform-provider-nubes/internal/resources_core" "github.com/hashicorp/terraform-plugin-framework/path" "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/types" ) // Code generated by tools/gen_v2. DO NOT EDIT. // Service: nifi // Service ID: 117 var _ resource.Resource = &NifiResource{} var _ resource.ResourceWithModifyPlan = &NifiResource{} var _ resource.ResourceWithImportState = &NifiResource{} type NifiResource struct { client *core.UniversalClient } type NifiModel struct { ID types.String `tfsdk:"id"` ResourceName types.String `tfsdk:"resource_name"` OperationTimeout types.String `tfsdk:"operation_timeout"` KafkaUid types.String `tfsdk:"kafka_uid"` NameTopic types.String `tfsdk:"name_topic"` Partitions types.Int64 `tfsdk:"partitions"` Replicas types.Int64 `tfsdk:"replicas"` SuspendOnDestroy types.Bool `tfsdk:"suspend_on_destroy"` AdoptExistingOnCreate types.Bool `tfsdk:"adopt_existing_on_create"` StateParams types.Map `tfsdk:"state_params"` StateOut types.Map `tfsdk:"state_out"` StateParamsFlat types.Map `tfsdk:"state_params_flat"` StateOutFlat types.Map `tfsdk:"state_out_flat"` VaultSecrets types.Map `tfsdk:"vault_secrets"` VaultUrl types.String `tfsdk:"vault_url"` VaultUserPath types.String `tfsdk:"vault_user_path"` VaultFields types.List `tfsdk:"vault_fields"` } func NewNifiResource() resource.Resource { return &NifiResource{} } func (r *NifiResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_nifi" } func (r *NifiResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { attrs := map[string]schema.Attribute{ "id": schema.StringAttribute{Computed: true}, "resource_name": schema.StringAttribute{Required: true}, "operation_timeout": schema.StringAttribute{Optional: true}, "kafka_uid": schema.StringAttribute{Required: true, MarkdownDescription: "Выбрать кластер из списка Не изменяется после создания."}, "name_topic": schema.StringAttribute{Required: true, MarkdownDescription: "Пример: `orders-events` Не изменяется после создания."}, "partitions": schema.Int64Attribute{Required: true, MarkdownDescription: "Число партиций на кластер ≤ 100 × (число брокеров) × (фактор репликации). Пример `50`"}, "replicas": schema.Int64Attribute{Required: true, MarkdownDescription: "Количество партиций ≤ (число брокеров). Пример `2`"}, "suspend_on_destroy": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(false)}, "adopt_existing_on_create": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(false)}, "state_params": schema.MapAttribute{Computed: true, ElementType: types.StringType}, "state_out": schema.MapAttribute{Computed: true, ElementType: types.StringType}, "state_params_flat": schema.MapAttribute{Computed: true, ElementType: types.StringType}, "state_out_flat": schema.MapAttribute{Computed: true, ElementType: types.StringType}, "vault_secrets": schema.MapAttribute{Computed: true, ElementType: types.StringType, Sensitive: true}, "vault_url": schema.StringAttribute{Computed: true}, "vault_user_path": schema.StringAttribute{Computed: true}, "vault_fields": schema.ListAttribute{Computed: true, ElementType: types.StringType}, } resp.Schema = schema.Schema{Attributes: attrs} } func (r *NifiResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { if r.client == nil { return } var config *NifiModel resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) if resp.Diagnostics.HasError() { return } if config == nil { return } var state *NifiModel resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } if req.State.Raw.IsNull() && req.Plan.Raw.IsNull() { return } if state != nil && !state.ID.IsNull() && !state.ID.IsUnknown() { var plan NifiModel resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) if resp.Diagnostics.HasError() { return } planChanged := false if !plan.KafkaUid.IsNull() && !plan.KafkaUid.IsUnknown() { resolvedKafkaUid, err := r.client.ResolveRefSvcParamValue(ctx, 116, plan.KafkaUid.ValueString()) if err != nil { resp.Diagnostics.AddWarning("Failed to resolve kafka_uid", err.Error()) } else if resolvedKafkaUid != "" && resolvedKafkaUid != plan.KafkaUid.ValueString() { plan.KafkaUid = types.StringValue(resolvedKafkaUid) planChanged = true } } if planChanged { resp.Diagnostics.Append(resp.Plan.Set(ctx, &plan)...) if resp.Diagnostics.HasError() { return } } if !plan.ResourceName.IsNull() && !plan.ResourceName.IsUnknown() && !state.ResourceName.IsNull() && !state.ResourceName.IsUnknown() { if plan.ResourceName.ValueString() != state.ResourceName.ValueString() { resp.Diagnostics.AddError("Нельзя изменить resource_name", "Параметр resource_name задается при создании и не может быть изменен. Создайте новый ресурс с другим именем.") return } } if !plan.KafkaUid.IsNull() && !plan.KafkaUid.IsUnknown() && !state.KafkaUid.IsNull() && !state.KafkaUid.IsUnknown() { if plan.KafkaUid.ValueString() != state.KafkaUid.ValueString() { resp.Diagnostics.AddError("Нельзя изменить kafka_uid", "Параметр задается при создании и не может быть изменен.") return } } if !plan.NameTopic.IsNull() && !plan.NameTopic.IsUnknown() && !state.NameTopic.IsNull() && !state.NameTopic.IsUnknown() { if plan.NameTopic.ValueString() != state.NameTopic.ValueString() { resp.Diagnostics.AddError("Нельзя изменить name_topic", "Параметр задается при создании и не может быть изменен.") return } } return } if config.KafkaUid.IsNull() || config.KafkaUid.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "kafka_uid is required.") return } if config.NameTopic.IsNull() || config.NameTopic.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "name_topic is required.") return } if config.Partitions.IsNull() || config.Partitions.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "partitions is required.") return } if config.Replicas.IsNull() || config.Replicas.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "replicas is required.") return } if config.ResourceName.IsNull() || config.ResourceName.IsUnknown() { return } adoptExistingOnCreate := false if !config.AdoptExistingOnCreate.IsNull() && !config.AdoptExistingOnCreate.IsUnknown() { adoptExistingOnCreate = config.AdoptExistingOnCreate.ValueBool() } if !config.KafkaUid.IsNull() && !config.KafkaUid.IsUnknown() { resolvedKafkaUid, err := r.client.ResolveRefSvcParamValue(ctx, 116, config.KafkaUid.ValueString()) if err != nil { resp.Diagnostics.AddWarning("Failed to resolve kafka_uid", err.Error()) } else if resolvedKafkaUid != "" && resolvedKafkaUid != config.KafkaUid.ValueString() { config.KafkaUid = types.StringValue(resolvedKafkaUid) } } params := map[int]string{ 471: resources_core.FormatString(config.KafkaUid), 472: resources_core.FormatInt64(config.Partitions), 473: resources_core.FormatInt64(config.Replicas), 474: resources_core.FormatString(config.NameTopic), } desiredDomain := "" domainServiceIDs := []int{2, 50, 81, 82, 88, 89, 94, 95, 96, 97, 98, 99, 119, 149} resp.Diagnostics.Append(resources_core.PlanExistingResourceDiagnosticsWithParamsAndDomainAndServices(ctx, r.client, 117, config.ResourceName.ValueString(), adoptExistingOnCreate, params, desiredDomain, domainServiceIDs)...) } func (r *NifiResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { var data NifiModel resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } if data.KafkaUid.IsNull() || data.KafkaUid.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "kafka_uid is required.") return } if data.NameTopic.IsNull() || data.NameTopic.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "name_topic is required.") return } if data.Partitions.IsNull() || data.Partitions.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "partitions is required.") return } if data.Replicas.IsNull() || data.Replicas.IsUnknown() { resp.Diagnostics.AddError("Missing required attribute", "replicas is required.") return } if !data.KafkaUid.IsNull() && !data.KafkaUid.IsUnknown() { resolvedKafkaUid, err := r.client.ResolveRefSvcParamValue(ctx, 116, data.KafkaUid.ValueString()) if err != nil { resp.Diagnostics.AddError("Ошибка клиента", err.Error()) return } if resolvedKafkaUid != "" && resolvedKafkaUid != data.KafkaUid.ValueString() { data.KafkaUid = types.StringValue(resolvedKafkaUid) } } resourceName := data.ResourceName.ValueString() desiredDomain := "" domainServiceIDs := []int{2, 50, 81, 82, 88, 89, 94, 95, 96, 97, 98, 99, 119, 149} resp.Diagnostics.Append(resources_core.CreateExistingResourceDiagnosticsWithDomainAndServices(ctx, r.client, 117, resourceName, data.AdoptExistingOnCreate.ValueBool(), desiredDomain, domainServiceIDs)...) params := map[int]string{ 471: resources_core.FormatString(data.KafkaUid), 472: resources_core.FormatInt64(data.Partitions), 473: resources_core.FormatInt64(data.Replicas), 474: resources_core.FormatString(data.NameTopic), } operationTimeout := "" if !data.OperationTimeout.IsNull() && !data.OperationTimeout.IsUnknown() { operationTimeout = data.OperationTimeout.ValueString() } id, err := resources_core.CreateResourceWithTimeout(ctx, r.client, 117, resourceName, data.AdoptExistingOnCreate.ValueBool(), params, operationTimeout) if err != nil { resp.Diagnostics.AddError("Ошибка клиента", err.Error()) return } data.ID = types.StringValue(id) state, diags := resources_core.RefreshResourceState(ctx, r.client, id, 117, data, []resources_core.StateField{ {Code: "state_params"}, {Code: "state_out"}, {Code: "state_params_flat"}, {Code: "state_out_flat"}, {Code: "vault_secrets"}, {Code: "vault_url"}, {Code: "vault_user_path"}, {Code: "vault_fields"}, }, []resources_core.InputField{ {Code: "kafkaUid", Field: "KafkaUid", Type: "string"}, {Code: "nameTopic", Field: "NameTopic", Type: "string"}, {Code: "partitions", Field: "Partitions", Type: "int64"}, {Code: "replicas", Field: "Replicas", Type: "int64"}, }) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } if !state.KafkaUid.IsNull() && !state.KafkaUid.IsUnknown() { resolvedKafkaUid, err := r.client.ResolveRefSvcParamValue(ctx, 116, state.KafkaUid.ValueString()) if err != nil { resp.Diagnostics.AddWarning("Failed to resolve kafka_uid", err.Error()) } else if resolvedKafkaUid != "" && resolvedKafkaUid != state.KafkaUid.ValueString() { state.KafkaUid = types.StringValue(resolvedKafkaUid) } } resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } func (r *NifiResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { var state NifiModel resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } if state.ID.IsNull() || state.ID.IsUnknown() { return } if r.client != nil { remove, err := resources_core.ShouldRemoveFromState(ctx, r.client, state.ID.ValueString()) if err != nil { resp.Diagnostics.AddError("Ошибка клиента", err.Error()) return } if remove { resp.State.RemoveResource(ctx) return } } newState, diags := resources_core.RefreshResourceState(ctx, r.client, state.ID.ValueString(), 117, state, []resources_core.StateField{ {Code: "state_params"}, {Code: "state_out"}, {Code: "state_params_flat"}, {Code: "state_out_flat"}, {Code: "vault_secrets"}, {Code: "vault_url"}, {Code: "vault_user_path"}, {Code: "vault_fields"}, }, []resources_core.InputField{ {Code: "kafkaUid", Field: "KafkaUid", Type: "string"}, {Code: "nameTopic", Field: "NameTopic", Type: "string"}, {Code: "partitions", Field: "Partitions", Type: "int64"}, {Code: "replicas", Field: "Replicas", Type: "int64"}, }) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } if !newState.KafkaUid.IsNull() && !newState.KafkaUid.IsUnknown() { resolvedKafkaUid, err := r.client.ResolveRefSvcParamValue(ctx, 116, newState.KafkaUid.ValueString()) if err != nil { resp.Diagnostics.AddWarning("Failed to resolve kafka_uid", err.Error()) } else if resolvedKafkaUid != "" && resolvedKafkaUid != newState.KafkaUid.ValueString() { newState.KafkaUid = types.StringValue(resolvedKafkaUid) } } resp.Diagnostics.Append(resp.State.Set(ctx, &newState)...) } func (r *NifiResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { var plan NifiModel var state NifiModel resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } instanceID := state.ID if instanceID.IsNull() || instanceID.IsUnknown() { instanceID = plan.ID } if instanceID.IsNull() || instanceID.IsUnknown() { resp.Diagnostics.AddError("Ошибка клиента", "отсутствует идентификатор экземпляра для modify") return } hasServiceParamChanges := false if !hasServiceParamChanges { if plan.Partitions.IsNull() != state.Partitions.IsNull() || plan.Partitions.IsUnknown() != state.Partitions.IsUnknown() { hasServiceParamChanges = true } else if !plan.Partitions.IsNull() && !plan.Partitions.IsUnknown() && !state.Partitions.IsNull() && !state.Partitions.IsUnknown() { if plan.Partitions.ValueInt64() != state.Partitions.ValueInt64() { hasServiceParamChanges = true } } } if !hasServiceParamChanges { if plan.Replicas.IsNull() != state.Replicas.IsNull() || plan.Replicas.IsUnknown() != state.Replicas.IsUnknown() { hasServiceParamChanges = true } else if !plan.Replicas.IsNull() && !plan.Replicas.IsUnknown() && !state.Replicas.IsNull() && !state.Replicas.IsUnknown() { if plan.Replicas.ValueInt64() != state.Replicas.ValueInt64() { hasServiceParamChanges = true } } } if !hasServiceParamChanges { plan.ID = instanceID plan.StateParams = state.StateParams plan.StateOut = state.StateOut plan.StateParamsFlat = state.StateParamsFlat plan.StateOutFlat = state.StateOutFlat plan.VaultSecrets = state.VaultSecrets plan.VaultUrl = state.VaultUrl plan.VaultUserPath = state.VaultUserPath plan.VaultFields = state.VaultFields resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...) return } params := map[int]string{ 477: resources_core.FormatInt64(plan.Partitions), 478: resources_core.FormatInt64(plan.Replicas), } operationTimeout := "" if !plan.OperationTimeout.IsNull() && !plan.OperationTimeout.IsUnknown() { operationTimeout = plan.OperationTimeout.ValueString() } if err := resources_core.UpdateResourceWithTimeout(ctx, r.client, instanceID.ValueString(), params, operationTimeout); err != nil { resp.Diagnostics.AddError("Ошибка клиента", err.Error()) return } plan.ID = instanceID state, diags := resources_core.RefreshResourceState(ctx, r.client, instanceID.ValueString(), 117, plan, []resources_core.StateField{ {Code: "state_params"}, {Code: "state_out"}, {Code: "state_params_flat"}, {Code: "state_out_flat"}, {Code: "vault_secrets"}, {Code: "vault_url"}, {Code: "vault_user_path"}, {Code: "vault_fields"}, }, []resources_core.InputField{ {Code: "kafkaUid", Field: "KafkaUid", Type: "string"}, {Code: "nameTopic", Field: "NameTopic", Type: "string"}, {Code: "partitions", Field: "Partitions", Type: "int64"}, {Code: "replicas", Field: "Replicas", Type: "int64"}, }) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } if !state.KafkaUid.IsNull() && !state.KafkaUid.IsUnknown() { resolvedKafkaUid, err := r.client.ResolveRefSvcParamValue(ctx, 116, state.KafkaUid.ValueString()) if err != nil { resp.Diagnostics.AddWarning("Failed to resolve kafka_uid", err.Error()) } else if resolvedKafkaUid != "" && resolvedKafkaUid != state.KafkaUid.ValueString() { state.KafkaUid = types.StringValue(resolvedKafkaUid) } } resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) } func (r *NifiResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { var state NifiModel resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } if state.ID.IsNull() || state.ID.IsUnknown() { return } deleteMode := "delete" operationTimeout := "" if !state.OperationTimeout.IsNull() && !state.OperationTimeout.IsUnknown() { operationTimeout = state.OperationTimeout.ValueString() } if err := resources_core.DeleteResourceWithTimeout(ctx, r.client, state.ID.ValueString(), deleteMode, operationTimeout); err != nil { resp.Diagnostics.AddError("Ошибка клиента", err.Error()) return } } func (r *NifiResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) } func (r *NifiResource) 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 }