Skip to content

Commit bbed183

Browse files
Add three structs for load balance, failover and normal scenarios
1 parent 5bfc8db commit bbed183

File tree

2 files changed

+53
-32
lines changed

2 files changed

+53
-32
lines changed

import-export-cli/cmd/importAPI.go

+29-18
Original file line numberDiff line numberDiff line change
@@ -158,31 +158,32 @@ func mergeAPI(apiDirectory string, environmentParams *params.Environment) error
158158
return err
159159
}
160160

161-
if environmentParams.Endpoints != nil && environmentParams.EndpointsList != nil {
162-
return errors.New("Both endpoints and endpointsList fields are specified in the api_params.yaml file for " +
163-
environmentParams.Name + ". Please remove one field and continue...")
161+
if !isEndpointsFieldsValid(environmentParams.Endpoints, environmentParams.LoadBalanceEndpoints, environmentParams.FailoverEndpoints) {
162+
return errors.New("Please specify only one field from endpoints, loadBalanceEndpoints or failOverEndpoints in the api_params.yaml file for " +
163+
environmentParams.Name + " and continue...")
164164
}
165165

166166
configData, err := json.Marshal(environmentParams.Endpoints)
167167
if err != nil {
168168
return err
169169
}
170170

171-
// If the user wants to have load balancing or failover endpoints, environmentParams.EndpointsList will not be null
172-
if environmentParams.EndpointsList != nil {
173-
// Check whether the endpoint type is failover
174-
if environmentParams.EndpointsList.EndpointType == "failover" {
175-
environmentParams.EndpointsList.Failover = true
176-
} else {
177-
// If the endpoint type is load_balance, make Failover false and
178-
// make ProductionFailovers and SandboxFailovers nil if the user has mistakenly specify those
179-
environmentParams.EndpointsList.Failover = false
180-
environmentParams.EndpointsList.ProductionFailovers = nil
181-
environmentParams.EndpointsList.SandboxFailovers = nil
182-
// The default class of the algorithm to be used should be set to RoundRobin
183-
environmentParams.EndpointsList.AlgorithmClassName = "org.apache.synapse.endpoints.algorithms.RoundRobin"
184-
}
185-
configData, err = json.Marshal(environmentParams.EndpointsList)
171+
// If the user wants to have load balancing, environmentParams.LoadBalanceEndpoints will not be null
172+
if environmentParams.LoadBalanceEndpoints != nil {
173+
environmentParams.LoadBalanceEndpoints.EndpointType = "load_balance"
174+
// The default class of the algorithm to be used should be set to RoundRobin
175+
environmentParams.LoadBalanceEndpoints.AlgorithmClassName = "org.apache.synapse.endpoints.algorithms.RoundRobin"
176+
configData, err = json.Marshal(environmentParams.LoadBalanceEndpoints)
177+
if err != nil {
178+
return err
179+
}
180+
}
181+
182+
// If the user wants to have failover, environmentParams.FailoverEndpoints will not be null
183+
if environmentParams.FailoverEndpoints != nil {
184+
environmentParams.FailoverEndpoints.EndpointType = "failover"
185+
environmentParams.FailoverEndpoints.Failover = true
186+
configData, err = json.Marshal(environmentParams.FailoverEndpoints)
186187
if err != nil {
187188
return err
188189
}
@@ -216,6 +217,16 @@ func mergeAPI(apiDirectory string, environmentParams *params.Environment) error
216217
return nil
217218
}
218219

220+
// isEndpointsFieldsValid returns false if either of the two fields: endpoints, loadBalanceEndpoints and failOverEndpoints are defined
221+
// in api_params.yaml file by the user mistakenly. This will return true , if only one of them is defined.
222+
func isEndpointsFieldsValid(endpoints *params.EndpointData, loadBalanceEndpoints *params.LoadBalanceEndpointsData, failoverEndpoints *params.FailoverEndpointsData) bool {
223+
if endpoints != nil {
224+
return loadBalanceEndpoints == nil && failoverEndpoints == nil
225+
} else {
226+
return (loadBalanceEndpoints != nil && failoverEndpoints == nil) || (loadBalanceEndpoints == nil && failoverEndpoints != nil)
227+
}
228+
}
229+
219230
// resolveImportFilePath resolves the archive/directory for import
220231
// First will resolve in given path, if not found will try to load from exported directory
221232
func resolveImportFilePath(file, defaultExportDirectory string) (string, error) {

import-export-cli/specs/params/params.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,13 @@ type EndpointData struct {
3636
Sandbox *Endpoint `yaml:"sandbox" json:"sandbox_endpoints,omitempty"`
3737
}
3838

39-
// EndpointsListData contains details about endpoints mainly to be used in load balancing (or failover)
40-
type EndpointsListData struct {
41-
// Endpoint type (can be "load_balance" or "failover")
42-
EndpointType string `yaml:"endpointType" json:"endpoint_type,omitempty"`
43-
// Production endpoints list for load balancing and failover endpoint types
39+
// LoadBalanceEndpointsData contains details about endpoints mainly to be used in load balancing
40+
type LoadBalanceEndpointsData struct {
41+
EndpointType string `yaml:"endpoint_type" json:"endpoint_type"`
42+
// Production endpoints list for load balancing
4443
Production []Endpoint `yaml:"production" json:"production_endpoints,omitempty"`
45-
// Production failover endpoints list for failover endpoint types
46-
ProductionFailovers []Endpoint `yaml:"productionFailovers" json:"production_failovers,omitempty"`
47-
// Sandbox endpoints list for load balancing and failover endpoint types
44+
// Sandbox endpoints list for load balancing
4845
Sandbox []Endpoint `yaml:"sandbox" json:"sandbox_endpoints,omitempty"`
49-
// Production failover endpoints list for failover endpoint types
50-
SandboxFailovers []Endpoint `yaml:"sandboxFailovers" json:"sandbox_failovers,omitempty"`
51-
// To enable failover endpoints
52-
Failover bool `yaml:"failOver" json:"failOver,omitempty"`
5346
// Session management method from the load balancing group. Values can be "none", "transport" (by default), "soap", "simpleClientSession" (Client ID)
5447
SessionManagement string `yaml:"sessionManagement" json:"sessionManagement,omitempty"`
5548
// Session timeout means the number of milliseconds after which the session would time out
@@ -58,6 +51,21 @@ type EndpointsListData struct {
5851
AlgorithmClassName string `yaml:"algoClassName" json:"algoClassName,omitempty"`
5952
}
6053

54+
// FailoverEndpointsData contains details about endpoints mainly to be used in load balancing
55+
type FailoverEndpointsData struct {
56+
EndpointType string `yaml:"endpoint_type" json:"endpoint_type"`
57+
// Primary production endpoint for failover
58+
Production *Endpoint `yaml:"production" json:"production_endpoints,omitempty"`
59+
// Production failover endpoints list for failover
60+
ProductionFailovers []Endpoint `yaml:"productionFailovers" json:"production_failovers,omitempty"`
61+
// Primary sandbox endpoint for failover
62+
Sandbox *Endpoint `yaml:"sandbox" json:"sandbox_endpoints,omitempty"`
63+
// Production failover endpoints list for failover endpoint types
64+
SandboxFailovers []Endpoint `yaml:"sandboxFailovers" json:"sandbox_failovers,omitempty"`
65+
// To enable failover endpoints
66+
Failover bool `yaml:"failOver" json:"failOver,omitempty"`
67+
}
68+
6169
// Cert stores certificate details
6270
type Cert struct {
6371
// Host of the certificate
@@ -76,8 +84,10 @@ type Environment struct {
7684
Name string `yaml:"name"`
7785
// Endpoints contain details about endpoints in a configuration
7886
Endpoints *EndpointData `yaml:"endpoints"`
79-
// EndpointsList contain details about endpoints in a configuration for load balancing or failover scenarios
80-
EndpointsList *EndpointsListData `yaml:"endpointsList"`
87+
// LoadBalanceEndpoints contain details about endpoints in a configuration for load balancing scenarios
88+
LoadBalanceEndpoints *LoadBalanceEndpointsData `yaml:"loadBalanceEndpoints"`
89+
// FailoverEndpoints contain details about endpoints in a configuration for failover scenarios
90+
FailoverEndpoints *FailoverEndpointsData `yaml:"failoverEndpoints"`
8191
// GatewayEnvironments contains environments that used to deploy API
8292
GatewayEnvironments []string `yaml:"gatewayEnvironments"`
8393
// Certs for environment

0 commit comments

Comments
 (0)