github.com/Axway/agent-sdk@v1.1.101/pkg/agent/handler/tracemanagedapplication.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/watchmanager/proto"
    10  )
    11  
    12  type traceManagedApplication struct {
    13  	marketplaceHandler
    14  	cache agentcache.Manager
    15  }
    16  
    17  // NewTraceManagedApplicationHandler creates a Handler for Access Requests for trace agent
    18  func NewTraceManagedApplicationHandler(cache agentcache.Manager) Handler {
    19  	return &traceManagedApplication{
    20  		cache: cache,
    21  	}
    22  }
    23  
    24  // Handle processes grpc events triggered for ManagedApplications for trace agent
    25  func (h *traceManagedApplication) Handle(ctx context.Context, _ *proto.EventMeta, resource *apiv1.ResourceInstance) error {
    26  	action := GetActionFromContext(ctx)
    27  	if resource.Kind != management.ManagedApplicationGVK().Kind {
    28  		return nil
    29  	}
    30  
    31  	if action == proto.Event_DELETED {
    32  		return h.cache.DeleteManagedApplication(resource.Metadata.ID)
    33  	}
    34  
    35  	app := &management.ManagedApplication{}
    36  	err := app.FromInstance(resource)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	ok := isStatusFound(app.Status)
    42  	if !ok {
    43  		return nil
    44  	}
    45  
    46  	if h.shouldProcessForTrace(app.Status, app.Metadata.State) {
    47  		cachedApp := h.cache.GetManagedApplication(resource.Metadata.ID)
    48  		if cachedApp == nil {
    49  			h.cache.AddManagedApplication(resource)
    50  		}
    51  	}
    52  	return nil
    53  }