@@ -445,11 +445,49 @@ public IEnumerable<IPAddress> ListAddressesByNetwork(string serverId, string net
445445
446446 var urlPath = new Uri ( string . Format ( "{0}/servers/{1}/ips/{2}" , GetServiceEndpoint ( identity , region ) , serverId , network ) ) ;
447447
448- var response = ExecuteRESTRequest < ServerAddresses > ( identity , urlPath , HttpMethod . GET ) ;
449- if ( response == null || response . Data == null )
450- return null ;
448+ try
449+ {
450+ var response = ExecuteRESTRequest < ServerAddresses > ( identity , urlPath , HttpMethod . GET ) ;
451+ if ( response == null || response . Data == null )
452+ return null ;
451453
452- return response . Data [ network ] ;
454+ return response . Data [ network ] ;
455+ }
456+ catch ( ItemNotFoundException )
457+ {
458+ // if the specified server and network exist separately, then the 404 was only caused by server
459+ // not being connected to the particular network
460+ // https://github.com/openstacknetsdk/openstack.net/issues/176
461+ bool foundServer = false ;
462+ try
463+ {
464+ Server details = GetDetails ( serverId ) ;
465+ foundServer = details != null ;
466+ }
467+ catch ( ResponseException )
468+ {
469+ }
470+
471+ if ( ! foundServer )
472+ throw ;
473+
474+ bool foundNetwork = false ;
475+ try
476+ {
477+ INetworksProvider networksProvider = new CloudNetworksProvider ( DefaultIdentity , DefaultRegion , IdentityProvider , RestService ) ;
478+ IEnumerable < CloudNetwork > networks = networksProvider . ListNetworks ( region , identity ) ;
479+ if ( networks != null && networks . Any ( i => network . Equals ( i . Label , StringComparison . OrdinalIgnoreCase ) ) )
480+ foundNetwork = true ;
481+ }
482+ catch ( ResponseException )
483+ {
484+ }
485+
486+ if ( ! foundNetwork )
487+ throw ;
488+
489+ return Enumerable . Empty < IPAddress > ( ) ;
490+ }
453491 }
454492
455493 #endregion
0 commit comments