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 utilityLogicalPortCheckPresence(ctx context.Context, d *schema.ResourceData, m interface{}) (*logicalports.LogicalPort, error) {
	c := m.(*controller.ControllerCfg)

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

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

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

	return logicalPort, nil
}
