public AsyncCreateResponse build() { return new AsyncCreateResponse(id, jobId); }
@Override public String toString() { return string().toString(); }
@Override public boolean deleteImage(String id) { try { AsyncCreateResponse deleteJob = client.getTemplateApi().deleteTemplate(id); jobComplete.apply(deleteJob.getJobId()); return true; } catch (Exception e) { return false; } }
public void testApply() { String id = "1"; String jobId = "2"; CloudStackClient client = createMock(CloudStackClient.class); Predicate<String> jobComplete = Predicates.alwaysTrue(); AsyncJobClient jobClient = createMock(AsyncJobClient.class); expect(client.getAsyncJobClient()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn(AsyncJob.builder().id(jobId).result("foo").build()).atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply(AsyncCreateResponse.builder().id(id).jobId( jobId).build()), "foo"); verify(client); verify(jobClient); }
/** * * @param job * @return result of the job's execution * @throws ExecutionException * if the job contained an error */ public <T> T apply(AsyncCreateResponse job) { boolean completed = jobComplete.apply(job.getJobId()); logger.trace("<< job(%s) complete(%s)", job, completed); AsyncJob<T> jobWithResult = client.getAsyncJobClient().<T> getAsyncJob(job.getJobId()); checkState(completed, "job %s failed to complete in time %s", job.getJobId(), jobWithResult); if (jobWithResult.getError() != null) throw new UncheckedExecutionException(String.format("job %s failed with exception %s", job.getJobId(), jobWithResult.getError().toString())) { }; return jobWithResult.getResult(); } }
public void testApply() { String id = "1"; String jobId = "2"; CloudStackApi client = createMock(CloudStackApi.class); Predicate<String> jobComplete = Predicates.alwaysTrue(); AsyncJobApi jobClient = createMock(AsyncJobApi.class); expect(client.getAsyncJobApi()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn(AsyncJob.builder().id(jobId).result("foo").build()).atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply(AsyncCreateResponse.builder().id(id).jobId( jobId).build()), "foo"); verify(client); verify(jobClient); }
/** * * @param job * @return result of the job's execution * @throws ExecutionException * if the job contained an error */ public <T> T apply(AsyncCreateResponse job) { boolean completed = jobComplete.apply(job.getJobId()); logger.trace("<< job(%s) complete(%s)", job, completed); AsyncJob<T> jobWithResult = client.getAsyncJobClient().<T> getAsyncJob(job.getJobId()); checkState(completed, "job %s failed to complete in time %s", job.getJobId(), jobWithResult); if (jobWithResult.getError() != null) throw new UncheckedExecutionException(String.format("job %s failed with exception %s", job.getJobId(), jobWithResult.getError().toString())) { }; return jobWithResult.getResult(); } }
@Test(expectedExceptions = IllegalStateException.class) public void testJobDoesntCompleteThrowsIllegalStateException() { String id = "1"; String jobId = "2"; CloudStackClient client = createMock(CloudStackClient.class); // the alwaysfalse predicate should blow up with IllegalStateException Predicate<String> jobComplete = Predicates.alwaysFalse(); AsyncJobClient jobClient = createMock(AsyncJobClient.class); expect(client.getAsyncJobClient()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn(AsyncJob.builder().id(jobId).result("foo").build()).atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply( AsyncCreateResponse.builder().id(id).jobId(jobId).build()), "foo"); verify(client); verify(jobClient); }
@Override public String toString() { return string().toString(); }
public AsyncCreateResponse build() { return new AsyncCreateResponse(id, jobId); }
/** * * @param job * @return result of the job's execution * @throws ExecutionException * if the job contained an error */ public <T> T apply(AsyncCreateResponse job) { boolean completed = jobComplete.apply(job.getJobId()); logger.trace("<< job(%s) complete(%s)", job, completed); AsyncJob<T> jobWithResult = client.getAsyncJobApi().<T> getAsyncJob(job.getJobId()); checkState(completed, "job %s failed to complete in time %s", job.getJobId(), jobWithResult); if (jobWithResult.getError() != null) throw new UncheckedExecutionException(String.format("job %s failed with exception %s", job.getJobId(), jobWithResult.getError().toString())) { }; return jobWithResult.getResult(); } }
public void testAssociateWorks() throws SecurityException, NoSuchMethodException { // create mocks CloudStackClient client = createMock(CloudStackClient.class); BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult = createMock(BlockUntilJobCompletesAndReturnResult.class); AddressClient addressClient = createMock(AddressClient.class); expect(client.getAddressClient()).andReturn(addressClient).atLeastOnce(); // no ip addresses available expect(addressClient.listPublicIPAddresses(allocatedOnly(true).networkId(networkId))).andReturn( ImmutableSet.<PublicIPAddress> of()); AsyncCreateResponse job = AsyncCreateResponse.builder().id("1").jobId("2").build(); // make sure we created the job relating to a new ip expect(addressClient.associateIPAddressInZone(zoneId, networkId(networkId))).andReturn(job); expect(blockUntilJobCompletesAndReturnResult.apply(job)).andReturn(address); replay(client); replay(addressClient); replay(blockUntilJobCompletesAndReturnResult); assertEquals( new ReuseOrAssociateNewPublicIPAddress(client, blockUntilJobCompletesAndReturnResult).apply(Network .builder().id(networkId).zoneId(zoneId).build()), address); verify(client); verify(addressClient); verify(blockUntilJobCompletesAndReturnResult); }
@Override public String toString() { return string().toString(); }
public AsyncCreateResponse build() { return new AsyncCreateResponse(id, jobId); }
protected void createVolumeToTag() { AsyncCreateResponse job = client.getVolumeApi().createVolumeFromDiskOfferingInZone(prefix + "-jclouds-volume", getPreferredDiskOffering().getId(), zoneId); assertTrue(jobComplete.apply(job.getJobId())); logger.info("created volume " + job.getId()); volumeToDelete = job.getId(); }
protected boolean systemOpenFirewall(String publicIpId, Cidr cidr, int lowerBoundPort, int upperBoundPort, FirewallRule.Protocol protocol) { try { boolean success = true; CreateFirewallRuleOptions options = CreateFirewallRuleOptions.Builder. startPort(lowerBoundPort).endPort(upperBoundPort).CIDRs(ImmutableSet.of(cidr.toString())); AsyncCreateResponse job = cloudstackClient.getCloudstackGlobalClient().getFirewallApi().createFirewallRuleForIpAndProtocol( publicIpId, protocol, options); success &= cloudstackClient.waitForJobsSuccess(Arrays.asList(job.getJobId())); if (!success) { log.error("Failed creating firewall rule on "+this+" to "+publicIpId+":"+lowerBoundPort+"-"+upperBoundPort); // it might already be created, so don't crash and burn too hard! return false; } } catch (Exception e) { log.error("Failed creating firewall rule on "+this+" to "+publicIpId+":"+lowerBoundPort+"-"+upperBoundPort); // it might already be created, so don't crash and burn too hard! return false; } return true; }
@Test(expectedExceptions = IllegalStateException.class) public void testJobDoesntCompleteThrowsIllegalStateException() { String id = "1"; String jobId = "2"; CloudStackApi client = createMock(CloudStackApi.class); // the alwaysfalse predicate should blow up with IllegalStateException Predicate<String> jobComplete = Predicates.alwaysFalse(); AsyncJobApi jobClient = createMock(AsyncJobApi.class); expect(client.getAsyncJobApi()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn(AsyncJob.builder().id(jobId).result("foo").build()).atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply( AsyncCreateResponse.builder().id(id).jobId(jobId).build()), "foo"); verify(client); verify(jobClient); }