github.com/Axway/agent-sdk@v1.1.101/pkg/agent/cache/apiserviceinstance.go (about) 1 package cache 2 3 import ( 4 v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 5 defs "github.com/Axway/agent-sdk/pkg/apic/definitions" 6 "github.com/Axway/agent-sdk/pkg/util" 7 ) 8 9 // API service instance management 10 11 // AddAPIServiceInstance - add/update APIServiceInstance resource in cache 12 func (c *cacheManager) AddAPIServiceInstance(resource *v1.ResourceInstance) { 13 defer c.setCacheUpdated(true) 14 c.logger. 15 WithField("resource", resource.Name). 16 WithField("apiID", resource.Metadata.ID). 17 Trace("AddAPIServiceInstance") 18 19 cachedRI, _ := c.GetAPIServiceInstanceByID(resource.Metadata.ID) 20 c.instanceMap.SetWithSecondaryKey(resource.Metadata.ID, resource.Name, resource) 21 22 if cachedRI == nil { 23 apiID, _ := util.GetAgentDetailsValue(resource, defs.AttrExternalAPIID) 24 primaryKey, _ := util.GetAgentDetailsValue(resource, defs.AttrExternalAPIPrimaryKey) 25 c.addToServiceInstanceCount(apiID, primaryKey) 26 } 27 } 28 29 // GetAPIServiceInstanceKeys - returns keys for APIServiceInstance cache 30 func (c *cacheManager) GetAPIServiceInstanceKeys() []string { 31 c.ApplyResourceReadLock() 32 defer c.ReleaseResourceReadLock() 33 34 return c.instanceMap.GetKeys() 35 } 36 37 // GetAPIServiceInstanceByID - returns resource from APIServiceInstance cache based on instance ID 38 func (c *cacheManager) GetAPIServiceInstanceByID(id string) (*v1.ResourceInstance, error) { 39 c.ApplyResourceReadLock() 40 defer c.ReleaseResourceReadLock() 41 42 item, err := c.instanceMap.Get(id) 43 if item != nil { 44 instance, ok := item.(*v1.ResourceInstance) 45 if ok { 46 return instance, nil 47 } 48 } 49 return nil, err 50 } 51 52 // GetAPIServiceInstanceByName - returns resource from APIServiceInstance cache based on instance name 53 func (c *cacheManager) GetAPIServiceInstanceByName(name string) (*v1.ResourceInstance, error) { 54 c.ApplyResourceReadLock() 55 defer c.ReleaseResourceReadLock() 56 57 item, err := c.instanceMap.GetBySecondaryKey(name) 58 if item != nil { 59 instance, ok := item.(*v1.ResourceInstance) 60 if ok { 61 return instance, nil 62 } 63 } 64 return nil, err 65 } 66 67 // DeleteAPIServiceInstance - remove APIServiceInstance resource from cache based on instance ID 68 func (c *cacheManager) DeleteAPIServiceInstance(id string) error { 69 defer c.setCacheUpdated(true) 70 71 ri, _ := c.GetAPIServiceInstanceByID(id) 72 if ri != nil { 73 apiID, _ := util.GetAgentDetailsValue(ri, defs.AttrExternalAPIID) 74 primaryKey, _ := util.GetAgentDetailsValue(ri, defs.AttrExternalAPIPrimaryKey) 75 c.removeFromServiceInstanceCount(apiID, primaryKey) 76 } 77 78 return c.instanceMap.Delete(id) 79 } 80 81 // DeleteAllAPIServiceInstance - remove all APIServiceInstance resource from cache 82 func (c *cacheManager) DeleteAllAPIServiceInstance() { 83 defer c.setCacheUpdated(true) 84 85 c.deleteAllServiceInstanceCounts() 86 c.instanceMap.Flush() 87 } 88 89 func (c *cacheManager) ListAPIServiceInstances() []*v1.ResourceInstance { 90 keys := c.GetAPIServiceInstanceKeys() 91 c.ApplyResourceReadLock() 92 defer c.ReleaseResourceReadLock() 93 94 var instances []*v1.ResourceInstance 95 96 for _, key := range keys { 97 item, _ := c.instanceMap.Get(key) 98 if item != nil { 99 instance, ok := item.(*v1.ResourceInstance) 100 if ok { 101 instances = append(instances, instance) 102 } 103 } 104 } 105 106 return instances 107 } 108 109 // countCachedInstancesForAPIService - count any instances in the cache for the newly added api 110 func (c *cacheManager) countCachedInstancesForAPIService(apiID, primaryKey string) { 111 c.logger. 112 WithField("primary-key", primaryKey). 113 WithField("api-id", apiID). 114 Trace("countCachedInstancesForAPIService") 115 116 for _, k := range c.instanceMap.GetKeys() { 117 item, _ := c.instanceMap.Get(k) 118 inst, ok := item.(*v1.ResourceInstance) 119 if !ok { 120 continue 121 } 122 instAPIID, _ := util.GetAgentDetailsValue(inst, defs.AttrExternalAPIID) 123 instPrimary, _ := util.GetAgentDetailsValue(inst, defs.AttrExternalAPIPrimaryKey) 124 if apiID == instAPIID || primaryKey == instPrimary { 125 c.addToServiceInstanceCount(apiID, primaryKey) 126 } 127 } 128 }