From 337c77955e31d42fd8a89e8b53e694eee4695080 Mon Sep 17 00:00:00 2001 From: mbhaskar Date: Sat, 8 Jun 2019 22:32:07 -0700 Subject: [PATCH] Adding permissions and throughput (#177) --- .../azure/cosmos/CosmosContainer.java | 59 ++++++++ .../azure/cosmos/CosmosDatabase.java | 56 ++++++++ .../azure/cosmos/CosmosPermission.java | 109 ++++++++++++++ .../cosmos/CosmosPermissionResponse.java | 56 ++++++++ .../cosmos/CosmosPermissionSettings.java | 134 ++++++++++++++++++ .../CosmosPermissionsRequestOptions.java | 40 ++++++ .../microsoft/azure/cosmos/CosmosUser.java | 91 ++++++++++++ .../azure/cosmos/CosmosContainerCrudTest.java | 27 ++++ 8 files changed, 572 insertions(+) create mode 100644 sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermission.java create mode 100644 sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionResponse.java create mode 100644 sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionSettings.java create mode 100644 sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionsRequestOptions.java diff --git a/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosContainer.java b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosContainer.java index 9f168dbd83d34..68bb673a1255e 100644 --- a/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosContainer.java +++ b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosContainer.java @@ -24,14 +24,17 @@ import com.microsoft.azure.cosmosdb.BridgeInternal; import com.microsoft.azure.cosmosdb.ChangeFeedOptions; +import com.microsoft.azure.cosmosdb.DocumentClientException; import com.microsoft.azure.cosmosdb.FeedOptions; import com.microsoft.azure.cosmosdb.FeedResponse; +import com.microsoft.azure.cosmosdb.Offer; import com.microsoft.azure.cosmosdb.RequestOptions; import com.microsoft.azure.cosmosdb.SqlQuerySpec; import com.microsoft.azure.cosmosdb.StoredProcedure; import com.microsoft.azure.cosmosdb.Trigger; import com.microsoft.azure.cosmosdb.UserDefinedFunction; import com.microsoft.azure.cosmosdb.internal.Constants; +import com.microsoft.azure.cosmosdb.internal.HttpConstants; import com.microsoft.azure.cosmosdb.internal.Paths; import com.microsoft.azure.cosmosdb.rx.AsyncDocumentClient; import com.microsoft.azure.cosmosdb.internal.routing.PartitionKeyInternal; @@ -681,6 +684,62 @@ public CosmosTrigger getTrigger(String id){ return new CosmosTrigger(id, this); } + /** + * Gets the throughput of the container + * + * @return a {@link Mono} containing throughput or an error. + */ + public Mono readProvisionedThroughput(){ + return this.read() + .flatMap(cosmosContainerResponse -> + RxJava2Adapter.singleToMono( + RxJavaInterop.toV2Single(database.getDocClientWrapper() + .queryOffers("select * from c where c.offerResourceId = '" + + cosmosContainerResponse.getResourceSettings().getResourceId() + + "'", new FeedOptions()).toSingle())) + .flatMap(offerFeedResponse -> { + if(offerFeedResponse.getResults().isEmpty()){ + return Mono.error(new DocumentClientException(HttpConstants.StatusCodes.BADREQUEST, + "No offers found for the resource")); + } + return RxJava2Adapter.singleToMono( + RxJavaInterop.toV2Single(database.getDocClientWrapper() + .readOffer(offerFeedResponse.getResults() + .get(0) + .getSelfLink()).toSingle())); + }) + .map(cosmosOfferResponse -> cosmosOfferResponse + .getResource() + .getThroughput())); + } + + /** + * Sets throughput provisioned for a container in measurement of Requests-per-Unit in the Azure Cosmos service. + * + * @param requestUnitsPerSecond the cosmos container throughput, expressed in Request Units per second + * @return a {@link Mono} containing throughput or an error. + */ + public Mono replaceProvisionedThroughputAsync(int requestUnitsPerSecond){ + return this.read() + .flatMap(cosmosContainerResponse -> + RxJava2Adapter.singleToMono( + RxJavaInterop.toV2Single(database.getDocClientWrapper() + .queryOffers("select * from c where c.offerResourceId = '" + + cosmosContainerResponse.getResourceSettings().getResourceId() + + "'", new FeedOptions()).toSingle())) + .flatMap(offerFeedResponse -> { + if(offerFeedResponse.getResults().isEmpty()){ + return Mono.error(new DocumentClientException(HttpConstants.StatusCodes.BADREQUEST, + "No offers found for the resource")); + } + Offer offer = offerFeedResponse.getResults().get(0); + offer.setThroughput(requestUnitsPerSecond); + return RxJava2Adapter.singleToMono( + RxJavaInterop.toV2Single(database.getDocClientWrapper() + .replaceOffer(offer).toSingle())); + }).map(offerResourceResponse -> offerResourceResponse.getResource().getThroughput())); + } + /** * Gets the parent Database * diff --git a/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosDatabase.java b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosDatabase.java index 75fb16970a13e..3afbe6953f7dd 100644 --- a/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosDatabase.java +++ b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosDatabase.java @@ -26,6 +26,7 @@ import com.microsoft.azure.cosmosdb.DocumentClientException; import com.microsoft.azure.cosmosdb.FeedOptions; import com.microsoft.azure.cosmosdb.FeedResponse; +import com.microsoft.azure.cosmosdb.Offer; import com.microsoft.azure.cosmosdb.RequestOptions; import com.microsoft.azure.cosmosdb.SqlQuerySpec; import com.microsoft.azure.cosmosdb.internal.HttpConstants; @@ -375,6 +376,61 @@ public CosmosUser getUser(String id) { return new CosmosUser(id, this); } + /** + * Gets the throughput of the database + * + * @return a {@link Mono} containing throughput or an error. + */ + public Mono readProvisionedThroughput(){ + return this.read() + .flatMap(cosmosDatabaseResponse -> + RxJava2Adapter.singleToMono( + RxJavaInterop.toV2Single(getDocClientWrapper().queryOffers("select * from c where c.offerResourceId = '" + + cosmosDatabaseResponse.getResourceSettings().getResourceId() + + "'", new FeedOptions()).toSingle())) + .flatMap(offerFeedResponse -> { + if(offerFeedResponse.getResults().isEmpty()){ + return Mono.error(new DocumentClientException(HttpConstants.StatusCodes.BADREQUEST, + "No offers found for the resource")); + } + return RxJava2Adapter.singleToMono( + RxJavaInterop.toV2Single(getDocClientWrapper() + .readOffer(offerFeedResponse.getResults() + .get(0) + .getSelfLink()).toSingle())); + }) + .map(cosmosContainerResponse1 -> cosmosContainerResponse1 + .getResource() + .getThroughput())); + } + + /** + * Sets throughput provisioned for a container in measurement of Requests-per-Unit in the Azure Cosmos service. + * + * @param requestUnitsPerSecond the cosmos container throughput, expressed in Request Units per second + * @return a {@link Mono} containing throughput or an error. + */ + public Mono replaceProvisionedThroughputAsync(int requestUnitsPerSecond){ + return this.read() + .flatMap(cosmosDatabaseResponse -> + RxJava2Adapter.singleToMono( + RxJavaInterop.toV2Single(this.getDocClientWrapper() + .queryOffers("select * from c where c.offerResourceId = '" + + cosmosDatabaseResponse.getResourceSettings().getResourceId() + + "'", new FeedOptions()).toSingle())) + .flatMap(offerFeedResponse -> { + if(offerFeedResponse.getResults().isEmpty()){ + return Mono.error(new DocumentClientException(HttpConstants.StatusCodes.BADREQUEST, + "No offers found for the resource")); + } + Offer offer = offerFeedResponse.getResults().get(0); + offer.setThroughput(requestUnitsPerSecond); + return RxJava2Adapter.singleToMono( + RxJavaInterop.toV2Single(this.getDocClientWrapper() + .replaceOffer(offer).toSingle())); + }).map(offerResourceResponse -> offerResourceResponse.getResource().getThroughput())); + } + CosmosClient getClient() { return client; } diff --git a/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermission.java b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermission.java new file mode 100644 index 0000000000000..995dcff0dfef3 --- /dev/null +++ b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermission.java @@ -0,0 +1,109 @@ +/* + * The MIT License (MIT) + * Copyright (c) 2018 Microsoft Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.microsoft.azure.cosmos; + +import com.microsoft.azure.cosmosdb.RequestOptions; +import com.microsoft.azure.cosmosdb.internal.Paths; +import hu.akarnokd.rxjava.interop.RxJavaInterop; +import reactor.adapter.rxjava.RxJava2Adapter; +import reactor.core.publisher.Mono; + +public class CosmosPermission extends CosmosResource{ + + private final CosmosUser cosmosUser; + + CosmosPermission(String id, CosmosUser user){ + super(id); + this.cosmosUser = user; + } + + /** + * Reads a permission. + *

+ * After subscription the operation will be performed. + * The {@link Mono} upon successful completion will contain a single resource response with the read permission. + * In case of failure the {@link Mono} will error. + * + * @param options the request options. + * @return an {@link Mono} containing the single resource response with the read permission or an error. + */ + public Mono read(RequestOptions options) { + + return RxJava2Adapter.singleToMono(RxJavaInterop.toV2Single(cosmosUser.getDatabase() + .getDocClientWrapper() + .readPermission(getLink(),options) + .map(response -> new CosmosPermissionResponse(response, cosmosUser)) + .toSingle())); + } + + /** + * Replaces a permission. + *

+ * After subscription the operation will be performed. + * The {@link Mono} upon successful completion will contain a single resource response with the replaced permission. + * In case of failure the {@link Mono} will error. + * + * @param permissionSettings the permission settings to use. + * @param options the request options. + * @return an {@link Mono} containing the single resource response with the replaced permission or an error. + */ + public Mono replace(CosmosPermissionSettings permissionSettings, RequestOptions options) { + + return RxJava2Adapter.singleToMono(RxJavaInterop.toV2Single(cosmosUser.getDatabase() + .getDocClientWrapper() + .replacePermission(permissionSettings.getV2Permissions(), options) + .map(response -> new CosmosPermissionResponse(response, cosmosUser)) + .toSingle())); + } + + /** + * Deletes a permission. + *

+ * After subscription the operation will be performed. + * The {@link Mono} upon successful completion will contain a single resource response for the deleted permission. + * In case of failure the {@link Mono} will error. + * + * @param options the request options. + * @return an {@link Mono} containing the single resource response for the deleted permission or an error. + */ + public Mono delete(CosmosPermissionsRequestOptions options) { + if(options == null){ + options = new CosmosPermissionsRequestOptions(); + } + return RxJava2Adapter.singleToMono(RxJavaInterop.toV2Single(cosmosUser.getDatabase() + .getDocClientWrapper() + .deletePermission(getLink(), options.toRequestOptions()) + .map(response -> new CosmosPermissionResponse(response, cosmosUser)) + .toSingle())); + } + + @Override + protected String getURIPathSegment() { + return Paths.PERMISSIONS_PATH_SEGMENT; + } + + @Override + protected String getParentLink() { + return cosmosUser.getLink(); + } +} diff --git a/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionResponse.java b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionResponse.java new file mode 100644 index 0000000000000..1724654dfe51f --- /dev/null +++ b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionResponse.java @@ -0,0 +1,56 @@ +/* + * The MIT License (MIT) + * Copyright (c) 2018 Microsoft Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.microsoft.azure.cosmos; + +import com.microsoft.azure.cosmosdb.Permission; +import com.microsoft.azure.cosmosdb.ResourceResponse; + +public class CosmosPermissionResponse extends CosmosResponse { + CosmosPermission permissionClient; + + CosmosPermissionResponse(ResourceResponse response, CosmosUser cosmosUser) { + super(response); + if(response.getResource() == null){ + super.setResourceSettings(null); + }else{ + super.setResourceSettings(new CosmosPermissionSettings(response.getResource().toJson())); + permissionClient = new CosmosPermission(response.getResource().getId(), cosmosUser); + } + } + + /** + * Get the permission settings + * @return the permission settings + */ + public CosmosPermissionSettings getPermissionSettings(){ + return super.getResourceSettings(); + } + + /** + * Gets the CosmosPermission + * @return the cosmos permission + */ + public CosmosPermission getPermission() { + return permissionClient; + } +} diff --git a/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionSettings.java b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionSettings.java new file mode 100644 index 0000000000000..926afe7f24d94 --- /dev/null +++ b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionSettings.java @@ -0,0 +1,134 @@ +/* + * The MIT License (MIT) + * Copyright (c) 2018 Microsoft Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.microsoft.azure.cosmos; + +import com.microsoft.azure.cosmosdb.PartitionKey; +import com.microsoft.azure.cosmosdb.Permission; +import com.microsoft.azure.cosmosdb.PermissionMode; +import com.microsoft.azure.cosmosdb.Resource; +import com.microsoft.azure.cosmosdb.internal.Constants; +import org.apache.commons.text.WordUtils; + +import java.util.List; +import java.util.stream.Collectors; + +public class CosmosPermissionSettings extends Resource { + + + public static List getFromV2Results(List results) { + return results.stream().map(permission -> new CosmosPermissionSettings(permission.toJson())).collect(Collectors.toList()); + } + + /** + * Initialize a permission object. + */ + public CosmosPermissionSettings() { + super(); + } + + /** + * Initialize a permission object from json string. + * + * @param jsonString the json string that represents the permission. + */ + public CosmosPermissionSettings(String jsonString) { + super(jsonString); + } + + /** + * Gets the self-link of resource to which the permission applies. + * + * @return the resource link. + */ + public String getResourceLink() { + return super.getString(Constants.Properties.RESOURCE_LINK); + } + + /** + * Sets the self-link of resource to which the permission applies. + * + * @param resourceLink the resource link. + */ + public void setResourceLink(String resourceLink) { + super.set(Constants.Properties.RESOURCE_LINK, resourceLink); + } + + /** + * Gets the permission mode. + * + * @return the permission mode. + */ + public PermissionMode getPermissionMode() { + String value = super.getString(Constants.Properties.PERMISSION_MODE); + return PermissionMode.valueOf(WordUtils.capitalize(value)); + } + + /** + * Sets the permission mode. + * + * @param permissionMode the permission mode. + */ + public void setPermissionMode(PermissionMode permissionMode) { + this.set(Constants.Properties.PERMISSION_MODE, + permissionMode.name().toLowerCase()); + } + + /** + * Gets the access token granting the defined permission. + * + * @return the access token. + */ + public String getToken() { + return super.getString(Constants.Properties.TOKEN); + } + + //TODO: need getValue from JsonSerializable +// /** +// * Gets the resource partition key associated with this permission object. +// * +// * @return the partition key. +// */ +// public PartitionKey getResourcePartitionKey() { +// PartitionKey key = null; +// Object value = super.get(Constants.Properties.RESOURCE_PARTITION_KEY); +// if (value != null) { +// ArrayNode arrayValue = (ArrayNode) value; +// key = new PartitionKey(getValue(arrayValue.get(0))); +// } +// +// return key; +// } + + /** + * Sets the resource partition key associated with this permission object. + * + * @param partitionkey the partition key. + */ + public void setResourcePartitionKey(PartitionKey partitionkey) { + super.set(Constants.Properties.RESOURCE_PARTITION_KEY, partitionkey.getInternalPartitionKey().toJson()); + } + + public Permission getV2Permissions() { + return null; + } +} diff --git a/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionsRequestOptions.java b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionsRequestOptions.java new file mode 100644 index 0000000000000..d804050a9fce8 --- /dev/null +++ b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermissionsRequestOptions.java @@ -0,0 +1,40 @@ +/* + * The MIT License (MIT) + * Copyright (c) 2018 Microsoft Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.microsoft.azure.cosmos; + +import com.microsoft.azure.cosmosdb.RequestOptions; + +/** + * Contains the request options of CosmosPermission + */ +public class CosmosPermissionsRequestOptions extends CosmosRequestOptions { + //TODO: Need to add respective options + + + @Override + protected RequestOptions toRequestOptions() { + //TODO: Should we set any default values instead of nulls? + super.toRequestOptions(); + return requestOptions; + } +} diff --git a/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosUser.java b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosUser.java index fc8b3cc7f8f1a..8d9af78a0c29f 100644 --- a/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosUser.java +++ b/sdk/src/main/java/com/microsoft/azure/cosmos/CosmosUser.java @@ -1,9 +1,14 @@ package com.microsoft.azure.cosmos; +import com.microsoft.azure.cosmosdb.BridgeInternal; +import com.microsoft.azure.cosmosdb.FeedOptions; +import com.microsoft.azure.cosmosdb.FeedResponse; +import com.microsoft.azure.cosmosdb.Permission; import com.microsoft.azure.cosmosdb.RequestOptions; import com.microsoft.azure.cosmosdb.internal.Paths; import hu.akarnokd.rxjava.interop.RxJavaInterop; import reactor.adapter.rxjava.RxJava2Adapter; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public class CosmosUser extends CosmosResource { @@ -57,6 +62,88 @@ public Mono delete(RequestOptions options) { .map(response -> new CosmosUserResponse(response, database)).toSingle())); } + /** + * 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 permissionSettings the permission settings 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(CosmosPermissionSettings permissionSettings, CosmosPermissionsRequestOptions options) { + if(options == null){ + options = new CosmosPermissionsRequestOptions(); + } + Permission permission = permissionSettings.getV2Permissions(); + return RxJava2Adapter.singleToMono(RxJavaInterop.toV2Single(database.getDocClientWrapper() + .createPermission(getLink(), permission, options.toRequestOptions()) + .map(response -> new CosmosPermissionResponse(response, this)) + .toSingle())); + } + + /** + * 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 permissionSettings the permission settings 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(CosmosPermissionSettings permissionSettings, CosmosPermissionsRequestOptions options) { + Permission permission = permissionSettings.getV2Permissions(); + if(options == null){ + options = new CosmosPermissionsRequestOptions(); + } + return RxJava2Adapter.singleToMono(RxJavaInterop.toV2Single(database.getDocClientWrapper() + .upsertPermission(getLink(), permission, options.toRequestOptions()) + .map(response -> new CosmosPermissionResponse(response, this)) + .toSingle())); + } + + + /** + * Reads all permissions. + *

+ * After subscription the operation will be performed. + * The {@link Flux} will contain one or several feed response pages of the read permissions. + * In case of failure the {@link Flux} will error. + * + * @param options the feed options. + * @return an {@link Flux} containing one or several feed response pages of the read permissions or an error. + */ + public Flux> listPermissions(FeedOptions options) { + return RxJava2Adapter.flowableToFlux( + RxJavaInterop.toV2Flowable(getDatabase().getDocClientWrapper() + .readPermissions(getLink(), options) + .map(response-> BridgeInternal.createFeedResponse(CosmosPermissionSettings.getFromV2Results(response.getResults()), + response.getResponseHeaders())))); + } + + /** + * Query for permissions. + *

+ * After subscription the operation will be performed. + * The {@link Flux} will contain one or several feed response pages of the obtained permissions. + * In case of failure the {@link Flux} will error. + * + * @param query the query. + * @param options the feed options. + * @return an {@link Flux} containing one or several feed response pages of the obtained permissions or an error. + */ + public Flux> queryPermissions(String query, FeedOptions options) { + return RxJava2Adapter.flowableToFlux( + RxJavaInterop.toV2Flowable(getDatabase().getDocClientWrapper() + .queryPermissions(getLink(), query, options) + .map(response-> BridgeInternal.createFeedResponse(CosmosPermissionSettings.getFromV2Results(response.getResults()), + response.getResponseHeaders())))); + } + @Override protected String getURIPathSegment() { return Paths.USERS_PATH_SEGMENT; @@ -66,4 +153,8 @@ protected String getURIPathSegment() { protected String getParentLink() { return database.getLink() ; } + + CosmosDatabase getDatabase() { + return database; + } } \ No newline at end of file diff --git a/sdk/src/test/java/com/microsoft/azure/cosmos/CosmosContainerCrudTest.java b/sdk/src/test/java/com/microsoft/azure/cosmos/CosmosContainerCrudTest.java index 3491fb571e102..5aacc530925bf 100644 --- a/sdk/src/test/java/com/microsoft/azure/cosmos/CosmosContainerCrudTest.java +++ b/sdk/src/test/java/com/microsoft/azure/cosmos/CosmosContainerCrudTest.java @@ -148,6 +148,33 @@ public void testreplaceContainer() throws Exception { validateSuccess(replaceMono, validator); } + @Test(groups = { "cosmosv3" }, timeOut = TIMEOUT) + public void testGetThroughput(){ + CosmosDatabase database = client.getDatabase(PRE_EXISTING_DATABASE_ID); + + Mono containerMono = database.createContainer(getContainerSettings()); + + CosmosContainerResponse containerResponse = containerMono.block(); + CosmosContainer container = containerResponse.getContainer(); + + Integer throughput = container.readProvisionedThroughput().block(); + } + + @Test(groups = { "cosmosv3" }, timeOut = TIMEOUT) + public void testReplaceThroughput(){ + CosmosDatabase database = client.getDatabase(PRE_EXISTING_DATABASE_ID); + int newThroughput = 1000; + + Mono containerMono = database.createContainer(getContainerSettings()); + + CosmosContainerResponse containerResponse = containerMono.block(); + CosmosContainer container = containerResponse.getContainer(); + + Integer throughput = container.replaceProvisionedThroughputAsync(newThroughput).block(); + assertThat(throughput).isEqualTo(newThroughput); + } + + @BeforeClass(groups = { "cosmosv3" }, timeOut = SETUP_TIMEOUT) public void beforeClass() { client = clientBuilder.build();