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

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