com.azure.cosmos.CosmosAsyncUser Maven / Gradle / Ivy
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.cosmos;
import com.azure.core.util.Context;
import com.azure.cosmos.implementation.ImplementationBridgeHelpers;
import com.azure.cosmos.implementation.OperationType;
import com.azure.cosmos.implementation.Paths;
import com.azure.cosmos.implementation.Permission;
import com.azure.cosmos.implementation.QueryFeedOperationState;
import com.azure.cosmos.implementation.ResourceType;
import com.azure.cosmos.models.CosmosPermissionProperties;
import com.azure.cosmos.models.CosmosPermissionRequestOptions;
import com.azure.cosmos.models.CosmosPermissionResponse;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.models.CosmosUserProperties;
import com.azure.cosmos.models.CosmosUserResponse;
import com.azure.cosmos.models.ModelBridgeInternal;
import com.azure.cosmos.util.CosmosPagedFlux;
import com.azure.cosmos.util.UtilBridgeInternal;
import reactor.core.publisher.Mono;
import static com.azure.core.util.FluxUtil.withContext;
/**
* The type Cosmos async user.
*/
public class CosmosAsyncUser {
private static final ImplementationBridgeHelpers.CosmosQueryRequestOptionsHelper.CosmosQueryRequestOptionsAccessor queryOptionsAccessor =
ImplementationBridgeHelpers.CosmosQueryRequestOptionsHelper.getCosmosQueryRequestOptionsAccessor();
private static final ImplementationBridgeHelpers.FeedResponseHelper.FeedResponseAccessor feedResponseAccessor =
ImplementationBridgeHelpers.FeedResponseHelper.getFeedResponseAccessor();
private final CosmosAsyncDatabase database;
@SuppressWarnings("EnforceFinalFields")
private String id;
CosmosAsyncUser(String id, CosmosAsyncDatabase database) {
this.id = id;
this.database = database;
}
/**
* Get the id of the {@link CosmosAsyncUser}
*
* @return the id of the {@link CosmosAsyncUser}
*/
public String getId() {
return id;
}
/**
* Set the id of the {@link CosmosAsyncUser}
*
* @param id the id of the {@link CosmosAsyncUser}
* @return the same {@link CosmosAsyncUser} that had the id set
*/
CosmosAsyncUser setId(String id) {
this.id = id;
return this;
}
/**
* Reads a cosmos user
*
* @return a {@link Mono} containing the single resource response with the read user or an error.
*/
public Mono read() {
return withContext(this::readInternal);
}
/**
* Replace a cosmos user
*
* @param userProperties the user properties to use
* @return a {@link Mono} containing the single resource response with the replaced user or an error.
*/
public Mono replace(CosmosUserProperties userProperties) {
return withContext(context -> replaceInternal(userProperties, context));
}
/**
* Delete a cosmos user
*
* @return a {@link Mono} containing the single resource response with the deleted user or an error.
*/
public Mono delete() {
return withContext(this::deleteInternal);
}
/**
* Creates a permission.
*
* After subscription the operation will be performed.
* The {@link Mono} upon successful completion will contain a single resource response with the created permission.
* In case of failure the {@link Mono} will error.
*
* @param permissionProperties the permission properties to create.
* @param options the request options.
* @return an {@link Mono} containing the single resource response with the created permission or an error.
*/
public Mono createPermission(
CosmosPermissionProperties permissionProperties,
CosmosPermissionRequestOptions options) {
final CosmosPermissionRequestOptions requestOptions = options == null ? new CosmosPermissionRequestOptions() : options;
Permission permission = ModelBridgeInternal.getPermission(permissionProperties, database.getId());
return withContext(context -> createPermissionInternal(permission, requestOptions, context));
}
/**
* Upserts a permission.
*
* After subscription the operation will be performed.
* The {@link Mono} upon successful completion will contain a single resource response with the upserted permission.
* In case of failure the {@link Mono} will error.
*
* @param permissionProperties the permission properties to upsert.
* @param options the request options.
* @return an {@link Mono} containing the single resource response with the upserted permission or an error.
*/
public Mono upsertPermission(
CosmosPermissionProperties permissionProperties,
CosmosPermissionRequestOptions options) {
Permission permission = ModelBridgeInternal.getPermission(permissionProperties, database.getId());
final CosmosPermissionRequestOptions requestOptions = options == null ? new CosmosPermissionRequestOptions() : options;
return withContext(context -> upsertPermissionInternal(permission, requestOptions, context));
}
/**
* Reads all permissions.
*
* After subscription the operation will be performed.
* The {@link CosmosPagedFlux} will contain one or several feed response pages of the read permissions.
* In case of failure the {@link CosmosPagedFlux} will error.
*
* @return a {@link CosmosPagedFlux} containing one or several feed response pages of the read permissions or an
* error.
*/
public CosmosPagedFlux readAllPermissions() {
return readAllPermissions(new CosmosQueryRequestOptions());
}
/**
* Reads all permissions.
*
* After subscription the operation will be performed.
* The {@link CosmosPagedFlux} will contain one or several feed response pages of the read permissions.
* In case of failure the {@link CosmosPagedFlux} will error.
*
* @param options the query request options.
* @return a {@link CosmosPagedFlux} containing one or several feed response pages of the read permissions or an
* error.
*/
CosmosPagedFlux readAllPermissions(CosmosQueryRequestOptions options) {
return UtilBridgeInternal.createCosmosPagedFlux(pagedFluxOptions -> {
String spanName = "readAllPermissions." + this.getId();
CosmosAsyncClient client = this.getDatabase().getClient();
CosmosQueryRequestOptions nonNullOptions = options != null ? options : new CosmosQueryRequestOptions();
QueryFeedOperationState state = new QueryFeedOperationState(
client,
spanName,
this.getDatabase().getId(),
null,
ResourceType.Permission,
OperationType.ReadFeed,
queryOptionsAccessor.getQueryNameOrDefault(nonNullOptions, spanName),
nonNullOptions,
pagedFluxOptions
);
pagedFluxOptions.setFeedOperationState(state);
return getDatabase().getDocClientWrapper()
.readPermissions(getLink(), state)
.map(response -> feedResponseAccessor.createFeedResponse(
ModelBridgeInternal.getCosmosPermissionPropertiesFromResults(response.getResults()),
response.getResponseHeaders(),
response.getCosmosDiagnostics()));
});
}
/**
* Query for permissions.
*
* After subscription the operation will be performed.
* The {@link CosmosPagedFlux} will contain one or several feed response pages of the obtained permissions.
* In case of failure the {@link CosmosPagedFlux} will error.
*
* @param query the query.
* @return a {@link CosmosPagedFlux} containing one or several feed response pages of the obtained permissions or
* an error.
*/
public CosmosPagedFlux queryPermissions(String query) {
return queryPermissions(query, new CosmosQueryRequestOptions());
}
/**
* Query for permissions.
*
* After subscription the operation will be performed.
* The {@link CosmosPagedFlux} will contain one or several feed response pages of the obtained permissions.
* In case of failure the {@link CosmosPagedFlux} will error.
*
* @param query the query.
* @param options the query request options.
* @return a {@link CosmosPagedFlux} containing one or several feed response pages of the obtained permissions or
* an error.
*/
public CosmosPagedFlux queryPermissions(String query, CosmosQueryRequestOptions options) {
CosmosQueryRequestOptions requestOptions = options == null ? new CosmosQueryRequestOptions() : options;
return UtilBridgeInternal.createCosmosPagedFlux(pagedFluxOptions -> {
String spanName = "queryPermissions." + this.getId();
CosmosAsyncClient client = this.getDatabase().getClient();
QueryFeedOperationState state = new QueryFeedOperationState(
client,
spanName,
this.getDatabase().getId(),
null,
ResourceType.Permission,
OperationType.Query,
queryOptionsAccessor.getQueryNameOrDefault(requestOptions, spanName),
requestOptions,
pagedFluxOptions
);
pagedFluxOptions.setFeedOperationState(state);
return getDatabase().getDocClientWrapper()
.queryPermissions(getLink(), query, state)
.map(response -> feedResponseAccessor.createFeedResponse(
ModelBridgeInternal.getCosmosPermissionPropertiesFromResults(response.getResults()),
response.getResponseHeaders(),
response.getCosmosDiagnostics()));
});
}
/**
* Get cosmos permission without making a call to backend
*
* @param id the id
* @return the cosmos permission
*/
public CosmosAsyncPermission getPermission(String id) {
return new CosmosAsyncPermission(id, this);
}
String getURIPathSegment() {
return Paths.USERS_PATH_SEGMENT;
}
String getParentLink() {
return database.getLink();
}
String getLink() {
return getParentLink()
+ "/"
+ getURIPathSegment()
+ "/"
+ getId();
}
/**
* Gets the parent Database
*
* @return the (@link CosmosAsyncDatabase)
*/
CosmosAsyncDatabase getDatabase() {
return database;
}
private Mono readInternal(Context context) {
String spanName = "readUser." + getId();
Mono responseMono = this.database.getDocClientWrapper()
.readUser(getLink(), null)
.map(ModelBridgeInternal::createCosmosUserResponse).single();
CosmosAsyncClient client = database.getClient();
return client.getDiagnosticsProvider().traceEnabledCosmosResponsePublisher(
responseMono,
context,
spanName,
database.getId(),
null,
client,
null,
OperationType.Read,
ResourceType.User,
null);
}
private Mono replaceInternal(CosmosUserProperties userSettings, Context context) {
String spanName = "replaceUser." + getId();
Mono responseMono = this.database.getDocClientWrapper()
.replaceUser(ModelBridgeInternal.getV2User(userSettings)
, null)
.map(ModelBridgeInternal::createCosmosUserResponse).single();
CosmosAsyncClient client = database.getClient();
return client.getDiagnosticsProvider().traceEnabledCosmosResponsePublisher(
responseMono,
context,
spanName,
database.getId(),
null,
client,
null,
OperationType.Replace,
ResourceType.User,
null);
}
private Mono deleteInternal(Context context) {
String spanName = "deleteUser." + getId();
Mono responseMono = this.database.getDocClientWrapper()
.deleteUser(getLink(), null)
.map(ModelBridgeInternal::createCosmosUserResponse).single();
CosmosAsyncClient client = database.getClient();
return client.getDiagnosticsProvider().traceEnabledCosmosResponsePublisher(
responseMono,
context,
spanName,
database.getId(),
null,
client,
null,
OperationType.Delete,
ResourceType.User,
null);
}
private Mono createPermissionInternal(
Permission permission,
CosmosPermissionRequestOptions options,
Context context) {
String spanName = "createPermission." + getId();
Mono responseMono = database.getDocClientWrapper()
.createPermission(getLink(), permission, ModelBridgeInternal.toRequestOptions(options))
.map(ModelBridgeInternal::createCosmosPermissionResponse)
.single();
CosmosAsyncClient client = database.getClient();
return client.getDiagnosticsProvider().traceEnabledCosmosResponsePublisher(
responseMono,
context,
spanName,
database.getId(),
null,
client,
null,
OperationType.Create,
ResourceType.Permission,
null);
}
private Mono upsertPermissionInternal(
Permission permission,
CosmosPermissionRequestOptions options,
Context context) {
String spanName = "upsertPermission." + getId();
Mono responseMono = database.getDocClientWrapper()
.upsertPermission(getLink(), permission, ModelBridgeInternal.toRequestOptions(options))
.map(ModelBridgeInternal::createCosmosPermissionResponse)
.single();
CosmosAsyncClient client = database.getClient();
return client.getDiagnosticsProvider().traceEnabledCosmosResponsePublisher(
responseMono,
context,
spanName,
database.getId(),
null,
client,
null,
OperationType.Upsert,
ResourceType.Permission,
null);
}
}