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

     1  package cache
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
     8  )
     9  
    10  // Custom watch resource cache related methods
    11  func (c *cacheManager) GetWatchResourceCacheKeys(group, kind string) []string {
    12  	c.ApplyResourceReadLock()
    13  	defer c.ReleaseResourceReadLock()
    14  	groupKindKeyPrefix := fmt.Sprintf("%s:%s", group, kind)
    15  
    16  	keys := make([]string, 0)
    17  	for _, key := range c.watchResourceMap.GetKeys() {
    18  		if strings.HasPrefix(key, groupKindKeyPrefix) {
    19  			keys = append(keys, key)
    20  		}
    21  	}
    22  	return keys
    23  }
    24  
    25  func (c *cacheManager) getWatchResourceKey(group, kind, key string) string {
    26  	return fmt.Sprintf("%s:%s:%s", group, kind, key)
    27  }
    28  
    29  func (c *cacheManager) AddWatchResource(ri *v1.ResourceInstance) {
    30  	if ri == nil {
    31  		return
    32  	}
    33  	group := ri.Group
    34  	kind := ri.Kind
    35  
    36  	c.watchResourceMap.SetWithSecondaryKey(c.getWatchResourceKey(group, kind, ri.Metadata.ID), c.getWatchResourceKey(group, kind, ri.Name), ri)
    37  }
    38  
    39  func (c *cacheManager) GetWatchResourceByKey(key string) *v1.ResourceInstance {
    40  	c.ApplyResourceReadLock()
    41  	defer c.ReleaseResourceReadLock()
    42  
    43  	resource, _ := c.watchResourceMap.Get(key)
    44  	if resource != nil {
    45  		if ri, ok := resource.(*v1.ResourceInstance); ok {
    46  			return ri
    47  		}
    48  	}
    49  	return nil
    50  }
    51  
    52  func (c *cacheManager) GetWatchResourceByID(group, kind, id string) *v1.ResourceInstance {
    53  	c.ApplyResourceReadLock()
    54  	defer c.ReleaseResourceReadLock()
    55  
    56  	resource, _ := c.watchResourceMap.Get(c.getWatchResourceKey(group, kind, id))
    57  	if resource != nil {
    58  		if ri, ok := resource.(*v1.ResourceInstance); ok {
    59  			return ri
    60  		}
    61  	}
    62  	return nil
    63  }
    64  
    65  func (c *cacheManager) GetWatchResourceByName(group, kind, name string) *v1.ResourceInstance {
    66  	c.ApplyResourceReadLock()
    67  	defer c.ReleaseResourceReadLock()
    68  
    69  	resource, _ := c.watchResourceMap.GetBySecondaryKey(c.getWatchResourceKey(group, kind, name))
    70  	if resource != nil {
    71  		if ri, ok := resource.(*v1.ResourceInstance); ok {
    72  			return ri
    73  		}
    74  	}
    75  	return nil
    76  }
    77  
    78  func (c *cacheManager) DeleteWatchResource(group, kind, id string) error {
    79  	return c.watchResourceMap.Delete(c.getWatchResourceKey(group, kind, id))
    80  }