use of com.google.api.services.compute.model.Instance in project google-cloud-java by GoogleCloudPlatform.
the class HttpComputeRpc method listInstances.
@Override
public Tuple<String, Iterable<Instance>> listInstances(String zone, Map<Option, ?> options) {
try {
InstanceList instanceList = compute.instances().list(this.options.getProjectId(), zone).setFilter(Option.FILTER.getString(options)).setMaxResults(Option.MAX_RESULTS.getLong(options)).setPageToken(Option.PAGE_TOKEN.getString(options)).setFields(Option.FIELDS.getString(options)).execute();
Iterable<Instance> instances = instanceList.getItems();
return Tuple.of(instanceList.getNextPageToken(), instances);
} catch (IOException ex) {
throw translate(ex);
}
}
use of com.google.api.services.compute.model.Instance in project google-cloud-java by GoogleCloudPlatform.
the class InstanceInfo method toPb.
Instance toPb() {
Instance instancePb = new Instance();
if (generatedId != null) {
instancePb.setId(new BigInteger(generatedId));
}
if (creationTimestamp != null) {
instancePb.setCreationTimestamp(TIMESTAMP_FORMATTER.print(creationTimestamp));
}
instancePb.setName(instanceId.getInstance());
instancePb.setDescription(description);
instancePb.setSelfLink(instanceId.getSelfLink());
instancePb.setZone(instanceId.getZoneId().getSelfLink());
if (status != null) {
instancePb.setStatus(status.name());
}
instancePb.setStatusMessage(statusMessage);
if (tags != null) {
instancePb.setTags(tags.toPb());
}
if (machineType != null) {
instancePb.setMachineType(machineType.getSelfLink());
}
instancePb.setCanIpForward(canIpForward);
if (networkInterfaces != null) {
instancePb.setNetworkInterfaces(Lists.transform(networkInterfaces, NetworkInterface.TO_PB_FUNCTION));
}
if (attachedDisks != null) {
instancePb.setDisks(Lists.transform(attachedDisks, AttachedDisk.TO_PB_FUNCTION));
}
if (metadata != null) {
instancePb.setMetadata(metadata.toPb());
}
if (serviceAccounts != null) {
instancePb.setServiceAccounts(Lists.transform(serviceAccounts, ServiceAccount.TO_PB_FUNCTION));
}
if (schedulingOptions != null) {
instancePb.setScheduling(schedulingOptions.toPb());
}
instancePb.setCpuPlatform(cpuPlatform);
return instancePb;
}
use of com.google.api.services.compute.model.Instance in project photon-model by vmware.
the class GCPTestUtil method provisionInstances.
/**
* Provision the given number of instances in specified project and zone.
* @param host The test service host.
* @param compute The Google Compute Engine client.
* @param userEmail The service account's client email.
* @param projectId The GCP project ID.
* @param zoneId The GCP project's zone ID.
* @param n The number of instances to provision.
* @param batchSize The batch size.
* @param interval The waiting interval.
* @throws Throwable The exception during provisioning remote instances.
*/
public static List<String> provisionInstances(VerificationHost host, Compute compute, String userEmail, String projectId, String zoneId, int n, int batchSize, long interval) throws Throwable {
if (n < 0) {
throw new IllegalArgumentException("the number of instances to be provisioned cannot be negative.");
}
if (batchSize <= 0) {
throw new IllegalArgumentException("batch size cannot be less or equal to zero.");
}
if (interval <= 0) {
throw new IllegalArgumentException("waiting interval cannot be less or equal to zero");
}
List<String> scopes = Collections.singletonList(ComputeScopes.CLOUD_PLATFORM);
List<String> instanceNames = new ArrayList<>();
com.google.api.services.compute.model.Operation[] ops = new com.google.api.services.compute.model.Operation[n];
String[] zones = new String[n];
String[] opIds = new String[n];
Instance instance = createInstanceTemplate(userEmail, projectId, zoneId, scopes);
for (int i = 0; i < n; i++) {
String instanceName = ADAPTER_TEST_INSTANCE + UUID.randomUUID().toString();
instanceNames.add(instanceName);
ops[i] = provisionOneInstance(compute, instance, instanceName, projectId, zoneId);
zones[i] = ops[i].getZone();
zones[i] = extractZoneFromZoneUri(zones[i]);
opIds[i] = ops[i].getName();
// This is to prevent making too much API calls in a very short time.
if ((i + 1) % batchSize == 0) {
TimeUnit.MILLISECONDS.sleep(interval);
}
}
waitForOperationsDone(host, compute, projectId, ops, zones, opIds);
return instanceNames;
}
use of com.google.api.services.compute.model.Instance in project photon-model by vmware.
the class GCPTestUtil method getInstanceNumber.
/**
* Get the number of instances on specified GCP project and zone.
* @param compute The GCE client object.
* @param projectId The project id.
* @param zoneId The zone id.
* @return The number of instances.
* @throws Throwable Exception during querying the instances
*/
public static int getInstanceNumber(Compute compute, String projectId, String zoneId) throws Throwable {
Instances instanceList = compute.instances();
Instances.List list = instanceList.list(projectId, zoneId);
InstanceList ins = list.execute();
List<Instance> instances = ins.getItems();
if (instances == null) {
return 0;
}
return instances.size();
}
use of com.google.api.services.compute.model.Instance in project cloudbreak by hortonworks.
the class GcpInstanceResourceBuilder method build.
@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> customTags) throws Exception {
InstanceTemplate template = group.getReferenceInstanceConfiguration().getTemplate();
String projectId = context.getProjectId();
Location location = context.getLocation();
boolean noPublicIp = context.getNoPublicIp();
Compute compute = context.getCompute();
List<CloudResource> computeResources = context.getComputeResources(privateId);
List<AttachedDisk> listOfDisks = new ArrayList<>();
listOfDisks.addAll(getBootDiskList(computeResources, projectId, location.getAvailabilityZone()));
listOfDisks.addAll(getAttachedDisks(computeResources, projectId, location.getAvailabilityZone()));
Instance instance = new Instance();
instance.setMachineType(String.format("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/%s", projectId, location.getAvailabilityZone().value(), template.getFlavor()));
instance.setName(buildableResource.get(0).getName());
instance.setCanIpForward(Boolean.TRUE);
instance.setNetworkInterfaces(getNetworkInterface(context.getNetworkResources(), computeResources, location.getRegion(), group, compute, projectId, noPublicIp));
instance.setDisks(listOfDisks);
Scheduling scheduling = new Scheduling();
boolean preemptible = false;
if (template.getParameter(PREEMPTIBLE, Boolean.class) != null) {
preemptible = template.getParameter(PREEMPTIBLE, Boolean.class);
}
scheduling.setPreemptible(preemptible);
instance.setScheduling(scheduling);
Tags tags = new Tags();
List<String> tagList = new ArrayList<>();
Map<String, String> labels = new HashMap<>();
String groupname = group.getName().toLowerCase().replaceAll("[^A-Za-z0-9 ]", "");
tagList.add(groupname);
Map<String, String> instanceTag = defaultCostTaggingService.prepareInstanceTagging();
for (Entry<String, String> entry : instanceTag.entrySet()) {
tagList.add(String.format("%s-%s", entry.getKey(), entry.getValue()));
labels.put(entry.getKey(), entry.getValue());
}
tagList.add(GcpStackUtil.getClusterTag(auth.getCloudContext()));
tagList.add(GcpStackUtil.getGroupClusterTag(auth.getCloudContext(), group));
customTags.forEach((key, value) -> tagList.add(key + '-' + value));
labels.putAll(customTags);
tags.setItems(tagList);
instance.setTags(tags);
instance.setLabels(labels);
Metadata metadata = new Metadata();
metadata.setItems(new ArrayList<>());
Items sshMetaData = new Items();
sshMetaData.setKey("ssh-keys");
sshMetaData.setValue(group.getInstanceAuthentication().getLoginUserName() + ':' + group.getInstanceAuthentication().getPublicKey());
Items blockProjectWideSsh = new Items();
blockProjectWideSsh.setKey("block-project-ssh-keys");
blockProjectWideSsh.setValue("TRUE");
Items startupScript = new Items();
startupScript.setKey("startup-script");
startupScript.setValue(image.getUserDataByType(group.getType()));
metadata.getItems().add(sshMetaData);
metadata.getItems().add(startupScript);
metadata.getItems().add(blockProjectWideSsh);
instance.setMetadata(metadata);
Insert insert = compute.instances().insert(projectId, location.getAvailabilityZone().value(), instance);
insert.setPrettyPrint(Boolean.TRUE);
try {
Operation operation = insert.execute();
if (operation.getHttpErrorStatusCode() != null) {
throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), buildableResource.get(0).getName());
}
return Collections.singletonList(createOperationAwareCloudResource(buildableResource.get(0), operation));
} catch (GoogleJsonResponseException e) {
throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
}
}
Aggregations