private static ComputeException translate(IOException exception) { return new ComputeException(exception); }
/** * This method returns {@code null} if the error code of {@code exception} was 404, re-throws the * exception otherwise. * * @throws ComputeException if the error code of {@code exception} was not 404 */ private static <T> T nullForNotFound(IOException exception) { ComputeException serviceException = translate(exception); if (serviceException.getCode() == HTTP_NOT_FOUND) { return null; } throw serviceException; }
@Override public Operation deleteNetwork(final NetworkId network, OperationOption... options) { final Map<ComputeRpc.Option, ?> optionsMap = optionMap(options); try { com.google.api.services.compute.model.Operation answer = runWithRetries( new Callable<com.google.api.services.compute.model.Operation>() { @Override public com.google.api.services.compute.model.Operation call() { return computeRpc.deleteNetwork(network.getNetwork(), optionsMap); } }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } }
private static Page<Disk> listDisks( final ComputeOptions serviceOptions, final Map<ComputeRpc.Option, ?> optionsMap) { try { Tuple<String, Iterable<com.google.api.services.compute.model.Disk>> result = runWithRetries( new Callable<Tuple<String, Iterable<com.google.api.services.compute.model.Disk>>>() { @Override public Tuple<String, Iterable<com.google.api.services.compute.model.Disk>> call() { return serviceOptions.getComputeRpcV1().listDisks(optionsMap); } }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<Disk> disks = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Disk>of() : result.y(), diskFromPb(serviceOptions)); return new PageImpl<>( new AggregatedDiskPageFetcher(serviceOptions, cursor, optionsMap), cursor, disks); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } }
try { Tuple<String, Iterable<com.google.api.services.compute.model.Operation>> result = runWithRetries( new Callable< Tuple<String, Iterable<com.google.api.services.compute.model.Operation>>>() { serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<Operation> operations = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Operation>of() : result.y(), operationFromPb(serviceOptions)); return new PageImpl<>( new RegionOperationPageFetcher(region, serviceOptions, cursor, optionsMap), operations); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e);
try { Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>> result = runWithRetries( new Callable< Tuple<String, Iterable<com.google.api.services.compute.model.DiskType>>>() { serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<DiskType> diskTypes = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.DiskType>of() : result.y(), new Function<com.google.api.services.compute.model.DiskType, DiskType>() { @Override new DiskTypePageFetcher(zone, serviceOptions, cursor, optionsMap), cursor, diskTypes); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e);
private static Page<Disk> listDisks( final String zone, final ComputeOptions serviceOptions, final Map<ComputeRpc.Option, ?> optionsMap) { try { Tuple<String, Iterable<com.google.api.services.compute.model.Disk>> result = runWithRetries( new Callable<Tuple<String, Iterable<com.google.api.services.compute.model.Disk>>>() { @Override public Tuple<String, Iterable<com.google.api.services.compute.model.Disk>> call() { return serviceOptions.getComputeRpcV1().listDisks(zone, optionsMap); } }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<Disk> disks = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Disk>of() : result.y(), diskFromPb(serviceOptions)); return new PageImpl<>( new DiskPageFetcher(zone, serviceOptions, cursor, optionsMap), cursor, disks); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } }
try { Tuple<String, Iterable<com.google.api.services.compute.model.Instance>> result = runWithRetries( new Callable< Tuple<String, Iterable<com.google.api.services.compute.model.Instance>>>() { serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<Instance> instances = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Instance>of() : result.y(), instanceFromPb(serviceOptions)); return new PageImpl<>( new InstancePageFetcher(zone, serviceOptions, cursor, optionsMap), cursor, instances); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e);
try { Tuple<String, Iterable<com.google.api.services.compute.model.Subnetwork>> result = runWithRetries( new Callable< Tuple<String, Iterable<com.google.api.services.compute.model.Subnetwork>>>() { serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<Subnetwork> subnetworks = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Subnetwork>of() : result.y(), subnetworkFromPb(serviceOptions)); return new PageImpl<>( new SubnetworkPageFetcher(region, serviceOptions, cursor, optionsMap), subnetworks); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e);
try { Tuple<String, Iterable<com.google.api.services.compute.model.Image>> result = runWithRetries( new Callable<Tuple<String, Iterable<com.google.api.services.compute.model.Image>>>() { @Override serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<Image> images = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Image>of() : result.y(), new Function<com.google.api.services.compute.model.Image, Image>() { @Override new ImagePageFetcher(project, serviceOptions, cursor, optionsMap), cursor, images); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e);
try { Tuple<String, Iterable<com.google.api.services.compute.model.Address>> result = runWithRetries( new Callable< Tuple<String, Iterable<com.google.api.services.compute.model.Address>>>() { serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<Address> operations = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Address>of() : result.y(), addressFromPb(serviceOptions)); return new PageImpl<>( new RegionAddressPageFetcher(region, serviceOptions, cursor, optionsMap), operations); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e);
try { Operation operation = RetryHelper.poll( new Callable<Operation>() { @Override options.getClock()); return operation == null ? null : reload(); } catch (ExecutionException e) { throw ComputeException.translateAndThrow(e);
@Test public void testResourceManagerException() { ComputeException exception = new ComputeException(500, "message"); assertEquals(500, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertTrue(exception.isRetryable()); exception = new ComputeException(403, "message"); assertEquals(403, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertFalse(exception.isRetryable()); IOException cause = new SocketTimeoutException(); exception = new ComputeException(cause); assertNull(exception.getReason()); assertNull(exception.getMessage()); assertTrue(exception.isRetryable()); assertSame(cause, exception.getCause()); exception = new ComputeException(403, "message", cause); assertEquals(403, exception.getCode()); assertEquals("message", exception.getMessage()); assertNull(exception.getReason()); assertFalse(exception.isRetryable()); assertSame(cause, exception.getCause()); }
@Test public void testRetryableException() { EasyMock.expect( computeRpcMock.getDiskType( DISK_TYPE_ID.getZone(), DISK_TYPE_ID.getType(), EMPTY_RPC_OPTIONS)) .andThrow(new ComputeException(500, "InternalError")) .andReturn(DISK_TYPE.toPb()); EasyMock.replay(computeRpcMock); compute = options .toBuilder() .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build() .getService(); DiskType diskType = compute.getDiskType(DISK_TYPE_ID); assertEquals(DISK_TYPE, diskType); }
@Test public void testTranslateAndThrow() throws Exception { Exception cause = new ComputeException(500, "message"); RetryHelperException exceptionMock = createMock(RetryHelperException.class); expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { ComputeException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(500, ex.getCode()); replay(exceptionMock); try { ComputeException.translateAndThrow(exceptionMock); } catch (BaseServiceException ex) { assertEquals(ComputeException.UNKNOWN_CODE, ex.getCode());
private static Page<Operation> listGlobalOperations( final ComputeOptions serviceOptions, final Map<ComputeRpc.Option, ?> optionsMap) { try { Tuple<String, Iterable<com.google.api.services.compute.model.Operation>> result = runWithRetries( new Callable< Tuple<String, Iterable<com.google.api.services.compute.model.Operation>>>() { @Override public Tuple<String, Iterable<com.google.api.services.compute.model.Operation>> call() { return serviceOptions.getComputeRpcV1().listGlobalOperations(optionsMap); } }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<Operation> operations = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Operation>of() : result.y(), operationFromPb(serviceOptions)); return new PageImpl<>( new GlobalOperationPageFetcher(serviceOptions, cursor, optionsMap), cursor, operations); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } }
try { Tuple<String, Iterable<com.google.api.services.compute.model.Operation>> result = runWithRetries( new Callable< Tuple<String, Iterable<com.google.api.services.compute.model.Operation>>>() { serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<Operation> operations = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Operation>of() : result.y(), operationFromPb(serviceOptions)); return new PageImpl<>( new ZoneOperationPageFetcher(zone, serviceOptions, cursor, optionsMap), operations); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e);
try { Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>> result = runWithRetries( new Callable< Tuple<String, Iterable<com.google.api.services.compute.model.MachineType>>>() { serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<MachineType> machineTypes = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.MachineType>of() : result.y(), new Function<com.google.api.services.compute.model.MachineType, MachineType>() { @Override machineTypes); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e);
@Test public void testNonRetryableException() { String exceptionMessage = "Not Implemented"; EasyMock.expect( computeRpcMock.getDiskType( DISK_TYPE_ID.getZone(), DISK_TYPE_ID.getType(), EMPTY_RPC_OPTIONS)) .andThrow(new ComputeException(501, exceptionMessage)); EasyMock.replay(computeRpcMock); compute = options .toBuilder() .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build() .getService(); thrown.expect(ComputeException.class); thrown.expectMessage(exceptionMessage); compute.getDiskType(DISK_TYPE_ID); }
private static Page<Address> listGlobalAddresses( final ComputeOptions serviceOptions, final Map<ComputeRpc.Option, ?> optionsMap) { try { Tuple<String, Iterable<com.google.api.services.compute.model.Address>> result = runWithRetries( new Callable< Tuple<String, Iterable<com.google.api.services.compute.model.Address>>>() { @Override public Tuple<String, Iterable<com.google.api.services.compute.model.Address>> call() { return serviceOptions.getComputeRpcV1().listGlobalAddresses(optionsMap); } }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable<Address> operations = Iterables.transform( result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Address>of() : result.y(), addressFromPb(serviceOptions)); return new PageImpl<>( new GlobalAddressPageFetcher(serviceOptions, cursor, optionsMap), cursor, operations); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } }