@Override public NodeMetadata apply(Instance input) { String group = groupFromMapOrName(input.metadata().asMap(), input.name(), nodeNamingConvention); NodeMetadataBuilder builder = new NodeMetadataBuilder(); Location zone = locationsByUri.get().get(input.zone()); if (zone == null) { throw new IllegalStateException( String.format("zone %s not present in %s", input.zone(), locationsByUri.get().keySet())); URI diskSource = input.disks().get(0).source(); Optional<Image> image = diskURIToImage.getUnchecked(diskSource); if (isCustomMachineTypeURI(input.machineType())) { hardware = machineTypeURIToCustomHardware(input.machineType()); hardware = hardwares.get().get(input.machineType()); builder.id(input.selfLink().toString()) .name(input.name()) .providerId(input.id()) .hostname(input.name()) .location(zone) .imageId(image.isPresent() ? image.get().selfLink().toString() : null) .hardware(hardware) .status(input.status() != null ? toPortableNodeStatus.get(input.status()) : Status.UNRECOGNIZED) .tags(input.tags().items()) .uri(input.selfLink()) .userMetadata(input.metadata().asMap()) .group(group) .privateAddresses(collectPrivateAddresses(input))
private List<String> collectPrivateAddresses(Instance input) { ImmutableList.Builder<String> privateAddressesBuilder = ImmutableList.builder(); for (Instance.NetworkInterface networkInterface : input.networkInterfaces()) { if (networkInterface.networkIP() != null) { privateAddressesBuilder.add(networkInterface.networkIP()); } } return privateAddressesBuilder.build(); }
AtomicReference<Instance> instance = Atomics.newReference(Instance.create( // "0000000000000000000", // id can't be null, but isn't available until provisioning is done. diskURIToImage.getUnchecked(instance.get().disks().get(0).source()); .build(); return new NodeAndInitialCredentials<Instance>(instance.get(), instance.get().selfLink().toString(), credentials);
private void assertInstanceEquals(Instance result, NewInstance expected) { assertEquals(result.name(), expected.name()); assertEquals(result.metadata().asMap(), expected.metadata().asMap()); // ignore fingerprint! // Do not assume tags come in the same order List<String> resultTags = Ordering.from(String.CASE_INSENSITIVE_ORDER).sortedCopy(result.tags().items()); List<String> expectedTags = Ordering.from(String.CASE_INSENSITIVE_ORDER).sortedCopy(expected.tags().items()); assertEquals(resultTags, expectedTags); }
logger.debug("Generating windows key for instance %s, by updating metadata", instance.get().name()); final InstanceApi instanceApi = api.instancesInZone(zone); Metadata metadata = instance.get().metadata(); AtomicReference<Operation> operation = Atomics.newReference(instanceApi.setMetadata(instance.get().name(), metadata)); operationDone.apply(operation);
@Override public boolean apply(Instance instance) { return Iterables.contains(selfLinks, instance.selfLink().toString()); } });
public boolean apply(Instance instance) { String serialPortContents = instanceApi.getSerialPortOutput(instance.name(), 4).contents(); if (!serialPortContents.startsWith("{\"ready\":true")) { return false; } String[] contentEntries = serialPortContents.split("\n"); passwordDict.clear(); passwordDict.putAll(new Gson().fromJson(contentEntries[contentEntries.length - 1], Map.class)); passwordDict.put("passwordDictContentEntries", contentEntries[contentEntries.length - 1]); return passwordDict.get("encryptedPassword") != null; } }, 10 * 60, 30, TimeUnit.SECONDS).apply(instance.get()); // Notice that timeoutDuration should be less than EXPIRE_DURATION
private boolean existsDiskWithNameAndAutoDelete(String instanceName, final String diskName, final boolean autoDelete){ Instance revertedInstance = api().get(instanceName); return Iterables.any(revertedInstance.disks(), new Predicate<AttachedDisk>() { @Override public boolean apply(AttachedDisk disk) { return disk.type() == AttachedDisk.Type.PERSISTENT && diskName.equals(disk.deviceName()) && disk.autoDelete() == autoDelete; } }); }
public void testCreatePreemptibleNodeWithSsd() throws Exception { String group = this.group + "ssd"; try { TemplateOptions options = client.templateOptions(); options.as(GoogleComputeEngineTemplateOptions.class).bootDiskType("pd-ssd").preemptible(true); // create a node Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, options); assertEquals(nodes.size(), 1, "One node should have been created"); // Verify the disk on the instance is an ssd. NodeMetadata node = Iterables.get(nodes, 0); GoogleComputeEngineApi api = client.getContext().unwrapApi(GoogleComputeEngineApi.class); Instance instance = api.instancesInZone(node.getLocation().getId()).get(node.getName()); Disk disk = api.disksInZone(node.getLocation().getId()).get(toName(instance.disks().get(0).source())); assertTrue(disk.type().toString().endsWith("pd-ssd")); assertTrue(instance.scheduling().preemptible()); } finally { client.destroyNodesMatching(inGroup(group)); } } /**
@Test(groups = "live", dependsOnMethods = "testStopInstance") public void testStartInstance() { Instance originalInstance = api().get(INSTANCE_NAME2); assertEquals(originalInstance.status(), Instance.Status.TERMINATED); assertOperationDoneSuccessfully(api().start(INSTANCE_NAME2)); originalInstance = api().get(INSTANCE_NAME2); assertEquals(originalInstance.status(), Instance.Status.RUNNING); }
@Test(groups = "live", dependsOnMethods = "testListInstance") public void testSetMetadataForInstance() { Instance originalInstance = api().get(INSTANCE_NAME); Metadata update = Metadata.create(originalInstance.metadata().fingerprint()) .put(METADATA_ITEM_KEY, METADATA_ITEM_VALUE); assertOperationDoneSuccessfully(api().setMetadata(INSTANCE_NAME, update)); Instance modifiedInstance = api().get(INSTANCE_NAME); assertTrue(modifiedInstance.metadata().containsKey(METADATA_ITEM_KEY)); assertEquals(modifiedInstance.metadata().get(METADATA_ITEM_KEY), METADATA_ITEM_VALUE); assertNotNull(modifiedInstance.metadata().fingerprint()); }
@Test(groups = "live", dependsOnMethods = "testListInstance") public void testSetTagsForInstance() { Instance originalInstance = api().get(INSTANCE_NAME); assertOperationDoneSuccessfully( api().setTags(INSTANCE_NAME, TAGS, originalInstance.tags().fingerprint())); Instance modifiedInstance = api().get(INSTANCE_NAME); assertTrue(modifiedInstance.tags().items().containsAll(TAGS)); assertNotNull(modifiedInstance.tags().fingerprint()); }
@Consumes(APPLICATION_JSON) public Instance expected(String baseUrl) { return Instance.create( // "13051190678907570425", // id parse("2012-11-25T23:48:20.758"), // creationTimestamp
AtomicReference<Instance> instance = Atomics.newReference(Instance.create( // "0000000000000000000", // id can't be null, but isn't available until provisioning is done. diskURIToImage.getUnchecked(instance.get().disks().get(0).source()); .build(); return new NodeAndInitialCredentials<Instance>(instance.get(), instance.get().selfLink().toString(), credentials);
logger.debug("Generating windows key for instance %s, by updating metadata", instance.get().name()); final InstanceApi instanceApi = api.instancesInZone(zone); Metadata metadata = instance.get().metadata(); AtomicReference<Operation> operation = Atomics.newReference(instanceApi.setMetadata(instance.get().name(), metadata)); operationDone.apply(operation);
@Override public boolean apply(Instance instance) { return Iterables.contains(selfLinks, instance.selfLink().toString()); } });
public boolean apply(Instance instance) { String serialPortContents = instanceApi.getSerialPortOutput(instance.name(), 4).contents(); if (!serialPortContents.startsWith("{\"ready\":true")) { return false; } String[] contentEntries = serialPortContents.split("\n"); passwordDict.clear(); passwordDict.putAll(new Gson().fromJson(contentEntries[contentEntries.length - 1], Map.class)); passwordDict.put("passwordDictContentEntries", contentEntries[contentEntries.length - 1]); return passwordDict.get("encryptedPassword") != null; } }, 10 * 60, 30, TimeUnit.SECONDS).apply(instance.get()); // Notice that timeoutDuration should be less than EXPIRE_DURATION
@Test(groups = "live", dependsOnMethods = "testAttachDiskToInstance", alwaysRun = true) public void testDetachDiskFromInstance() { Instance originalInstance = api().get(INSTANCE_NAME); assertOperationDoneSuccessfully(api().detachDisk(INSTANCE_NAME, ATTACH_DISK_DEVICE_NAME)); Instance modifiedInstance = api().get(INSTANCE_NAME); assertTrue(modifiedInstance.disks().size() < originalInstance.disks().size()); assertOperationDoneSuccessfully(diskApi().delete(ATTACH_DISK_NAME)); }
@Test(groups = "live", dependsOnMethods = "testGetInstance2") public void testStopInstance() { Instance originalInstance = api().get(INSTANCE_NAME2); assertEquals(originalInstance.status(), Instance.Status.RUNNING); assertOperationDoneSuccessfully(api().stop(INSTANCE_NAME2)); }
@Override public NodeMetadata apply(Instance input) { String group = groupFromMapOrName(input.metadata().asMap(), input.name(), nodeNamingConvention); NodeMetadataBuilder builder = new NodeMetadataBuilder(); Location zone = locationsByUri.get().get(input.zone()); if (zone == null) { throw new IllegalStateException( String.format("zone %s not present in %s", input.zone(), locationsByUri.get().keySet())); Image image = diskURIToImage.getUnchecked(input.disks().get(0).source()); if (isCustomMachineTypeURI(input.machineType())) { hardware = machineTypeURIToCustomHardware(input.machineType()); hardware = hardwares.get().get(input.machineType()); builder.id(input.selfLink().toString()) .name(input.name()) .providerId(input.id()) .hostname(input.name()) .location(zone) .imageId(image != null ? image.selfLink().toString() : null) .hardware(hardware) .status(input.status() != null ? toPortableNodeStatus.get(input.status()) : Status.UNRECOGNIZED) .tags(input.tags().items()) .uri(input.selfLink()) .userMetadata(input.metadata().asMap()) .group(group) .privateAddresses(collectPrivateAddresses(input))