Fix NPE when GCE region is empty

When GCE region is empty we get back from the API something like:

```
{
  "id": "dummy"
}
```

instead of:

```
{
  "id": "dummy",
  "items":[ ]
}
```

This generates a NPE when we aggregate all the lists into a single one.

Closes #16967.
This commit is contained in:
David Pilato 2016-06-30 11:12:20 +02:00
parent c77dc4a82c
commit 66e3b15d21
4 changed files with 53 additions and 1 deletions

View File

@ -97,7 +97,7 @@ public class GceComputeServiceImpl extends AbstractLifecycleComponent<GceCompute
}
});
// assist type inference
return instanceList.isEmpty() ? Collections.<Instance>emptyList() : instanceList.getItems();
return instanceList.isEmpty() || instanceList.getItems() == null ? Collections.<Instance>emptyList() : instanceList.getItems();
} catch (PrivilegedActionException e) {
logger.warn("Problem fetching instance list for zone {}", e, zoneId);
logger.debug("Full exception:", e);

View File

@ -254,4 +254,17 @@ public class GceDiscoveryTests extends ESTestCase {
assertThat(expected.getMessage(), containsString("one or more gce discovery settings are missing."));
}
}
/**
* For issue https://github.com/elastic/elasticsearch/issues/16967
*/
public void testEmptyRegion16967() {
Settings nodeSettings = Settings.builder()
.put(GceComputeService.PROJECT_SETTING.getKey(), projectName)
.putArray(GceComputeService.ZONE_SETTING.getKey(), "europe-west1-b", "us-central1-a")
.build();
mock = new GceComputeServiceMock(nodeSettings, networkService);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(1));
}
}

View File

@ -0,0 +1,36 @@
{
"id": "dummy",
"items":[
{
"description": "ES Node 1",
"id": "9309873766428965105",
"kind": "compute#instance",
"machineType": "n1-standard-1",
"name": "test1",
"networkInterfaces": [
{
"accessConfigs": [
{
"kind": "compute#accessConfig",
"name": "External NAT",
"natIP": "104.155.13.147",
"type": "ONE_TO_ONE_NAT"
}
],
"name": "nic0",
"network": "default",
"networkIP": "10.240.79.59"
}
],
"status": "RUNNING",
"tags": {
"fingerprint": "xA6QJb-rGtg=",
"items": [
"elasticsearch",
"dev"
]
},
"zone": "europe-west1-b"
}
]
}