TerremarkVCloudComputeClient { public ComputeServiceAdapter.NodeAndInitialCredentials<VApp> startAndReturnCredentials(@Nullable URI VDC, URI templateId, String name, InstantiateVAppTemplateOptions options, int... portsToOpen) { if (portsToOpen.length > 0 && !options.shouldBlock()) throw new IllegalArgumentException("We cannot open ports on terremark unless we can deploy the vapp"); String password = null; VAppTemplate template = client.getVAppTemplate(templateId); if (template.getDescription().indexOf("Windows") != -1) { password = passwordGenerator.get(); options.getProperties().put("password", password); } checkNotNull(options, "options"); logger.debug(">> instantiating vApp vDC(%s) template(%s) name(%s) options(%s) ", VDC, templateId, name, options); VApp vAppResponse = client.instantiateVAppTemplateInVDC(VDC, templateId, name, options); logger.debug("<< instantiated VApp(%s)", vAppResponse.getName()); if (options.shouldDeploy()) { logger.debug(">> deploying vApp(%s)", vAppResponse.getName()); Task task = client.deployVApp(vAppResponse.getHref()); if (options.shouldBlock()) { if (!taskTester.apply(task.getHref())) { throw new RuntimeException(String.format("failed to %s %s: %s", "deploy", vAppResponse.getName(), task)); } logger.debug("<< deployed vApp(%s)", vAppResponse.getName()); if (options.shouldPowerOn()) { logger.debug(">> powering vApp(%s)", vAppResponse.getName()); task = client.powerOnVApp(vAppResponse.getHref()); if (!taskTester.apply(task.getHref())) { throw new RuntimeException(String.format("failed to %s %s: %s", "powerOn", vAppResponse.getName(), task)); } logger.debug("<< on vApp(%s)", vAppResponse.getName()); } } } if (portsToOpen.length > 0) createPublicAddressMappedToPorts(vAppResponse.getHref(), portsToOpen); return new ComputeServiceAdapter.NodeAndInitialCredentials<VApp>(vAppResponse, vAppResponse.getHref().toASCIIString(), password!= null?LoginCredentials.builder().password(password).build():null); } @Inject protected  TerremarkVCloudComputeClient(TerremarkVCloudClient client,
         @Named("PASSWORD") Provider<String> passwordGenerator, Predicate<URI> successTester,
         Map<Status, NodeMetadata.Status> vAppStatusToNodeStatus, Map<String, Credentials> credentialStore,
         InternetServiceAndPublicIpAddressSupplier internetServiceAndPublicIpAddressSupplier); ComputeServiceAdapter.NodeAndInitialCredentials<VApp> startAndReturnCredentials(@Nullable URI VDC, URI templateId, String name, InstantiateVAppTemplateOptions options,
            int... portsToOpen); VApp start(@Nullable URI VDC, URI templateId, String name, InstantiateVAppTemplateOptions options,
            int... portsToOpen); String createPublicAddressMappedToPorts(URI vAppId, int... ports); void stop(URI id); Set<String> getPrivateAddresses(URI id); Set<String> getPublicAddresses(URI id); void reset(URI id);  }
@SuppressWarnings("unchecked") @Test public void testStartWindows() throws IOException { Map<String, Credentials> credentialStore = Maps.newHashMap(); InputStream is = getClass().getResourceAsStream("/windows_description.txt"); String description = new String(ByteStreams.toByteArray(is)); VAppTemplate template = createMock(VAppTemplate.class); VDC vdc = createMock(VDC.class); URI templateURI = URI.create("template"); URI vdcURI = URI.create("vdc"); expect(template.getDescription()).andReturn(description).atLeastOnce(); TerremarkVCloudClient client = createMock(TerremarkVCloudClient.class); VApp vApp = createMock(VApp.class); InternetServiceAndPublicIpAddressSupplier supplier = createMock(InternetServiceAndPublicIpAddressSupplier.class); expect(client.getVAppTemplate(templateURI)).andReturn(template); expect( client.instantiateVAppTemplateInVDC(vdcURI, templateURI, "name", new InstantiateVAppTemplateOptions().productProperty("password", "password"))).andReturn(vApp); Task task = createMock(Task.class); URI vappLocation = URI.create("vapp"); URI taskLocation = URI.create("task"); expect(vApp.getHref()).andReturn(vappLocation).atLeastOnce(); expect(vApp.getName()).andReturn("name").atLeastOnce(); expect(client.deployVApp(vappLocation)).andReturn(task); expect(task.getHref()).andReturn(taskLocation).atLeastOnce(); Predicate<URI> successTester = createMock(Predicate.class); expect(successTester.apply(taskLocation)).andReturn(true).atLeastOnce(); expect(client.powerOnVApp(vappLocation)).andReturn(task); Predicate<VApp> notFoundTester = createMock(Predicate.class); Map<Status, NodeMetadata.Status> vAppStatusToNodeStatus = createMock(Map.class); TerremarkVCloudComputeClient computeClient = new TerremarkVCloudComputeClient(client, new Provider<String>() { @Override public String get() { return "password"; } }, successTester, vAppStatusToNodeStatus, credentialStore, supplier); replay(vdc); replay(template); replay(vApp); replay(task); replay(client); replay(successTester); replay(notFoundTester); replay(vAppStatusToNodeStatus); NodeAndInitialCredentials<VApp> response = computeClient.startAndReturnCredentials(vdcURI, templateURI, "name", new InstantiateVAppTemplateOptions()); assertEquals(response.getNodeId(), "vapp"); assertEquals(response.getNode(),vApp); assertEquals(response.getCredentials(), LoginCredentials.builder().password("password").build()); verify(vdc); verify(template); verify(vApp); verify(task); verify(client); verify(successTester); verify(notFoundTester); verify(vAppStatusToNodeStatus); }