From fa17e70a210b0b7b9d67a63c7979d6b1d765f6a6 Mon Sep 17 00:00:00 2001 From: Srikanta <51379715+srnagar@users.noreply.github.com> Date: Mon, 15 Jul 2019 16:21:24 -0700 Subject: [PATCH] Keyvault context for async clients (#4430) * Support context for ConfigurationAsyncClient APIs * Enable reactor context for KeyVault Keys and Secrets async client * Fix formatting * Remove unused import * Add missing annotations --- .../keyvault/keys/KeyAsyncClient.java | 122 ++++++++++-------- .../keyvault/secrets/SecretAsyncClient.java | 93 +++++++------ 2 files changed, 125 insertions(+), 90 deletions(-) diff --git a/sdk/keyvault/azure-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/KeyAsyncClient.java b/sdk/keyvault/azure-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/KeyAsyncClient.java index f1e5d5057d5f0..b8b4ad5c4816c 100644 --- a/sdk/keyvault/azure-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/KeyAsyncClient.java +++ b/sdk/keyvault/azure-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/KeyAsyncClient.java @@ -13,7 +13,9 @@ import com.azure.core.http.rest.PagedResponse; import com.azure.core.http.rest.PagedFlux; import com.azure.core.implementation.RestProxy; +import com.azure.core.implementation.annotation.ReturnType; import com.azure.core.implementation.annotation.ServiceClient; +import com.azure.core.implementation.annotation.ServiceMethod; import com.azure.core.util.logging.ClientLogger; import com.azure.security.keyvault.keys.models.DeletedKey; import com.azure.security.keyvault.keys.models.EcKeyCreateOptions; @@ -34,6 +36,8 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import static com.azure.core.implementation.util.FluxUtil.monoContext; + /** * The KeyAsyncClient provides asynchronous methods to manage {@link Key keys} in the Azure Key Vault. The client * supports creating, retrieving, updating, deleting, purging, backing up, restoring and listing the {@link Key keys}. The client @@ -91,12 +95,13 @@ public final class KeyAsyncClient { * @throws HttpRequestException if {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Key created key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> createKey(String name, KeyType keyType) { KeyRequestParameters parameters = new KeyRequestParameters().kty(keyType); - return service.createKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.createKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Creating key - {}", name)) .doOnSuccess(response -> logger.info("Created key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to create key - {}", name, error)); + .doOnError(error -> logger.warning("Failed to create key - {}", name, error))); } /** @@ -127,16 +132,17 @@ public Mono> createKey(String name, KeyType keyType) { * @throws HttpRequestException if {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Key created key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> createKey(KeyCreateOptions keyCreateOptions) { Objects.requireNonNull(keyCreateOptions, "The key options parameter cannot be null."); KeyRequestParameters parameters = new KeyRequestParameters() .kty(keyCreateOptions.keyType()) .keyOps(keyCreateOptions.keyOperations()) .keyAttributes(new KeyRequestAttributes(keyCreateOptions)); - return service.createKey(endpoint, keyCreateOptions.name(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.createKey(endpoint, keyCreateOptions.name(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Creating key - {}", keyCreateOptions.name())) .doOnSuccess(response -> logger.info("Created key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to create key - {}", keyCreateOptions.name(), error)); + .doOnError(error -> logger.warning("Failed to create key - {}", keyCreateOptions.name(), error))); } /** @@ -169,6 +175,7 @@ public Mono> createKey(KeyCreateOptions keyCreateOptions) { * @throws HttpRequestException if {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Key created key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> createRsaKey(RsaKeyCreateOptions rsaKeyCreateOptions) { Objects.requireNonNull(rsaKeyCreateOptions, "The Rsa key options parameter cannot be null."); KeyRequestParameters parameters = new KeyRequestParameters() @@ -176,10 +183,10 @@ public Mono> createRsaKey(RsaKeyCreateOptions rsaKeyCreateOptions) .keySize(rsaKeyCreateOptions.keySize()) .keyOps(rsaKeyCreateOptions.keyOperations()) .keyAttributes(new KeyRequestAttributes(rsaKeyCreateOptions)); - return service.createKey(endpoint, rsaKeyCreateOptions.name(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.createKey(endpoint, rsaKeyCreateOptions.name(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Creating Rsa key - {}", rsaKeyCreateOptions.name())) .doOnSuccess(response -> logger.info("Created Rsa key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to create Rsa key - {}", rsaKeyCreateOptions.name(), error)); + .doOnError(error -> logger.warning("Failed to create Rsa key - {}", rsaKeyCreateOptions.name(), error))); } /** @@ -212,6 +219,7 @@ public Mono> createRsaKey(RsaKeyCreateOptions rsaKeyCreateOptions) * @throws HttpRequestException if {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Key created key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> createEcKey(EcKeyCreateOptions ecKeyCreateOptions) { Objects.requireNonNull(ecKeyCreateOptions, "The Ec key options options cannot be null."); KeyRequestParameters parameters = new KeyRequestParameters() @@ -219,10 +227,10 @@ public Mono> createEcKey(EcKeyCreateOptions ecKeyCreateOptions) { .curve(ecKeyCreateOptions.curve()) .keyOps(ecKeyCreateOptions.keyOperations()) .keyAttributes(new KeyRequestAttributes(ecKeyCreateOptions)); - return service.createKey(endpoint, ecKeyCreateOptions.name(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.createKey(endpoint, ecKeyCreateOptions.name(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Creating Ec key - {}", ecKeyCreateOptions.name())) .doOnSuccess(response -> logger.info("Created Ec key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to create Ec key - {}", ecKeyCreateOptions.name(), error)); + .doOnError(error -> logger.warning("Failed to create Ec key - {}", ecKeyCreateOptions.name(), error))); } /** @@ -242,12 +250,13 @@ public Mono> createEcKey(EcKeyCreateOptions ecKeyCreateOptions) { * @throws HttpRequestException if {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Key imported key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> importKey(String name, JsonWebKey keyMaterial) { KeyImportRequestParameters parameters = new KeyImportRequestParameters().key(keyMaterial); - return service.importKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.importKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Importing key - {}", name)) .doOnSuccess(response -> logger.info("Imported key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to import key - {}", name, error)); + .doOnError(error -> logger.warning("Failed to import key - {}", name, error))); } /** @@ -276,16 +285,17 @@ public Mono> importKey(String name, JsonWebKey keyMaterial) { * @throws HttpRequestException if {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Key imported key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> importKey(KeyImportOptions keyImportOptions) { Objects.requireNonNull(keyImportOptions, "The key import configuration parameter cannot be null."); KeyImportRequestParameters parameters = new KeyImportRequestParameters() .key(keyImportOptions.keyMaterial()) .hsm(keyImportOptions.hsm()) .keyAttributes(new KeyRequestAttributes(keyImportOptions)); - return service.importKey(endpoint, keyImportOptions.name(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.importKey(endpoint, keyImportOptions.name(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Importing key - {}", keyImportOptions.name())) .doOnSuccess(response -> logger.info("Imported key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to import key - {}", keyImportOptions.name(), error)); + .doOnError(error -> logger.warning("Failed to import key - {}", keyImportOptions.name(), error))); } /** @@ -307,15 +317,15 @@ public Mono> importKey(KeyImportOptions keyImportOptions) { * @throws HttpRequestException if {@code name} or {@code version} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the requested {@link Key key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> getKey(String name, String version) { - String keyVersion = ""; - if (version != null) { - keyVersion = version; + if (version == null) { + return getKey(name); } - return service.getKey(endpoint, name, keyVersion, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.getKey(endpoint, name, version, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Retrieving key - {}", name)) .doOnSuccess(response -> logger.info("Retrieved key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to get key - {}", name, error)); + .doOnError(error -> logger.warning("Failed to get key - {}", name, error))); } /** @@ -336,11 +346,12 @@ public Mono> getKey(String name, String version) { * @throws HttpRequestException if {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the requested {@link Key key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> getKey(String name) { - return getKey(name, "") + return monoContext(context -> getKey(name, "") .doOnRequest(ignored -> logger.info("Retrieving key - {}", name)) .doOnSuccess(response -> logger.info("Retrieved key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to get key - {}", name, error)); + .doOnError(error -> logger.warning("Failed to get key - {}", name, error))); } @@ -362,16 +373,14 @@ public Mono> getKey(String name) { * @throws HttpRequestException if {@link KeyBase#name()} name} or {@link KeyBase#version() version} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the requested {@link Key key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> getKey(KeyBase keyBase) { Objects.requireNonNull(keyBase, "The Key Base parameter cannot be null."); - String keyVersion = ""; - if (keyBase.version() != null) { - keyVersion = keyBase.version(); + if (keyBase.version() == null) { + return getKey(keyBase.name()); } - return service.getKey(endpoint, keyBase.name(), keyVersion, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) - .doOnRequest(ignored -> logger.info("Retrieving key - {}", keyBase.name())) - .doOnSuccess(response -> logger.info("Retrieved key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to get key - {}", keyBase.name(), error)); + + return getKey(keyBase.name(), keyBase.version()); } /** @@ -398,16 +407,17 @@ public Mono> getKey(KeyBase keyBase) { * @throws HttpRequestException if {@link KeyBase#name() name} or {@link KeyBase#version() version} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link KeyBase updated key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> updateKey(KeyBase key) { Objects.requireNonNull(key, "The key input parameter cannot be null."); KeyRequestParameters parameters = new KeyRequestParameters() .tags(key.tags()) .keyAttributes(new KeyRequestAttributes(key)); - return service.updateKey(endpoint, key.name(), key.version(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.updateKey(endpoint, key.name(), key.version(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Updating key - {}", key.name())) .doOnSuccess(response -> logger.info("Updated key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to update key - {}", key.name(), error)); + .doOnError(error -> logger.warning("Failed to update key - {}", key.name(), error))); } /** @@ -435,6 +445,7 @@ public Mono> updateKey(KeyBase key) { * @throws HttpRequestException if {@link KeyBase#name() name} or {@link KeyBase#version() version} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link KeyBase updated key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> updateKey(KeyBase key, KeyOperation... keyOperations) { Objects.requireNonNull(key, "The key input parameter cannot be null."); KeyRequestParameters parameters = new KeyRequestParameters() @@ -442,10 +453,10 @@ public Mono> updateKey(KeyBase key, KeyOperation... keyOperations) .keyOps(Arrays.asList(keyOperations)) .keyAttributes(new KeyRequestAttributes(key)); - return service.updateKey(endpoint, key.name(), key.version(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.updateKey(endpoint, key.name(), key.version(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Updating key - {}", key.name())) .doOnSuccess(response -> logger.info("Updated key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to update key - {}", key.name(), error)); + .doOnError(error -> logger.warning("Failed to update key - {}", key.name(), error))); } /** @@ -468,11 +479,12 @@ public Mono> updateKey(KeyBase key, KeyOperation... keyOperations) * @throws HttpRequestException when a key with {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link DeletedKey deleted key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> deleteKey(String name) { - return service.deleteKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.deleteKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Deleting key - {}", name)) .doOnSuccess(response -> logger.info("Deleted key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to delete key - {}", name, error)); + .doOnError(error -> logger.warning("Failed to delete key - {}", name, error))); } /** @@ -493,11 +505,12 @@ public Mono> deleteKey(String name) { * @throws HttpRequestException when a key with {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link DeletedKey deleted key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> getDeletedKey(String name) { - return service.getDeletedKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.getDeletedKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Retrieving deleted key - {}", name)) .doOnSuccess(response -> logger.info("Retrieved deleted key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to get key - {}", name, error)); + .doOnError(error -> logger.warning("Failed to get key - {}", name, error))); } /** @@ -518,11 +531,12 @@ public Mono> getDeletedKey(String name) { * @throws HttpRequestException when a key with {@code name} is empty string. * @return A {@link Mono} containing a {@link VoidResponse}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono purgeDeletedKey(String name) { - return service.purgeDeletedKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.purgeDeletedKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Purging deleted key - {}", name)) .doOnSuccess(response -> logger.info("Purged deleted key - {}", name)) - .doOnError(error -> logger.warning("Failed to purge deleted key - {}", name, error)); + .doOnError(error -> logger.warning("Failed to purge deleted key - {}", name, error))); } /** @@ -544,11 +558,12 @@ public Mono purgeDeletedKey(String name) { * @throws HttpRequestException when a key with {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Key recovered key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> recoverDeletedKey(String name) { - return service.recoverDeletedKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.recoverDeletedKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Recovering deleted key - {}", name)) .doOnSuccess(response -> logger.info("Recovered deleted key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to recover deleted key - {}", name, error)); + .doOnError(error -> logger.warning("Failed to recover deleted key - {}", name, error))); } /** @@ -574,13 +589,14 @@ public Mono> recoverDeletedKey(String name) { * @throws HttpRequestException when a key with {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the backed up key blob. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> backupKey(String name) { - return service.backupKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.backupKey(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Backing up key - {}", name)) .doOnSuccess(response -> logger.info("Backed up key - {}", name)) .doOnError(error -> logger.warning("Failed to backup key - {}", name, error)) .flatMap(base64URLResponse -> Mono.just(new SimpleResponse(base64URLResponse.request(), - base64URLResponse.statusCode(), base64URLResponse.headers(), base64URLResponse.value().value()))); + base64URLResponse.statusCode(), base64URLResponse.headers(), base64URLResponse.value().value())))); } /** @@ -605,12 +621,13 @@ public Mono> backupKey(String name) { * @throws ResourceModifiedException when {@code backup} blob is malformed. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Key restored key}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> restoreKey(byte[] backup) { KeyRestoreRequestParameters parameters = new KeyRestoreRequestParameters().keyBackup(backup); - return service.restoreKey(endpoint, API_VERSION, parameters, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.restoreKey(endpoint, API_VERSION, parameters, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Attempting to restore key")) .doOnSuccess(response -> logger.info("Restored Key - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to restore key - {}", error)); + .doOnError(error -> logger.warning("Failed to restore key - {}", error))); } /** @@ -628,10 +645,11 @@ public Mono> restoreKey(byte[] backup) { * * @return A {@link PagedFlux} containing {@link KeyBase key} of all the keys in the vault. */ + @ServiceMethod(returns = ReturnType.COLLECTION) public PagedFlux listKeys() { - return new PagedFlux<>(() -> - listKeysFirstPage(), - continuationToken -> listKeysNextPage(continuationToken)); + return new PagedFlux<>( + () -> monoContext(context -> listKeysFirstPage()), + continuationToken -> monoContext(context -> listKeysNextPage(continuationToken))); } /* @@ -673,10 +691,11 @@ private Mono> listKeysFirstPage() { * * @return A {@link PagedFlux} containing all of the {@link DeletedKey deleted keys} in the vault. */ + @ServiceMethod(returns = ReturnType.COLLECTION) public PagedFlux listDeletedKeys() { - return new PagedFlux<>(() -> - listDeletedKeysFirstPage(), - continuationToken -> listDeletedKeysNextPage(continuationToken)); + return new PagedFlux<>( + () -> monoContext(context -> listDeletedKeysFirstPage()), + continuationToken -> monoContext(context -> listDeletedKeysNextPage(continuationToken))); } /* @@ -722,10 +741,11 @@ private Mono> listDeletedKeysFirstPage() { * @throws HttpRequestException when a key with {@code name} is empty string. * @return A {@link PagedFlux} containing {@link KeyBase key} of all the versions of the specified key in the vault. Flux is empty if key with {@code name} does not exist in key vault. */ + @ServiceMethod(returns = ReturnType.COLLECTION) public PagedFlux listKeyVersions(String name) { - return new PagedFlux<>(() -> - listKeyVersionsFirstPage(name), - continuationToken -> listKeyVersionsNextPage(continuationToken)); + return new PagedFlux<>( + () -> monoContext(context -> listKeyVersionsFirstPage(name)), + continuationToken -> monoContext(context -> listKeyVersionsNextPage(continuationToken))); } private Mono> listKeyVersionsFirstPage(String name) { diff --git a/sdk/keyvault/azure-keyvault-secrets/src/main/java/com/azure/security/keyvault/secrets/SecretAsyncClient.java b/sdk/keyvault/azure-keyvault-secrets/src/main/java/com/azure/security/keyvault/secrets/SecretAsyncClient.java index d7f491e520303..8adfceac47ffe 100644 --- a/sdk/keyvault/azure-keyvault-secrets/src/main/java/com/azure/security/keyvault/secrets/SecretAsyncClient.java +++ b/sdk/keyvault/azure-keyvault-secrets/src/main/java/com/azure/security/keyvault/secrets/SecretAsyncClient.java @@ -10,7 +10,9 @@ import com.azure.core.http.rest.PagedFlux; import com.azure.core.http.rest.SimpleResponse; import com.azure.core.implementation.RestProxy; +import com.azure.core.implementation.annotation.ReturnType; import com.azure.core.implementation.annotation.ServiceClient; +import com.azure.core.implementation.annotation.ServiceMethod; import com.azure.core.util.Context; import com.azure.core.util.logging.ClientLogger; import com.azure.security.keyvault.secrets.models.DeletedSecret; @@ -26,6 +28,8 @@ import com.azure.core.exception.ResourceModifiedException; import com.azure.core.exception.HttpRequestException; +import static com.azure.core.implementation.util.FluxUtil.monoContext; + /** * The SecretAsyncClient provides asynchronous methods to manage {@link Secret secrets} in the Azure Key Vault. The client * supports creating, retrieving, updating, deleting, purging, backing up, restoring and listing the {@link Secret secrets}. The client @@ -87,6 +91,7 @@ public final class SecretAsyncClient { * @throws HttpRequestException if {@link Secret#name() name} or {@link Secret#value() value} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Secret created secret}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> setSecret(Secret secret) { Objects.requireNonNull(secret, "The Secret input parameter cannot be null."); SecretRequestParameters parameters = new SecretRequestParameters() @@ -95,10 +100,10 @@ public Mono> setSecret(Secret secret) { .contentType(secret.contentType()) .secretAttributes(new SecretRequestAttributes(secret)); - return service.setSecret(endpoint, secret.name(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.setSecret(endpoint, secret.name(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Setting secret - {}", secret.name())) .doOnSuccess(response -> logger.info("Set secret - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to set secret - {}", secret.name(), error)); + .doOnError(error -> logger.warning("Failed to set secret - {}", secret.name(), error))); } /** @@ -119,12 +124,13 @@ public Mono> setSecret(Secret secret) { * @throws HttpRequestException if {@code name} or {@code value} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Secret created secret}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> setSecret(String name, String value) { SecretRequestParameters parameters = new SecretRequestParameters().value(value); - return service.setSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.setSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Setting secret - {}", name)) .doOnSuccess(response -> logger.info("Set secret - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to set secret - {}", name, error)); + .doOnError(error -> logger.warning("Failed to set secret - {}", name, error))); } /** @@ -147,15 +153,16 @@ public Mono> setSecret(String name, String value) { * @throws HttpRequestException if {@code name} name} or {@code version} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the requested {@link Secret secret}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> getSecret(String name, String version) { - String secretVersion = ""; - if (version != null) { - secretVersion = version; + if (version == null) { + return getSecret(name); } - return service.getSecret(endpoint, name, secretVersion, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + + return monoContext(context -> service.getSecret(endpoint, name, version, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignoredValue -> logger.info("Retrieving secret - {}", name)) .doOnSuccess(response -> logger.info("Retrieved secret - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to get secret - {}", name, error)); + .doOnError(error -> logger.warning("Failed to get secret - {}", name, error))); } /** @@ -176,16 +183,13 @@ public Mono> getSecret(String name, String version) { * @throws HttpRequestException if {@link SecretBase#name()} name} or {@link SecretBase#version() version} is empty string. * @return A {@link Response} whose {@link Response#value() value} contains the requested {@link Secret secret}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> getSecret(SecretBase secretBase) { Objects.requireNonNull(secretBase, "The Secret Base parameter cannot be null."); - String secretVersion = ""; - if (secretBase.version() != null) { - secretVersion = secretBase.version(); + if (secretBase.version() == null) { + return getSecret(secretBase.name()); } - return service.getSecret(endpoint, secretBase.name(), secretVersion, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) - .doOnRequest(ignoredValue -> logger.info("Retrieving secret - {}", secretBase.name())) - .doOnSuccess(response -> logger.info("Retrieved secret - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to get secret - {}", secretBase.name(), error)); + return getSecret(secretBase.name(), secretBase.version()); } /** * Get the latest version of the specified secret from the key vault. The get operation is applicable to any secret stored in Azure Key Vault. @@ -205,6 +209,7 @@ public Mono> getSecret(SecretBase secretBase) { * @throws HttpRequestException if {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the requested {@link Secret secret}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> getSecret(String name) { return getSecret(name, ""); } @@ -234,6 +239,7 @@ public Mono> getSecret(String name) { * @throws HttpRequestException if {@link SecretBase#name()} name} or {@link SecretBase#version() version} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link SecretBase updated secret}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> updateSecret(SecretBase secret) { Objects.requireNonNull(secret, "The secret input parameter cannot be null."); SecretRequestParameters parameters = new SecretRequestParameters() @@ -241,10 +247,10 @@ public Mono> updateSecret(SecretBase secret) { .contentType(secret.contentType()) .secretAttributes(new SecretRequestAttributes(secret)); - return service.updateSecret(endpoint, secret.name(), secret.version(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.updateSecret(endpoint, secret.name(), secret.version(), API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Updating secret - {}", secret.name())) .doOnSuccess(response -> logger.info("Updated secret - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to update secret - {}", secret.name(), error)); + .doOnError(error -> logger.warning("Failed to update secret - {}", secret.name(), error))); } /** @@ -265,11 +271,12 @@ public Mono> updateSecret(SecretBase secret) { * @throws HttpRequestException when a secret with {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link DeletedSecret deleted secret}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> deleteSecret(String name) { - return service.deleteSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.deleteSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Deleting secret - {}", name)) .doOnSuccess(response -> logger.info("Deleted secret - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to delete secret - {}", name, error)); + .doOnError(error -> logger.warning("Failed to delete secret - {}", name, error))); } /** @@ -290,11 +297,12 @@ public Mono> deleteSecret(String name) { * @throws HttpRequestException when a secret with {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link DeletedSecret deleted secret}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> getDeletedSecret(String name) { - return service.getDeletedSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.getDeletedSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Retrieving deleted secret - {}", name)) .doOnSuccess(response -> logger.info("Retrieved deleted secret - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to retrieve deleted secret - {}", name, error)); + .doOnError(error -> logger.warning("Failed to retrieve deleted secret - {}", name, error))); } /** @@ -315,11 +323,12 @@ public Mono> getDeletedSecret(String name) { * @throws HttpRequestException when a secret with {@code name} is empty string. * @return A {@link Mono} containing a {@link VoidResponse}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono purgeDeletedSecret(String name) { - return service.purgeDeletedSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.purgeDeletedSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Purging deleted secret - {}", name)) .doOnSuccess(response -> logger.info("Purged deleted secret - {}", name)) - .doOnError(error -> logger.warning("Failed to purge deleted secret - {}", name, error)); + .doOnError(error -> logger.warning("Failed to purge deleted secret - {}", name, error))); } /** @@ -340,11 +349,12 @@ public Mono purgeDeletedSecret(String name) { * @throws HttpRequestException when a secret with {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Secret recovered secret}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> recoverDeletedSecret(String name) { - return service.recoverDeletedSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.recoverDeletedSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Recovering deleted secret - {}", name)) .doOnSuccess(response -> logger.info("Recovered deleted secret - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to recover deleted secret - {}", name, error)); + .doOnError(error -> logger.warning("Failed to recover deleted secret - {}", name, error))); } /** @@ -364,13 +374,14 @@ public Mono> recoverDeletedSecret(String name) { * @throws HttpRequestException when a secret with {@code name} is empty string. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the backed up secret blob. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> backupSecret(String name) { - return service.backupSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.backupSecret(endpoint, name, API_VERSION, ACCEPT_LANGUAGE, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Backing up secret - {}", name)) .doOnSuccess(response -> logger.info("Backed up secret - {}", name)) .doOnError(error -> logger.warning("Failed to back up secret - {}", name, error)) .flatMap(base64URLResponse -> Mono.just(new SimpleResponse(base64URLResponse.request(), - base64URLResponse.statusCode(), base64URLResponse.headers(), base64URLResponse.value().value()))); + base64URLResponse.statusCode(), base64URLResponse.headers(), base64URLResponse.value().value())))); } /** @@ -389,12 +400,13 @@ public Mono> backupSecret(String name) { * @throws ResourceModifiedException when {@code backup} blob is malformed. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the {@link Secret restored secret}. */ + @ServiceMethod(returns = ReturnType.SINGLE) public Mono> restoreSecret(byte[] backup) { SecretRestoreRequestParameters parameters = new SecretRestoreRequestParameters().secretBackup(backup); - return service.restoreSecret(endpoint, API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) + return monoContext(context -> service.restoreSecret(endpoint, API_VERSION, ACCEPT_LANGUAGE, parameters, CONTENT_TYPE_HEADER_VALUE) .doOnRequest(ignored -> logger.info("Attempting to restore secret")) .doOnSuccess(response -> logger.info("Restored secret - {}", response.value().name())) - .doOnError(error -> logger.warning("Failed to restore secret", error)); + .doOnError(error -> logger.warning("Failed to restore secret", error))); } /** @@ -412,10 +424,11 @@ public Mono> restoreSecret(byte[] backup) { * * @return A {@link PagedFlux} containing {@link SecretBase secret} of all the secrets in the vault. */ + @ServiceMethod(returns = ReturnType.COLLECTION) public PagedFlux listSecrets() { - return new PagedFlux<>(() -> - listSecretsFirstPage(), - continuationToken -> listSecretsNextPage(continuationToken)); + return new PagedFlux<>( + () -> monoContext(context -> listSecretsFirstPage()), + continuationToken -> monoContext(context -> listSecretsNextPage(continuationToken))); } /* @@ -456,10 +469,11 @@ private Mono> listSecretsFirstPage() { * * @return A {@link Flux} containing all of the {@link DeletedSecret deleted secrets} in the vault. */ + @ServiceMethod(returns = ReturnType.COLLECTION) public PagedFlux listDeletedSecrets() { - return new PagedFlux<>(() -> - listDeletedSecretsFirstPage(), - continuationToken -> listDeletedSecretsNextPage(continuationToken, Context.NONE)); + return new PagedFlux<>( + () -> monoContext(context -> listDeletedSecretsFirstPage()), + continuationToken -> monoContext(context -> listDeletedSecretsNextPage(continuationToken, Context.NONE))); } /** @@ -505,10 +519,11 @@ private Mono> listDeletedSecretsFirstPage() { * @throws HttpRequestException when a secret with {@code name} is empty string. * @return A {@link PagedFlux} containing {@link SecretBase secret} of all the versions of the specified secret in the vault. Flux is empty if secret with {@code name} does not exist in key vault */ + @ServiceMethod(returns = ReturnType.COLLECTION) public PagedFlux listSecretVersions(String name) { - return new PagedFlux<>(() -> - listSecretVersionsFirstPage(name), - continuationToken -> listSecretVersionsNextPage(continuationToken)); + return new PagedFlux<>( + () -> monoContext(context -> listSecretVersionsFirstPage(name)), + continuationToken -> monoContext(context -> listSecretVersionsNextPage(continuationToken))); } /*