-
Notifications
You must be signed in to change notification settings - Fork 13
/
util.go
52 lines (45 loc) · 1.63 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package agent
import (
"reflect"
"github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/util"
)
// ApplyResourceToConfig - applies the resources to agent configs
// Uses reflection to get the IResourceConfigCallback interface on the config struct or
// struct variable.
// Makes call to ApplyResources method with dataplane and agent resources from API server
func ApplyResourceToConfig(cfg interface{}) error {
// This defer func is to catch a possible panic that WILL occur if the cfg object that is passed in embedds the IResourceConfig interface
// within its struct, but does NOT implement the ApplyResources method. While it might be that this method really isn't necessary, we will
// log an error alerting the user in case it wasn't intentional.
defer util.HandleInterfaceFuncNotImplemented(cfg, "ApplyResources", "IResourceConfigCallback")
agentRes := GetAgentResource()
if agentRes == nil {
return nil
}
if objInterface, ok := cfg.(config.IResourceConfigCallback); ok {
err := objInterface.ApplyResources(agentRes)
if err != nil {
return err
}
}
// If the parameter is of struct pointer, use indirection to get the
// real value object
v := reflect.ValueOf(cfg)
if v.Kind() == reflect.Ptr {
v = reflect.Indirect(v)
}
// Look for ApplyResouceToConfig method on struct properties and invoke it
for i := 0; i < v.NumField(); i++ {
if v.Field(i).CanInterface() {
fieldInterface := v.Field(i).Interface()
if objInterface, ok := fieldInterface.(config.IResourceConfigCallback); ok {
err := ApplyResourceToConfig(objInterface)
if err != nil {
return err
}
}
}
}
return nil
}