/** * Deletes a bucket, even if non-empty. This method blocks until the deletion completes or fails. * * @param storage the storage service to be used to issue requests * @param bucket the bucket to be deleted * @throws StorageException if an exception is encountered during bucket deletion */ public static void forceDelete(Storage storage, String bucket) { new DeleteBucketTask(storage, bucket).call(); }
/** * Deletes a bucket, even if non-empty. Objects in the bucket are listed and deleted until bucket * deletion succeeds or {@code timeout} expires. To allow for the timeout, this method uses a * separate thread to send the delete requests. Use {@link #forceDelete(Storage storage, String * bucket)} if spawning an additional thread is undesirable, such as in the App Engine production * runtime. * * @param storage the storage service to be used to issue requests * @param bucket the bucket to be deleted * @param timeout the maximum time to wait * @param unit the time unit of the timeout argument * @return true if deletion succeeded, false if timeout expired * @throws InterruptedException if the thread deleting the bucket is interrupted while waiting * @throws ExecutionException if an exception was thrown while deleting bucket or bucket objects */ public static Boolean forceDelete(Storage storage, String bucket, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException { return forceDelete(storage, bucket, timeout, unit, ""); }
public static StorageHelperException translate(Exception ex) { return new StorageHelperException(ex.getMessage(), ex); } }
@BeforeClass public static void beforeClass() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { remoteStorageHelper = RemoteStorageHelper.create(); topicAdminClient = TopicAdminClient.create(); storageService = remoteStorageHelper.getOptions().getService(); notificationService = new DefaultNotificationFactory().create(remoteStorageHelper.getOptions().getService()); storageService.create(BucketInfo.of(BUCKET)); projectId = ServiceOptions.getDefaultProjectId(); }
@AfterClass public static void afterClass() throws ExecutionException, InterruptedException { if (storage != null) { // In beforeClass, we make buckets auto-delete blobs older than a day old. // Here, delete all buckets older than 2 days. They should already be empty and easy. long cleanTime = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2); long cleanTimeout = System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(1); RemoteStorageHelper.cleanBuckets(storage, cleanTime, cleanTimeout); boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 1, TimeUnit.MINUTES); if (!wasDeleted && log.isLoggable(Level.WARNING)) { log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET); } } }
@Test public void testGetServiceAccount() { String projectId = remoteStorageHelper.getOptions().getProjectId(); ServiceAccount serviceAccount = storage.getServiceAccount(projectId); assertNotNull(serviceAccount); assertTrue(serviceAccount.getEmail().endsWith(SERVICE_ACCOUNT_EMAIL_SUFFIX)); } }
.setCredentials(GoogleCredentials.fromStream(keyStream)) .setProjectId(projectId) .setRetrySettings(retrySettings()) .setTransportOptions(transportOptions) .build(); return new RemoteStorageHelper(storageOptions); } catch (IOException ex) { if (log.isLoggable(Level.WARNING)) { log.log(Level.WARNING, ex.getMessage()); throw StorageHelperException.translate(ex);
private static void assertNoMatch(Policy expected, Policy actual) { assertFalse(new ApiPolicyMatcher(expected).matches(actual)); }
@Test public void testEquivalence() { assertMatch(API_POLICY_1, API_POLICY_2); assertMatch(API_POLICY_2, API_POLICY_1); assertNoMatch(API_POLICY_1, API_POLICY_MISSING_BINDINGS); assertNoMatch(API_POLICY_MISSING_BINDINGS, API_POLICY_1); assertNoMatch(API_POLICY_1, API_POLICY_MISSING_ETAG); assertNoMatch(API_POLICY_MISSING_ETAG, API_POLICY_1); assertNoMatch(API_POLICY_MISSING_BINDINGS, API_POLICY_MISSING_ETAG); assertNoMatch(API_POLICY_MISSING_ETAG, API_POLICY_MISSING_BINDINGS); }
@Test public void testStaticMocker() { PolicyAcceptor mock = EasyMock.createMock(PolicyAcceptor.class); EasyMock.expect(mock.accept(eqApiPolicy(API_POLICY_1))).andReturn(0); EasyMock.replay(mock); mock.accept(API_POLICY_2); EasyMock.verify(mock); } }
/** * Creates a {@code RemoteStorageHelper} object using default project id and authentication * credentials. */ public static RemoteStorageHelper create() throws StorageHelperException { HttpTransportOptions transportOptions = StorageOptions.getDefaultHttpTransportOptions(); transportOptions = transportOptions.toBuilder().setConnectTimeout(60000).setReadTimeout(60000).build(); StorageOptions storageOptions = StorageOptions.newBuilder() .setRetrySettings(retrySettings()) .setTransportOptions(transportOptions) .build(); return new RemoteStorageHelper(storageOptions); }
/** * Deletes a bucket, even if non-empty. Objects in the bucket are listed and deleted until bucket * deletion succeeds or {@code timeout} expires. To allow for the timeout, this method uses a * separate thread to send the delete requests. Use {@link #forceDelete(Storage storage, String * bucket)} if spawning an additional thread is undesirable, such as in the App Engine production * runtime. * * @param storage the storage service to be used to issue requests * @param bucket the bucket to be deleted * @param timeout the maximum time to wait * @param unit the time unit of the timeout argument * @param userProject the project to bill for requester-pays buckets (or "") * @return true if deletion succeeded, false if timeout expired * @throws InterruptedException if the thread deleting the bucket is interrupted while waiting * @throws ExecutionException if an exception was thrown while deleting bucket or bucket objects */ public static Boolean forceDelete( Storage storage, String bucket, long timeout, TimeUnit unit, String userProject) throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newSingleThreadExecutor(); Future<Boolean> future = executor.submit(new DeleteBucketTask(storage, bucket, userProject)); try { return future.get(timeout, unit); } catch (TimeoutException ex) { return false; } finally { executor.shutdown(); } }
public static Policy eqApiPolicy(Policy in) { EasyMock.reportMatcher(new ApiPolicyMatcher(in)); return null; }
public ApiPolicyMatcher(Policy expected) { expectedBindings = toMap(expected.getBindings()); expectedEtag = expected.getEtag(); }
@BeforeClass public static void beforeClass() throws IOException { // loads the credentials from local disk as par README RemoteStorageHelper gcsHelper = RemoteStorageHelper.create(); storageOptions = gcsHelper.getOptions(); project = storageOptions.getProjectId(); storage = storageOptions.getService(); // create and populate test bucket storage.create(BucketInfo.of(BUCKET)); fillFile(storage, BUCKET, SML_FILE, SML_SIZE); fillFile(storage, BUCKET, BIG_FILE, BIG_SIZE); BucketInfo requesterPaysBucket = BucketInfo.newBuilder(REQUESTER_PAYS_BUCKET).setRequesterPays(true).build(); storage.create(requesterPaysBucket); fillRequesterPaysFile(storage, SML_FILE, SML_SIZE); }
@AfterClass public static void afterClass() throws ExecutionException, InterruptedException { if (storage != null) { for (String bucket : new String[] {BUCKET, REQUESTER_PAYS_BUCKET}) { if (!RemoteStorageHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS, project) && log.isLoggable(Level.WARNING)) { log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", bucket); } } } }
private static void assertMatch(Policy expected, Policy actual) { assertTrue(new ApiPolicyMatcher(expected).matches(actual)); }
@Test public void testCreateFromStream() { RemoteStorageHelper helper = RemoteStorageHelper.create(PROJECT_ID, JSON_KEY_STREAM); StorageOptions options = helper.getOptions(); assertEquals(PROJECT_ID, options.getProjectId()); assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getConnectTimeout()); assertEquals(60000, ((HttpTransportOptions) options.getTransportOptions()).getReadTimeout()); assertEquals(10, options.getRetrySettings().getMaxAttempts()); assertEquals(Duration.ofMillis(30000), options.getRetrySettings().getMaxRetryDelay()); assertEquals(Duration.ofMillis(120000), options.getRetrySettings().getTotalTimeout()); assertEquals(Duration.ofMillis(250), options.getRetrySettings().getInitialRetryDelay()); } }
@AfterClass public static void afterClass() throws ExecutionException, InterruptedException { if (bigquery != null) { RemoteBigQueryHelper.forceDelete(bigquery, DATASET); } if (storage != null) { boolean wasDeleted = RemoteStorageHelper.forceDelete(storage, BUCKET, 10, TimeUnit.SECONDS); if (!wasDeleted && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET); } } }
@AfterClass public static void afterClass() throws Exception { topicAdminClient.close(); if (notificationService != null) { boolean wasDeleted = RemoteStorageHelper.forceDelete(storageService, BUCKET, 5, TimeUnit.SECONDS); if (!wasDeleted && log.isLoggable(Level.WARNING)) { log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET); } } }