use of com.google.container.v1.Operation in project java-docs-samples by GoogleCloudPlatform.
the class CreateWindowsOsImage method createWindowsOsImage.
// Creates a new Windows image from the specified source disk.
public static void createWindowsOsImage(String project, String zone, String sourceDiskName, String imageName, String storageLocation, boolean forceCreate) throws IOException, ExecutionException, InterruptedException, TimeoutException {
try (ImagesClient imagesClient = ImagesClient.create();
InstancesClient instancesClient = InstancesClient.create();
DisksClient disksClient = DisksClient.create()) {
Disk disk = disksClient.get(project, zone, sourceDiskName);
// Getting instances where source disk is attached.
for (String fullInstanceName : disk.getUsersList()) {
Map<String, String> instanceInfo = parseInstanceName(fullInstanceName);
Instance instance = instancesClient.get(instanceInfo.get("instanceProjectId"), instanceInfo.get("instanceZone"), instanceInfo.get("instanceName"));
// Сhecking whether the instances is stopped.
if (!Arrays.asList("TERMINATED", "STOPPED").contains(instance.getStatus()) && !forceCreate) {
throw new IllegalStateException(String.format("Instance %s should be stopped. Please stop the instance using GCESysprep command or set forceCreate parameter to true (not recommended). More information here: https://cloud.google.com/compute/docs/instances/windows/creating-windows-os-image#api.", instanceInfo.get("instanceName")));
}
}
if (forceCreate) {
System.out.println("Warning: forceCreate option compromise the integrity of your image. " + "Stop the instance before you create the image if possible.");
}
// Create Image.
Image image = Image.newBuilder().setName(imageName).setSourceDisk(String.format("/zones/%s/disks/%s", zone, sourceDiskName)).addStorageLocations(storageLocation.isEmpty() ? "" : storageLocation).build();
InsertImageRequest insertImageRequest = InsertImageRequest.newBuilder().setProject(project).setForceCreate(forceCreate).setImageResource(image).build();
Operation response = imagesClient.insertAsync(insertImageRequest).get(3, TimeUnit.MINUTES);
if (response.hasError()) {
System.out.println("Windows OS Image creation failed ! ! " + response);
return;
}
System.out.println("Image created.");
}
}
use of com.google.container.v1.Operation in project java-docs-samples by GoogleCloudPlatform.
the class PatchFirewallRule method patchFirewallPriority.
// Modifies the priority of a given firewall rule.
public static void patchFirewallPriority(String project, String firewallRuleName, int priority) throws IOException, ExecutionException, InterruptedException, TimeoutException {
/* Initialize client that will be used to send requests. This client only needs to be created
once, and can be reused for multiple requests. After completing all of your requests, call
the `firewallsClient.close()` method on the client to safely
clean up any remaining background resources. */
try (FirewallsClient firewallsClient = FirewallsClient.create()) {
/* The patch operation doesn't require the full definition of a Firewall object. It will only
update the values that were set in it, in this case it will only change the priority. */
Firewall firewall = Firewall.newBuilder().setPriority(priority).build();
PatchFirewallRequest patchFirewallRequest = PatchFirewallRequest.newBuilder().setProject(project).setFirewall(firewallRuleName).setFirewallResource(firewall).build();
OperationFuture<Operation, Operation> operation = firewallsClient.patchAsync(patchFirewallRequest);
operation.get(3, TimeUnit.MINUTES);
System.out.println("Firewall Patch applied successfully ! ");
}
}
use of com.google.container.v1.Operation in project java-docs-samples by GoogleCloudPlatform.
the class ResumeInstance method resumeInstance.
// Resume a suspended Google Compute Engine instance (with unencrypted disks).
// Instance state changes to RUNNING, if successfully resumed.
public static void resumeInstance(String project, String zone, String instanceName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Instantiates a client.
try (InstancesClient instancesClient = InstancesClient.create()) {
String currentInstanceState = instancesClient.get(project, zone, instanceName).getStatus();
// Check if the instance is currently suspended.
if (!currentInstanceState.equalsIgnoreCase(Status.SUSPENDED.toString())) {
throw new RuntimeException(String.format("Only suspended instances can be resumed. Instance %s is in %s state.", instanceName, currentInstanceState));
}
Operation operation = instancesClient.resumeAsync(project, zone, instanceName).get(300, TimeUnit.SECONDS);
if (operation.hasError() || !instancesClient.get(project, zone, instanceName).getStatus().equalsIgnoreCase(Status.RUNNING.toString())) {
System.out.println("Cannot resume instance. Try again!");
return;
}
System.out.printf("Instance resumed successfully ! %s", instanceName);
}
}
use of com.google.container.v1.Operation in project java-docs-samples by GoogleCloudPlatform.
the class SuspendInstance method suspendInstance.
// Suspend a running Google Compute Engine instance.
// For limitations and compatibility on which instances can be suspended,
// see: https://cloud.google.com/compute/docs/instances/suspend-resume-instance#limitations
public static void suspendInstance(String project, String zone, String instanceName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Instantiates a client.
try (InstancesClient instancesClient = InstancesClient.create()) {
Operation operation = instancesClient.suspendAsync(project, zone, instanceName).get(300, TimeUnit.SECONDS);
if (operation.hasError() || !instancesClient.get(project, zone, instanceName).getStatus().equalsIgnoreCase(Status.SUSPENDED.toString())) {
System.out.println("Cannot suspend instance. Try again!");
return;
}
System.out.printf("Instance suspended successfully ! %s", instanceName);
}
}
use of com.google.container.v1.Operation in project geo-platform by geosdi.
the class CatalogCapabilitiesTest method testGetCapabilitiesWithConnector.
@Test
public void testGetCapabilitiesWithConnector() throws Exception {
URL url = new URL("http://catalog.geosdi.org/geonetwork/srv/eng/csw");
GPCatalogConnectorStore serverConnector = newConnector().withServerUrl(url).build();
CatalogGetCapabilitiesRequest<CapabilitiesType> request = serverConnector.createGetCapabilitiesRequest();
CapabilitiesType response = request.getResponse();
logger.info("CSW GET_CAPABILITIES VERSION @@@@@@@@@@@@@@@@@@@@@@@ {}", response.getVersion());
List<Operation> operationList = response.getOperationsMetadata().getOperation();
for (Operation operation : operationList) {
String operationName = operation.getName();
if ("GetRecordById".equals(operationName)) {
List<DomainType> parameterList = operation.getParameter();
for (DomainType parameter : parameterList) {
String parameterName = parameter.getName();
if ("outputSchema".equals(parameterName)) {
List<String> valueList = parameter.getValue();
logger.info("########################### outputSchema\n");
for (String value : valueList) {
logger.info("*** {}", value);
}
}
}
}
}
}
Aggregations