package hypervisors

import (
	"context"

	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/sdn/hypervisors"
	"repository.basistech.ru/BASIS/terraform-provider-decort/internal/controller"
)

func utilityHypervisorCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*hypervisors.RecordHypervisor, error) {
	c := m.(*controller.ControllerCfg)

	req := hypervisors.GetRequest{}

	if d.Id() != "" {
		req.Name = d.Id()
	} else {
		req.Name = d.Get("name").(string)
	}

	if portInfo, ok := d.GetOk("port_info"); ok {
		req.PortInfo = portInfo.(string)
	}

	hypervisor, err := c.SDN().Hypervisors().Get(ctx, req)
	if err != nil {
		return nil, err
	}

	return hypervisor, nil
}
