Skip to content

Commit

Permalink
Merge pull request Azure#1372 from anudeepsharma/SqlDatabase
Browse files Browse the repository at this point in the history
Done changes as per review comments from Asir
  • Loading branch information
Martin Sawicki authored Jan 5, 2017
2 parents 0dc4ebc + e42950c commit 9eca49f
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.microsoft.azure.management.sql;

import com.microsoft.azure.management.apigeneration.Fluent;
import com.microsoft.azure.management.apigeneration.Method;
import com.microsoft.azure.management.resources.fluentcore.arm.models.IndependentChildResource;
import com.microsoft.azure.management.resources.fluentcore.model.Appliable;
import com.microsoft.azure.management.resources.fluentcore.model.Creatable;
Expand Down Expand Up @@ -122,7 +123,8 @@ public interface SqlDatabase extends
/**
* @return SqlWarehouse instance for more operations
*/
SqlWarehouse castToWarehouse();
@Method
SqlWarehouse asWarehouse();

/**
* @return returns the list of all restore points on the database
Expand Down Expand Up @@ -166,13 +168,10 @@ public interface SqlDatabase extends
interface Definition extends
DefinitionStages.Blank,
DefinitionStages.WithCreate,
DefinitionStages.WithCollation,
DefinitionStages.WithEdition,
DefinitionStages.WithElasticPoolName,
DefinitionStages.WithSourceDatabaseId,
DefinitionStages.WithElasticPoolName,
DefinitionStages.WithCreateMode,
DefinitionStages.WithCreateWithLessOptions,
DefinitionStages.WithExistingDatabase {
DefinitionStages.WithCreateWithLessOptions {
}

/**
Expand All @@ -182,20 +181,22 @@ interface DefinitionStages {
/**
* The first stage of the SQL Server definition.
*/
interface Blank extends WithElasticPoolName {
interface Blank extends WithAllDifferentOptions {
}

/**
* The SQL Database interface with all starting options for definition.
*/
interface WithAllDifferentOptions extends
WithElasticPoolName,
WithSourceDatabaseId,
WithCreate {
}

/**
* The SQL Database definition to set the elastic pool for database.
*/
interface WithElasticPoolName {
/**
* Specifies database to be created without elastic pool.
*
* @return The next stage of the definition.
*/
WithExistingDatabase withoutElasticPool();

/**
* Sets the existing elastic pool for the SQLDatabase.
*
Expand Down Expand Up @@ -224,13 +225,7 @@ interface WithElasticPoolName {
/**
* The stage to decide whether using existing database or not.
*/
interface WithExistingDatabase extends WithSourceDatabaseId {
/**
* Sets the creation flow to ask relevant question when source database is not specified.
*
* @return The next stage of the definition.
*/
WithCreate withoutSourceDatabaseId();
interface WithExistingDatabase extends WithSourceDatabaseId, WithCreateWithElasticPoolOptions {
}

/**
Expand Down Expand Up @@ -271,10 +266,17 @@ interface WithCreateMode {
WithCreateWithLessOptions withMode(CreateMode createMode);
}

/**
*
*/
interface WithCreateWithElasticPoolOptions extends WithCollation, WithMaxSizeBytes, WithCreateWithLessOptions {

}

/**
* The SQL Database definition to set the collation for database.
*/
interface WithCollation {
interface WithCollationAllCreateOptions {
/**
* Sets the collation for the SQL Database.
*
Expand All @@ -284,6 +286,19 @@ interface WithCollation {
WithCreate withCollation(String collation);
}

/**
* The SQL Database definition to set the collation for database.
*/
interface WithCollation {
/**
* Sets the collation for the SQL Database.
*
* @param collation collation to be set for database
* @return The next stage of the definition
*/
WithCreateWithElasticPoolOptions withCollation(String collation);
}

/**
* The SQL Database definition to set the edition for database.
*/
Expand All @@ -297,10 +312,11 @@ interface WithEdition {
WithCreate withEdition(DatabaseEditions edition);
}


/**
* The SQL Database definition to set the Max Size in Bytes for database.
*/
interface WithMaxSizeBytes {
interface WithMaxSizeBytesAllCreateOptions {
/**
* Sets the max size in bytes for SQL Database.
*
Expand All @@ -313,6 +329,22 @@ interface WithMaxSizeBytes {
WithCreate withMaxSizeBytes(long maxSizeBytes);
}

/**
* The SQL Database definition to set the Max Size in Bytes for database.
*/
interface WithMaxSizeBytes {
/**
* Sets the max size in bytes for SQL Database.
*
* @param maxSizeBytes max size of the Azure SQL Database expressed in bytes. Note: Only
* the following sizes are supported (in addition to limitations being
* placed on each edition): { 100 MB | 500 MB |1 GB | 5 GB | 10 GB | 20
* GB | 30 GB … 150 GB | 200 GB … 500 GB }
* @return The next stage of the definition.
*/
WithCreateWithElasticPoolOptions withMaxSizeBytes(long maxSizeBytes);
}

/**
* The SQL Database definition to set the service level objective.
*/
Expand All @@ -332,10 +364,10 @@ interface WithServiceObjective {
* specify.
*/
interface WithCreate extends
WithMaxSizeBytes,
WithServiceObjective,
WithCollation,
WithEdition,
WithCollationAllCreateOptions,
WithMaxSizeBytesAllCreateOptions,
WithCreateWithLessOptions {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class SqlDatabaseImpl
SqlDatabaseImpl>
implements SqlDatabase,
SqlDatabase.Definition,
SqlDatabase.DefinitionStages.WithCreateWithElasticPoolOptions,
SqlDatabase.DefinitionStages.WithExistingDatabase,
SqlDatabase.Update,
IndependentChild.DefinitionStages.WithParentResource<SqlDatabase, SqlServer> {
protected final DatabasesInner innerCollection;
Expand Down Expand Up @@ -145,7 +147,7 @@ public boolean isDataWarehouse() {
}

@Override
public SqlWarehouse castToWarehouse() {
public SqlWarehouse asWarehouse() {
if (this.isDataWarehouse()) {
return (SqlWarehouse) this;
}
Expand Down Expand Up @@ -333,11 +335,6 @@ public SqlDatabaseImpl withMode(CreateMode createMode) {
return this;
}

@Override
public SqlDatabaseImpl withoutSourceDatabaseId() {
return this;
}

@Override
public SqlDatabase.DefinitionStages.WithCreateMode withSourceDatabase(String sourceDatabaseId) {
this.inner().withSourceDatabaseId(sourceDatabaseId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public SqlElasticPoolImpl withStorageCapacity(int storageMB) {
@Override
public SqlElasticPoolImpl withNewDatabase(String databaseName) {
this.databaseCreatableMap.put(databaseName,
(SqlDatabaseImpl) this.databasesImpl.define(databaseName).withExistingElasticPool(this.name()).withoutSourceDatabaseId());
(SqlDatabaseImpl) this.databasesImpl.define(databaseName).withExistingElasticPool(this.name()));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class SqlServerImpl
private final RecommendedElasticPoolsInner recommendedElasticPoolsInner;
private final Map<String, SqlElasticPool.DefinitionStages.WithCreate> elasticPoolCreatableMap;
private final Map<String, SqlFirewallRule.DefinitionStages.WithCreate> firewallRuleCreatableMap;
private final Map<String, SqlDatabase.DefinitionStages.WithCreate> databaseCreatableMap;
private final Map<String, SqlDatabase.DefinitionStages.WithAllDifferentOptions> databaseCreatableMap;
private FirewallRulesImpl firewallRulesImpl;
private ElasticPoolsImpl elasticPoolsImpl;
private DatabasesImpl databasesImpl;
Expand Down Expand Up @@ -236,7 +236,7 @@ public SqlServerImpl withNewDatabase(String databaseName) {
this.databaseCreatableMap.remove(databaseName);

this.databaseCreatableMap.put(databaseName,
this.databases().define(databaseName).withoutElasticPool().withoutSourceDatabaseId());
this.databases().define(databaseName));
return this;
}

Expand Down Expand Up @@ -276,9 +276,8 @@ private void withDatabaseInElasticPool(String databaseName, String elasticPoolNa
this.databaseCreatableMap.remove(databaseName);

this.databaseCreatableMap.put(databaseName,
this.databases().define(databaseName)
.withExistingElasticPool(elasticPoolName)
.withoutSourceDatabaseId());
(SqlDatabase.DefinitionStages.WithAllDifferentOptions) this.databases().define(databaseName)
.withExistingElasticPool(elasticPoolName));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ public void canCRUDSqlDatabase() throws Exception {
SqlServer sqlServer = createSqlServer();
Observable<Indexable> resourceStream = sqlServer.databases()
.define(SQL_DATABASE_NAME)
.withoutElasticPool()
.withoutSourceDatabaseId()
.withCollation(COLLATION)
.withEdition(DatabaseEditions.STANDARD)
.createAsync();

SqlDatabase sqlDatabase = Utils.<SqlDatabase>rootResource(resourceStream)
Expand Down Expand Up @@ -338,10 +334,8 @@ public void canCRUDSqlDatabase() throws Exception {
// Add another database to the server
resourceStream = sqlServer.databases()
.define("newDatabase")
.withoutElasticPool()
.withoutSourceDatabaseId()
.withCollation(COLLATION)
.withEdition(DatabaseEditions.STANDARD)
.withCollation(COLLATION)
.createAsync();

sqlDatabase = Utils.<SqlDatabase>rootResource(resourceStream)
Expand All @@ -362,10 +356,8 @@ public void canManageReplicationLinks() throws Exception {

Observable<Indexable> resourceStream = sqlServer1.databases()
.define(SQL_DATABASE_NAME)
.withoutElasticPool()
.withoutSourceDatabaseId()
.withCollation(COLLATION)
.withEdition(DatabaseEditions.STANDARD)
.withCollation(COLLATION)
.createAsync();

SqlDatabase databaseInServer1 = Utils.<SqlDatabase>rootResource(resourceStream)
Expand All @@ -375,7 +367,6 @@ public void canManageReplicationLinks() throws Exception {
validateSqlDatabase(databaseInServer1, SQL_DATABASE_NAME);
SqlDatabase databaseInServer2 = sqlServer2.databases()
.define(SQL_DATABASE_NAME)
.withoutElasticPool()
.withSourceDatabase(databaseInServer1.id())
.withMode(CreateMode.ONLINE_SECONDARY)
.create();
Expand Down Expand Up @@ -428,10 +419,8 @@ public void canDoOperationsOnDataWarehouse() throws Exception {

Observable<Indexable> resourceStream = sqlServer.databases()
.define(SQL_DATABASE_NAME)
.withoutElasticPool()
.withoutSourceDatabaseId()
.withCollation(COLLATION)
.withEdition(DatabaseEditions.DATA_WAREHOUSE)
.withCollation(COLLATION)
.createAsync();

SqlDatabase sqlDatabase = Utils.<SqlDatabase>rootResource(resourceStream)
Expand All @@ -444,7 +433,7 @@ public void canDoOperationsOnDataWarehouse() throws Exception {
Assert.assertTrue(sqlDatabase.isDataWarehouse());

// Get
SqlWarehouse dataWarehouse = sqlServer.databases().get(SQL_DATABASE_NAME).castToWarehouse();
SqlWarehouse dataWarehouse = sqlServer.databases().get(SQL_DATABASE_NAME).asWarehouse();

Assert.assertNotNull(dataWarehouse);
Assert.assertEquals(dataWarehouse.name(), SQL_DATABASE_NAME);
Expand Down Expand Up @@ -479,10 +468,7 @@ public void canCRUDSqlDatabaseWithElasticPool() throws Exception {
Observable<Indexable> resourceStream = sqlServer.databases()
.define(SQL_DATABASE_NAME)
.withNewElasticPool(sqlElasticPoolCreatable)
.withoutSourceDatabaseId()
.withCollation(COLLATION)
.withEdition(DatabaseEditions.STANDARD)
.withServiceObjective(ServiceObjectiveName.S1)
.createAsync();

SqlDatabase sqlDatabase = Utils.<SqlDatabase>rootResource(resourceStream)
Expand Down Expand Up @@ -580,9 +566,7 @@ public void canCRUDSqlDatabaseWithElasticPool() throws Exception {
resourceStream = sqlServer.databases()
.define("newDatabase")
.withExistingElasticPool(sqlElasticPool)
.withoutSourceDatabaseId()
.withCollation(COLLATION)
.withEdition(DatabaseEditions.STANDARD)
.createAsync();

sqlDatabase = Utils.<SqlDatabase>rootResource(resourceStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.microsoft.azure.management.sql.implementation.SqlServerManager;
import okhttp3.logging.HttpLoggingInterceptor;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;
Expand All @@ -15,12 +17,9 @@ public abstract class SqlServerTestBase {
protected static ResourceManager resourceManager;
protected static SqlServerManager sqlServerManager;

public static void createClients() {
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(
System.getenv("client-id"),
System.getenv("domain"),
System.getenv("secret"),
AzureEnvironment.AZURE);
public static void createClients() throws IOException {
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);

RestClient restClient = new RestClient.Builder()
.withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
Expand All @@ -32,9 +31,9 @@ public static void createClients() {

resourceManager = ResourceManager
.authenticate(restClient)
.withSubscription(System.getenv("subscription-id"));
.withSubscription(credentials.defaultSubscriptionId());

sqlServerManager = SqlServerManager
.authenticate(restClient, System.getenv("subscription-id"));
.authenticate(restClient, credentials.defaultSubscriptionId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ public static void main(String[] args) {

System.out.println("Creating SQL database " + sqlDbName + "...");

SqlDatabase db = server.databases().define(sqlDbName)
.withoutElasticPool()
.withoutSourceDatabaseId()
.create();
SqlDatabase db = server.databases().define(sqlDbName).create();

System.out.println("Created SQL database " + db.name());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ public static void main(String[] args) {
// Create a Database in SQL server created above.
System.out.println("Creating a database");

SqlDatabase database = sqlServer.databases().define(databaseName)
.withoutElasticPool()
.withoutSourceDatabaseId()
.withEdition(DatabaseEditions.BASIC)
SqlDatabase database = sqlServer.databases()
.define(databaseName)
.create();
Utils.print(database);

Expand Down
Loading

0 comments on commit 9eca49f

Please sign in to comment.