// Code generated by "gen/generator.go"; DO NOT EDIT.
// In order to regenerate this file execute `go generate` from the repository root.
// More details can be found in the [README](https://github.com/CiscoDevNet/terraform-provider-aci/blob/master/README.md).

package provider

import (
	"context"
	"encoding/json"
	"fmt"
	"strings"

	customTypes "github.com/CiscoDevNet/terraform-provider-aci/v2/internal/custom_types"
	"github.com/CiscoDevNet/terraform-provider-aci/v2/internal/validators"
	"github.com/ciscoecosystem/aci-go-client/v2/client"
	"github.com/ciscoecosystem/aci-go-client/v2/container"
	"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
	"github.com/hashicorp/terraform-plugin-framework/attr"
	"github.com/hashicorp/terraform-plugin-framework/diag"
	"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/planmodifier"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
	"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
	"github.com/hashicorp/terraform-plugin-framework/schema/validator"
	"github.com/hashicorp/terraform-plugin-framework/types"
	"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
	"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &MgmtInstPResource{}
var _ resource.ResourceWithImportState = &MgmtInstPResource{}

func NewMgmtInstPResource() resource.Resource {
	return &MgmtInstPResource{}
}

// MgmtInstPResource defines the resource implementation.
type MgmtInstPResource struct {
	client *client.Client
}

// MgmtInstPResourceModel describes the resource data model.
type MgmtInstPResourceModel struct {
	Id            types.String                         `tfsdk:"id"`
	Annotation    types.String                         `tfsdk:"annotation"`
	Descr         types.String                         `tfsdk:"description"`
	Name          types.String                         `tfsdk:"name"`
	NameAlias     types.String                         `tfsdk:"name_alias"`
	Prio          customTypes.MgmtInstPPrioStringValue `tfsdk:"priority"`
	MgmtRsOoBCons types.Set                            `tfsdk:"relation_to_consumed_out_of_band_contracts"`
	TagAnnotation types.Set                            `tfsdk:"annotations"`
	TagTag        types.Set                            `tfsdk:"tags"`
}

func getEmptyMgmtInstPResourceModel() *MgmtInstPResourceModel {
	return &MgmtInstPResourceModel{
		Id:         basetypes.NewStringNull(),
		Annotation: basetypes.NewStringNull(),
		Descr:      basetypes.NewStringNull(),
		Name:       basetypes.NewStringNull(),
		NameAlias:  basetypes.NewStringNull(),
		Prio:       customTypes.NewMgmtInstPPrioStringNull(),
		MgmtRsOoBCons: types.SetNull(types.ObjectType{
			AttrTypes: map[string]attr.Type{
				"annotation":                types.StringType,
				"priority":                  types.StringType,
				"out_of_band_contract_name": types.StringType,
				"annotations":               types.SetType{ElemType: TagAnnotationMgmtRsOoBConsMgmtInstPType},
				"tags":                      types.SetType{ElemType: TagTagMgmtRsOoBConsMgmtInstPType},
			},
		}),
		TagAnnotation: types.SetNull(types.ObjectType{
			AttrTypes: map[string]attr.Type{
				"key":   types.StringType,
				"value": types.StringType,
			},
		}),
		TagTag: types.SetNull(types.ObjectType{
			AttrTypes: map[string]attr.Type{
				"key":   types.StringType,
				"value": types.StringType,
			},
		}),
	}
}

// MgmtRsOoBConsMgmtInstPResourceModel describes the resource data model for the children without relation ships.
type MgmtRsOoBConsMgmtInstPResourceModel struct {
	Annotation      types.String                             `tfsdk:"annotation"`
	Prio            customTypes.MgmtRsOoBConsPrioStringValue `tfsdk:"priority"`
	TnVzOOBBrCPName types.String                             `tfsdk:"out_of_band_contract_name"`
	TagAnnotation   types.Set                                `tfsdk:"annotations"`
	TagTag          types.Set                                `tfsdk:"tags"`
}

func getEmptyMgmtRsOoBConsMgmtInstPResourceModel() MgmtRsOoBConsMgmtInstPResourceModel {
	return MgmtRsOoBConsMgmtInstPResourceModel{
		Annotation:      basetypes.NewStringNull(),
		Prio:            customTypes.NewMgmtRsOoBConsPrioStringNull(),
		TnVzOOBBrCPName: basetypes.NewStringNull(),
		TagAnnotation: types.SetNull(types.ObjectType{
			AttrTypes: map[string]attr.Type{
				"key":   types.StringType,
				"value": types.StringType,
			},
		}),
		TagTag: types.SetNull(types.ObjectType{
			AttrTypes: map[string]attr.Type{
				"key":   types.StringType,
				"value": types.StringType,
			},
		}),
	}
}

var MgmtRsOoBConsMgmtInstPType = types.ObjectType{
	AttrTypes: map[string]attr.Type{
		"annotation":                types.StringType,
		"priority":                  types.StringType,
		"out_of_band_contract_name": types.StringType,
		"annotations":               types.SetType{ElemType: TagAnnotationMgmtRsOoBConsMgmtInstPType},
		"tags":                      types.SetType{ElemType: TagTagMgmtRsOoBConsMgmtInstPType},
	},
}

func MgmtRsOoBConsMgmtInstPSetToSetNullWhenStateIsNullPlanIsUnknownDuringUpdate(ctx context.Context, planValue, stateValue types.Set) basetypes.SetValue {
	//  Function is needed to handle the case that an attribute is not yet supported in a version and gets set to null during read
	var planSetValues, stateSetValues []MgmtRsOoBConsMgmtInstPResourceModel
	stateValue.ElementsAs(ctx, &stateSetValues, false)
	planValue.ElementsAs(ctx, &planSetValues, false)

	// If the length of the state and plan values are different a change is already detected the loop can be skipped
	if len(stateSetValues) == len(planSetValues) {
		for index, stateValue := range stateSetValues {
			nullInStateFound := false
			if stateValue.Annotation.IsNull() {
				nullInStateFound = true
				planSetValues[index].Annotation = basetypes.NewStringNull()
			}
			if stateValue.Prio.IsNull() {
				nullInStateFound = true
				planSetValues[index].Prio = customTypes.NewMgmtRsOoBConsPrioStringNull()
			}
			if stateValue.TnVzOOBBrCPName.IsNull() {
				nullInStateFound = true
				planSetValues[index].TnVzOOBBrCPName = basetypes.NewStringNull()
			}
			if !nullInStateFound {
				// when there are no null fields we can conclude the version supports all attributes in set
				break
			}
		}
	}
	planSet, _ := types.SetValueFrom(ctx, MgmtRsOoBConsMgmtInstPType, planSetValues)
	return planSet

}

// TagAnnotationMgmtRsOoBConsMgmtInstPResourceModel describes the resource data model for the children without relation ships.
type TagAnnotationMgmtRsOoBConsMgmtInstPResourceModel struct {
	Key   types.String `tfsdk:"key"`
	Value types.String `tfsdk:"value"`
}

func getEmptyTagAnnotationMgmtRsOoBConsMgmtInstPResourceModel() TagAnnotationMgmtRsOoBConsMgmtInstPResourceModel {
	return TagAnnotationMgmtRsOoBConsMgmtInstPResourceModel{
		Key:   basetypes.NewStringNull(),
		Value: basetypes.NewStringNull(),
	}
}

var TagAnnotationMgmtRsOoBConsMgmtInstPType = types.ObjectType{
	AttrTypes: map[string]attr.Type{
		"key":   types.StringType,
		"value": types.StringType,
	},
}

// TagTagMgmtRsOoBConsMgmtInstPResourceModel describes the resource data model for the children without relation ships.
type TagTagMgmtRsOoBConsMgmtInstPResourceModel struct {
	Key   types.String `tfsdk:"key"`
	Value types.String `tfsdk:"value"`
}

func getEmptyTagTagMgmtRsOoBConsMgmtInstPResourceModel() TagTagMgmtRsOoBConsMgmtInstPResourceModel {
	return TagTagMgmtRsOoBConsMgmtInstPResourceModel{
		Key:   basetypes.NewStringNull(),
		Value: basetypes.NewStringNull(),
	}
}

var TagTagMgmtRsOoBConsMgmtInstPType = types.ObjectType{
	AttrTypes: map[string]attr.Type{
		"key":   types.StringType,
		"value": types.StringType,
	},
}

// TagAnnotationMgmtInstPResourceModel describes the resource data model for the children without relation ships.
type TagAnnotationMgmtInstPResourceModel struct {
	Key   types.String `tfsdk:"key"`
	Value types.String `tfsdk:"value"`
}

func getEmptyTagAnnotationMgmtInstPResourceModel() TagAnnotationMgmtInstPResourceModel {
	return TagAnnotationMgmtInstPResourceModel{
		Key:   basetypes.NewStringNull(),
		Value: basetypes.NewStringNull(),
	}
}

var TagAnnotationMgmtInstPType = types.ObjectType{
	AttrTypes: map[string]attr.Type{
		"key":   types.StringType,
		"value": types.StringType,
	},
}

// TagTagMgmtInstPResourceModel describes the resource data model for the children without relation ships.
type TagTagMgmtInstPResourceModel struct {
	Key   types.String `tfsdk:"key"`
	Value types.String `tfsdk:"value"`
}

func getEmptyTagTagMgmtInstPResourceModel() TagTagMgmtInstPResourceModel {
	return TagTagMgmtInstPResourceModel{
		Key:   basetypes.NewStringNull(),
		Value: basetypes.NewStringNull(),
	}
}

var TagTagMgmtInstPType = types.ObjectType{
	AttrTypes: map[string]attr.Type{
		"key":   types.StringType,
		"value": types.StringType,
	},
}

type MgmtInstPIdentifier struct {
	Name types.String
}

func (r *MgmtInstPResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
	if !req.Plan.Raw.IsNull() {
		var planData, stateData *MgmtInstPResourceModel
		resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
		resp.Diagnostics.Append(req.State.Get(ctx, &stateData)...)

		if resp.Diagnostics.HasError() {
			return
		}

		if (planData.Id.IsUnknown() || planData.Id.IsNull()) && !planData.Name.IsUnknown() {
			setMgmtInstPId(ctx, planData)
		}

		if stateData == nil && !globalAllowExistingOnCreate && !planData.Id.IsUnknown() && !planData.Id.IsNull() {
			CheckDn(ctx, &resp.Diagnostics, r.client, "mgmtInstP", planData.Id.ValueString())
			if resp.Diagnostics.HasError() {
				return
			}
		}

		resp.Diagnostics.Append(resp.Plan.Set(ctx, &planData)...)
	}
}

func (r *MgmtInstPResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
	tflog.Debug(ctx, "Start metadata of resource: aci_external_management_network_instance_profile")
	resp.TypeName = req.ProviderTypeName + "_external_management_network_instance_profile"
	tflog.Debug(ctx, "End metadata of resource: aci_external_management_network_instance_profile")
}

func (r *MgmtInstPResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
	tflog.Debug(ctx, "Start schema of resource: aci_external_management_network_instance_profile")
	resp.Schema = schema.Schema{
		// This description is used by the documentation generator and the language server.
		MarkdownDescription: "The external_management_network_instance_profile resource for the 'mgmtInstP' class",

		Attributes: map[string]schema.Attribute{
			"id": schema.StringAttribute{
				Computed:            true,
				MarkdownDescription: "The distinguished name (DN) of the External Management Network Instance Profile object.",
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
				},
			},
			"annotation": schema.StringAttribute{
				Optional: true,
				Computed: true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
					SetToStringNullWhenStateIsNullPlanIsUnknownDuringUpdate(),
				},
				Default:             stringdefault.StaticString(globalAnnotation),
				MarkdownDescription: `The annotation of the External Management Network Instance Profile object.`,
			},
			"description": schema.StringAttribute{
				Optional: true,
				Computed: true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
					SetToStringNullWhenStateIsNullPlanIsUnknownDuringUpdate(),
				},
				MarkdownDescription: `The description of the External Management Network Instance Profile object.`,
			},
			"name": schema.StringAttribute{
				Required: true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
					SetToStringNullWhenStateIsNullPlanIsUnknownDuringUpdate(),
					stringplanmodifier.RequiresReplace(),
				},
				MarkdownDescription: `The name of the External Management Network Instance Profile object.`,
			},
			"name_alias": schema.StringAttribute{
				Optional: true,
				Computed: true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
					SetToStringNullWhenStateIsNullPlanIsUnknownDuringUpdate(),
				},
				MarkdownDescription: `The name alias of the External Management Network Instance Profile object.`,
			},
			"priority": schema.StringAttribute{
				CustomType: customTypes.MgmtInstPPrioStringType{},
				Optional:   true,
				Computed:   true,
				PlanModifiers: []planmodifier.String{
					stringplanmodifier.UseStateForUnknown(),
					SetToStringNullWhenStateIsNullPlanIsUnknownDuringUpdate(),
				},
				Validators: []validator.String{
					stringvalidator.Any(
						stringvalidator.OneOf("level1", "level2", "level3", "level4", "level5", "level6", "unspecified"),
						validators.InBetweenFromString(0, 9),
					),
				},
				MarkdownDescription: `The Quality of Service (QoS) priority class ID. QoS refers to the capability of a network to provide better service to selected network traffic over various technologies. The primary goal of QoS is to provide priority including dedicated bandwidth, controlled jitter and latency (required by some real-time and interactive traffic), and improved loss characteristics. You can configure the bandwidth of each QoS level using QoS profiles.`,
			},
			"relation_to_consumed_out_of_band_contracts": schema.SetNestedAttribute{
				MarkdownDescription: `An external management entity instance profile to an out-of-band binary contract profile. The instance profiles of external management entities can communicate with nodes that are part of out-of-band management endpoint group. To enable this communication, a contract is required between the instance profile and the out-of-band management endpoint group.`,
				Optional:            true,
				Computed:            true,
				PlanModifiers: []planmodifier.Set{
					setplanmodifier.UseStateForUnknown(),
					SetToSetNullWhenStateIsNullPlanIsUnknownDuringUpdate(MgmtRsOoBConsMgmtInstPSetToSetNullWhenStateIsNullPlanIsUnknownDuringUpdate),
				},
				NestedObject: schema.NestedAttributeObject{
					Attributes: map[string]schema.Attribute{
						"annotation": schema.StringAttribute{
							Optional: true,
							Computed: true,
							PlanModifiers: []planmodifier.String{
								stringplanmodifier.UseStateForUnknown(),
							},
							MarkdownDescription: `The annotation of the Relation To Consumed Out Of Band Contract object.`,
						},
						"priority": schema.StringAttribute{
							CustomType: customTypes.MgmtRsOoBConsPrioStringType{},
							Optional:   true,
							Computed:   true,
							PlanModifiers: []planmodifier.String{
								stringplanmodifier.UseStateForUnknown(),
							},
							Validators: []validator.String{
								stringvalidator.Any(
									stringvalidator.OneOf("level1", "level2", "level3", "level4", "level5", "level6", "unspecified"),
									validators.InBetweenFromString(0, 9),
								),
							},
							MarkdownDescription: `The Quality of Service (QoS) priority class ID. QoS refers to the capability of a network to provide better service to selected network traffic over various technologies. The primary goal of QoS is to provide priority including dedicated bandwidth, controlled jitter and latency (required by some real-time and interactive traffic), and improved loss characteristics. You can configure the bandwidth of each QoS level using QoS profiles.`,
						},
						"out_of_band_contract_name": schema.StringAttribute{
							Required: true,
							PlanModifiers: []planmodifier.String{
								stringplanmodifier.UseStateForUnknown(),
							},
							MarkdownDescription: `The name of the Out Of Band Contract object.`,
						},
						"annotations": schema.SetNestedAttribute{
							MarkdownDescription: ``,
							Optional:            true,
							Computed:            true,
							PlanModifiers: []planmodifier.Set{
								setplanmodifier.UseStateForUnknown(),
							},
							NestedObject: schema.NestedAttributeObject{
								Attributes: map[string]schema.Attribute{
									"key": schema.StringAttribute{
										Required: true,
										PlanModifiers: []planmodifier.String{
											stringplanmodifier.UseStateForUnknown(),
										},
										MarkdownDescription: `The key used to uniquely identify this configuration object.`,
									},
									"value": schema.StringAttribute{
										Required: true,
										PlanModifiers: []planmodifier.String{
											stringplanmodifier.UseStateForUnknown(),
										},
										MarkdownDescription: `The value of the property.`,
									},
								},
							},
						},
						"tags": schema.SetNestedAttribute{
							MarkdownDescription: ``,
							Optional:            true,
							Computed:            true,
							PlanModifiers: []planmodifier.Set{
								setplanmodifier.UseStateForUnknown(),
							},
							NestedObject: schema.NestedAttributeObject{
								Attributes: map[string]schema.Attribute{
									"key": schema.StringAttribute{
										Required: true,
										PlanModifiers: []planmodifier.String{
											stringplanmodifier.UseStateForUnknown(),
										},
										MarkdownDescription: `The key used to uniquely identify this configuration object.`,
									},
									"value": schema.StringAttribute{
										Required: true,
										PlanModifiers: []planmodifier.String{
											stringplanmodifier.UseStateForUnknown(),
										},
										MarkdownDescription: `The value of the property.`,
									},
								},
							},
						},
					},
				},
			},
			"annotations": schema.SetNestedAttribute{
				MarkdownDescription: ``,
				Optional:            true,
				Computed:            true,
				PlanModifiers: []planmodifier.Set{
					setplanmodifier.UseStateForUnknown(),
				},
				NestedObject: schema.NestedAttributeObject{
					Attributes: map[string]schema.Attribute{
						"key": schema.StringAttribute{
							Required: true,
							PlanModifiers: []planmodifier.String{
								stringplanmodifier.UseStateForUnknown(),
							},
							MarkdownDescription: `The key used to uniquely identify this configuration object.`,
						},
						"value": schema.StringAttribute{
							Required: true,
							PlanModifiers: []planmodifier.String{
								stringplanmodifier.UseStateForUnknown(),
							},
							MarkdownDescription: `The value of the property.`,
						},
					},
				},
			},
			"tags": schema.SetNestedAttribute{
				MarkdownDescription: ``,
				Optional:            true,
				Computed:            true,
				PlanModifiers: []planmodifier.Set{
					setplanmodifier.UseStateForUnknown(),
				},
				NestedObject: schema.NestedAttributeObject{
					Attributes: map[string]schema.Attribute{
						"key": schema.StringAttribute{
							Required: true,
							PlanModifiers: []planmodifier.String{
								stringplanmodifier.UseStateForUnknown(),
							},
							MarkdownDescription: `The key used to uniquely identify this configuration object.`,
						},
						"value": schema.StringAttribute{
							Required: true,
							PlanModifiers: []planmodifier.String{
								stringplanmodifier.UseStateForUnknown(),
							},
							MarkdownDescription: `The value of the property.`,
						},
					},
				},
			},
		},
	}
	tflog.Debug(ctx, "End schema of resource: aci_external_management_network_instance_profile")
}

func (r *MgmtInstPResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
	tflog.Debug(ctx, "Start configure of resource: aci_external_management_network_instance_profile")
	// Prevent panic if the provider has not been configured.
	if req.ProviderData == nil {
		return
	}

	client, ok := req.ProviderData.(*client.Client)

	if !ok {
		resp.Diagnostics.AddError(
			"Unexpected Resource Configure Type",
			fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
		)

		return
	}

	r.client = client
	tflog.Debug(ctx, "End configure of resource: aci_external_management_network_instance_profile")
}

func (r *MgmtInstPResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
	tflog.Debug(ctx, "Start create of resource: aci_external_management_network_instance_profile")
	// On create retrieve information on current state prior to making any changes in order to determine child delete operations
	var stateData *MgmtInstPResourceModel
	resp.Diagnostics.Append(req.Plan.Get(ctx, &stateData)...)
	if stateData.Id.IsUnknown() || stateData.Id.IsNull() {
		setMgmtInstPId(ctx, stateData)
	}
	getAndSetMgmtInstPAttributes(ctx, &resp.Diagnostics, r.client, stateData)
	if !globalAllowExistingOnCreate && !stateData.Id.IsNull() {
		resp.Diagnostics.AddError(
			"Object Already Exists",
			fmt.Sprintf("The mgmtInstP object with DN '%s' already exists.", stateData.Id.ValueString()),
		)
		return
	}

	var data *MgmtInstPResourceModel

	// Read Terraform plan data into the model
	resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

	if resp.Diagnostics.HasError() {
		return
	}

	if data.Id.IsUnknown() || data.Id.IsNull() {
		setMgmtInstPId(ctx, data)
	}

	tflog.Debug(ctx, fmt.Sprintf("Create of resource aci_external_management_network_instance_profile with id '%s'", data.Id.ValueString()))

	var mgmtRsOoBConsPlan, mgmtRsOoBConsState []MgmtRsOoBConsMgmtInstPResourceModel
	data.MgmtRsOoBCons.ElementsAs(ctx, &mgmtRsOoBConsPlan, false)
	stateData.MgmtRsOoBCons.ElementsAs(ctx, &mgmtRsOoBConsState, false)
	var tagAnnotationPlan, tagAnnotationState []TagAnnotationMgmtInstPResourceModel
	data.TagAnnotation.ElementsAs(ctx, &tagAnnotationPlan, false)
	stateData.TagAnnotation.ElementsAs(ctx, &tagAnnotationState, false)
	var tagTagPlan, tagTagState []TagTagMgmtInstPResourceModel
	data.TagTag.ElementsAs(ctx, &tagTagPlan, false)
	stateData.TagTag.ElementsAs(ctx, &tagTagState, false)
	jsonPayload := getMgmtInstPCreateJsonPayload(ctx, &resp.Diagnostics, true, data, mgmtRsOoBConsPlan, mgmtRsOoBConsState, tagAnnotationPlan, tagAnnotationState, tagTagPlan, tagTagState)

	if resp.Diagnostics.HasError() {
		return
	}

	DoRestRequest(ctx, &resp.Diagnostics, r.client, fmt.Sprintf("api/mo/%s.json", data.Id.ValueString()), "POST", jsonPayload)

	if resp.Diagnostics.HasError() {
		return
	}

	getAndSetMgmtInstPAttributes(ctx, &resp.Diagnostics, r.client, data)

	// Save data into Terraform state
	resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
	tflog.Debug(ctx, fmt.Sprintf("End create of resource aci_external_management_network_instance_profile with id '%s'", data.Id.ValueString()))
}

func (r *MgmtInstPResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
	tflog.Debug(ctx, "Start read of resource: aci_external_management_network_instance_profile")
	var data *MgmtInstPResourceModel

	// Read Terraform prior state data into the model
	resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

	if resp.Diagnostics.HasError() {
		return
	}

	tflog.Debug(ctx, fmt.Sprintf("Read of resource aci_external_management_network_instance_profile with id '%s'", data.Id.ValueString()))

	getAndSetMgmtInstPAttributes(ctx, &resp.Diagnostics, r.client, data)

	// Save updated data into Terraform state
	if data.Id.IsNull() {
		var emptyData *MgmtInstPResourceModel
		resp.Diagnostics.Append(resp.State.Set(ctx, &emptyData)...)
	} else {
		resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
	}

	tflog.Debug(ctx, fmt.Sprintf("End read of resource aci_external_management_network_instance_profile with id '%s'", data.Id.ValueString()))
}

func (r *MgmtInstPResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
	tflog.Debug(ctx, "Start update of resource: aci_external_management_network_instance_profile")
	var data *MgmtInstPResourceModel
	var stateData *MgmtInstPResourceModel

	// Read Terraform plan data into the model
	resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
	resp.Diagnostics.Append(req.State.Get(ctx, &stateData)...)

	if resp.Diagnostics.HasError() {
		return
	}

	tflog.Debug(ctx, fmt.Sprintf("Update of resource aci_external_management_network_instance_profile with id '%s'", data.Id.ValueString()))

	var mgmtRsOoBConsPlan, mgmtRsOoBConsState []MgmtRsOoBConsMgmtInstPResourceModel
	data.MgmtRsOoBCons.ElementsAs(ctx, &mgmtRsOoBConsPlan, false)
	stateData.MgmtRsOoBCons.ElementsAs(ctx, &mgmtRsOoBConsState, false)
	var tagAnnotationPlan, tagAnnotationState []TagAnnotationMgmtInstPResourceModel
	data.TagAnnotation.ElementsAs(ctx, &tagAnnotationPlan, false)
	stateData.TagAnnotation.ElementsAs(ctx, &tagAnnotationState, false)
	var tagTagPlan, tagTagState []TagTagMgmtInstPResourceModel
	data.TagTag.ElementsAs(ctx, &tagTagPlan, false)
	stateData.TagTag.ElementsAs(ctx, &tagTagState, false)
	jsonPayload := getMgmtInstPCreateJsonPayload(ctx, &resp.Diagnostics, false, data, mgmtRsOoBConsPlan, mgmtRsOoBConsState, tagAnnotationPlan, tagAnnotationState, tagTagPlan, tagTagState)

	if resp.Diagnostics.HasError() {
		return
	}

	DoRestRequest(ctx, &resp.Diagnostics, r.client, fmt.Sprintf("api/mo/%s.json", data.Id.ValueString()), "POST", jsonPayload)

	if resp.Diagnostics.HasError() {
		return
	}

	getAndSetMgmtInstPAttributes(ctx, &resp.Diagnostics, r.client, data)

	// Save updated data into Terraform state
	resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
	tflog.Debug(ctx, fmt.Sprintf("End update of resource aci_external_management_network_instance_profile with id '%s'", data.Id.ValueString()))
}

func (r *MgmtInstPResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
	tflog.Debug(ctx, "Start delete of resource: aci_external_management_network_instance_profile")
	var data *MgmtInstPResourceModel

	// Read Terraform prior state data into the model
	resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

	if resp.Diagnostics.HasError() {
		return
	}

	tflog.Debug(ctx, fmt.Sprintf("Delete of resource aci_external_management_network_instance_profile with id '%s'", data.Id.ValueString()))
	jsonPayload := GetDeleteJsonPayload(ctx, &resp.Diagnostics, "mgmtInstP", data.Id.ValueString())
	if resp.Diagnostics.HasError() {
		return
	}
	DoRestRequest(ctx, &resp.Diagnostics, r.client, fmt.Sprintf("api/mo/%s.json", data.Id.ValueString()), "POST", jsonPayload)
	if resp.Diagnostics.HasError() {
		return
	}
	tflog.Debug(ctx, fmt.Sprintf("End delete of resource aci_external_management_network_instance_profile with id '%s'", data.Id.ValueString()))
}

func (r *MgmtInstPResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
	tflog.Debug(ctx, "Start import state of resource: aci_external_management_network_instance_profile")
	resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)

	var stateData *MgmtInstPResourceModel
	resp.Diagnostics.Append(resp.State.Get(ctx, &stateData)...)
	tflog.Debug(ctx, fmt.Sprintf("Import state of resource aci_external_management_network_instance_profile with id '%s'", stateData.Id.ValueString()))

	tflog.Debug(ctx, "End import of state resource: aci_external_management_network_instance_profile")
}

func getAndSetMgmtInstPAttributes(ctx context.Context, diags *diag.Diagnostics, client *client.Client, data *MgmtInstPResourceModel) {
	requestData := DoRestRequest(ctx, diags, client, fmt.Sprintf("api/mo/%s.json?rsp-subtree=full&rsp-subtree-class=%s", data.Id.ValueString(), "mgmtInstP,mgmtRsOoBCons,tagAnnotation,tagTag,tagAnnotation,tagTag"), "GET", nil)

	readData := getEmptyMgmtInstPResourceModel()

	if diags.HasError() {
		return
	}
	if requestData.Search("imdata").Search("mgmtInstP").Data() != nil {
		classReadInfo := requestData.Search("imdata").Search("mgmtInstP").Data().([]interface{})
		if len(classReadInfo) == 1 {
			attributes := classReadInfo[0].(map[string]interface{})["attributes"].(map[string]interface{})
			for attributeName, attributeValue := range attributes {
				if attributeName == "dn" {
					readData.Id = basetypes.NewStringValue(attributeValue.(string))
				}
				if attributeName == "annotation" {
					readData.Annotation = basetypes.NewStringValue(attributeValue.(string))
				}
				if attributeName == "descr" {
					readData.Descr = basetypes.NewStringValue(attributeValue.(string))
				}
				if attributeName == "name" {
					readData.Name = basetypes.NewStringValue(attributeValue.(string))
				}
				if attributeName == "nameAlias" {
					readData.NameAlias = basetypes.NewStringValue(attributeValue.(string))
				}
				if attributeName == "prio" {
					readData.Prio = customTypes.NewMgmtInstPPrioStringValue(attributeValue.(string))
				}
			}
			MgmtRsOoBConsMgmtInstPList := make([]MgmtRsOoBConsMgmtInstPResourceModel, 0)
			TagAnnotationMgmtInstPList := make([]TagAnnotationMgmtInstPResourceModel, 0)
			TagTagMgmtInstPList := make([]TagTagMgmtInstPResourceModel, 0)
			_, ok := classReadInfo[0].(map[string]interface{})["children"]
			if ok {
				children := classReadInfo[0].(map[string]interface{})["children"].([]interface{})
				for _, child := range children {
					for childClassName, childClassDetails := range child.(map[string]interface{}) {
						childAttributes := childClassDetails.(map[string]interface{})["attributes"].(map[string]interface{})
						if childClassName == "mgmtRsOoBCons" {
							MgmtRsOoBConsMgmtInstP := getEmptyMgmtRsOoBConsMgmtInstPResourceModel()
							for childAttributeName, childAttributeValue := range childAttributes {
								if childAttributeName == "annotation" {
									MgmtRsOoBConsMgmtInstP.Annotation = basetypes.NewStringValue(childAttributeValue.(string))
								}
								if childAttributeName == "prio" {
									MgmtRsOoBConsMgmtInstP.Prio = customTypes.NewMgmtRsOoBConsPrioStringValue(childAttributeValue.(string))
								}
								if childAttributeName == "tnVzOOBBrCPName" {
									MgmtRsOoBConsMgmtInstP.TnVzOOBBrCPName = basetypes.NewStringValue(childAttributeValue.(string))
								}

							}
							TagAnnotationMgmtRsOoBConsMgmtInstPList := make([]TagAnnotationMgmtRsOoBConsMgmtInstPResourceModel, 0)
							TagTagMgmtRsOoBConsMgmtInstPList := make([]TagTagMgmtRsOoBConsMgmtInstPResourceModel, 0)
							childrenOfMgmtRsOoBConsMgmtInstP, childrenOfMgmtRsOoBConsMgmtInstPExist := childClassDetails.(map[string]interface{})["children"]
							if childrenOfMgmtRsOoBConsMgmtInstPExist {
								for _, childMgmtRsOoBConsMgmtInstP := range childrenOfMgmtRsOoBConsMgmtInstP.([]interface{}) {
									for childClassNameMgmtRsOoBConsMgmtInstP, childClassDetailsMgmtRsOoBConsMgmtInstP := range childMgmtRsOoBConsMgmtInstP.(map[string]interface{}) {
										if childClassNameMgmtRsOoBConsMgmtInstP == "tagAnnotation" {
											TagAnnotationMgmtRsOoBConsMgmtInstP := getEmptyTagAnnotationMgmtRsOoBConsMgmtInstPResourceModel()
											tagAnnotationchildAttributeValue := childClassDetailsMgmtRsOoBConsMgmtInstP.(map[string]interface{})["attributes"].(map[string]interface{})
											for childAttributeName, childAttributeValue := range tagAnnotationchildAttributeValue {
												if childAttributeName == "key" {
													TagAnnotationMgmtRsOoBConsMgmtInstP.Key = basetypes.NewStringValue(childAttributeValue.(string))
												}
												if childAttributeName == "value" {
													TagAnnotationMgmtRsOoBConsMgmtInstP.Value = basetypes.NewStringValue(childAttributeValue.(string))
												}
											}
											TagAnnotationMgmtRsOoBConsMgmtInstPList = append(TagAnnotationMgmtRsOoBConsMgmtInstPList, TagAnnotationMgmtRsOoBConsMgmtInstP)
										}
										if childClassNameMgmtRsOoBConsMgmtInstP == "tagTag" {
											TagTagMgmtRsOoBConsMgmtInstP := getEmptyTagTagMgmtRsOoBConsMgmtInstPResourceModel()
											tagTagchildAttributeValue := childClassDetailsMgmtRsOoBConsMgmtInstP.(map[string]interface{})["attributes"].(map[string]interface{})
											for childAttributeName, childAttributeValue := range tagTagchildAttributeValue {
												if childAttributeName == "key" {
													TagTagMgmtRsOoBConsMgmtInstP.Key = basetypes.NewStringValue(childAttributeValue.(string))
												}
												if childAttributeName == "value" {
													TagTagMgmtRsOoBConsMgmtInstP.Value = basetypes.NewStringValue(childAttributeValue.(string))
												}
											}
											TagTagMgmtRsOoBConsMgmtInstPList = append(TagTagMgmtRsOoBConsMgmtInstPList, TagTagMgmtRsOoBConsMgmtInstP)
										}
									}
								}
							}
							TagAnnotationMgmtRsOoBConsMgmtInstPSet, _ := types.SetValueFrom(ctx, TagAnnotationMgmtRsOoBConsMgmtInstPType, TagAnnotationMgmtRsOoBConsMgmtInstPList)
							MgmtRsOoBConsMgmtInstP.TagAnnotation = TagAnnotationMgmtRsOoBConsMgmtInstPSet
							TagTagMgmtRsOoBConsMgmtInstPSet, _ := types.SetValueFrom(ctx, TagTagMgmtRsOoBConsMgmtInstPType, TagTagMgmtRsOoBConsMgmtInstPList)
							MgmtRsOoBConsMgmtInstP.TagTag = TagTagMgmtRsOoBConsMgmtInstPSet
							MgmtRsOoBConsMgmtInstPList = append(MgmtRsOoBConsMgmtInstPList, MgmtRsOoBConsMgmtInstP)
						}
						if childClassName == "tagAnnotation" {
							TagAnnotationMgmtInstP := getEmptyTagAnnotationMgmtInstPResourceModel()
							for childAttributeName, childAttributeValue := range childAttributes {
								if childAttributeName == "key" {
									TagAnnotationMgmtInstP.Key = basetypes.NewStringValue(childAttributeValue.(string))
								}
								if childAttributeName == "value" {
									TagAnnotationMgmtInstP.Value = basetypes.NewStringValue(childAttributeValue.(string))
								}

							}
							TagAnnotationMgmtInstPList = append(TagAnnotationMgmtInstPList, TagAnnotationMgmtInstP)
						}
						if childClassName == "tagTag" {
							TagTagMgmtInstP := getEmptyTagTagMgmtInstPResourceModel()
							for childAttributeName, childAttributeValue := range childAttributes {
								if childAttributeName == "key" {
									TagTagMgmtInstP.Key = basetypes.NewStringValue(childAttributeValue.(string))
								}
								if childAttributeName == "value" {
									TagTagMgmtInstP.Value = basetypes.NewStringValue(childAttributeValue.(string))
								}

							}
							TagTagMgmtInstPList = append(TagTagMgmtInstPList, TagTagMgmtInstP)
						}
					}
				}
			}
			mgmtRsOoBConsSet, _ := types.SetValueFrom(ctx, readData.MgmtRsOoBCons.ElementType(ctx), MgmtRsOoBConsMgmtInstPList)
			readData.MgmtRsOoBCons = mgmtRsOoBConsSet
			tagAnnotationSet, _ := types.SetValueFrom(ctx, readData.TagAnnotation.ElementType(ctx), TagAnnotationMgmtInstPList)
			readData.TagAnnotation = tagAnnotationSet
			tagTagSet, _ := types.SetValueFrom(ctx, readData.TagTag.ElementType(ctx), TagTagMgmtInstPList)
			readData.TagTag = tagTagSet
		} else {
			diags.AddError(
				"too many results in response",
				fmt.Sprintf("%v matches returned for class 'mgmtInstP'. Please report this issue to the provider developers.", len(classReadInfo)),
			)
		}
	} else {
		readData.Id = basetypes.NewStringNull()
	}
	*data = *readData
}

func getMgmtInstPRn(ctx context.Context, data *MgmtInstPResourceModel) string {
	return fmt.Sprintf("tn-mgmt/extmgmt-default/instp-%s", data.Name.ValueString())
}

func setMgmtInstPId(ctx context.Context, data *MgmtInstPResourceModel) {
	rn := getMgmtInstPRn(ctx, data)
	data.Id = types.StringValue(fmt.Sprintf("%s/%s", strings.Split([]string{"uni/tn-mgmt/extmgmt-default/instp-{name}"}[0], "/")[0], rn))
}

func getMgmtInstPMgmtRsOoBConsChildPayloads(ctx context.Context, diags *diag.Diagnostics, data *MgmtInstPResourceModel, mgmtRsOoBConsMgmtInstPPlan, mgmtRsOoBConsMgmtInstPState []MgmtRsOoBConsMgmtInstPResourceModel) []map[string]interface{} {
	childPayloads := []map[string]interface{}{}
	if !data.MgmtRsOoBCons.IsNull() && !data.MgmtRsOoBCons.IsUnknown() {
		mgmtRsOoBConsIdentifiers := []MgmtRsOoBConsIdentifier{}
		for _, mgmtRsOoBConsMgmtInstP := range mgmtRsOoBConsMgmtInstPPlan {
			MgmtRsOoBConsMgmtInstPChildren := make([]map[string]interface{}, 0)
			childMap := NewAciObject()
			if !mgmtRsOoBConsMgmtInstP.Annotation.IsNull() && !mgmtRsOoBConsMgmtInstP.Annotation.IsUnknown() {
				childMap.Attributes["annotation"] = mgmtRsOoBConsMgmtInstP.Annotation.ValueString()
			} else {
				childMap.Attributes["annotation"] = globalAnnotation
			}
			if !mgmtRsOoBConsMgmtInstP.Prio.IsNull() && !mgmtRsOoBConsMgmtInstP.Prio.IsUnknown() {
				childMap.Attributes["prio"] = mgmtRsOoBConsMgmtInstP.Prio.ValueString()
			}
			if !mgmtRsOoBConsMgmtInstP.TnVzOOBBrCPName.IsNull() && !mgmtRsOoBConsMgmtInstP.TnVzOOBBrCPName.IsUnknown() {
				childMap.Attributes["tnVzOOBBrCPName"] = mgmtRsOoBConsMgmtInstP.TnVzOOBBrCPName.ValueString()
			}

			var tagAnnotationMgmtRsOoBConsMgmtInstPPlan, tagAnnotationMgmtRsOoBConsMgmtInstPState []TagAnnotationMgmtRsOoBConsMgmtInstPResourceModel
			mgmtRsOoBConsMgmtInstP.TagAnnotation.ElementsAs(ctx, &tagAnnotationMgmtRsOoBConsMgmtInstPPlan, false)
			for _, tagAnnotationMgmtRsOoBConsMgmtInstPstate := range mgmtRsOoBConsMgmtInstPState {
				tagAnnotationMgmtRsOoBConsMgmtInstPstate.TagAnnotation.ElementsAs(ctx, &tagAnnotationMgmtRsOoBConsMgmtInstPState, false)
			}
			if !mgmtRsOoBConsMgmtInstP.TagAnnotation.IsNull() && !mgmtRsOoBConsMgmtInstP.TagAnnotation.IsUnknown() {
				tagAnnotationIdentifiers := []TagAnnotationIdentifier{}
				for _, tagAnnotationMgmtRsOoBConsMgmtInstP := range tagAnnotationMgmtRsOoBConsMgmtInstPPlan {
					tagAnnotationMgmtRsOoBConsMgmtInstPChildMap := NewAciObject()
					if !tagAnnotationMgmtRsOoBConsMgmtInstP.Key.IsNull() && !tagAnnotationMgmtRsOoBConsMgmtInstP.Key.IsUnknown() {
						tagAnnotationMgmtRsOoBConsMgmtInstPChildMap.Attributes["key"] = tagAnnotationMgmtRsOoBConsMgmtInstP.Key.ValueString()
					}
					if !tagAnnotationMgmtRsOoBConsMgmtInstP.Value.IsNull() && !tagAnnotationMgmtRsOoBConsMgmtInstP.Value.IsUnknown() {
						tagAnnotationMgmtRsOoBConsMgmtInstPChildMap.Attributes["value"] = tagAnnotationMgmtRsOoBConsMgmtInstP.Value.ValueString()
					}
					MgmtRsOoBConsMgmtInstPChildren = append(MgmtRsOoBConsMgmtInstPChildren, map[string]interface{}{"tagAnnotation": tagAnnotationMgmtRsOoBConsMgmtInstPChildMap})
					tagAnnotationIdentifier := TagAnnotationIdentifier{}
					tagAnnotationIdentifier.Key = tagAnnotationMgmtRsOoBConsMgmtInstP.Key
					tagAnnotationIdentifiers = append(tagAnnotationIdentifiers, tagAnnotationIdentifier)
				}
				for _, tagAnnotationMgmtRsOoBConsMgmtInstP := range tagAnnotationMgmtRsOoBConsMgmtInstPState {
					delete := true
					for _, tagAnnotationIdentifier := range tagAnnotationIdentifiers {
						if tagAnnotationIdentifier.Key == tagAnnotationMgmtRsOoBConsMgmtInstP.Key {
							delete = false
							break
						}
					}
					if delete {
						tagAnnotationMgmtRsOoBConsMgmtInstPChildMapForDelete := NewAciObject()
						tagAnnotationMgmtRsOoBConsMgmtInstPChildMapForDelete.Attributes["status"] = "deleted"
						tagAnnotationMgmtRsOoBConsMgmtInstPChildMapForDelete.Attributes["key"] = tagAnnotationMgmtRsOoBConsMgmtInstP.Key.ValueString()
						MgmtRsOoBConsMgmtInstPChildren = append(MgmtRsOoBConsMgmtInstPChildren, map[string]interface{}{"tagAnnotation": tagAnnotationMgmtRsOoBConsMgmtInstPChildMapForDelete})
					}
				}
			}

			var tagTagMgmtRsOoBConsMgmtInstPPlan, tagTagMgmtRsOoBConsMgmtInstPState []TagTagMgmtRsOoBConsMgmtInstPResourceModel
			mgmtRsOoBConsMgmtInstP.TagTag.ElementsAs(ctx, &tagTagMgmtRsOoBConsMgmtInstPPlan, false)
			for _, tagTagMgmtRsOoBConsMgmtInstPstate := range mgmtRsOoBConsMgmtInstPState {
				tagTagMgmtRsOoBConsMgmtInstPstate.TagTag.ElementsAs(ctx, &tagTagMgmtRsOoBConsMgmtInstPState, false)
			}
			if !mgmtRsOoBConsMgmtInstP.TagTag.IsNull() && !mgmtRsOoBConsMgmtInstP.TagTag.IsUnknown() {
				tagTagIdentifiers := []TagTagIdentifier{}
				for _, tagTagMgmtRsOoBConsMgmtInstP := range tagTagMgmtRsOoBConsMgmtInstPPlan {
					tagTagMgmtRsOoBConsMgmtInstPChildMap := NewAciObject()
					if !tagTagMgmtRsOoBConsMgmtInstP.Key.IsNull() && !tagTagMgmtRsOoBConsMgmtInstP.Key.IsUnknown() {
						tagTagMgmtRsOoBConsMgmtInstPChildMap.Attributes["key"] = tagTagMgmtRsOoBConsMgmtInstP.Key.ValueString()
					}
					if !tagTagMgmtRsOoBConsMgmtInstP.Value.IsNull() && !tagTagMgmtRsOoBConsMgmtInstP.Value.IsUnknown() {
						tagTagMgmtRsOoBConsMgmtInstPChildMap.Attributes["value"] = tagTagMgmtRsOoBConsMgmtInstP.Value.ValueString()
					}
					MgmtRsOoBConsMgmtInstPChildren = append(MgmtRsOoBConsMgmtInstPChildren, map[string]interface{}{"tagTag": tagTagMgmtRsOoBConsMgmtInstPChildMap})
					tagTagIdentifier := TagTagIdentifier{}
					tagTagIdentifier.Key = tagTagMgmtRsOoBConsMgmtInstP.Key
					tagTagIdentifiers = append(tagTagIdentifiers, tagTagIdentifier)
				}
				for _, tagTagMgmtRsOoBConsMgmtInstP := range tagTagMgmtRsOoBConsMgmtInstPState {
					delete := true
					for _, tagTagIdentifier := range tagTagIdentifiers {
						if tagTagIdentifier.Key == tagTagMgmtRsOoBConsMgmtInstP.Key {
							delete = false
							break
						}
					}
					if delete {
						tagTagMgmtRsOoBConsMgmtInstPChildMapForDelete := NewAciObject()
						tagTagMgmtRsOoBConsMgmtInstPChildMapForDelete.Attributes["status"] = "deleted"
						tagTagMgmtRsOoBConsMgmtInstPChildMapForDelete.Attributes["key"] = tagTagMgmtRsOoBConsMgmtInstP.Key.ValueString()
						MgmtRsOoBConsMgmtInstPChildren = append(MgmtRsOoBConsMgmtInstPChildren, map[string]interface{}{"tagTag": tagTagMgmtRsOoBConsMgmtInstPChildMapForDelete})
					}
				}
			}
			childMap.Children = MgmtRsOoBConsMgmtInstPChildren
			childPayloads = append(childPayloads, map[string]interface{}{"mgmtRsOoBCons": childMap})
			mgmtRsOoBConsIdentifier := MgmtRsOoBConsIdentifier{}
			mgmtRsOoBConsIdentifier.TnVzOOBBrCPName = mgmtRsOoBConsMgmtInstP.TnVzOOBBrCPName
			mgmtRsOoBConsIdentifiers = append(mgmtRsOoBConsIdentifiers, mgmtRsOoBConsIdentifier)
		}
		for _, mgmtRsOoBCons := range mgmtRsOoBConsMgmtInstPState {
			delete := true
			for _, mgmtRsOoBConsIdentifier := range mgmtRsOoBConsIdentifiers {
				if mgmtRsOoBConsIdentifier.TnVzOOBBrCPName == mgmtRsOoBCons.TnVzOOBBrCPName {
					delete = false
					break
				}
			}
			if delete {
				mgmtRsOoBConsChildMapForDelete := NewAciObject()
				mgmtRsOoBConsChildMapForDelete.Attributes["status"] = "deleted"
				mgmtRsOoBConsChildMapForDelete.Attributes["tnVzOOBBrCPName"] = mgmtRsOoBCons.TnVzOOBBrCPName.ValueString()
				childPayloads = append(childPayloads, map[string]interface{}{"mgmtRsOoBCons": mgmtRsOoBConsChildMapForDelete})
			}
		}
	} else {
		data.MgmtRsOoBCons = types.SetNull(data.MgmtRsOoBCons.ElementType(ctx))
	}

	return childPayloads
}

func getMgmtInstPTagAnnotationChildPayloads(ctx context.Context, diags *diag.Diagnostics, data *MgmtInstPResourceModel, tagAnnotationMgmtInstPPlan, tagAnnotationMgmtInstPState []TagAnnotationMgmtInstPResourceModel) []map[string]interface{} {
	childPayloads := []map[string]interface{}{}
	if !data.TagAnnotation.IsNull() && !data.TagAnnotation.IsUnknown() {
		tagAnnotationIdentifiers := []TagAnnotationIdentifier{}
		for _, tagAnnotationMgmtInstP := range tagAnnotationMgmtInstPPlan {
			childMap := NewAciObject()
			if !tagAnnotationMgmtInstP.Key.IsNull() && !tagAnnotationMgmtInstP.Key.IsUnknown() {
				childMap.Attributes["key"] = tagAnnotationMgmtInstP.Key.ValueString()
			}
			if !tagAnnotationMgmtInstP.Value.IsNull() && !tagAnnotationMgmtInstP.Value.IsUnknown() {
				childMap.Attributes["value"] = tagAnnotationMgmtInstP.Value.ValueString()
			}
			childPayloads = append(childPayloads, map[string]interface{}{"tagAnnotation": childMap})
			tagAnnotationIdentifier := TagAnnotationIdentifier{}
			tagAnnotationIdentifier.Key = tagAnnotationMgmtInstP.Key
			tagAnnotationIdentifiers = append(tagAnnotationIdentifiers, tagAnnotationIdentifier)
		}
		for _, tagAnnotation := range tagAnnotationMgmtInstPState {
			delete := true
			for _, tagAnnotationIdentifier := range tagAnnotationIdentifiers {
				if tagAnnotationIdentifier.Key == tagAnnotation.Key {
					delete = false
					break
				}
			}
			if delete {
				tagAnnotationChildMapForDelete := NewAciObject()
				tagAnnotationChildMapForDelete.Attributes["status"] = "deleted"
				tagAnnotationChildMapForDelete.Attributes["key"] = tagAnnotation.Key.ValueString()
				childPayloads = append(childPayloads, map[string]interface{}{"tagAnnotation": tagAnnotationChildMapForDelete})
			}
		}
	} else {
		data.TagAnnotation = types.SetNull(data.TagAnnotation.ElementType(ctx))
	}

	return childPayloads
}

func getMgmtInstPTagTagChildPayloads(ctx context.Context, diags *diag.Diagnostics, data *MgmtInstPResourceModel, tagTagMgmtInstPPlan, tagTagMgmtInstPState []TagTagMgmtInstPResourceModel) []map[string]interface{} {
	childPayloads := []map[string]interface{}{}
	if !data.TagTag.IsNull() && !data.TagTag.IsUnknown() {
		tagTagIdentifiers := []TagTagIdentifier{}
		for _, tagTagMgmtInstP := range tagTagMgmtInstPPlan {
			childMap := NewAciObject()
			if !tagTagMgmtInstP.Key.IsNull() && !tagTagMgmtInstP.Key.IsUnknown() {
				childMap.Attributes["key"] = tagTagMgmtInstP.Key.ValueString()
			}
			if !tagTagMgmtInstP.Value.IsNull() && !tagTagMgmtInstP.Value.IsUnknown() {
				childMap.Attributes["value"] = tagTagMgmtInstP.Value.ValueString()
			}
			childPayloads = append(childPayloads, map[string]interface{}{"tagTag": childMap})
			tagTagIdentifier := TagTagIdentifier{}
			tagTagIdentifier.Key = tagTagMgmtInstP.Key
			tagTagIdentifiers = append(tagTagIdentifiers, tagTagIdentifier)
		}
		for _, tagTag := range tagTagMgmtInstPState {
			delete := true
			for _, tagTagIdentifier := range tagTagIdentifiers {
				if tagTagIdentifier.Key == tagTag.Key {
					delete = false
					break
				}
			}
			if delete {
				tagTagChildMapForDelete := NewAciObject()
				tagTagChildMapForDelete.Attributes["status"] = "deleted"
				tagTagChildMapForDelete.Attributes["key"] = tagTag.Key.ValueString()
				childPayloads = append(childPayloads, map[string]interface{}{"tagTag": tagTagChildMapForDelete})
			}
		}
	} else {
		data.TagTag = types.SetNull(data.TagTag.ElementType(ctx))
	}

	return childPayloads
}

func getMgmtInstPCreateJsonPayload(ctx context.Context, diags *diag.Diagnostics, createType bool, data *MgmtInstPResourceModel, mgmtRsOoBConsPlan, mgmtRsOoBConsState []MgmtRsOoBConsMgmtInstPResourceModel, tagAnnotationPlan, tagAnnotationState []TagAnnotationMgmtInstPResourceModel, tagTagPlan, tagTagState []TagTagMgmtInstPResourceModel) *container.Container {
	payloadMap := map[string]interface{}{}
	payloadMap["attributes"] = map[string]string{}

	if createType && !globalAllowExistingOnCreate {
		payloadMap["attributes"].(map[string]string)["status"] = "created"
	}
	childPayloads := []map[string]interface{}{}

	MgmtRsOoBConschildPayloads := getMgmtInstPMgmtRsOoBConsChildPayloads(ctx, diags, data, mgmtRsOoBConsPlan, mgmtRsOoBConsState)
	if MgmtRsOoBConschildPayloads == nil {
		return nil
	}
	childPayloads = append(childPayloads, MgmtRsOoBConschildPayloads...)

	TagAnnotationchildPayloads := getMgmtInstPTagAnnotationChildPayloads(ctx, diags, data, tagAnnotationPlan, tagAnnotationState)
	if TagAnnotationchildPayloads == nil {
		return nil
	}
	childPayloads = append(childPayloads, TagAnnotationchildPayloads...)

	TagTagchildPayloads := getMgmtInstPTagTagChildPayloads(ctx, diags, data, tagTagPlan, tagTagState)
	if TagTagchildPayloads == nil {
		return nil
	}
	childPayloads = append(childPayloads, TagTagchildPayloads...)

	payloadMap["children"] = childPayloads
	if !data.Annotation.IsNull() && !data.Annotation.IsUnknown() {
		payloadMap["attributes"].(map[string]string)["annotation"] = data.Annotation.ValueString()
	}
	if !data.Descr.IsNull() && !data.Descr.IsUnknown() {
		payloadMap["attributes"].(map[string]string)["descr"] = data.Descr.ValueString()
	}
	if !data.Name.IsNull() && !data.Name.IsUnknown() {
		payloadMap["attributes"].(map[string]string)["name"] = data.Name.ValueString()
	}
	if !data.NameAlias.IsNull() && !data.NameAlias.IsUnknown() {
		payloadMap["attributes"].(map[string]string)["nameAlias"] = data.NameAlias.ValueString()
	}
	if !data.Prio.IsNull() && !data.Prio.IsUnknown() {
		payloadMap["attributes"].(map[string]string)["prio"] = data.Prio.ValueString()
	}
	payload, err := json.Marshal(map[string]interface{}{"mgmtInstP": payloadMap})
	if err != nil {
		diags.AddError(
			"Marshalling of json payload failed",
			fmt.Sprintf("Err: %s. Please report this issue to the provider developers.", err),
		)
		return nil
	}

	jsonPayload, err := container.ParseJSON(payload)

	if err != nil {
		diags.AddError(
			"Construction of json payload failed",
			fmt.Sprintf("Err: %s. Please report this issue to the provider developers.", err),
		)
		return nil
	}
	return jsonPayload
}
