diff --git a/pkg/apis/compute/loadbalancer_const.go b/pkg/apis/compute/loadbalancer_const.go index 8d1e9e3ed1..4afd01b02c 100644 --- a/pkg/apis/compute/loadbalancer_const.go +++ b/pkg/apis/compute/loadbalancer_const.go @@ -160,6 +160,11 @@ var LB_LISTENER_TYPES = []string{ LB_LISTENER_TYPE_HTTPS, } +var LB_APP_LISTENER_TYPES = []string{ + LB_LISTENER_TYPE_HTTP, + LB_LISTENER_TYPE_HTTPS, +} + // aws_network_lb_listener var AWS_NETWORK_LB_LISTENER_TYPES = choices.NewChoices( LB_LISTENER_TYPE_TCP, diff --git a/pkg/apis/compute/loadbalancerlistener.go b/pkg/apis/compute/loadbalancerlistener.go index 4ced042ae8..1808d9be1e 100644 --- a/pkg/apis/compute/loadbalancerlistener.go +++ b/pkg/apis/compute/loadbalancerlistener.go @@ -181,6 +181,10 @@ type LoadbalancerListenerCreateInput struct { AclType string `json:"acl_type"` } +func (input LoadbalancerListenerCreateInput) IsRedirect() bool { + return len(input.Redirect) > 0 && input.Redirect != LB_REDIRECT_OFF +} + func (self *LoadbalancerListenerCreateInput) Validate() error { if len(self.Status) == 0 { self.Status = LB_STATUS_ENABLED @@ -191,7 +195,13 @@ func (self *LoadbalancerListenerCreateInput) Validate() error { if !utils.IsInStringArray(self.SendProxy, LB_SENDPROXY_CHOICES) { return httperrors.NewInputParameterError("invalid send_proxy %s", self.SendProxy) } - if !utils.IsInStringArray(self.Scheduler, LB_SCHEDULER_TYPES) { + if len(self.Redirect) == 0 { + self.Redirect = LB_REDIRECT_OFF + } + if !utils.IsInStringArray(self.Redirect, []string{LB_REDIRECT_OFF, LB_REDIRECT_RAW}) { + return httperrors.NewInputParameterError("invalid redirect %s", self.Redirect) + } + if !self.IsRedirect() && !utils.IsInStringArray(self.Scheduler, LB_SCHEDULER_TYPES) { return httperrors.NewInputParameterError("invalid scheduler %s", self.Scheduler) } if len(self.StickySession) == 0 { @@ -223,6 +233,9 @@ func (self *LoadbalancerListenerCreateInput) Validate() error { if !utils.IsInStringArray(self.ListenerType, LB_LISTENER_TYPES) { return httperrors.NewInputParameterError("invalid listener_type %s", self.ListenerType) } + if self.IsRedirect() && !utils.IsInStringArray(self.ListenerType, LB_APP_LISTENER_TYPES) { + return httperrors.NewInputParameterError("redirect is only supported for http/https listeners") + } if self.ListenerPort < 1 || self.ListenerPort > 65535 { return httperrors.NewOutOfRangeError("listener_port out of range 1-65535") } @@ -272,12 +285,7 @@ func (self *LoadbalancerListenerCreateInput) Validate() error { self.HealthCheckInterval = 30 } } - if len(self.Redirect) == 0 { - self.Redirect = LB_REDIRECT_OFF - } - if !utils.IsInStringArray(self.Redirect, []string{LB_REDIRECT_OFF, LB_REDIRECT_RAW}) { - return httperrors.NewInputParameterError("invalid redirect %s", self.Redirect) - } + return nil } diff --git a/pkg/compute/models/loadbalancerlisteners.go b/pkg/compute/models/loadbalancerlisteners.go index 3c64110d05..7459aabba6 100644 --- a/pkg/compute/models/loadbalancerlisteners.go +++ b/pkg/compute/models/loadbalancerlisteners.go @@ -359,14 +359,6 @@ func (man *SLoadbalancerListenerManager) ValidateCreateData(ctx context.Context, return nil, err } lb := lbObj.(*SLoadbalancer) - lbbgObj, err := validators.ValidateModel(ctx, userCred, LoadbalancerBackendGroupManager, &input.BackendGroupId) - if err != nil { - return nil, err - } - lbbg := lbbgObj.(*SLoadbalancerBackendGroup) - if lbbg.LoadbalancerId != lb.Id { - return nil, httperrors.NewConflictError("backendgroup_id not same with listener's loadbalancer") - } region, err := lb.GetRegion() if err != nil { return nil, errors.Wrapf(err, "GetRegion") @@ -378,6 +370,19 @@ func (man *SLoadbalancerListenerManager) ValidateCreateData(ctx context.Context, if err != nil { return nil, err } + var lbbg *SLoadbalancerBackendGroup + if input.IsRedirect() { + input.BackendGroupId = "" + } else { + lbbgObj, err := validators.ValidateModel(ctx, userCred, LoadbalancerBackendGroupManager, &input.BackendGroupId) + if err != nil { + return nil, err + } + lbbg = lbbgObj.(*SLoadbalancerBackendGroup) + if lbbg.LoadbalancerId != lb.Id { + return nil, httperrors.NewConflictError("backendgroup_id not same with listener's loadbalancer") + } + } if utils.IsInStringArray(input.ListenerType, []string{api.LB_LISTENER_TYPE_TCP, api.LB_LISTENER_TYPE_UDP}) { }