diff --git a/import-export-cli/cmd/importAPI.go b/import-export-cli/cmd/importAPI.go index 94d4196f5..4c05e1fdd 100644 --- a/import-export-cli/cmd/importAPI.go +++ b/import-export-cli/cmd/importAPI.go @@ -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 @@ -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) { @@ -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 } diff --git a/import-export-cli/specs/params/params.go b/import-export-cli/specs/params/params.go index 24dff2811..e7ebb0bbd 100644 --- a/import-export-cli/specs/params/params.go +++ b/import-export-cli/specs/params/params.go @@ -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 @@ -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 @@ -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 { @@ -126,4 +159,3 @@ func (config ApiParams) GetEnv(key string) *Environment { } return nil } -