Search in sources :

Example 1 with Instance

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(Map<Option, ?> options) {
    try {
        InstanceAggregatedList aggregatedList = compute.instances().aggregatedList(this.options.getProjectId()).setFilter(Option.FILTER.getString(options)).setMaxResults(Option.MAX_RESULTS.getLong(options)).setPageToken(Option.PAGE_TOKEN.getString(options)).execute();
        ImmutableList.Builder<Instance> builder = ImmutableList.builder();
        Map<String, InstancesScopedList> scopedList = aggregatedList.getItems();
        if (scopedList != null) {
            for (InstancesScopedList instancesScopedList : scopedList.values()) {
                if (instancesScopedList.getInstances() != null) {
                    builder.addAll(instancesScopedList.getInstances());
                }
            }
        }
        return Tuple.<String, Iterable<Instance>>of(aggregatedList.getNextPageToken(), builder.build());
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : InstancesScopedList(com.google.api.services.compute.model.InstancesScopedList) Instance(com.google.api.services.compute.model.Instance) ImmutableList(com.google.common.collect.ImmutableList) InstanceAggregatedList(com.google.api.services.compute.model.InstanceAggregatedList) IOException(java.io.IOException)

Example 2 with Instance

use of com.google.api.services.compute.model.Instance in project java-docs-samples by GoogleCloudPlatform.

the class ComputeEngineSample method printInstances.

// [START list_instances]
/**
 * Print available machine instances.
 *
 * @param compute The main API access point
 * @return {@code true} if the instance created by this sample app is in the list
 */
public static boolean printInstances(Compute compute) throws IOException {
    System.out.println("================== Listing Compute Engine Instances ==================");
    Compute.Instances.List instances = compute.instances().list(PROJECT_ID, ZONE_NAME);
    InstanceList list = instances.execute();
    boolean found = false;
    if (list.getItems() == null) {
        System.out.println("No instances found. Sign in to the Google Developers Console and create " + "an instance at: https://console.developers.google.com/");
    } else {
        for (Instance instance : list.getItems()) {
            System.out.println(instance.toPrettyString());
            if (instance.getName().equals(SAMPLE_INSTANCE_NAME)) {
                found = true;
            }
        }
    }
    return found;
}
Also used : Instance(com.google.api.services.compute.model.Instance) InstanceList(com.google.api.services.compute.model.InstanceList)

Example 3 with Instance

use of com.google.api.services.compute.model.Instance in project java-docs-samples by GoogleCloudPlatform.

the class ComputeEngineSample method startInstance.

// [END list_instances]
// [START create_instances]
public static Operation startInstance(Compute compute, String instanceName) throws IOException {
    System.out.println("================== Starting New Instance ==================");
    // Create VM Instance object with the required properties.
    Instance instance = new Instance();
    instance.setName(instanceName);
    instance.setMachineType("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/zones/" + ZONE_NAME + "/machineTypes/n1-standard-1");
    // Add Network Interface to be used by VM Instance.
    NetworkInterface ifc = new NetworkInterface();
    ifc.setNetwork("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/global/networks/default");
    List<AccessConfig> configs = new ArrayList<>();
    AccessConfig config = new AccessConfig();
    config.setType(NETWORK_INTERFACE_CONFIG);
    config.setName(NETWORK_ACCESS_CONFIG);
    configs.add(config);
    ifc.setAccessConfigs(configs);
    instance.setNetworkInterfaces(Collections.singletonList(ifc));
    // Add attached Persistent Disk to be used by VM Instance.
    AttachedDisk disk = new AttachedDisk();
    disk.setBoot(true);
    disk.setAutoDelete(true);
    disk.setType("PERSISTENT");
    AttachedDiskInitializeParams params = new AttachedDiskInitializeParams();
    // Assign the Persistent Disk the same name as the VM Instance.
    params.setDiskName(instanceName);
    // Specify the source operating system machine image to be used by the VM Instance.
    params.setSourceImage(SOURCE_IMAGE_PREFIX + SOURCE_IMAGE_PATH);
    // Specify the disk type as Standard Persistent Disk
    params.setDiskType("https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID + "/zones/" + ZONE_NAME + "/diskTypes/pd-standard");
    disk.setInitializeParams(params);
    instance.setDisks(Collections.singletonList(disk));
    // Initialize the service account to be used by the VM Instance and set the API access scopes.
    ServiceAccount account = new ServiceAccount();
    account.setEmail("default");
    List<String> scopes = new ArrayList<>();
    scopes.add("https://www.googleapis.com/auth/devstorage.full_control");
    scopes.add("https://www.googleapis.com/auth/compute");
    account.setScopes(scopes);
    instance.setServiceAccounts(Collections.singletonList(account));
    // Optional - Add a startup script to be used by the VM Instance.
    Metadata meta = new Metadata();
    Metadata.Items item = new Metadata.Items();
    item.setKey("startup-script-url");
    // If you put a script called "vm-startup.sh" in this Google Cloud Storage
    // bucket, it will execute on VM startup.  This assumes you've created a
    // bucket named the same as your PROJECT_ID.
    // For info on creating buckets see: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
    item.setValue("gs://" + PROJECT_ID + "/vm-startup.sh");
    meta.setItems(Collections.singletonList(item));
    instance.setMetadata(meta);
    System.out.println(instance.toPrettyString());
    Compute.Instances.Insert insert = compute.instances().insert(PROJECT_ID, ZONE_NAME, instance);
    return insert.execute();
}
Also used : ServiceAccount(com.google.api.services.compute.model.ServiceAccount) Instance(com.google.api.services.compute.model.Instance) ArrayList(java.util.ArrayList) Metadata(com.google.api.services.compute.model.Metadata) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) AttachedDiskInitializeParams(com.google.api.services.compute.model.AttachedDiskInitializeParams) AccessConfig(com.google.api.services.compute.model.AccessConfig)

Example 4 with Instance

use of com.google.api.services.compute.model.Instance in project photon-model by vmware.

the class GCPTestUtil method getStaleInstanceNames.

/**
 * Get the names of all stale instances remaining from previous runs due to read timeout
 * failures.
 * @param compute The GCE client object.
 * @param projectId The project id.
 * @param zoneId The zone id.
 * @return Names of stale instances
 * @throws Throwable Exception during querying the instances
 */
public static List<String> getStaleInstanceNames(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();
    List<String> names = new ArrayList<String>();
    if (instances == null) {
        return null;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat(GCPConstants.VM_CREATION_TIMESTAMP_FORMAT);
    dateFormat.setTimeZone(TimeZone.getTimeZone(GCPConstants.UTC_TIMEZONE_ID));
    for (Instance i : instances) {
        Date date = dateFormat.parse(i.getCreationTimestamp());
        long time = TimeUnit.MILLISECONDS.toMicros(date.getTime());
        if (Utils.getNowMicrosUtc() - time > ONE_HOUR_DIFFERENCE_MICROS && i.getName().startsWith("adapter-test-instance")) {
            names.add(i.getName());
        }
    }
    return names;
}
Also used : Instances(com.google.api.services.compute.Compute.Instances) Instance(com.google.api.services.compute.model.Instance) ArrayList(java.util.ArrayList) InstanceList(com.google.api.services.compute.model.InstanceList) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with Instance

use of com.google.api.services.compute.model.Instance in project photon-model by vmware.

the class GCPTestUtil method createInstanceTemplate.

/**
 * Create an instance template for later provisioning.
 * @param userEmail The service account's client email.
 * @param projectId The project id.
 * @param zoneId The zone id.
 * @param scopes The priority scopes.
 * @return The instance template.
 */
private static Instance createInstanceTemplate(String userEmail, String projectId, String zoneId, List<String> scopes) {
    Instance instance = new Instance();
    instance.setMachineType(String.format(ENUMERATION_TEST_MACHINE_TYPE, projectId, zoneId));
    NetworkInterface ifc = new NetworkInterface();
    ifc.setNetwork(String.format(NETWORK_INTERFACE, projectId));
    List<AccessConfig> configs = new ArrayList<>();
    AccessConfig config = new AccessConfig();
    config.setType(NETWORK_INTERFACE_CONFIG);
    config.setName(NETWORK_ACCESS_CONFIG);
    configs.add(config);
    ifc.setAccessConfigs(configs);
    instance.setNetworkInterfaces(Collections.singletonList(ifc));
    AttachedDisk disk = new AttachedDisk();
    disk.setBoot(true);
    disk.setAutoDelete(true);
    disk.setType(DISK_TYPE_PERSISTENT);
    AttachedDiskInitializeParams params = new AttachedDiskInitializeParams();
    params.setSourceImage(SOURCE_IMAGE);
    params.setDiskType(String.format(DISK_TYPE, projectId, zoneId));
    disk.setInitializeParams(params);
    instance.setDisks(Collections.singletonList(disk));
    ServiceAccount account = new ServiceAccount();
    account.setEmail(userEmail);
    account.setScopes(scopes);
    instance.setServiceAccounts(Collections.singletonList(account));
    return instance;
}
Also used : ServiceAccount(com.google.api.services.compute.model.ServiceAccount) Instance(com.google.api.services.compute.model.Instance) ArrayList(java.util.ArrayList) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) AttachedDiskInitializeParams(com.google.api.services.compute.model.AttachedDiskInitializeParams) AccessConfig(com.google.api.services.compute.model.AccessConfig)

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