@@ -141,32 +141,41 @@ public Long getNetworkId() {
141141 Long zoneId = getZoneId ();
142142
143143 if (zoneId == null ) {
144- throw new InvalidParameterValueException ("Either networkId or zoneId has to be specified" );
144+ Long ipId = getSourceIpAddressId ();
145+ if (ipId == null ) {
146+ throw new InvalidParameterValueException ("Either networkId or zoneId or publicIpId has to be specified" );
147+ }
145148 }
146149
147- DataCenter zone = _configService .getZone (zoneId );
148- if (zone .getNetworkType () == NetworkType .Advanced ) {
149- List <? extends Network > networks = _networkService .getIsolatedNetworksOwnedByAccountInZone (getZoneId (), _accountService .getAccount (getEntityOwnerId ()));
150- if (networks .size () == 0 ) {
151- String domain = _domainService .getDomain (getDomainId ()).getName ();
152- throw new InvalidParameterValueException ("Account name=" + getAccountName () + " domain=" + domain + " doesn't have virtual networks in zone=" + zone .getName ());
153- }
154-
155- if (networks .size () < 1 ) {
156- throw new InvalidParameterValueException ("Account doesn't have any Isolated networks in the zone" );
157- } else if (networks .size () > 1 ) {
158- throw new InvalidParameterValueException ("Account has more than one Isolated network in the zone" );
159- }
160-
161- return networks .get (0 ).getId ();
162- } else {
163- Network defaultGuestNetwork = _networkService .getExclusiveGuestNetwork (zoneId );
164- if (defaultGuestNetwork == null ) {
165- throw new InvalidParameterValueException ("Unable to find a default Guest network for account " + getAccountName () + " in domain id=" + getDomainId ());
150+ if (zoneId != null ) {
151+ DataCenter zone = _configService .getZone (zoneId );
152+ if (zone .getNetworkType () == NetworkType .Advanced ) {
153+ List <? extends Network > networks = _networkService .getIsolatedNetworksOwnedByAccountInZone (getZoneId (), _accountService .getAccount (getEntityOwnerId ()));
154+ if (networks .size () == 0 ) {
155+ String domain = _domainService .getDomain (getDomainId ()).getName ();
156+ throw new InvalidParameterValueException ("Account name=" + getAccountName () + " domain=" + domain + " doesn't have virtual networks in zone=" + zone .getName ());
157+ }
158+
159+ if (networks .size () < 1 ) {
160+ throw new InvalidParameterValueException ("Account doesn't have any Isolated networks in the zone" );
161+ } else if (networks .size () > 1 ) {
162+ throw new InvalidParameterValueException ("Account has more than one Isolated network in the zone" );
163+ }
164+
165+ return networks .get (0 ).getId ();
166166 } else {
167- return defaultGuestNetwork .getId ();
167+ Network defaultGuestNetwork = _networkService .getExclusiveGuestNetwork (zoneId );
168+ if (defaultGuestNetwork == null ) {
169+ throw new InvalidParameterValueException ("Unable to find a default Guest network for account " + getAccountName () + " in domain id=" + getDomainId ());
170+ } else {
171+ return defaultGuestNetwork .getId ();
172+ }
168173 }
174+ } else {
175+ IpAddress ipAddr = _networkService .getIp (publicIpId );
176+ return ipAddr .getAssociatedWithNetworkId ();
169177 }
178+
170179 }
171180
172181 public Integer getPublicPort () {
0 commit comments