Skip to content

Commit

Permalink
Adding permissions and throughput (Azure#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhaskar authored and Christopher Anderson committed Jun 9, 2019
1 parent cdd68cf commit 337c779
Show file tree
Hide file tree
Showing 8 changed files with 572 additions and 0 deletions.
59 changes: 59 additions & 0 deletions sdk/src/main/java/com/microsoft/azure/cosmos/CosmosContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Integer> 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<Integer> 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
*
Expand Down
56 changes: 56 additions & 0 deletions sdk/src/main/java/com/microsoft/azure/cosmos/CosmosDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Integer> 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<Integer> 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;
}
Expand Down
109 changes: 109 additions & 0 deletions sdk/src/main/java/com/microsoft/azure/cosmos/CosmosPermission.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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<CosmosPermissionResponse> read(RequestOptions options) {

return RxJava2Adapter.singleToMono(RxJavaInterop.toV2Single(cosmosUser.getDatabase()
.getDocClientWrapper()
.readPermission(getLink(),options)
.map(response -> new CosmosPermissionResponse(response, cosmosUser))
.toSingle()));
}

/**
* Replaces a permission.
* <p>
* 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<CosmosPermissionResponse> 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.
* <p>
* 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<CosmosPermissionResponse> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<CosmosPermissionSettings> {
CosmosPermission permissionClient;

CosmosPermissionResponse(ResourceResponse<Permission> 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;
}
}
Loading

0 comments on commit 337c779

Please sign in to comment.