Skip to content

Commit 91d3848

Browse files
committed
Return an empty collection of IP addresses if a server is not connected to a specific network (fixes #176)
1 parent 01c4163 commit 91d3848

2 files changed

Lines changed: 50 additions & 14 deletions

File tree

src/corelib/Providers/Rackspace/CloudServersProvider.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/testing/integration/Providers/Rackspace/UserServerTests.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,17 @@ public void TestListAddressesByNetwork()
278278
foreach (CloudNetwork network in networks)
279279
{
280280
Console.WriteLine("Network: {0}", network.Label);
281-
try
281+
IEnumerable<IPAddress> addresses = provider.ListAddressesByNetwork(_server.Id, network.Label);
282+
bool foundAddressOnNetwork = false;
283+
foreach (IPAddress address in addresses)
282284
{
283-
IEnumerable<IPAddress> addresses = provider.ListAddressesByNetwork(_server.Id, network.Label);
284-
foreach (IPAddress address in addresses)
285-
{
286-
foundAddress = true;
287-
Console.WriteLine(" {0}", address);
288-
}
285+
foundAddress = true;
286+
foundAddressOnNetwork = true;
287+
Console.WriteLine(" {0}", address);
289288
}
290-
catch (ItemNotFoundException)
291-
{
289+
290+
if (!foundAddressOnNetwork)
292291
Console.WriteLine(" Server is not attached to this network.");
293-
}
294292
}
295293

296294
if (!foundAddress)

0 commit comments

Comments
 (0)