use of com.microsoft.azure.keyvault.SecretIdentifier in project azure-sdk-for-java by Azure.
the class SecretOperationsTest method listSecrets.
@Test
public void listSecrets() throws Exception {
HashSet<String> secrets = new HashSet<String>();
for (int i = 0; i < MAX_SECRETS; ++i) {
int failureCount = 0;
for (; ; ) {
try {
SecretBundle secret = keyVaultClient.setSecret(new SetSecretRequest.Builder(getVaultUri(), SECRET_NAME + i, SECRET_VALUE).build());
SecretIdentifier id = new SecretIdentifier(secret.id());
secrets.add(id.baseIdentifier());
break;
} catch (KeyVaultErrorException e) {
++failureCount;
if (e.body().error().code().equals("Throttled")) {
System.out.println("Waiting to avoid throttling");
Thread.sleep(failureCount * 1500);
continue;
}
throw e;
}
}
}
PagedList<SecretItem> listResult = keyVaultClient.listSecrets(getVaultUri(), PAGELIST_MAX_SECRETS);
Assert.assertTrue(PAGELIST_MAX_SECRETS >= listResult.currentPage().items().size());
HashSet<String> toDelete = new HashSet<String>();
for (SecretItem item : listResult) {
if (item != null) {
SecretIdentifier id = new SecretIdentifier(item.id());
toDelete.add(id.name());
secrets.remove(item.id());
}
}
Assert.assertEquals(0, secrets.size());
for (String secretName : toDelete) {
try {
keyVaultClient.deleteSecret(getVaultUri(), secretName);
} catch (KeyVaultErrorException e) {
// Ignore forbidden exception for certificate secrets that cannot be deleted
if (!e.body().error().code().equals("Forbidden"))
throw e;
}
}
}
Aggregations