Skip to content
Merged
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 @@ -8,6 +8,7 @@
import com.microsoft.azure.management.apigeneration.Beta;
import com.microsoft.azure.management.apigeneration.Fluent;
import com.microsoft.azure.management.apigeneration.Beta.SinceVersion;
import com.microsoft.azure.management.apigeneration.Method;
import com.microsoft.azure.management.cosmosdb.implementation.CosmosDBManager;
import com.microsoft.azure.management.cosmosdb.implementation.DatabaseAccountInner;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
Expand Down Expand Up @@ -101,6 +102,13 @@ public interface CosmosDBAccount extends
*/
Observable<DatabaseAccountListConnectionStringsResult> listConnectionStringsAsync();

/**
* @return a list that contains the Cosmos DB capabilities
*/
@Beta(SinceVersion.V1_10_0)
List<Capability> capabilities();


/**
* @param keyKind the key kind
*/
Expand Down Expand Up @@ -154,6 +162,61 @@ interface WithKind {
* @return the next stage of the definition
*/
WithConsistencyPolicy withKind(DatabaseAccountKind kind);

/**
* The database account kind for the CosmosDB account.
*
* @param kind the account kind
* @param capabilities the list of Cosmos DB capabilities for the account
* @return the next stage of the definition
*/
@Beta(SinceVersion.V1_10_0)
WithConsistencyPolicy withKind(DatabaseAccountKind kind, Capability... capabilities);

/**
* Creates a SQL CosmosDB account.
*
* @return the next stage of the definition
*/
@Method
@Beta(SinceVersion.V1_10_0)
WithConsistencyPolicy withDataModelSql();

/**
* Creates a MongoDB CosmosDB account.
*
* @return the next stage of the definition
*/
@Method
@Beta(SinceVersion.V1_10_0)
WithConsistencyPolicy withDataModelMongoDB();

/**
* Creates a Cassandra CosmosDB account.
*
* @return the next stage of the definition
*/
@Method
@Beta(SinceVersion.V1_10_0)
WithConsistencyPolicy withDataModelCassandra();

/**
* Creates an Azure Table CosmosDB account.
*
* @return the next stage of the definition
*/
@Method
@Beta(SinceVersion.V1_10_0)
WithConsistencyPolicy withDataModelAzureTable();
Copy link
Contributor

@hovsepm hovsepm Apr 24, 2018

Choose a reason for hiding this comment

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

withDataModelAzureTable [](start = 34, length = 23)

Should it be just withDataModel method and then a parameter of Enum type with all the supported DB Types as enum members? Too many methods in Intelisence that start with the same prefix (a long one in this case) creates some clutter IMHO

Copy link
Contributor

@hovsepm hovsepm Apr 24, 2018

Choose a reason for hiding this comment

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

also it is kind of weird construct withDataModelSql . More natural would be withSqlAccount or withSqlType but in that case you will have types in a hard to find order in the intelisence. So reiterating my proposal to have one method and bunch of enum values 😃

Copy link

@JREndeanMSFT JREndeanMSFT Apr 25, 2018

Choose a reason for hiding this comment

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

In the management portal it is called API. Perhaps withApi(Api.SQL) 😃
cosmosdbportalui

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do not need to match exactly what the Azure Portal does. Data model fits better the type of the account (see the explanation behind the little "i").

There's already a method that takes enums (see the withKind() overload I just added). These new methods are direct shortcuts for creating the respective CosmosDB account.


/**
* Creates a Gremlin CosmosDB account.
*
* @return the next stage of the definition
*/
@Method
@Beta(SinceVersion.V1_10_0)
WithConsistencyPolicy withDataModelGremlin();
}

/**
Expand Down Expand Up @@ -234,7 +297,8 @@ interface WithCreate extends
Creatable<CosmosDBAccount>,
WithConsistencyPolicy,
WithReadReplication,
WithIpRangeFilter {
WithIpRangeFilter,
DefinitionWithTags<WithCreate> {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.microsoft.azure.management.cosmosdb.implementation;

import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.cosmosdb.Capability;
import com.microsoft.azure.management.cosmosdb.CosmosDBAccount;
import com.microsoft.azure.management.cosmosdb.DatabaseAccountKind;
import com.microsoft.azure.management.cosmosdb.DatabaseAccountListConnectionStringsResult;
Expand All @@ -23,6 +24,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -145,6 +147,15 @@ public DatabaseAccountListConnectionStringsResult call(DatabaseAccountListConnec
});
}

@Override
public List<Capability> capabilities() {
List<Capability> capabilities = this.inner().capabilities();
if (capabilities == null) {
capabilities = new ArrayList<>();
}
return Collections.unmodifiableList(capabilities);
}

@Override
public void regenerateKey(KeyKind keyKind) {
this.regenerateKeyAsync(keyKind).toBlocking().last();
Expand All @@ -162,6 +173,52 @@ public CosmosDBAccountImpl withKind(DatabaseAccountKind kind) {
return this;
}

@Override
public CosmosDBAccountImpl withKind(DatabaseAccountKind kind, Capability... capabilities) {
this.inner().withKind(kind);
this.inner().withCapabilities(Arrays.asList(capabilities));
return this;
}

@Override
public CosmosDBAccountImpl withDataModelSql() {
this.inner().withKind(DatabaseAccountKind.GLOBAL_DOCUMENT_DB);
return this;

Choose a reason for hiding this comment

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

Are you sure the "defaultExperience" tags are not needed on these? If you create each API type in the portal, they all have a "defaultExperience" tag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tags do not impact resource provisioning; if they do then it's a bug. While the tests I've added set a generic tag it's not required to create an CosmosDB account.

}

@Override
public CosmosDBAccountImpl withDataModelMongoDB() {
this.inner().withKind(DatabaseAccountKind.MONGO_DB);
return this;
}

@Override
public CosmosDBAccountImpl withDataModelCassandra() {
this.inner().withKind(DatabaseAccountKind.PARSE);
List<Capability> capabilities = new ArrayList<Capability>();
capabilities.add(new Capability().withName("EnableCassandra"));
this.inner().withCapabilities(capabilities);
return this;
}

@Override
public CosmosDBAccountImpl withDataModelAzureTable() {
this.inner().withKind(DatabaseAccountKind.PARSE);
List<Capability> capabilities = new ArrayList<Capability>();
capabilities.add(new Capability().withName("EnableTable"));
this.inner().withCapabilities(capabilities);
return this;
}

@Override
public CosmosDBAccountImpl withDataModelGremlin() {
this.inner().withKind(DatabaseAccountKind.PARSE);
List<Capability> capabilities = new ArrayList<Capability>();
capabilities.add(new Capability().withName("EnableGremlin"));
this.inner().withCapabilities(capabilities);
return this;
}


@Override
public CosmosDBAccountImpl withIpRangeFilter(String ipRangeFilter) {
Expand Down Expand Up @@ -251,6 +308,7 @@ private DatabaseAccountCreateUpdateParametersInner createUpdateParametersInner(D
DatabaseAccountOfferType.STANDARD.toString());
createUpdateParametersInner.withIpRangeFilter(inner.ipRangeFilter());
createUpdateParametersInner.withKind(inner.kind());
createUpdateParametersInner.withCapabilities(inner.capabilities());
createUpdateParametersInner.withTags(inner.getTags());
this.addLocationsForCreateUpdateParameters(createUpdateParametersInner, this.failoverPolicies);
return createUpdateParametersInner;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.microsoft.azure.management.cosmosdb;

import com.microsoft.azure.management.cosmosdb.implementation.CosmosDBManager;
import com.microsoft.azure.management.resources.core.TestBase;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import com.microsoft.azure.management.resources.implementation.ResourceManager;
import com.microsoft.rest.RestClient;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

public class CosmosDBTests extends TestBase {

private static String RG_NAME = "";
protected ResourceManager resourceManager;
protected CosmosDBManager cosmosDBManager;
// final String sqlPrimaryServerName = SdkContext.randomResourceName("sqlpri", 22);

public CosmosDBTests() {
super(TestBase.RunCondition.BOTH);
}

@Override
protected void initializeClients(RestClient restClient, String defaultSubscription, String domain) throws IOException {
RG_NAME = generateRandomResourceName("rgcosmosdb", 20);
resourceManager = ResourceManager
.authenticate(restClient)
.withSubscription(defaultSubscription);

cosmosDBManager = CosmosDBManager.authenticate(restClient, defaultSubscription);
}

@Override
protected void cleanUpResources() {
resourceManager.resourceGroups().beginDeleteByName(RG_NAME);
}

@Test
public void CanCreateCosmosDbSqlAccount() {
final String cosmosDbAccountName = SdkContext.randomResourceName("cosmosdb", 22);

CosmosDBAccount cosmosDBAccount = cosmosDBManager.databaseAccounts()
.define(cosmosDbAccountName)
.withRegion(Region.US_WEST_CENTRAL)
.withNewResourceGroup(RG_NAME)
.withDataModelSql()
.withEventualConsistency()
.withWriteReplication(Region.US_EAST)
.withReadReplication(Region.US_CENTRAL)
.withIpRangeFilter("")
.withTag("tag1", "value1")
.create();

Assert.assertEquals(cosmosDBAccount.name(), cosmosDbAccountName.toLowerCase());
Assert.assertEquals(cosmosDBAccount.kind(), DatabaseAccountKind.GLOBAL_DOCUMENT_DB);
Assert.assertEquals(cosmosDBAccount.writableReplications().size(), 1);
Assert.assertEquals(cosmosDBAccount.readableReplications().size(), 2);
Assert.assertEquals(cosmosDBAccount.defaultConsistencyLevel(), DefaultConsistencyLevel.EVENTUAL);
}

@Test
public void CanCreateCosmosDbMongoDBAccount() {
final String cosmosDbAccountName = SdkContext.randomResourceName("cosmosdb", 22);

CosmosDBAccount cosmosDBAccount = cosmosDBManager.databaseAccounts()
.define(cosmosDbAccountName)
.withRegion(Region.US_WEST_CENTRAL)
.withNewResourceGroup(RG_NAME)
.withDataModelMongoDB()
.withEventualConsistency()
.withWriteReplication(Region.US_EAST)
.withReadReplication(Region.US_CENTRAL)
.withIpRangeFilter("")
.withTag("tag1", "value1")
.create();

Assert.assertEquals(cosmosDBAccount.name(), cosmosDbAccountName.toLowerCase());
Assert.assertEquals(cosmosDBAccount.kind(), DatabaseAccountKind.MONGO_DB);
Assert.assertEquals(cosmosDBAccount.writableReplications().size(), 1);
Assert.assertEquals(cosmosDBAccount.readableReplications().size(), 2);
Assert.assertEquals(cosmosDBAccount.defaultConsistencyLevel(), DefaultConsistencyLevel.EVENTUAL);
}

@Test
public void CanCreateCosmosDbCassandraAccount() {
final String cosmosDbAccountName = SdkContext.randomResourceName("cosmosdb", 22);

CosmosDBAccount cosmosDBAccount = cosmosDBManager.databaseAccounts()
.define(cosmosDbAccountName)
.withRegion(Region.US_WEST_CENTRAL)
.withNewResourceGroup(RG_NAME)
.withDataModelCassandra()
.withEventualConsistency()
.withWriteReplication(Region.US_EAST)
.withReadReplication(Region.US_CENTRAL)
.withIpRangeFilter("")
.withTag("tag1", "value1")
.create();

Assert.assertEquals(cosmosDBAccount.name(), cosmosDbAccountName.toLowerCase());
Assert.assertEquals(cosmosDBAccount.kind(), DatabaseAccountKind.PARSE);
Assert.assertEquals(cosmosDBAccount.capabilities().get(0).name(), "EnableCassandra");
Assert.assertEquals(cosmosDBAccount.writableReplications().size(), 1);
Assert.assertEquals(cosmosDBAccount.readableReplications().size(), 2);
Assert.assertEquals(cosmosDBAccount.defaultConsistencyLevel(), DefaultConsistencyLevel.EVENTUAL);
}

@Test
public void CanCreateCosmosDbAzureTableAccount() {
final String cosmosDbAccountName = SdkContext.randomResourceName("cosmosdb", 22);

CosmosDBAccount cosmosDBAccount = cosmosDBManager.databaseAccounts()
.define(cosmosDbAccountName)
.withRegion(Region.US_WEST_CENTRAL)
.withNewResourceGroup(RG_NAME)
.withDataModelAzureTable()
.withEventualConsistency()
.withWriteReplication(Region.US_EAST)
.withReadReplication(Region.US_CENTRAL)
.withIpRangeFilter("")
.withTag("tag1", "value1")
.create();

Assert.assertEquals(cosmosDBAccount.name(), cosmosDbAccountName.toLowerCase());
Assert.assertEquals(cosmosDBAccount.kind(), DatabaseAccountKind.PARSE);
Assert.assertEquals(cosmosDBAccount.capabilities().get(0).name(), "EnableTable");
Assert.assertEquals(cosmosDBAccount.writableReplications().size(), 1);
Assert.assertEquals(cosmosDBAccount.readableReplications().size(), 2);
Assert.assertEquals(cosmosDBAccount.defaultConsistencyLevel(), DefaultConsistencyLevel.EVENTUAL);
}

}
Loading