github.com/Axway/agent-sdk@v1.1.101/pkg/agent/handler/apiservice.go (about)

     1  package handler
     2  
     3  import (
     4  	"context"
     5  
     6  	agentcache "github.com/Axway/agent-sdk/pkg/agent/cache"
     7  	apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
     8  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
     9  	"github.com/Axway/agent-sdk/pkg/apic/definitions"
    10  	"github.com/Axway/agent-sdk/pkg/util"
    11  	"github.com/Axway/agent-sdk/pkg/watchmanager/proto"
    12  )
    13  
    14  type apiSvcHandler struct {
    15  	agentCacheManager agentcache.Manager
    16  	envName           string
    17  }
    18  
    19  // NewAPISvcHandler creates a Handler for API Services.
    20  func NewAPISvcHandler(agentCacheManager agentcache.Manager, envName string) Handler {
    21  	return &apiSvcHandler{
    22  		agentCacheManager: agentCacheManager,
    23  		envName:           envName,
    24  	}
    25  }
    26  
    27  func (h *apiSvcHandler) Handle(ctx context.Context, _ *proto.EventMeta, resource *apiv1.ResourceInstance) error {
    28  	action := GetActionFromContext(ctx)
    29  	if resource.Kind != management.APIServiceGVK().Kind {
    30  		return nil
    31  	}
    32  
    33  	if resource.Metadata.Scope.Name != h.envName {
    34  		return nil
    35  	}
    36  
    37  	log := getLoggerFromContext(ctx).
    38  		WithComponent("apiServiceHandler").
    39  		WithField("action", action).
    40  		WithField("resource", resource.Name).
    41  		WithField("resourceID", resource.Metadata.ID)
    42  
    43  	id, err := util.GetAgentDetailsValue(resource, definitions.AttrExternalAPIID)
    44  	if err != nil {
    45  		log.WithError(err).Error("could not find the external API ID on the API Service")
    46  	}
    47  	log = log.WithField("apiID", id)
    48  
    49  	defer log.Trace("finished processing request")
    50  
    51  	if action != proto.Event_DELETED {
    52  		log.Debug("adding or updating api service in cache")
    53  		err := h.agentCacheManager.AddAPIService(resource)
    54  		if err != nil {
    55  			log.WithError(err).Error("could not handle api service event")
    56  		}
    57  		return err
    58  	}
    59  
    60  	// remove the service from the cache by name
    61  	return h.agentCacheManager.DeleteAPIService(resource.Name)
    62  }