Skip to content

Commit

Permalink
Merge pull request #321 from wasuradananjith/multiple-endpoints-fix-2…
Browse files Browse the repository at this point in the history
….x.x

[2.x] Handling load balance and fail over endpoints through api_params.yaml file from APIMCLI 2.x.x
  • Loading branch information
malinthaprasan authored May 29, 2020
2 parents 7e08218 + bbed183 commit c9fa663
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
38 changes: 37 additions & 1 deletion import-export-cli/cmd/importAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,37 @@ func mergeAPI(apiDirectory string, environmentParams *params.Environment) error
return err
}

if !isEndpointsFieldsValid(environmentParams.Endpoints, environmentParams.LoadBalanceEndpoints, environmentParams.FailoverEndpoints) {
return errors.New("Please specify only one field from endpoints, loadBalanceEndpoints or failOverEndpoints in the api_params.yaml file for " +
environmentParams.Name + " and continue...")
}

configData, err := json.Marshal(environmentParams.Endpoints)
if err != nil {
return err
}

// If the user wants to have load balancing, environmentParams.LoadBalanceEndpoints will not be null
if environmentParams.LoadBalanceEndpoints != nil {
environmentParams.LoadBalanceEndpoints.EndpointType = "load_balance"
// The default class of the algorithm to be used should be set to RoundRobin
environmentParams.LoadBalanceEndpoints.AlgorithmClassName = "org.apache.synapse.endpoints.algorithms.RoundRobin"
configData, err = json.Marshal(environmentParams.LoadBalanceEndpoints)
if err != nil {
return err
}
}

// If the user wants to have failover, environmentParams.FailoverEndpoints will not be null
if environmentParams.FailoverEndpoints != nil {
environmentParams.FailoverEndpoints.EndpointType = "failover"
environmentParams.FailoverEndpoints.Failover = true
configData, err = json.Marshal(environmentParams.FailoverEndpoints)
if err != nil {
return err
}
}

mergedAPIEndpoints, err := utils.MergeJSON([]byte(apiEndpointData), configData)
if err != nil {
return err
Expand Down Expand Up @@ -191,6 +217,16 @@ func mergeAPI(apiDirectory string, environmentParams *params.Environment) error
return nil
}

// isEndpointsFieldsValid returns false if either of the two fields: endpoints, loadBalanceEndpoints and failOverEndpoints are defined
// in api_params.yaml file by the user mistakenly. This will return true , if only one of them is defined.
func isEndpointsFieldsValid(endpoints *params.EndpointData, loadBalanceEndpoints *params.LoadBalanceEndpointsData, failoverEndpoints *params.FailoverEndpointsData) bool {
if endpoints != nil {
return loadBalanceEndpoints == nil && failoverEndpoints == nil
} else {
return (loadBalanceEndpoints != nil && failoverEndpoints == nil) || (loadBalanceEndpoints == nil && failoverEndpoints != nil)
}
}

// resolveImportFilePath resolves the archive/directory for import
// First will resolve in given path, if not found will try to load from exported directory
func resolveImportFilePath(file, defaultExportDirectory string) (string, error) {
Expand Down Expand Up @@ -803,7 +839,7 @@ func ImportAPI(credential credentials.Credential, importPath, apiImportExportEnd
}

// check for API existence
id, err := getApiID(apiInfo.ID.APIName, apiInfo.ID.Version, providerName , importEnvironment, accessOAuthToken)
id, err := getApiID(apiInfo.ID.APIName, apiInfo.ID.Version, providerName, importEnvironment, accessOAuthToken)
if err != nil {
return err
}
Expand Down
36 changes: 34 additions & 2 deletions import-export-cli/specs/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ type EndpointData struct {
Sandbox *Endpoint `yaml:"sandbox" json:"sandbox_endpoints,omitempty"`
}

// LoadBalanceEndpointsData contains details about endpoints mainly to be used in load balancing
type LoadBalanceEndpointsData struct {
EndpointType string `yaml:"endpoint_type" json:"endpoint_type"`
// Production endpoints list for load balancing
Production []Endpoint `yaml:"production" json:"production_endpoints,omitempty"`
// Sandbox endpoints list for load balancing
Sandbox []Endpoint `yaml:"sandbox" json:"sandbox_endpoints,omitempty"`
// Session management method from the load balancing group. Values can be "none", "transport" (by default), "soap", "simpleClientSession" (Client ID)
SessionManagement string `yaml:"sessionManagement" json:"sessionManagement,omitempty"`
// Session timeout means the number of milliseconds after which the session would time out
SessionTimeout int `yaml:"sessionTimeOut" json:"sessionTimeOut,omitempty"`
// Class name for algorithm to be used if load balancing should be done
AlgorithmClassName string `yaml:"algoClassName" json:"algoClassName,omitempty"`
}

// FailoverEndpointsData contains details about endpoints mainly to be used in load balancing
type FailoverEndpointsData struct {
EndpointType string `yaml:"endpoint_type" json:"endpoint_type"`
// Primary production endpoint for failover
Production *Endpoint `yaml:"production" json:"production_endpoints,omitempty"`
// Production failover endpoints list for failover
ProductionFailovers []Endpoint `yaml:"productionFailovers" json:"production_failovers,omitempty"`
// Primary sandbox endpoint for failover
Sandbox *Endpoint `yaml:"sandbox" json:"sandbox_endpoints,omitempty"`
// Production failover endpoints list for failover endpoint types
SandboxFailovers []Endpoint `yaml:"sandboxFailovers" json:"sandbox_failovers,omitempty"`
// To enable failover endpoints
Failover bool `yaml:"failOver" json:"failOver,omitempty"`
}

// Cert stores certificate details
type Cert struct {
// Host of the certificate
Expand All @@ -54,6 +84,10 @@ type Environment struct {
Name string `yaml:"name"`
// Endpoints contain details about endpoints in a configuration
Endpoints *EndpointData `yaml:"endpoints"`
// LoadBalanceEndpoints contain details about endpoints in a configuration for load balancing scenarios
LoadBalanceEndpoints *LoadBalanceEndpointsData `yaml:"loadBalanceEndpoints"`
// FailoverEndpoints contain details about endpoints in a configuration for failover scenarios
FailoverEndpoints *FailoverEndpointsData `yaml:"failoverEndpoints"`
// GatewayEnvironments contains environments that used to deploy API
GatewayEnvironments []string `yaml:"gatewayEnvironments"`
// Certs for environment
Expand Down Expand Up @@ -116,7 +150,6 @@ func ExtractAPIEndpointConfig(b []byte) (string, error) {
return apiConfig.EPConfig, err
}


// GetEnv returns the EndpointData associated for key in the ApiParams, if not found returns nil
func (config ApiParams) GetEnv(key string) *Environment {
for index, env := range config.Environments {
Expand All @@ -126,4 +159,3 @@ func (config ApiParams) GetEnv(key string) *Environment {
}
return nil
}

0 comments on commit c9fa663

Please sign in to comment.