Skip to content

Commit

Permalink
Add three structs for load balance, failover and normal scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
wasuradananjith committed May 28, 2020
1 parent 5bfc8db commit bbed183
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
47 changes: 29 additions & 18 deletions import-export-cli/cmd/importAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,32 @@ func mergeAPI(apiDirectory string, environmentParams *params.Environment) error
return err
}

if environmentParams.Endpoints != nil && environmentParams.EndpointsList != nil {
return errors.New("Both endpoints and endpointsList fields are specified in the api_params.yaml file for " +
environmentParams.Name + ". Please remove one field and continue...")
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 or failover endpoints, environmentParams.EndpointsList will not be null
if environmentParams.EndpointsList != nil {
// Check whether the endpoint type is failover
if environmentParams.EndpointsList.EndpointType == "failover" {
environmentParams.EndpointsList.Failover = true
} else {
// If the endpoint type is load_balance, make Failover false and
// make ProductionFailovers and SandboxFailovers nil if the user has mistakenly specify those
environmentParams.EndpointsList.Failover = false
environmentParams.EndpointsList.ProductionFailovers = nil
environmentParams.EndpointsList.SandboxFailovers = nil
// The default class of the algorithm to be used should be set to RoundRobin
environmentParams.EndpointsList.AlgorithmClassName = "org.apache.synapse.endpoints.algorithms.RoundRobin"
}
configData, err = json.Marshal(environmentParams.EndpointsList)
// 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
}
Expand Down Expand Up @@ -216,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
38 changes: 24 additions & 14 deletions import-export-cli/specs/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,13 @@ type EndpointData struct {
Sandbox *Endpoint `yaml:"sandbox" json:"sandbox_endpoints,omitempty"`
}

// EndpointsListData contains details about endpoints mainly to be used in load balancing (or failover)
type EndpointsListData struct {
// Endpoint type (can be "load_balance" or "failover")
EndpointType string `yaml:"endpointType" json:"endpoint_type,omitempty"`
// Production endpoints list for load balancing and failover endpoint types
// 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"`
// Production failover endpoints list for failover endpoint types
ProductionFailovers []Endpoint `yaml:"productionFailovers" json:"production_failovers,omitempty"`
// Sandbox endpoints list for load balancing and failover endpoint types
// Sandbox endpoints list for load balancing
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"`
// 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
Expand All @@ -58,6 +51,21 @@ type EndpointsListData struct {
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 @@ -76,8 +84,10 @@ type Environment struct {
Name string `yaml:"name"`
// Endpoints contain details about endpoints in a configuration
Endpoints *EndpointData `yaml:"endpoints"`
// EndpointsList contain details about endpoints in a configuration for load balancing or failover scenarios
EndpointsList *EndpointsListData `yaml:"endpointsList"`
// 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

0 comments on commit bbed183

Please sign in to comment.