package logicalports

import (
	"context"

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

func utilityLogicalPortByUniqueIDCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*logicalports.LogicalPort, error) {
	c := m.(*controller.ControllerCfg)

	req := logicalports.GetByUniqueIdentifierRequest{
		ID: d.Id(),
	}

	if d.Id() != "" {
		req.ID = d.Id()
	} else {
		req.ID = d.Get("unique_identifier").(string)
	}

	logicalPort, err := c.SDN().LogicalPorts().GetByUniqueIdentifier(ctx, req)
	if err != nil {
		return nil, err
	}

	return logicalPort, nil
}
