func()

in ec/ecresource/deploymentresource/deployment/v2/deployment_update_payload.go [37:104]


func (plan DeploymentTF) getBaseUpdatePayloads(ctx context.Context, client *api.API, state DeploymentTF, migrateTemplateRequest *deployments.MigrateDeploymentTemplateOK) (*models.DeploymentUpdateResources, diag.Diagnostics) {
	var diags diag.Diagnostics

	newDtId := plan.DeploymentTemplateId.ValueString()
	prevDtId := state.DeploymentTemplateId.ValueString()

	template, err := deptemplateapi.Get(deptemplateapi.GetParams{
		API:                        client,
		TemplateID:                 newDtId,
		Region:                     plan.Region.ValueString(),
		HideInstanceConfigurations: true,
	})

	if err != nil {
		diags.AddError("Failed to get template", err.Error())
		return nil, diags
	}

	baseUpdatePayloads := &models.DeploymentUpdateResources{
		Apm:                template.DeploymentTemplate.Resources.Apm,
		Appsearch:          template.DeploymentTemplate.Resources.Appsearch,
		Elasticsearch:      template.DeploymentTemplate.Resources.Elasticsearch,
		EnterpriseSearch:   template.DeploymentTemplate.Resources.EnterpriseSearch,
		IntegrationsServer: template.DeploymentTemplate.Resources.IntegrationsServer,
		Kibana:             template.DeploymentTemplate.Resources.Kibana,
	}

	planHasNodeTypes, d := elasticsearchv2.PlanHasNodeTypes(ctx, plan.Elasticsearch)

	// Template migration isn't available for deployments using node types
	if planHasNodeTypes {
		return baseUpdatePayloads, diags
	}

	if d.HasError() {
		return nil, d
	}

	templateChanged := newDtId != prevDtId && prevDtId != ""

	// If the deployment template has changed, we should use the template migration API to build the base update payloads
	if templateChanged {
		// If the template has changed, we can't use the migrate request from private state. This is why we set
		// migrateTemplateRequest to nil here, so that a new request has to be performed
		d = plan.updateBasePayloadsForMigration(client, newDtId, nil, baseUpdatePayloads)
		diags.Append(d...)

		return baseUpdatePayloads, diags
	}

	// If migrate_to_latest_hardware isn't set, we won't update the base payloads
	if !plan.MigrateToLatestHardware.ValueBool() {
		return baseUpdatePayloads, diags
	}

	isMigrationAvailable, d := plan.checkAvailableMigration(ctx, state)
	diags.Append(d...)

	// If MigrateToLatestHardware is true and a migration is available, we should use the template migration API to
	// build the base update payloads
	if isMigrationAvailable {
		// Update baseUpdatePayloads according to migrateTemplateRequest
		d = plan.updateBasePayloadsForMigration(client, newDtId, migrateTemplateRequest, baseUpdatePayloads)
		diags.Append(d...)
	}

	return baseUpdatePayloads, diags
}