Search in sources :

Example 16 with Instance

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;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Instance(com.google.api.services.compute.model.Instance) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Compute(com.google.api.services.compute.Compute) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) IOException(java.io.IOException)

Example 17 with Instance

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<>();
}
Also used : Instance(com.google.api.services.compute.model.Instance) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) Compute(com.google.api.services.compute.Compute) ArrayList(java.util.ArrayList) InstanceList(com.google.api.services.compute.model.InstanceList) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 18 with Instance

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<>();
}
Also used : Instance(com.google.api.services.compute.model.Instance) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) Compute(com.google.api.services.compute.Compute) ArrayList(java.util.ArrayList) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) InstanceList(com.google.api.services.compute.model.InstanceList) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 19 with Instance

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;
}
Also used : Instance(com.google.api.services.compute.model.Instance) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) NetworkInterface(com.google.api.services.compute.model.NetworkInterface)

Example 20 with 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);
}
Also used : Instance(com.google.api.services.compute.model.Instance) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) InstanceList(com.google.api.services.compute.model.InstanceList) Test(org.junit.Test)

Aggregations

Instance (com.google.api.services.compute.model.Instance)22 InstanceList (com.google.api.services.compute.model.InstanceList)9 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 ManagedInstance (com.google.api.services.compute.model.ManagedInstance)6 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)6 Compute (com.google.api.services.compute.Compute)5 AccessConfig (com.google.api.services.compute.model.AccessConfig)5 Operation (com.google.api.services.compute.model.Operation)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)3 Metadata (com.google.api.services.compute.model.Metadata)3 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)3 OpsException (org.platformlayer.ops.OpsException)3 Instances (com.google.api.services.compute.Compute.Instances)2 AttachedDiskInitializeParams (com.google.api.services.compute.model.AttachedDiskInitializeParams)2 Items (com.google.api.services.compute.model.Metadata.Items)2 ServiceAccount (com.google.api.services.compute.model.ServiceAccount)2 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)2 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)2