Here are the examples of the java api class com.google.cloud.compute.Compute taken from open source projects.
1. CreateInstance#main()
View licensepublic static void main(String... args) throws InterruptedException, TimeoutException { Compute compute = ComputeOptions.defaultInstance().service(); ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329"); NetworkId networkId = NetworkId.of("default"); AttachedDisk attachedDisk = AttachedDisk.of(AttachedDisk.CreateDiskConfiguration.of(imageId)); NetworkInterface networkInterface = NetworkInterface.of(networkId); InstanceId instanceId = InstanceId.of("us-central1-a", "instance-name"); MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1"); Operation operation = compute.create(InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface)); operation = operation.waitFor(); if (operation.errors() == null) { // use instance Instance instance = compute.getInstance(instanceId); } }
2. CreateSnapshot#main()
View licensepublic static void main(String... args) throws InterruptedException, TimeoutException { Compute compute = ComputeOptions.defaultInstance().service(); DiskId diskId = DiskId.of("us-central1-a", "disk-name"); Disk disk = compute.getDisk(diskId, Compute.DiskOption.fields()); if (disk != null) { String snapshotName = "disk-name-snapshot"; Operation operation = disk.createSnapshot(snapshotName); operation = operation.waitFor(); if (operation.errors() == null) { // use snapshot Snapshot snapshot = compute.getSnapshot(snapshotName); } } }
3. ComputeExample#main()
View license@SuppressWarnings("unchecked") public static void main(String... args) throws Exception { if (args.length < 1) { System.out.println("Missing required project id and action"); printUsage(); return; } ComputeOptions.Builder optionsBuilder = ComputeOptions.builder(); ComputeAction action; String actionName; if (args.length >= 2 && !ACTIONS.containsKey(args[0])) { actionName = args[1]; optionsBuilder.projectId(args[0]); action = ACTIONS.get(args[1]); args = Arrays.copyOfRange(args, 2, args.length); } else { actionName = args[0]; action = ACTIONS.get(args[0]); args = Arrays.copyOfRange(args, 1, args.length); } if (action == null) { System.out.println("Unrecognized action."); printUsage(); return; } Compute compute = optionsBuilder.build().service(); Object arg; try { arg = action.parse(args); } catch (IllegalArgumentException ex) { System.out.printf("Invalid input for action '%s'. %s%n", actionName, ex.getMessage()); System.out.printf("Expected: %s%n", action.params()); return; } catch (Exception ex) { System.out.println("Failed to parse arguments."); ex.printStackTrace(); return; } action.run(compute, arg); }
4. CreateAddressDiskAndInstance#main()
View licensepublic static void main(String... args) throws InterruptedException, TimeoutException { // Create a service object // Credentials are inferred from the environment. Compute compute = ComputeOptions.defaultInstance().service(); // Create an external region address RegionAddressId addressId = RegionAddressId.of("us-central1", "test-address"); Operation operation = compute.create(AddressInfo.of(addressId)); // Wait for operation to complete operation = operation.waitFor(); if (operation.errors() == null) { System.out.println("Address " + addressId + " was successfully created"); } else { // inspect operation.errors() throw new RuntimeException("Address creation failed"); } // Create a persistent disk ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329"); DiskId diskId = DiskId.of("us-central1-a", "test-disk"); ImageDiskConfiguration diskConfiguration = ImageDiskConfiguration.of(imageId); DiskInfo disk = DiskInfo.of(diskId, diskConfiguration); operation = compute.create(disk); // Wait for operation to complete operation = operation.waitFor(); if (operation.errors() == null) { System.out.println("Disk " + diskId + " was successfully created"); } else { // inspect operation.errors() throw new RuntimeException("Disk creation failed"); } // Create a virtual machine instance Address externalIp = compute.getAddress(addressId); InstanceId instanceId = InstanceId.of("us-central1-a", "test-instance"); NetworkId networkId = NetworkId.of("default"); PersistentDiskConfiguration attachConfiguration = PersistentDiskConfiguration.builder(diskId).boot(true).build(); AttachedDisk attachedDisk = AttachedDisk.of("dev0", attachConfiguration); NetworkInterface networkInterface = NetworkInterface.builder(networkId).accessConfigurations(AccessConfig.of(externalIp.address())).build(); MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1"); InstanceInfo instance = InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface); operation = compute.create(instance); // Wait for operation to complete operation = operation.waitFor(); if (operation.errors() == null) { System.out.println("Instance " + instanceId + " was successfully created"); } else { // inspect operation.errors() throw new RuntimeException("Instance creation failed"); } }