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

     1  package agent
     2  
     3  import (
     4  	"github.com/Axway/agent-sdk/pkg/apic"
     5  	apiV1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
     6  	"github.com/Axway/agent-sdk/pkg/apic/definitions"
     7  	"github.com/Axway/agent-sdk/pkg/util"
     8  	"github.com/Axway/agent-sdk/pkg/util/log"
     9  )
    10  
    11  // PublishAPIFunc definition for the PublishAPI func
    12  type PublishAPIFunc func(serviceBody apic.ServiceBody) error
    13  
    14  // getAPIByPrimaryKey - finds the api by the Primary Key from cache or API Server query
    15  func getAPIByPrimaryKey(primaryKey string) *apiV1.ResourceInstance {
    16  	var api *apiV1.ResourceInstance
    17  	if agent.cacheManager != nil {
    18  		api = agent.cacheManager.GetAPIServiceWithPrimaryKey(primaryKey)
    19  	}
    20  	return api
    21  }
    22  
    23  // getAPIByID - finds the api by the ID from cache or API Server query
    24  func getAPIByID(externalAPIID string) *apiV1.ResourceInstance {
    25  	var api *apiV1.ResourceInstance
    26  	if agent.cacheManager != nil {
    27  		api = agent.cacheManager.GetAPIServiceWithAPIID(externalAPIID)
    28  	}
    29  	return api
    30  }
    31  
    32  // getAPIByName - finds the api by the Name from cache or API Server query
    33  func getAPIByName(apiName string) *apiV1.ResourceInstance {
    34  	var api *apiV1.ResourceInstance
    35  	if agent.cacheManager != nil {
    36  		api = agent.cacheManager.GetAPIServiceWithName(apiName)
    37  	}
    38  	return api
    39  }
    40  
    41  // IsAPIPublished  - DEPRECATED Returns true if the API Service is already published
    42  func IsAPIPublished(externalAPIID string) bool {
    43  	// DEPRECATED
    44  	log.DeprecationWarningReplace("IsAPIPublished", "IsAPIPublishedByID")
    45  	return IsAPIPublishedByID(externalAPIID)
    46  }
    47  
    48  // IsAPIPublishedByID  - Returns true if the API Service is already published
    49  func IsAPIPublishedByID(externalAPIID string) bool {
    50  	return getAPIByID(externalAPIID) != nil
    51  }
    52  
    53  // IsAPIPublishedByPrimaryKey  - Returns true if the API Service is already published
    54  func IsAPIPublishedByPrimaryKey(primaryKey string) bool {
    55  	return getAPIByPrimaryKey(primaryKey) != nil
    56  }
    57  
    58  // GetAttributeOnPublishedAPIByName - Returns the value on published proxy
    59  func GetAttributeOnPublishedAPIByName(apiName string, attrName string) string {
    60  	api := getAPIByName(apiName)
    61  	return getAttributeFromResource(api, attrName)
    62  }
    63  
    64  // GetAttributeOnPublishedAPI - DEPRECATED Returns the value on published proxy
    65  func GetAttributeOnPublishedAPI(externalAPIID string, attrName string) string {
    66  	// DEPRECATED
    67  	log.DeprecationWarningReplace("GetAttributeOnPublishedAPI", "GetAttributeOnPublishedAPIByID")
    68  	return GetAttributeOnPublishedAPIByID(externalAPIID, attrName)
    69  }
    70  
    71  func getAttributeFromResource(resource *apiV1.ResourceInstance, attrName string) string {
    72  	if resource == nil {
    73  		return ""
    74  	}
    75  	v, _ := util.GetAgentDetailsValue(resource, attrName)
    76  	return v
    77  }
    78  
    79  // GetAttributeOnPublishedAPIByID - Returns the value on published proxy
    80  func GetAttributeOnPublishedAPIByID(externalAPIID string, attrName string) string {
    81  	api := getAPIByID(externalAPIID)
    82  	return getAttributeFromResource(api, attrName)
    83  }
    84  
    85  // GetAttributeOnPublishedAPIByPrimaryKey - Returns the value on published proxy
    86  func GetAttributeOnPublishedAPIByPrimaryKey(primaryKey string, attrName string) string {
    87  	api := getAPIByPrimaryKey(primaryKey)
    88  	return getAttributeFromResource(api, attrName)
    89  }
    90  
    91  // GetOwnerOnPublishedAPIByName - Returns the owner spec of the published proxy
    92  func GetOwnerOnPublishedAPIByName(apiName string) *apiV1.Owner {
    93  	api := getAPIByName(apiName)
    94  	if api == nil {
    95  		return nil
    96  	}
    97  	return api.Owner
    98  }
    99  
   100  // GetOwnerOnPublishedAPIByID - Returns the owner spec of the published proxy
   101  func GetOwnerOnPublishedAPIByID(externalAPIID string) *apiV1.Owner {
   102  	api := getAPIByID(externalAPIID)
   103  	if api == nil {
   104  		return nil
   105  	}
   106  	return api.Owner
   107  }
   108  
   109  // GetOwnerOnPublishedAPIByPrimaryKey - Returns the owner spec of the published proxy
   110  func GetOwnerOnPublishedAPIByPrimaryKey(primaryKey string) *apiV1.Owner {
   111  	api := getAPIByPrimaryKey(primaryKey)
   112  	if api == nil {
   113  		return nil
   114  	}
   115  	return api.Owner
   116  }
   117  
   118  func PublishingLock() {
   119  	agent.publishingLock.Lock()
   120  }
   121  
   122  func PublishingUnlock() {
   123  	agent.publishingLock.Unlock()
   124  }
   125  
   126  // PublishAPI - Publishes the API
   127  func PublishAPI(serviceBody apic.ServiceBody) error {
   128  	if agent.apicClient != nil {
   129  
   130  		var err error
   131  		_, err = publishAccessRequestDefinition(&serviceBody)
   132  		if err != nil {
   133  			return err
   134  		}
   135  	}
   136  
   137  	_, err := agent.apicClient.PublishService(&serviceBody)
   138  	if err != nil {
   139  		return err
   140  	}
   141  	log.Infof("Published API %v-%v in environment %v", serviceBody.APIName, serviceBody.Version, agent.cfg.GetEnvironmentName())
   142  
   143  	return nil
   144  }
   145  
   146  // RemovePublishedAPIAgentDetail -
   147  func RemovePublishedAPIAgentDetail(externalAPIID, detailKey string) error {
   148  	apiSvc := agent.cacheManager.GetAPIServiceWithAPIID(externalAPIID)
   149  
   150  	details := util.GetAgentDetails(apiSvc)
   151  	if _, ok := details[detailKey]; !ok {
   152  		return nil
   153  	}
   154  
   155  	delete(details, detailKey)
   156  
   157  	util.SetAgentDetails(apiSvc, details)
   158  
   159  	err := agent.apicClient.CreateSubResource(apiSvc.ResourceMeta, map[string]interface{}{definitions.XAgentDetails: details})
   160  	if err != nil {
   161  		return err
   162  	}
   163  
   164  	err = agent.cacheManager.AddAPIService(apiSvc)
   165  	return err
   166  
   167  }
   168  
   169  func publishAccessRequestDefinition(serviceBody *apic.ServiceBody) (*apiV1.ResourceInstance, error) {
   170  	agent.ardLock.Lock()
   171  	defer agent.ardLock.Unlock()
   172  
   173  	if serviceBody.GetAccessRequestDefinition() != nil {
   174  		newARD, err := createOrUpdateAccessRequestDefinition(serviceBody.GetAccessRequestDefinition())
   175  		if err == nil && newARD != nil {
   176  			serviceBody.SetAccessRequestDefinitionName(newARD.Name, true)
   177  
   178  			ard, err := newARD.AsInstance()
   179  			if err == nil {
   180  				agent.cacheManager.AddAccessRequestDefinition(ard)
   181  			}
   182  			return ard, err
   183  		}
   184  		return nil, err
   185  	}
   186  	return nil, nil
   187  }
   188  
   189  func getAPIValidator() APIValidator {
   190  	agent.apiValidatorLock.Lock()
   191  	defer agent.apiValidatorLock.Unlock()
   192  
   193  	return agent.apiValidator
   194  }
   195  
   196  func setAPIValidator(apiValidator APIValidator) {
   197  	agent.apiValidatorLock.Lock()
   198  	defer agent.apiValidatorLock.Unlock()
   199  
   200  	agent.apiValidator = apiValidator
   201  }
   202  
   203  // RegisterAPIValidator - Registers callback for validating the API on gateway
   204  func RegisterAPIValidator(apiValidator APIValidator) {
   205  	setAPIValidator(apiValidator)
   206  }
   207  
   208  // RegisterDeleteServiceValidator - DEPRECATED Registers callback for validating if the service should be deleted
   209  func RegisterDeleteServiceValidator(validator interface{}) {
   210  	log.Warnf("the RegisterDeleteServiceValidator is no longer used, please remove the call to it")
   211  }