Skip to content

KV v2 logging improvements #45418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Code generated by Microsoft (R) TypeSpec Code Generator.
<dependency>
<groupId>io.clientcore</groupId>
<artifactId>core</artifactId>
<version>1.0.0-beta.9</version> <!-- {x-version-update;io.clientcore:core;dependency} -->
<version>1.0.0-beta.10</version> <!-- {x-version-update;unreleased_io.clientcore:core;dependency} -->
</dependency>
<dependency>
<groupId>com.azure.v2</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Objects;
import java.util.UUID;

import static com.azure.v2.security.keyvault.administration.KeyVaultAdministrationUtil.CANNOT_BE_NULL;
import static com.azure.v2.security.keyvault.administration.KeyVaultAdministrationUtil.mapPages;
import static com.azure.v2.security.keyvault.administration.KeyVaultAdministrationUtil.mapResponse;
import static com.azure.v2.security.keyvault.administration.KeyVaultAdministrationUtil.roleAssignmentToKeyVaultRoleAssignment;
Expand Down Expand Up @@ -279,18 +278,12 @@ public PagedIterable<KeyVaultRoleDefinition> listRoleDefinitions(KeyVaultRoleSco
public PagedIterable<KeyVaultRoleDefinition> listRoleDefinitions(KeyVaultRoleScope roleScope,
RequestContext requestContext) {

try {
Objects.requireNonNull(roleScope, String.format(CANNOT_BE_NULL, "'roleScope'"));

return mapPages(
pagingOptions -> clientImpl.getRoleDefinitions()
.listSinglePage(roleScope.getValue(), null, requestContext),
(pagingOptions, nextLink) -> clientImpl.getRoleDefinitions()
.listNextSinglePage(nextLink, requestContext),
KeyVaultAdministrationUtil::roleDefinitionToKeyVaultRoleDefinition);
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
Objects.requireNonNull(roleScope, "'roleScope' cannot be null.");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the String.format(CANNOT_BE_NULL, "'roleScope'") happens on every operation and there is really no need to do it


return mapPages(
pagingOptions -> clientImpl.getRoleDefinitions().listSinglePage(roleScope.getValue(), null, requestContext),
(pagingOptions, nextLink) -> clientImpl.getRoleDefinitions().listNextSinglePage(nextLink, requestContext),
KeyVaultAdministrationUtil::roleDefinitionToKeyVaultRoleDefinition);
}

/**
Expand Down Expand Up @@ -349,15 +342,11 @@ public KeyVaultRoleDefinition setRoleDefinition(KeyVaultRoleScope roleScope) {
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public KeyVaultRoleDefinition setRoleDefinition(KeyVaultRoleScope roleScope, String roleDefinitionName) {
try {
Objects.requireNonNull(roleScope, String.format(CANNOT_BE_NULL, "'roleScope'"));

return roleDefinitionToKeyVaultRoleDefinition(clientImpl.getRoleDefinitions()
.createOrUpdate(roleScope.toString(),
isNullOrEmpty(roleDefinitionName) ? UUID.randomUUID().toString() : roleDefinitionName, null));
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
Objects.requireNonNull(roleScope, "'roleScope' cannot be null.");

return roleDefinitionToKeyVaultRoleDefinition(clientImpl.getRoleDefinitions()
.createOrUpdate(roleScope.toString(),
isNullOrEmpty(roleDefinitionName) ? UUID.randomUUID().toString() : roleDefinitionName, null));
}

/**
Expand Down Expand Up @@ -423,15 +412,11 @@ public KeyVaultRoleDefinition setRoleDefinition(KeyVaultRoleScope roleScope, Str
public Response<KeyVaultRoleDefinition> setRoleDefinitionWithResponse(SetRoleDefinitionOptions options,
RequestContext requestContext) {

try {
return mapResponse(
clientImpl.getRoleDefinitions()
.createOrUpdateWithResponse(options.getRoleScope().toString(), options.getRoleDefinitionName(),
validateAndGetRoleDefinitionCreateParameters(options), requestContext),
KeyVaultAdministrationUtil::roleDefinitionToKeyVaultRoleDefinition);
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
return mapResponse(
clientImpl.getRoleDefinitions()
.createOrUpdateWithResponse(options.getRoleScope().toString(), options.getRoleDefinitionName(),
validateAndGetRoleDefinitionCreateParameters(options, LOGGER), requestContext),
KeyVaultAdministrationUtil::roleDefinitionToKeyVaultRoleDefinition);
}

/**
Expand Down Expand Up @@ -464,14 +449,10 @@ public Response<KeyVaultRoleDefinition> setRoleDefinitionWithResponse(SetRoleDef
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public KeyVaultRoleDefinition getRoleDefinition(KeyVaultRoleScope roleScope, String roleDefinitionName) {
try {
validateRoleDefinitionParameters(roleScope, roleDefinitionName);
validateRoleDefinitionParameters(roleScope, roleDefinitionName, LOGGER);

return roleDefinitionToKeyVaultRoleDefinition(
clientImpl.getRoleDefinitions().get(roleScope.toString(), roleDefinitionName));
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
return roleDefinitionToKeyVaultRoleDefinition(
clientImpl.getRoleDefinitions().get(roleScope.toString(), roleDefinitionName));
}

/**
Expand Down Expand Up @@ -514,16 +495,11 @@ public KeyVaultRoleDefinition getRoleDefinition(KeyVaultRoleScope roleScope, Str
public Response<KeyVaultRoleDefinition> getRoleDefinitionWithResponse(KeyVaultRoleScope roleScope,
String roleDefinitionName, RequestContext requestContext) {

try {
validateRoleDefinitionParameters(roleScope, roleDefinitionName);
validateRoleDefinitionParameters(roleScope, roleDefinitionName, LOGGER);

return mapResponse(
clientImpl.getRoleDefinitions()
.getWithResponse(roleScope.toString(), roleDefinitionName, requestContext),
KeyVaultAdministrationUtil::roleDefinitionToKeyVaultRoleDefinition);
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
return mapResponse(
clientImpl.getRoleDefinitions().getWithResponse(roleScope.toString(), roleDefinitionName, requestContext),
KeyVaultAdministrationUtil::roleDefinitionToKeyVaultRoleDefinition);
}

/**
Expand Down Expand Up @@ -552,13 +528,9 @@ public Response<KeyVaultRoleDefinition> getRoleDefinitionWithResponse(KeyVaultRo
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public void deleteRoleDefinition(KeyVaultRoleScope roleScope, String roleDefinitionName) {
try {
validateRoleDefinitionParameters(roleScope, roleDefinitionName);
validateRoleDefinitionParameters(roleScope, roleDefinitionName, LOGGER);

clientImpl.getRoleDefinitions().delete(roleScope.toString(), roleDefinitionName);
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
clientImpl.getRoleDefinitions().delete(roleScope.toString(), roleDefinitionName);
}

/**
Expand Down Expand Up @@ -600,15 +572,11 @@ public Response<Void> deleteRoleDefinitionWithResponse(KeyVaultRoleScope roleSco
try (Response<RoleDefinition> response = clientImpl.getRoleDefinitions()
.deleteWithResponse(roleScope.toString(), roleDefinitionName, requestContext)) {

validateRoleDefinitionParameters(roleScope, roleDefinitionName);
validateRoleDefinitionParameters(roleScope, roleDefinitionName, LOGGER);

return new Response<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), null);
} catch (RuntimeException e) {
if (e instanceof HttpResponseException) {
return swallowExceptionForStatusCode(404, (HttpResponseException) e, LOGGER);
}

throw LOGGER.logThrowableAsError(e);
} catch (HttpResponseException e) {
return swallowExceptionForStatusCode(404, e);
}
}

Expand Down Expand Up @@ -675,18 +643,14 @@ public PagedIterable<KeyVaultRoleAssignment> listRoleAssignments(KeyVaultRoleSco
public PagedIterable<KeyVaultRoleAssignment> listRoleAssignments(KeyVaultRoleScope roleScope,
RequestContext requestContext) {

try {
Objects.requireNonNull(roleScope, String.format(CANNOT_BE_NULL, "'roleScope'"));

return mapPages(
pagingOptions -> clientImpl.getRoleAssignments()
.listForScopeSinglePage(roleScope.toString(), null, requestContext),
(pagingOptions, nextLink) -> clientImpl.getRoleAssignments()
.listForScopeNextSinglePage(nextLink, requestContext),
KeyVaultAdministrationUtil::roleAssignmentToKeyVaultRoleAssignment);
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
Objects.requireNonNull(roleScope, "'roleScope' cannot be null.");

return mapPages(
pagingOptions -> clientImpl.getRoleAssignments()
.listForScopeSinglePage(roleScope.toString(), null, requestContext),
(pagingOptions, nextLink) -> clientImpl.getRoleAssignments()
.listForScopeNextSinglePage(nextLink, requestContext),
KeyVaultAdministrationUtil::roleAssignmentToKeyVaultRoleAssignment);
}

/**
Expand Down Expand Up @@ -769,16 +733,12 @@ public KeyVaultRoleAssignment createRoleAssignment(KeyVaultRoleScope roleScope,
public KeyVaultRoleAssignment createRoleAssignment(KeyVaultRoleScope roleScope, String roleDefinitionId,
String principalId, String roleAssignmentName) {

try {
RoleAssignmentCreateParameters parameters
= validateAndGetRoleAssignmentCreateParameters(roleScope, roleDefinitionId, principalId,
isNullOrEmpty(roleAssignmentName) ? UUID.randomUUID().toString() : roleAssignmentName);
RoleAssignmentCreateParameters parameters
= validateAndGetRoleAssignmentCreateParameters(roleScope, roleDefinitionId, principalId,
isNullOrEmpty(roleAssignmentName) ? UUID.randomUUID().toString() : roleAssignmentName, LOGGER);

return roleAssignmentToKeyVaultRoleAssignment(
clientImpl.getRoleAssignments().create(roleScope.toString(), roleAssignmentName, parameters));
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
return roleAssignmentToKeyVaultRoleAssignment(
clientImpl.getRoleAssignments().create(roleScope.toString(), roleAssignmentName, parameters));
}

/**
Expand Down Expand Up @@ -828,17 +788,13 @@ public KeyVaultRoleAssignment createRoleAssignment(KeyVaultRoleScope roleScope,
public Response<KeyVaultRoleAssignment> createRoleAssignmentWithResponse(KeyVaultRoleScope roleScope,
String roleDefinitionId, String principalId, String roleAssignmentName, RequestContext requestContext) {

try {
RoleAssignmentCreateParameters parameters = validateAndGetRoleAssignmentCreateParameters(roleScope,
roleDefinitionId, principalId, roleAssignmentName);
RoleAssignmentCreateParameters parameters = validateAndGetRoleAssignmentCreateParameters(roleScope,
roleDefinitionId, principalId, roleAssignmentName, LOGGER);

return mapResponse(
clientImpl.getRoleAssignments()
.createWithResponse(roleScope.toString(), roleAssignmentName, parameters, requestContext),
KeyVaultAdministrationUtil::roleAssignmentToKeyVaultRoleAssignment);
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
return mapResponse(
clientImpl.getRoleAssignments()
.createWithResponse(roleScope.toString(), roleAssignmentName, parameters, requestContext),
KeyVaultAdministrationUtil::roleAssignmentToKeyVaultRoleAssignment);
}

/**
Expand Down Expand Up @@ -869,14 +825,10 @@ public Response<KeyVaultRoleAssignment> createRoleAssignmentWithResponse(KeyVaul
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public KeyVaultRoleAssignment getRoleAssignment(KeyVaultRoleScope roleScope, String roleAssignmentName) {
try {
validateRoleAssignmentParameters(roleScope, roleAssignmentName);
validateRoleAssignmentParameters(roleScope, roleAssignmentName, LOGGER);

return roleAssignmentToKeyVaultRoleAssignment(
clientImpl.getRoleAssignments().get(roleScope.toString(), roleAssignmentName));
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
return roleAssignmentToKeyVaultRoleAssignment(
clientImpl.getRoleAssignments().get(roleScope.toString(), roleAssignmentName));
}

/**
Expand Down Expand Up @@ -917,16 +869,11 @@ public KeyVaultRoleAssignment getRoleAssignment(KeyVaultRoleScope roleScope, Str
public Response<KeyVaultRoleAssignment> getRoleAssignmentWithResponse(KeyVaultRoleScope roleScope,
String roleAssignmentName, RequestContext requestContext) {

try {
validateRoleAssignmentParameters(roleScope, roleAssignmentName);
validateRoleAssignmentParameters(roleScope, roleAssignmentName, LOGGER);

return mapResponse(
clientImpl.getRoleAssignments()
.getWithResponse(roleScope.toString(), roleAssignmentName, requestContext),
KeyVaultAdministrationUtil::roleAssignmentToKeyVaultRoleAssignment);
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
return mapResponse(
clientImpl.getRoleAssignments().getWithResponse(roleScope.toString(), roleAssignmentName, requestContext),
KeyVaultAdministrationUtil::roleAssignmentToKeyVaultRoleAssignment);
}

/**
Expand Down Expand Up @@ -954,13 +901,9 @@ public Response<KeyVaultRoleAssignment> getRoleAssignmentWithResponse(KeyVaultRo
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public void deleteRoleAssignment(KeyVaultRoleScope roleScope, String roleAssignmentName) {
try {
validateRoleAssignmentParameters(roleScope, roleAssignmentName);
validateRoleAssignmentParameters(roleScope, roleAssignmentName, LOGGER);

clientImpl.getRoleAssignments().delete(roleScope.toString(), roleAssignmentName);
} catch (RuntimeException e) {
throw LOGGER.logThrowableAsError(e);
}
clientImpl.getRoleAssignments().delete(roleScope.toString(), roleAssignmentName);
}

/**
Expand Down Expand Up @@ -1001,15 +944,11 @@ public Response<Void> deleteRoleAssignmentWithResponse(KeyVaultRoleScope roleSco
try (Response<RoleAssignment> response = clientImpl.getRoleAssignments()
.deleteWithResponse(roleScope.toString(), roleAssignmentName, requestContext)) {

validateRoleAssignmentParameters(roleScope, roleAssignmentName);
validateRoleAssignmentParameters(roleScope, roleAssignmentName, LOGGER);

return new Response<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), null);
} catch (RuntimeException e) {
if (e instanceof HttpResponseException) {
return swallowExceptionForStatusCode(404, (HttpResponseException) e, LOGGER);
}

throw LOGGER.logThrowableAsError(e);
} catch (HttpResponseException e) {
return swallowExceptionForStatusCode(404, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static io.clientcore.core.utils.CoreUtils.isNullOrEmpty;

Expand Down Expand Up @@ -122,19 +123,19 @@ public KeyVaultAccessControlClient buildClient() {
String endpoint = getEndpoint(configuration);

if (endpoint == null) {
throw LOGGER.logThrowableAsError(new IllegalStateException(
"An Azure Key Vault or Managed HSM endpoint is required. You can set one by using the"
throw LOGGER.throwableAtError()
.log("An Azure Key Vault or Managed HSM endpoint is required. You can set one by using the"
+ " KeyVaultAccessControlClientBuilder.endpoint() method or by setting the environment variable"
+ " 'AZURE_KEYVAULT_ENDPOINT'."));
+ " 'AZURE_KEYVAULT_ENDPOINT'.", IllegalStateException::new);
}

KeyVaultAdministrationServiceVersion version
= this.version == null ? KeyVaultAdministrationServiceVersion.getLatest() : this.version;

if (credential == null) {
throw LOGGER.logThrowableAsError(
new IllegalStateException("A credential object is required. You can set one by using the"
+ " KeyVaultAccessControlClientBuilder.credential() method."));
throw LOGGER.throwableAtError()
.log("A credential object is required. You can set one by using the"
+ " KeyVaultAccessControlClientBuilder.credential() method.", IllegalStateException::new);
}

// Closest to API goes first, closest to wire goes last.
Expand Down Expand Up @@ -179,16 +180,13 @@ public KeyVaultAccessControlClient buildClient() {
*/
@Override
public KeyVaultAccessControlClientBuilder endpoint(String endpoint) {
if (endpoint == null) {
throw LOGGER.logThrowableAsError(new NullPointerException("'endpoint' cannot be null."));
}

Objects.requireNonNull(endpoint, "'endpoint' cannot be null.");
try {
URI uri = new URI(endpoint);
this.endpoint = uri.toString();
} catch (URISyntaxException e) {
throw LOGGER
.logThrowableAsError(new IllegalArgumentException("The Azure Key Vault endpoint is malformed.", e));
throw LOGGER.throwableAtError()
.log("The Azure Key Vault endpoint is malformed.", e, IllegalArgumentException::new);
}

return this;
Expand All @@ -206,10 +204,7 @@ public KeyVaultAccessControlClientBuilder endpoint(String endpoint) {
*/
@Override
public KeyVaultAccessControlClientBuilder credential(TokenCredential credential) {
if (credential == null) {
throw LOGGER.logThrowableAsError(new NullPointerException("'credential' cannot be null."));
}

Objects.requireNonNull(credential, "'credential' cannot be null.");
this.credential = credential;

return this;
Expand Down Expand Up @@ -255,10 +250,7 @@ public KeyVaultAccessControlClientBuilder credential(TokenCredential credential)
*/
@Override
public KeyVaultAccessControlClientBuilder addHttpPipelinePolicy(HttpPipelinePolicy pipelinePolicy) {
if (pipelinePolicy == null) {
throw LOGGER.logThrowableAsError(new NullPointerException("'pipelinePolicy' cannot be null."));
}

Objects.requireNonNull(pipelinePolicy, "'pipelinePolicy' cannot be null.");
pipelinePolicies.add(pipelinePolicy);

return this;
Expand Down
Loading