@@ -44,6 +44,7 @@ const (
4444 KT_INTERFACE_LOOKUP_TEXT_FILTER = "KT_INTERFACE_LOOKUP_TEXT_FILTER"
4545 KT_NO_CUSTOM_COLUMNS = "KT_NO_CUSTOM_COLUMNS"
4646 KT_LOAD_CUSTOM_COLS_FOR_DEVS = "KT_LOAD_CUSTOM_COLS_FOR_DEVS"
47+ KT_API_LAZY_LOAD_CUSTOMS = "KT_API_LAZY_LOAD_CUSTOMS"
4748)
4849
4950var (
@@ -72,6 +73,7 @@ type KentikApi struct {
7273 config * ktranslate.Config
7374 tagLookupClient tagging.EnumerationsAdminServiceClient
7475 loadInterfaces bool
76+ lazyLoadCustoms bool
7577}
7678
7779func NewKentikApi (ctx context.Context , log logger.ContextL , cfg * ktranslate.Config ) (* KentikApi , error ) {
@@ -81,7 +83,6 @@ func NewKentikApi(ctx context.Context, log logger.ContextL, cfg *ktranslate.Conf
8183 intv , _ := strconv .Atoi (apiTimeoutStr )
8284 apiTimeout = time .Duration (intv ) * time .Second
8385 }
84- log .Infof ("Setting API timeout to %v" , apiTimeout )
8586
8687 tr := & http.Transport {
8788 DisableCompression : false ,
@@ -94,14 +95,17 @@ func NewKentikApi(ctx context.Context, log logger.ContextL, cfg *ktranslate.Conf
9495 client := & http.Client {Transport : tr , Timeout : apiTimeout }
9596
9697 kapi := & KentikApi {
97- ContextL : log ,
98- tr : tr ,
99- client : client ,
100- apiTimeout : apiTimeout ,
101- config : cfg ,
102- loadInterfaces : kt .LookupEnvBool (KT_API_LOAD_INTERFACES , false ),
98+ ContextL : log ,
99+ tr : tr ,
100+ client : client ,
101+ apiTimeout : apiTimeout ,
102+ config : cfg ,
103+ loadInterfaces : kt .LookupEnvBool (KT_API_LOAD_INTERFACES , false ),
104+ lazyLoadCustoms : kt .LookupEnvBool (KT_API_LAZY_LOAD_CUSTOMS , false ),
103105 }
104106
107+ log .Infof ("Setting API timeout to %v, loadInterfaces=%v, lazyLoadCustoms=%v" , apiTimeout , kapi .loadInterfaces , kapi .lazyLoadCustoms )
108+
105109 // Now, check to see if synthetics API works.
106110 err := kapi .connectSynthAndLookup (ctx )
107111 if err != nil {
@@ -235,12 +239,33 @@ func (api *KentikApi) GetDevicesAsMap(cid kt.Cid) map[string]*kt.Device {
235239 return res
236240}
237241
238- func (api * KentikApi ) GetDevice (cid kt.Cid , did kt.DeviceID ) * kt.Device {
242+ func (api * KentikApi ) GetDevice (ctx context. Context , cid kt.Cid , did kt.DeviceID ) * kt.Device {
239243 if api == nil {
240244 return nil
241245 }
242246 if c , ok := api .devices [cid ]; ok {
243- return c [did ]
247+ dev := c [did ]
248+ if dev == nil {
249+ return dev
250+ }
251+ if api .lazyLoadCustoms && ! dev .LoadedCustoms () {
252+ go func () {
253+ for _ , info := range api .config .KentikCreds {
254+ md := metadata .New (map [string ]string {
255+ "X-CH-Auth-Email" : info .APIEmail ,
256+ "X-CH-Auth-API-Token" : info .APIToken ,
257+ })
258+ ctxo := metadata .NewOutgoingContext (ctx , md )
259+ err := api .loadCustoms (ctxo , dev .IDStr , dev )
260+ if err != nil {
261+ api .Warnf ("Cannot load customs for %s %s, %v" , dev .IDStr , info .APIEmail , err )
262+ } else {
263+ return
264+ }
265+ }
266+ }()
267+ }
268+ return dev
244269 }
245270 return nil
246271}
0 commit comments