@@ -718,48 +718,46 @@ public LoadBalancer createLoadBalancerRule(CreateLoadBalancerRuleCmd lb, boolean
718718 IPAddressVO ipVO = null ;
719719 if (ipAddrId != null ) {
720720 ipVO = _ipAddressDao .findById (ipAddrId );
721-
722- // Validate ip address
723- if (ipVO == null ) {
724- throw new InvalidParameterValueException ("Unable to create load balance rule; ip id=" + ipAddrId + "" +
725- " doesn't exist in the system" );
726- } else if (ipVO .isOneToOneNat ()) {
727- throw new NetworkRuleConflictException ("Can't do load balance on ip address: " + ipVO .getAddress ());
728- }
729721 }
722+
723+ Network network = _networkMgr .getNetwork (lb .getNetworkId ());
730724
731725 // FIXME: breaking the dependency on ELB manager. This breaks functionality of ELB using virtual router
732726 // Bug CS-15411 opened to document this
733727 //LoadBalancer result = _elbMgr.handleCreateLoadBalancerRule(lb, lbOwner, lb.getNetworkId());
734728 LoadBalancer result = null ;
735729 if (result == null ) {
736730 IpAddress systemIp = null ;
737- Network guestNetwork = _networkMgr .getNetwork (lb .getNetworkId ());
738- NetworkOffering off = _configMgr .getNetworkOffering (guestNetwork .getNetworkOfferingId ());
739- if (off .getElasticLb () && ipVO == null ) {
731+ NetworkOffering off = _configMgr .getNetworkOffering (network .getNetworkOfferingId ());
732+ if (off .getElasticLb () && ipVO == null && network .getVpcId () == null ) {
740733 systemIp = _networkMgr .assignSystemIp (lb .getNetworkId (), lbOwner , true , false );
741734 lb .setSourceIpAddressId (systemIp .getId ());
735+ ipVO = _ipAddressDao .findById (systemIp .getId ());
736+ }
737+
738+ // Validate ip address
739+ if (ipVO == null ) {
740+ throw new InvalidParameterValueException ("Unable to create load balance rule; can't find/allocate source IP" );
741+ } else if (ipVO .isOneToOneNat ()) {
742+ throw new NetworkRuleConflictException ("Can't do load balance on ip address: " + ipVO .getAddress ());
742743 }
743744
744745 boolean performedIpAssoc = false ;
745746 try {
746- Network network = _networkMgr .getNetwork (lb .getNetworkId ());
747- if (ipVO != null ) {
748- if (ipVO .getAssociatedWithNetworkId () == null ) {
749- boolean assignToVpcNtwk = network .getVpcId () != null
750- && ipVO .getVpcId () != null && ipVO .getVpcId ().longValue () == network .getVpcId ();
751- if (assignToVpcNtwk ) {
752- //set networkId just for verification purposes
753- ipVO .setAssociatedWithNetworkId (lb .getNetworkId ());
754- _networkMgr .checkIpForService (ipVO , Service .Lb , lb .getNetworkId ());
755-
756- s_logger .debug ("The ip is not associated with the VPC network id=" + lb .getNetworkId () + " so assigning" );
757- ipVO = _networkMgr .associateIPToGuestNetwork (ipAddrId , lb .getNetworkId ());
758- performedIpAssoc = true ;
759- }
760- } else {
761- _networkMgr .checkIpForService (ipVO , Service .Lb , null );
762- }
747+ if (ipVO .getAssociatedWithNetworkId () == null ) {
748+ boolean assignToVpcNtwk = network .getVpcId () != null
749+ && ipVO .getVpcId () != null && ipVO .getVpcId ().longValue () == network .getVpcId ();
750+ if (assignToVpcNtwk ) {
751+ //set networkId just for verification purposes
752+ ipVO .setAssociatedWithNetworkId (lb .getNetworkId ());
753+ _networkMgr .checkIpForService (ipVO , Service .Lb , lb .getNetworkId ());
754+
755+ s_logger .debug ("The ip is not associated with the VPC network id=" + lb .getNetworkId () + " so assigning" );
756+ ipVO = _networkMgr .associateIPToGuestNetwork (ipAddrId , lb .getNetworkId ());
757+ performedIpAssoc = true ;
758+ }
759+ } else {
760+ _networkMgr .checkIpForService (ipVO , Service .Lb , null );
763761 }
764762
765763 if (ipVO .getAssociatedWithNetworkId () == null ) {
0 commit comments