use of com.google.api.services.compute.model.Instance in project cloudbreak by hortonworks.
the class GcpInstanceConnector method check.
@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
List<CloudVmInstanceStatus> statuses = new ArrayList<>();
CloudCredential credential = ac.getCloudCredential();
CloudContext cloudContext = ac.getCloudContext();
Compute compute = GcpStackUtil.buildCompute(credential);
for (CloudInstance instance : vms) {
InstanceStatus status = InstanceStatus.UNKNOWN;
try {
Instance executeInstance = getInstance(cloudContext, credential, compute, instance.getInstanceId());
if ("RUNNING".equals(executeInstance.getStatus())) {
status = InstanceStatus.STARTED;
} else if ("TERMINATED".equals(executeInstance.getStatus())) {
status = InstanceStatus.STOPPED;
}
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
status = InstanceStatus.TERMINATED;
} else {
LOGGER.warn(String.format("Instance %s is not reachable", instance), e);
}
} catch (IOException e) {
LOGGER.warn(String.format("Instance %s is not reachable", instance), e);
}
statuses.add(new CloudVmInstanceStatus(instance, status));
}
return statuses;
}
use of com.google.api.services.compute.model.Instance in project druid by druid-io.
the class GceAutoScaler method idToIpLookup.
/**
* Converts the IDs to IPs - this is actually never called from the outside but it is called once
* from inside the class if terminate is used instead of terminateWithIds
*/
@Override
public List<String> idToIpLookup(List<String> nodeIds) {
log.info("Asked IDs -> IPs for: [%s]", String.join(",", nodeIds));
if (nodeIds.isEmpty()) {
return new ArrayList<>();
}
final String project = envConfig.getProjectId();
final String zone = envConfig.getZoneName();
try {
Compute computeService = createComputeService();
Compute.Instances.List request = computeService.instances().list(project, zone);
request.setFilter(GceUtils.buildFilter(nodeIds, "name"));
List<String> instanceIps = new ArrayList<>();
InstanceList response;
do {
response = request.execute();
if (response.getItems() == null) {
continue;
}
for (Instance instance : response.getItems()) {
// Assuming that every server has at least one network interface...
String ip = instance.getNetworkInterfaces().get(0).getNetworkIP();
// it for maxScalingDuration time before doing anything else
if (ip != null && !"null".equals(ip)) {
instanceIps.add(ip);
} else {
// log and skip it
log.warn("Call returned null IP for %s, skipping", instance.getName());
}
}
request.setPageToken(response.getNextPageToken());
} while (response.getNextPageToken() != null);
return instanceIps;
} catch (Exception e) {
log.error(e, "Unable to convert IDs to IPs.");
}
return new ArrayList<>();
}
use of com.google.api.services.compute.model.Instance in project druid by druid-io.
the class GceAutoScaler method ipToIdLookup.
/**
* Converts the IPs to IDs
*/
@Override
public List<String> ipToIdLookup(List<String> ips) {
log.info("Asked IPs -> IDs for: [%s]", String.join(",", ips));
if (ips.isEmpty()) {
return new ArrayList<>();
}
// actually IPs and can send IDs to this function instead
if (!InetAddresses.isInetAddress(ips.get(0))) {
log.debug("Not IPs, doing nothing");
return ips;
}
final String project = envConfig.getProjectId();
final String zone = envConfig.getZoneName();
try {
Compute computeService = createComputeService();
Compute.Instances.List request = computeService.instances().list(project, zone);
// Cannot filter by IP atm, see below
// request.setFilter(GceUtils.buildFilter(ips, "networkInterfaces[0].networkIP"));
List<String> instanceIds = new ArrayList<>();
InstanceList response;
do {
response = request.execute();
if (response.getItems() == null) {
continue;
}
for (Instance instance : response.getItems()) {
// by IP, see https://issuetracker.google.com/issues/73455339
for (NetworkInterface ni : instance.getNetworkInterfaces()) {
if (ips.contains(ni.getNetworkIP())) {
instanceIds.add(instance.getName());
}
}
}
request.setPageToken(response.getNextPageToken());
} while (response.getNextPageToken() != null);
log.debug("Converted to [%s]", String.join(",", instanceIds));
return instanceIds;
} catch (Exception e) {
log.error(e, "Unable to convert IPs to IDs.");
}
return new ArrayList<>();
}
use of com.google.api.services.compute.model.Instance in project druid by druid-io.
the class GceAutoScalerTest method makeInstance.
private Instance makeInstance(String name, String ip) {
Instance instance = new Instance();
instance.setName(name);
NetworkInterface net = new NetworkInterface();
net.setNetworkIP(ip);
instance.setNetworkInterfaces(Collections.singletonList(net));
return instance;
}
use of com.google.api.services.compute.model.Instance in project druid by druid-io.
the class GceAutoScalerTest method testIdToIp.
@Test
public void testIdToIp() throws IOException, GeneralSecurityException, GceServiceException {
GceAutoScaler autoScaler = EasyMock.createMockBuilder(GceAutoScaler.class).withConstructor(int.class, int.class, GceEnvironmentConfig.class).withArgs(2, 4, new GceEnvironmentConfig(1, "proj-x", "us-central-1", "druid-mig")).addMockedMethod("createComputeServiceImpl").createMock();
EasyMock.expect(autoScaler.createComputeServiceImpl()).andReturn(null);
EasyMock.expect(autoScaler.createComputeServiceImpl()).andReturn(mockCompute);
EasyMock.replay(autoScaler);
// empty IPs
List<String> ids1 = Collections.emptyList();
List<String> ips1 = autoScaler.idToIpLookup(ids1);
Assert.assertEquals(0, ips1.size());
// actually IDs
// invalid ip, not returned
Instance i1 = makeInstance("foo", "null");
// valid ip, returned
Instance i2 = makeInstance("bar", "1.2.3.4");
InstanceList mockResponse = new InstanceList();
mockResponse.setNextPageToken(null);
mockResponse.setItems(Arrays.asList(i1, i2));
EasyMock.expect(mockIdToIpRequest.setFilter("(name = \"foo\") OR (name = \"bar\")")).andReturn(// the method needs to return something but it is actually irrelevant
mockIdToIpRequest);
EasyMock.expect(mockIdToIpRequest.execute()).andReturn(mockResponse);
EasyMock.expect(mockIdToIpRequest.setPageToken(EasyMock.anyString())).andReturn(// the method needs to return something but it is actually irrelevant
mockIdToIpRequest);
EasyMock.replay(mockIdToIpRequest);
EasyMock.expect(mockInstances.list("proj-x", "us-central-1")).andReturn(mockIdToIpRequest);
EasyMock.replay(mockInstances);
EasyMock.expect(mockCompute.instances()).andReturn(mockInstances);
EasyMock.replay(mockCompute);
List<String> ids3 = Arrays.asList("foo", "bar");
List<String> ips3 = autoScaler.idToIpLookup(ids3);
Assert.assertEquals(1, ips3.size());
Assert.assertEquals("1.2.3.4", ips3.get(0));
EasyMock.verify(mockCompute);
EasyMock.verify(mockInstances);
EasyMock.verify(mockIdToIpRequest);
}
Aggregations