use of com.google.api.services.compute.model.Instance in project cloudbreak by hortonworks.
the class GcpMetadataCollector method getCloudVmMetaDataStatus.
private CloudVmMetaDataStatus getCloudVmMetaDataStatus(AuthenticatedContext authenticatedContext, CloudResource cloudResource, CloudInstance matchedInstance) {
CloudVmMetaDataStatus cloudVmMetaDataStatus;
if (cloudResource != null) {
CloudInstance cloudInstance = new CloudInstance(cloudResource.getName(), matchedInstance.getTemplate(), matchedInstance.getAuthentication());
try {
CloudCredential credential = authenticatedContext.getCloudCredential();
CloudContext cloudContext = authenticatedContext.getCloudContext();
Compute compute = GcpStackUtil.buildCompute(credential);
Instance executeInstance = getInstance(cloudContext, credential, compute, cloudResource.getName());
String privateIp = executeInstance.getNetworkInterfaces().get(0).getNetworkIP();
String publicIp = null;
List<AccessConfig> acl = executeInstance.getNetworkInterfaces().get(0).getAccessConfigs();
if (acl != null && acl.get(0) != null) {
publicIp = executeInstance.getNetworkInterfaces().get(0).getAccessConfigs().get(0).getNatIP();
}
CloudInstanceMetaData metaData = new CloudInstanceMetaData(privateIp, publicIp);
CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, metaData);
} catch (IOException e) {
LOGGER.warn(String.format("Instance %s is not reachable", cloudResource.getName()), e);
CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.UNKNOWN);
cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, CloudInstanceMetaData.EMPTY_METADATA);
}
} else {
CloudVmInstanceStatus status = new CloudVmInstanceStatus(matchedInstance, InstanceStatus.TERMINATED);
cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, CloudInstanceMetaData.EMPTY_METADATA);
}
return cloudVmMetaDataStatus;
}
use of com.google.api.services.compute.model.Instance in project druid by druid-io.
the class GceAutoScalerTest method testIpToId.
@Test
public void testIpToId() 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> ips1 = Collections.emptyList();
List<String> ids1 = autoScaler.ipToIdLookup(ips1);
Assert.assertEquals(0, ids1.size());
// actually not IPs
List<String> ips2 = Collections.singletonList("foo-bar-baz");
List<String> ids2 = autoScaler.ipToIdLookup(ips2);
Assert.assertEquals(ips2, ids2);
// actually IPs
// not the one we look for
Instance i1 = makeInstance("foo", "1.2.3.5");
// the one we do look for
Instance i2 = makeInstance("bar", "1.2.3.4");
InstanceList mockResponse = new InstanceList();
mockResponse.setNextPageToken(null);
mockResponse.setItems(Arrays.asList(i1, i2));
EasyMock.expect(mockIpToIdRequest.execute()).andReturn(mockResponse);
EasyMock.expect(mockIpToIdRequest.setPageToken(EasyMock.anyString())).andReturn(// the method needs to return something, what is actually irrelevant here
mockIpToIdRequest);
EasyMock.replay(mockIpToIdRequest);
EasyMock.expect(mockInstances.list("proj-x", "us-central-1")).andReturn(mockIpToIdRequest);
EasyMock.replay(mockInstances);
EasyMock.expect(mockCompute.instances()).andReturn(mockInstances);
EasyMock.replay(mockCompute);
List<String> ips3 = Collections.singletonList("1.2.3.4");
List<String> ids3 = autoScaler.ipToIdLookup(ips3);
Assert.assertEquals(1, ids3.size());
Assert.assertEquals("bar", ids3.get(0));
EasyMock.verify(mockCompute);
EasyMock.verify(mockInstances);
EasyMock.verify(mockIpToIdRequest);
}
use of com.google.api.services.compute.model.Instance in project druid by druid-io.
the class GceAutoScalerTest method testTerminate.
@Test
public void testTerminate() 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);
// testing the ip --> id part
Instance i0 = makeInstance("baz", "1.2.3.6");
InstanceList mockInstanceListResponse = new InstanceList();
mockInstanceListResponse.setNextPageToken(null);
mockInstanceListResponse.setItems(Collections.singletonList(i0));
EasyMock.expect(mockIpToIdRequest.execute()).andReturn(mockInstanceListResponse);
EasyMock.expect(mockIpToIdRequest.setPageToken(EasyMock.anyString())).andReturn(// the method needs to return something, what is actually irrelevant here
mockIpToIdRequest);
EasyMock.replay(mockIpToIdRequest);
EasyMock.expect(mockInstances.list("proj-x", "us-central-1")).andReturn(mockIpToIdRequest);
EasyMock.expect(mockCompute.instances()).andReturn(mockInstances);
EasyMock.replay(mockInstances);
// testing the delete part
InstanceGroupManagersListManagedInstancesResponse beforeRunningInstance = createRunningInstances(Arrays.asList("http://xyz/foo", "http://xyz/bar", "http://xyz/baz"));
InstanceGroupManagersListManagedInstancesResponse afterRunningInstance = createRunningInstances(Arrays.asList("http://xyz/foo", "http://xyz/bar"));
// 1st call
EasyMock.expect(mockInstancesRequest.execute()).andReturn(beforeRunningInstance);
EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
// 2nd call
EasyMock.expect(mockInstancesRequest.execute()).andReturn(afterRunningInstance);
EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
EasyMock.replay(mockInstancesRequest);
EasyMock.expect(mockInstanceGroupManagers.listManagedInstances("proj-x", "us-central-1", "druid-mig")).andReturn(mockInstancesRequest).times(2);
// set up the delete operation
Operation mockResponse = new Operation();
mockResponse.setStatus("DONE");
mockResponse.setError(new Operation.Error());
EasyMock.expect(mockDeleteRequest.execute()).andReturn(mockResponse);
EasyMock.replay(mockDeleteRequest);
InstanceGroupManagersDeleteInstancesRequest requestBody = new InstanceGroupManagersDeleteInstancesRequest();
requestBody.setInstances(Collections.singletonList("zones/us-central-1/instances/baz"));
EasyMock.expect(mockInstanceGroupManagers.deleteInstances("proj-x", "us-central-1", "druid-mig", requestBody)).andReturn(mockDeleteRequest);
EasyMock.replay(mockInstanceGroupManagers);
// called twice in getRunningInstances...
EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
// ...and once in terminateWithIds
EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
// and that's all folks!
EasyMock.replay(mockCompute);
AutoScalingData autoScalingData = autoScaler.terminate(Collections.singletonList("1.2.3.6"));
Assert.assertEquals(1, autoScalingData.getNodeIds().size());
Assert.assertEquals("baz", autoScalingData.getNodeIds().get(0));
EasyMock.verify(mockCompute);
EasyMock.verify(mockIpToIdRequest);
EasyMock.verify(mockInstanceGroupManagers);
EasyMock.verify(mockDeleteRequest);
EasyMock.verify(mockInstancesRequest);
}
use of com.google.api.services.compute.model.Instance in project platformlayer by platformlayer.
the class GoogleComputeClient method findInstanceByName.
public Instance findInstanceByName(String name) throws OpsException {
try {
log.debug("Retrieving instance by name: " + name);
Instance instance = compute.instances().get(projectId, name).execute();
return instance;
} catch (GoogleJsonResponseException e) {
if (e.getStatusCode() == 404) {
return null;
}
throw new OpsException("Error getting instance", e);
} catch (IOException e) {
throw new OpsException("Error getting instance", e);
}
}
use of com.google.api.services.compute.model.Instance in project elasticsearch by elastic.
the class GceUnicastHostsProvider method buildDynamicNodes.
/**
* We build the list of Nodes from GCE Management API
* Information can be cached using `cloud.gce.refresh_interval` property if needed.
*/
@Override
public List<DiscoveryNode> buildDynamicNodes() {
// We check that needed properties have been set
if (this.project == null || this.project.isEmpty() || this.zones == null || this.zones.isEmpty()) {
throw new IllegalArgumentException("one or more gce discovery settings are missing. " + "Check elasticsearch.yml file. Should have [" + GceInstancesService.PROJECT_SETTING.getKey() + "] and [" + GceInstancesService.ZONE_SETTING.getKey() + "].");
}
if (refreshInterval.millis() != 0) {
if (cachedDiscoNodes != null && (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) {
if (logger.isTraceEnabled())
logger.trace("using cache to retrieve node list");
return cachedDiscoNodes;
}
lastRefresh = System.currentTimeMillis();
}
logger.debug("start building nodes list using GCE API");
cachedDiscoNodes = new ArrayList<>();
String ipAddress = null;
try {
InetAddress inetAddress = networkService.resolvePublishHostAddresses(null);
if (inetAddress != null) {
ipAddress = NetworkAddress.format(inetAddress);
}
} catch (IOException e) {
// We can't find the publish host address... Hmmm. Too bad :-(
// We won't simply filter it
}
try {
Collection<Instance> instances = gceInstancesService.instances();
if (instances == null) {
logger.trace("no instance found for project [{}], zones [{}].", this.project, this.zones);
return cachedDiscoNodes;
}
for (Instance instance : instances) {
String name = instance.getName();
String type = instance.getMachineType();
String status = instance.getStatus();
logger.trace("gce instance {} with status {} found.", name, status);
// See https://github.com/elastic/elasticsearch-cloud-gce/issues/3
if (Status.TERMINATED.equals(status)) {
logger.debug("node {} is TERMINATED. Ignoring", name);
continue;
}
// see if we need to filter by tag
boolean filterByTag = false;
if (tags.isEmpty() == false) {
logger.trace("start filtering instance {} with tags {}.", name, tags);
if (instance.getTags() == null || instance.getTags().isEmpty() || instance.getTags().getItems() == null || instance.getTags().getItems().isEmpty()) {
// If this instance have no tag, we filter it
logger.trace("no tags for this instance but we asked for tags. {} won't be part of the cluster.", name);
filterByTag = true;
} else {
// check that all tags listed are there on the instance
logger.trace("comparing instance tags {} with tags filter {}.", instance.getTags().getItems(), tags);
for (String tag : tags) {
boolean found = false;
for (String instancetag : instance.getTags().getItems()) {
if (instancetag.equals(tag)) {
found = true;
break;
}
}
if (!found) {
filterByTag = true;
break;
}
}
}
}
if (filterByTag) {
logger.trace("filtering out instance {} based tags {}, not part of {}", name, tags, instance.getTags() == null || instance.getTags().getItems() == null ? "" : instance.getTags());
continue;
} else {
logger.trace("instance {} with tags {} is added to discovery", name, tags);
}
String ip_public = null;
String ip_private = null;
List<NetworkInterface> interfaces = instance.getNetworkInterfaces();
for (NetworkInterface networkInterface : interfaces) {
if (ip_public == null) {
// Trying to get Public IP Address (For future use)
if (networkInterface.getAccessConfigs() != null) {
for (AccessConfig accessConfig : networkInterface.getAccessConfigs()) {
if (Strings.hasText(accessConfig.getNatIP())) {
ip_public = accessConfig.getNatIP();
break;
}
}
}
}
if (ip_private == null) {
ip_private = networkInterface.getNetworkIP();
}
// If we have both public and private, we can stop here
if (ip_private != null && ip_public != null)
break;
}
try {
if (ip_private.equals(ipAddress)) {
// We found the current node.
// We can ignore it in the list of DiscoveryNode
logger.trace("current node found. Ignoring {} - {}", name, ip_private);
} else {
String address = ip_private;
// Test if we have es_port metadata defined here
if (instance.getMetadata() != null && instance.getMetadata().containsKey("es_port")) {
Object es_port = instance.getMetadata().get("es_port");
logger.trace("es_port is defined with {}", es_port);
if (es_port instanceof String) {
address = address.concat(":").concat((String) es_port);
} else {
// Ignoring other values
logger.trace("es_port is instance of {}. Ignoring...", es_port.getClass().getName());
}
}
// ip_private is a single IP Address. We need to build a TransportAddress from it
// If user has set `es_port` metadata, we don't need to ping all ports
// we only limit to 1 addresses, makes no sense to ping 100 ports
TransportAddress[] addresses = transportService.addressesFromString(address, 1);
for (TransportAddress transportAddress : addresses) {
logger.trace("adding {}, type {}, address {}, transport_address {}, status {}", name, type, ip_private, transportAddress, status);
cachedDiscoNodes.add(new DiscoveryNode("#cloud-" + name + "-" + 0, transportAddress, emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
}
}
} catch (Exception e) {
final String finalIpPrivate = ip_private;
logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to add {}, address {}", name, finalIpPrivate), e);
}
}
} catch (Exception e) {
logger.warn("exception caught during discovery", e);
}
logger.debug("{} node(s) added", cachedDiscoNodes.size());
logger.debug("using dynamic discovery nodes {}", cachedDiscoNodes);
return cachedDiscoNodes;
}
Aggregations