diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/IndependentChildResourceImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/IndependentChildResourceImpl.java index f01b36e12d35..5431821e6a70 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/IndependentChildResourceImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/arm/models/implementation/IndependentChildResourceImpl.java @@ -161,7 +161,6 @@ public FluentModelImplT withExistingParentResource(String groupName, String pare public FluentModelImplT withExistingParentResource(FluentParentModelT existingParentResource) { this.inner().withLocation(existingParentResource.regionName()); return withExistingParentResource(existingParentResource.resourceGroupName(), existingParentResource.name()); - } @Override @@ -175,7 +174,9 @@ public FluentModelImplT withNewParentResource(Creatable pare @Override public void setInner(InnerModelT inner) { - this.parentName = ResourceId.parseResourceId(inner.id()).name(); + if (inner.id() != null) { + this.parentName = ResourceId.parseResourceId(inner.id()).parent().name(); + } super.setInner(inner); } diff --git a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/IndexableRefreshableWrapperImpl.java b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/IndexableRefreshableWrapperImpl.java index 1a90f26b6051..8a407435c84f 100644 --- a/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/IndexableRefreshableWrapperImpl.java +++ b/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/fluentcore/model/implementation/IndexableRefreshableWrapperImpl.java @@ -22,7 +22,7 @@ public abstract class IndexableRefreshableWrapperImpl private InnerModelT innerObject; protected IndexableRefreshableWrapperImpl(InnerModelT innerObject) { - this.innerObject = innerObject; + this.setInner(innerObject); } @Override diff --git a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlDatabase.java b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlDatabase.java index b55ef7733eec..e06b90f90c0b 100644 --- a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlDatabase.java +++ b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlDatabase.java @@ -9,6 +9,7 @@ import com.microsoft.azure.management.apigeneration.Fluent; 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; import com.microsoft.azure.management.resources.fluentcore.model.Refreshable; import com.microsoft.azure.management.resources.fluentcore.model.Updatable; import com.microsoft.azure.management.resources.fluentcore.model.Wrapper; @@ -25,7 +26,7 @@ /** - * An immutable client-side representation of an Azure SQL Server. + * An immutable client-side representation of an Azure SQL Database. */ @Fluent public interface SqlDatabase extends @@ -35,7 +36,7 @@ public interface SqlDatabase extends Wrapper { /** - * @return the SQL Server name in which this database resides. + * @return the SQL Server name to which this database belongs */ String sqlServerName(); @@ -188,19 +189,71 @@ interface WithEdition { interface WithElasticPoolName { /** * Sets the existing elastic pool for the SQLDatabase. - * @param elasticPoolName of for the SQL Database. + * @param elasticPoolName for the SQL Database. * @return The next stage of definition. */ WithCreate withExistingElasticPoolName(String elasticPoolName); + + /** + * Sets the existing elastic pool for the SQLDatabase. + * @param sqlElasticPool for the SQL Database. + * @return The next stage of definition. + */ + WithCreate withExistingElasticPoolName(SqlElasticPool sqlElasticPool); + + /** + * Sets the new elastic pool for the SQLDatabase, this will create a new elastic pool while creating database. + * @param elasticPoolName name for new elastic pool to be created for the SQL Database. + * @param elasticPoolEdition edition for new elastic pool to be created for the SQL Database. + * @return The next stage of definition. + */ + WithCreate withNewElasticPool(String elasticPoolName, ElasticPoolEditions elasticPoolEdition); + + /** + * Sets the new elastic pool for the SQLDatabase, this will create a new elastic pool while creating database. + * @param sqlElasticPool creatable definition for new elastic pool to be created for the SQL Database. + * @return The next stage of definition. + */ + WithCreate withNewElasticPool(SqlElasticPool.DefinitionStages.WithCreate sqlElasticPool); + } + + /** + * A resource definition allowing SQLServer to be attached with SQLDatabase. + */ + interface WithSqlServer { + /** + * Creates a new database resource under SQLServer. + * + * @param groupName the name of the resource group for SQLServer. + * @param sqlServerName the name of the sQLServer. + * @return the creatable for the child resource + */ + Creatable withExistingSqlServer(String groupName, String sqlServerName); + + /** + * Creates a new database resource under SQLServer. + * + * @param sqlServerCreatable a creatable definition for the SQLServer + * @return the creatable for the SQLDatabase + */ + Creatable withNewSqlServer(Creatable sqlServerCreatable); + + /** + * Creates a new database resource under SQLServer. + * + * @param existingSqlServer the SQLServer under which this database to be created. + * @return the creatable for the SQLDatabase + */ + Creatable withExistingSqlServer(SqlServer existingSqlServer); } /** - * A SQL Server definition with sufficient inputs to create a new + * A SQL Database definition with sufficient inputs to create a new * SQL Server in the cloud, but exposing additional optional inputs to * specify. */ interface WithCreate extends - IndependentChildResource.DefinitionStages.WithParentResource, + WithSqlServer, DefinitionWithTags, WithElasticPoolName { } diff --git a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlElasticPool.java b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlElasticPool.java new file mode 100644 index 000000000000..5da8d5fce60d --- /dev/null +++ b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlElasticPool.java @@ -0,0 +1,152 @@ +/** + * 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.sql; + +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; +import com.microsoft.azure.management.resources.fluentcore.model.Refreshable; +import com.microsoft.azure.management.resources.fluentcore.model.Updatable; +import com.microsoft.azure.management.resources.fluentcore.model.Wrapper; +import com.microsoft.azure.management.sql.implementation.ElasticPoolInner; +import org.joda.time.DateTime; + +/** + * An immutable client-side representation of an Azure SQL ElasticPool. + */ +public interface SqlElasticPool extends + IndependentChildResource, + Refreshable, + Updatable, + Wrapper { + + /** + * @return the SQL Server name to which this elastic pool belongs + */ + String sqlServerName(); + + /** + * @return the creation date of the Azure SQL Elastic Pool + */ + DateTime creationDate(); + + /** + * @return the state of the Azure SQL Elastic Pool + */ + String state(); + + /** + * @return the edition of Azure SQL Elastic Pool + */ + ElasticPoolEditions edition(); + + /** + * @return The total shared DTU for the SQL Azure Database Elastic Pool + */ + int dtu(); + + + /** + * @return the maximum DTU any one SQL Azure database can consume. + */ + int databaseDtuMax(); + + /** + * @return the minimum DTU all SQL Azure Databases are guaranteed + */ + int databaseDtuMin(); + + /** + * @return the storage limit for the SQL Azure Database Elastic Pool in MB + */ + int storageMB(); + + /** + * Container interface for all the definitions that need to be implemented. + */ + interface Definition extends + DefinitionStages.Blank, + DefinitionStages.WithEdition, + DefinitionStages.WithCreate { + + } + + /** + * Grouping of all the storage account definition stages. + */ + interface DefinitionStages { + /** + * The first stage of the SQL Server definition. + */ + interface Blank extends SqlElasticPool.DefinitionStages.WithEdition { + } + + /** + * The SQL Elastic Pool definition to set the edition for database. + */ + interface WithEdition { + /** + * Sets the edition for the SQL Elastic Pool. + * @param edition edition to be set for elastic pool. + * @return The next stage of definition. + */ + SqlElasticPool.DefinitionStages.WithCreate withEdition(ElasticPoolEditions edition); + } + + /** + * A resource definition allowing SQLServer to be attached with SQLDatabase. + */ + interface WithSqlServer { + /** + * Creates a new SqlElasticPool resource under SQLServer. + * + * @param groupName the name of the resource group for SQLServer. + * @param sqlServerName the name of the sQLServer. + * @return the creatable for the child resource + */ + Creatable withExistingSqlServer(String groupName, String sqlServerName); + + /** + * Creates a new SqlElasticPool resource under SQLServer. + * + * @param sqlServerCreatable a creatable definition for the SQLServer + * @return the creatable for the SQLDatabase + */ + Creatable withNewSqlServer(Creatable sqlServerCreatable); + + /** + * Creates a new SqlElasticPool resource under SQLServer. + * + * @param existingSqlServer the SQLServer under which this SqlElasticPool to be created. + * @return the creatable for the SQLDatabase + */ + Creatable withExistingSqlServer(SqlServer existingSqlServer); + } + /** + * A SQL Server definition with sufficient inputs to create a new + * SQL Server in the cloud, but exposing additional optional inputs to + * specify. + */ + interface WithCreate extends + WithSqlServer, + DefinitionWithTags { + } + } + + /** + * The template for a SQLElasticPool update operation, containing all the settings that can be modified. + */ + interface Update extends + Appliable { + } + + /** + * Grouping of all the SQLElasticPool update stages. + */ + interface UpdateStages { + } +} diff --git a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlElasticPools.java b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlElasticPools.java new file mode 100644 index 000000000000..b1597f112d43 --- /dev/null +++ b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlElasticPools.java @@ -0,0 +1,64 @@ +/** + * 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.sql; + +import com.microsoft.azure.PagedList; +import com.microsoft.azure.management.apigeneration.Fluent; +import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsDeletingByParent; +import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingById; +import com.microsoft.azure.management.resources.fluentcore.arm.models.GroupableResource; +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsBatchCreation; +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsCreating; +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsDeleting; + +/** + * Entry point to SQL Elastic Pool management API. + */ +@Fluent +public interface SqlElasticPools extends + SupportsCreating, + SupportsDeleting, + SupportsGettingById, + SupportsBatchCreation, + SupportsDeletingByParent { + + /** + * Gets the SQLElasticPool based on the resource group name, SQLServer name and SQLElasticPool name. + * + * @param resourceGroup the name of resource group. + * @param sqlServerName the name of SQLServer. + * @param name the name of SQLElasticPool. + * @return an immutable representation of the SQLElasticPool + */ + SqlElasticPool getBySqlServer(String resourceGroup, String sqlServerName, String name); + + /** + * Gets the SQLElasticPool based on the SQLServer instance and SQLElasticPool name. + * + * @param sqlServer the instance of SQLServer. + * @param name the name of SQLElasticPool + * @return an immutable representation of the SQLElasticPool + */ + SqlElasticPool getBySqlServer(GroupableResource sqlServer, String name); + + /** + * Lists resources of the specified type in the specified resource group and SQLServer. + * + * @param resourceGroupName the name of the resource group to list the resources from + * @param sqlServerName the name of SQLServer + * @return the list of SQLElasticPools in a SQLServer + */ + PagedList listBySqlServer(String resourceGroupName, String sqlServerName); + + /** + * Gets the SQLElasticPool based on the SQLServer. + * + * @param sqlServer the instance of SQLServer + * @return the list of SQLElasticPools in a SQLServer + */ + PagedList listBySqlServer(GroupableResource sqlServer); +} diff --git a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlServer.java b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlServer.java index 1bd2edba39a5..f5ce915f2963 100644 --- a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlServer.java +++ b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/SqlServer.java @@ -86,12 +86,10 @@ interface WithAdminUserName { * A SQL Server definition setting admin user password. */ interface WithPassword { - WithVersion withPassword(String password); + WithCreate withPassword(String password); } /** - * TODO - ans - Remove this if this is optional. Currently from REST APIs if I don't pass it then it - * it became 2.0. * A SQL Server definition setting version. */ interface WithVersion { @@ -105,7 +103,8 @@ interface WithVersion { */ interface WithCreate extends Creatable, - DefinitionWithTags { + DefinitionWithTags, + WithVersion { } } /** diff --git a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlDatabaseImpl.java b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlDatabaseImpl.java index 4fe084813e6c..84ad9b2e5701 100644 --- a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlDatabaseImpl.java +++ b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlDatabaseImpl.java @@ -6,11 +6,15 @@ package com.microsoft.azure.management.sql.implementation; +import com.microsoft.azure.management.resources.fluentcore.arm.models.IndependentChildResource; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.IndependentChildResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import com.microsoft.azure.management.sql.DatabaseEditions; +import com.microsoft.azure.management.sql.ElasticPoolEditions; import com.microsoft.azure.management.sql.SqlDatabase; +import com.microsoft.azure.management.sql.SqlElasticPool; +import com.microsoft.azure.management.sql.SqlElasticPools; import com.microsoft.azure.management.sql.SqlServer; -import org.apache.commons.lang3.NotImplementedException; import org.joda.time.DateTime; import rx.Observable; import rx.functions.Func1; @@ -29,14 +33,20 @@ public class SqlDatabaseImpl SqlDatabaseImpl> implements SqlDatabase, SqlDatabase.Definition, - SqlDatabase.Update { + SqlDatabase.Update, + IndependentChildResource.DefinitionStages.WithParentResource { private final DatabasesInner innerCollection; + private final SqlElasticPools sqlElasticPools; + private SqlElasticPool.DefinitionStages.WithCreate creatableSqlElasticPool; + private String elasticPoolCreatableKey; protected SqlDatabaseImpl(String name, DatabaseInner innerObject, - DatabasesInner innerCollection) { + DatabasesInner innerCollection, + SqlElasticPools sqlElasticPools) { super(name, innerObject); this.innerCollection = innerCollection; + this.sqlElasticPools = sqlElasticPools; } @Override @@ -143,6 +153,10 @@ public SqlDatabase refresh() { @Override protected Observable createChildResourceAsync() { final SqlDatabase self = this; + if (this.elasticPoolCreatableKey != null) { + SqlElasticPool sqlElasticPool = (SqlElasticPool) this.createdResource(this.elasticPoolCreatableKey); + withExistingElasticPoolName(sqlElasticPool); + } return this.innerCollection.createOrUpdateAsync(this.resourceGroupName(), this.sqlServerName(), this.name(), this.inner()) .map(new Func1() { @Override @@ -168,6 +182,57 @@ public SqlDatabaseImpl withEdition(DatabaseEditions edition) { @Override public SqlDatabaseImpl withExistingElasticPoolName(String elasticPoolName) { - throw new NotImplementedException("Will be implemented once we put support for elastic pools"); + this.inner().withElasticPoolName(elasticPoolName); + return this; + } + + @Override + public SqlDatabaseImpl withExistingElasticPoolName(SqlElasticPool sqlElasticPool) { + return this.withExistingElasticPoolName(sqlElasticPool.name()); + } + + @Override + public SqlDatabaseImpl withNewElasticPool(String elasticPoolName, ElasticPoolEditions elasticPoolEdition) { + creatableSqlElasticPool = this.sqlElasticPools.define(elasticPoolName).withEdition(elasticPoolEdition); + return this.withExistingElasticPoolName(elasticPoolName); + } + + @Override + public SqlDatabaseImpl withNewElasticPool(SqlElasticPool.DefinitionStages.WithCreate sqlElasticPool) { + creatableSqlElasticPool = sqlElasticPool; + return this; + } + + @Override + public Creatable withExistingSqlServer(String groupName, String sqlServerName) { + if (creatableSqlElasticPool != null) { + handleElasticPoolCreatable(creatableSqlElasticPool.withExistingSqlServer(groupName, sqlServerName)); + } + return withExistingParentResource(groupName, sqlServerName); + } + + @Override + public Creatable withNewSqlServer(Creatable sqlServerCreatable) { + if (creatableSqlElasticPool != null) { + handleElasticPoolCreatable(creatableSqlElasticPool.withNewSqlServer(sqlServerCreatable)); + } + return withNewParentResource(sqlServerCreatable); + } + + @Override + public Creatable withExistingSqlServer(SqlServer existingSqlServer) { + if (creatableSqlElasticPool != null) { + handleElasticPoolCreatable(creatableSqlElasticPool.withExistingSqlServer(existingSqlServer)); + } + return withExistingParentResource(existingSqlServer); + } + + + void handleElasticPoolCreatable(Creatable sqlElasticPoolCreatable) { + if (this.elasticPoolCreatableKey == null) { + this.elasticPoolCreatableKey = sqlElasticPoolCreatable.key(); + this.addCreatableDependency(sqlElasticPoolCreatable); + } + this.creatableSqlElasticPool = null; } } diff --git a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlDatabasesImpl.java b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlDatabasesImpl.java index be9e76d333b5..e441361074b8 100644 --- a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlDatabasesImpl.java +++ b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlDatabasesImpl.java @@ -39,7 +39,8 @@ protected SqlDatabaseImpl wrapModel(String name) { return new SqlDatabaseImpl( name, inner, - this.innerCollection); + this.innerCollection, + manager.sqlElasticPools()); } @Override @@ -54,7 +55,7 @@ public PagedList listByParent(String resourceGroupName, String pare @Override protected SqlDatabaseImpl wrapModel(DatabaseInner inner) { - return new SqlDatabaseImpl(inner.name(), inner, this.innerCollection); + return new SqlDatabaseImpl(inner.name(), inner, this.innerCollection, manager.sqlElasticPools()); } @Override diff --git a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlElasticPoolImpl.java b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlElasticPoolImpl.java new file mode 100644 index 000000000000..3645134f109d --- /dev/null +++ b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlElasticPoolImpl.java @@ -0,0 +1,121 @@ +/** + * 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.sql.implementation; + +import com.microsoft.azure.management.resources.fluentcore.arm.models.IndependentChildResource; +import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.IndependentChildResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.azure.management.sql.ElasticPoolEditions; +import com.microsoft.azure.management.sql.SqlElasticPool; +import com.microsoft.azure.management.sql.SqlServer; +import org.joda.time.DateTime; +import rx.Observable; +import rx.functions.Func1; + +/** + * Implementation for SqlElasticPool and its parent interfaces. + */ +public class SqlElasticPoolImpl + extends IndependentChildResourceImpl< + SqlElasticPool, + SqlServer, + ElasticPoolInner, + SqlElasticPoolImpl> + implements SqlElasticPool, + SqlElasticPool.Definition, + SqlElasticPool.Update, + IndependentChildResource.DefinitionStages.WithParentResource { + private final ElasticPoolsInner innerCollection; + + protected SqlElasticPoolImpl(String name, + ElasticPoolInner innerObject, + ElasticPoolsInner innerCollection) { + super(name, innerObject); + this.innerCollection = innerCollection; + } + + @Override + public String sqlServerName() { + return this.parentName; + } + + @Override + public DateTime creationDate() { + return this.inner().creationDate(); + } + + @Override + public String state() { + return this.inner().state(); + } + + @Override + public ElasticPoolEditions edition() { + return this.inner().edition(); + } + + @Override + public int dtu() { + return this.inner().dtu(); + } + + @Override + public int databaseDtuMax() { + return this.inner().databaseDtuMax(); + } + + @Override + public int databaseDtuMin() { + return this.inner().databaseDtuMin(); + } + + @Override + public int storageMB() { + return this.inner().storageMB(); + } + + @Override + public SqlElasticPool refresh() { + this.innerCollection.get(this.resourceGroupName(), this.sqlServerName(), this.name()); + return this; + } + + @Override + protected Observable createChildResourceAsync() { + final SqlElasticPool self = this; + return this.innerCollection.createOrUpdateAsync(this.resourceGroupName(), this.sqlServerName(), this.name(), this.inner()) + .map(new Func1() { + @Override + public SqlElasticPool call(ElasticPoolInner databaseInner) { + setInner(databaseInner); + + return self; + } + }); + } + + @Override + public SqlElasticPoolImpl withEdition(ElasticPoolEditions edition) { + this.inner().withEdition(edition); + return this; + } + + @Override + public Creatable withExistingSqlServer(String groupName, String sqlServerName) { + return this.withExistingParentResource(groupName, sqlServerName); + } + + @Override + public Creatable withNewSqlServer(Creatable sqlServerCreatable) { + return this.withNewParentResource(sqlServerCreatable); + } + + @Override + public Creatable withExistingSqlServer(SqlServer existingSqlServer) { + return this.withExistingParentResource(existingSqlServer); + } +} diff --git a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlElasticPoolsImpl.java b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlElasticPoolsImpl.java new file mode 100644 index 000000000000..9bd82e4abe04 --- /dev/null +++ b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlElasticPoolsImpl.java @@ -0,0 +1,89 @@ +/** + * 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.sql.implementation; + +import com.microsoft.azure.PagedList; +import com.microsoft.azure.management.apigeneration.LangDefinition; +import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsGettingByParent; +import com.microsoft.azure.management.resources.fluentcore.arm.collection.SupportsListingByParent; +import com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.IndependentChildResourcesImpl; +import com.microsoft.azure.management.resources.fluentcore.arm.models.GroupableResource; +import com.microsoft.azure.management.sql.SqlElasticPool; +import com.microsoft.azure.management.sql.SqlElasticPools; +import rx.Observable; + +/** + * Implementation for SQLElasticPools and its parent interfaces. + */ +@LangDefinition +public class SqlElasticPoolsImpl extends IndependentChildResourcesImpl< + SqlElasticPool, + SqlElasticPoolImpl, + ElasticPoolInner, + ElasticPoolsInner, + SqlServerManager> + implements SqlElasticPools, + SupportsGettingByParent, + SupportsListingByParent { + protected SqlElasticPoolsImpl(ElasticPoolsInner innerCollection, SqlServerManager manager) { + super(innerCollection, manager); + } + + @Override + protected SqlElasticPoolImpl wrapModel(String name) { + ElasticPoolInner inner = new ElasticPoolInner(); + return new SqlElasticPoolImpl( + name, + inner, + this.innerCollection); + } + + @Override + public SqlElasticPool getByParent(String resourceGroup, String parentName, String name) { + return wrapModel(this.innerCollection.get(resourceGroup, parentName, name)); + } + + @Override + public PagedList listByParent(String resourceGroupName, String parentName) { + return wrapList(this.innerCollection.listByServer(resourceGroupName, parentName)); + } + + @Override + protected SqlElasticPoolImpl wrapModel(ElasticPoolInner inner) { + return new SqlElasticPoolImpl(inner.name(), inner, this.innerCollection); + } + + @Override + public SqlElasticPool.DefinitionStages.Blank define(String name) { + return wrapModel(name); + } + + @Override + public Observable deleteAsync(String groupName, String parentName, String name) { + return this.innerCollection.deleteAsync(groupName, parentName, name); + } + + @Override + public SqlElasticPool getBySqlServer(String resourceGroup, String sqlServerName, String name) { + return this.getByParent(resourceGroup, sqlServerName, name); + } + + @Override + public SqlElasticPool getBySqlServer(GroupableResource sqlServer, String name) { + return this.getByParent(sqlServer, name); + } + + @Override + public PagedList listBySqlServer(String resourceGroupName, String sqlServerName) { + return this.listByParent(resourceGroupName, sqlServerName); + } + + @Override + public PagedList listBySqlServer(GroupableResource sqlServer) { + return this.listByParent(sqlServer); + } +} diff --git a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlServerManager.java b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlServerManager.java index 103696620f0a..ea0adec14cc2 100644 --- a/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlServerManager.java +++ b/azure-mgmt-sql/src/main/java/com/microsoft/azure/management/sql/implementation/SqlServerManager.java @@ -12,6 +12,7 @@ import com.microsoft.azure.management.resources.fluentcore.arm.implementation.AzureConfigurableImpl; import com.microsoft.azure.management.resources.fluentcore.arm.implementation.Manager; import com.microsoft.azure.management.sql.SqlDatabases; +import com.microsoft.azure.management.sql.SqlElasticPools; import com.microsoft.azure.management.sql.SqlServers; /** @@ -20,6 +21,7 @@ public class SqlServerManager extends Manager { private SqlServers sqlServers; private SqlDatabases sqlDatabases; + private SqlElasticPools sqlElasticPools; protected SqlServerManager(RestClient restClient, String subscriptionId) { super( @@ -61,6 +63,8 @@ public static SqlServerManager authenticate(RestClient restClient, String subscr return new SqlServerManager(restClient, subscriptionId); } + + /** * The interface allowing configurations to be set. */ @@ -99,7 +103,7 @@ public SqlServers sqlServers() { } /** - * @return the SQL Server management API entry point + * @return the SQL Database management API entry point */ public SqlDatabases sqlDatabases() { if (sqlDatabases == null) { @@ -110,4 +114,16 @@ public SqlDatabases sqlDatabases() { return sqlDatabases; } + + /** + * @return the SQL ElasticPool management API entry point + */ + public SqlElasticPools sqlElasticPools() { + if (sqlElasticPools == null) { + sqlElasticPools = new SqlElasticPoolsImpl( + super.innerManagementClient.elasticPools(), + this); + } + return sqlElasticPools; + } } diff --git a/azure-mgmt-sql/src/test/java/com/microsoft/azure/management/sql/SqlServerOperationsTests.java b/azure-mgmt-sql/src/test/java/com/microsoft/azure/management/sql/SqlServerOperationsTests.java index c2ba1e976f76..b62fe6b646d1 100644 --- a/azure-mgmt-sql/src/test/java/com/microsoft/azure/management/sql/SqlServerOperationsTests.java +++ b/azure-mgmt-sql/src/test/java/com/microsoft/azure/management/sql/SqlServerOperationsTests.java @@ -7,6 +7,7 @@ package com.microsoft.azure.management.sql; import com.microsoft.azure.CloudException; +import com.microsoft.azure.PagedList; import com.microsoft.azure.management.resources.fluentcore.arm.Region; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import org.junit.AfterClass; @@ -17,10 +18,12 @@ import java.util.List; public class SqlServerOperationsTests extends SqlServerTestBase { - private static final String RG_NAME = "javasqlserver1234"; - private static final String SQL_SERVER_NAME = "javasqlserver1234"; - private static final String SQL_DATABASE_NAME = "myTestDatabase"; + private static final String RG_NAME = "javasqlserver1238"; + private static final String SQL_SERVER_NAME = "javasqlserver1238"; + private static final String SQL_DATABASE_NAME = "myTestDatabase2"; private static final String COLLATION = "SQL_Latin1_General_CP1_CI_AS"; + private static final String SQL_ELASTIC_POOL_NAME = "testElasticPool2"; + @BeforeClass public static void setup() throws Exception { @@ -84,8 +87,8 @@ public void canCRUDSqlDatabase() throws Exception { SqlDatabase sqlDatabase = sqlServerManager.sqlDatabases() .define(SQL_DATABASE_NAME) .withCollation(COLLATION) - .withEdition(DatabaseEditions.FREE) - .withNewParentResource(sqlServerCreatable) + .withEdition(DatabaseEditions.STANDARD) + .withNewSqlServer(sqlServerCreatable) .createAsync().toBlocking().first(); validateSqlDatabase(sqlDatabase); @@ -104,11 +107,78 @@ public void canCRUDSqlDatabase() throws Exception { sqlServerManager.sqlDatabases().delete(sqlDatabase.id()); + // Add another database to the server + sqlDatabase = sqlServerManager.sqlDatabases() + .define("newDatabase") + .withCollation(COLLATION) + .withEdition(DatabaseEditions.STANDARD) + .withExistingSqlServer(sqlServer) + .createAsync().toBlocking().first(); + sqlServerManager.sqlDatabases().delete(sqlDatabase.resourceGroupName(), sqlDatabase.sqlServerName(), sqlDatabase.name()); + + sqlServerManager.sqlServers().delete(sqlServer.resourceGroupName(), sqlServer.name()); + try { + sqlServerManager.sqlServers().getById(sqlServer.id()); + Assert.assertTrue(false); + } + catch (CloudException exception) { + Assert.assertEquals(exception.getResponse().code(), 404); + } + } + + @Test + public void canCRUDSqlDatabaseWithElasticPool() throws Exception { + // Create + Creatable sqlServerCreatable = sqlServerManager.sqlServers() + .define(SQL_SERVER_NAME) + .withRegion(Region.US_CENTRAL) + .withNewResourceGroup(RG_NAME) + .withAdminUserName("userName") + .withPassword("P@ssword~1") + .withVersion(ServerVersion.ONE_TWO_FULL_STOP_ZERO); + + SqlDatabase sqlDatabase = sqlServerManager.sqlDatabases() + .define(SQL_DATABASE_NAME) + .withCollation(COLLATION) + .withEdition(DatabaseEditions.STANDARD) + .withNewElasticPool( + sqlServerManager.sqlElasticPools() + .define(SQL_ELASTIC_POOL_NAME) + .withEdition(ElasticPoolEditions.STANDARD)) + .withNewSqlServer(sqlServerCreatable) + .createAsync().toBlocking().first(); + + validateSqlDatabase(sqlDatabase); + + SqlServer sqlServer = sqlServerManager.sqlServers().getByGroup(RG_NAME, SQL_SERVER_NAME); + validateSqlServer(sqlServer); + + // Get Elastic pool + SqlElasticPool sqlElasticPool = sqlServerManager.sqlElasticPools().getBySqlServer(RG_NAME, SQL_SERVER_NAME, SQL_ELASTIC_POOL_NAME); + validateSqlElasticPool(sqlElasticPool); + + validateSqlElasticPool(sqlServerManager.sqlElasticPools().getById(sqlElasticPool.id())); + validateSqlElasticPool(sqlServerManager.sqlElasticPools().getBySqlServer(sqlServer, SQL_ELASTIC_POOL_NAME)); + validateSqlElasticPool(sqlServerManager.sqlElasticPools().getBySqlServer(sqlServer.resourceGroupName(), sqlServer.name(), SQL_ELASTIC_POOL_NAME)); + + // Get + validateSqlDatabaseWithElasticPool(sqlServerManager.sqlDatabases().getById(sqlDatabase.id())); + validateSqlDatabaseWithElasticPool(sqlServerManager.sqlDatabases().getBySqlServer(sqlServer, SQL_DATABASE_NAME)); + validateSqlDatabaseWithElasticPool(sqlServerManager.sqlDatabases().getBySqlServer(sqlServer.resourceGroupName(), sqlServer.name(), SQL_DATABASE_NAME)); + + // List + validateListSqlDatabase(sqlServerManager.sqlDatabases().listBySqlServer(sqlServer.resourceGroupName(), sqlServer.name())); + validateListSqlDatabase(sqlServerManager.sqlDatabases().listBySqlServer(sqlServer)); + + sqlServerManager.sqlDatabases().delete(sqlDatabase.id()); + + // Add another database to the server sqlDatabase = sqlServerManager.sqlDatabases() .define("newDatabase") .withCollation(COLLATION) - .withEdition(DatabaseEditions.FREE) - .withNewParentResource(sqlServerCreatable) + .withEdition(DatabaseEditions.STANDARD) + .withExistingElasticPoolName(sqlElasticPool) + .withExistingSqlServer(sqlServer) .createAsync().toBlocking().first(); sqlServerManager.sqlDatabases().delete(sqlDatabase.resourceGroupName(), sqlDatabase.sqlServerName(), sqlDatabase.name()); @@ -122,6 +192,78 @@ public void canCRUDSqlDatabase() throws Exception { } } + @Test + public void canCRUDSqlElasticPool() throws Exception { + // Create + Creatable sqlServerCreatable = sqlServerManager.sqlServers() + .define(SQL_SERVER_NAME) + .withRegion(Region.US_CENTRAL) + .withNewResourceGroup(RG_NAME) + .withAdminUserName("userName") + .withPassword("P@ssword~1") + .withVersion(ServerVersion.ONE_TWO_FULL_STOP_ZERO); + + SqlElasticPool sqlElasticPool = sqlServerManager.sqlElasticPools() + .define(SQL_ELASTIC_POOL_NAME) + .withEdition(ElasticPoolEditions.STANDARD) + .withNewSqlServer(sqlServerCreatable) + .createAsync().toBlocking().first(); + + validateSqlElasticPool(sqlElasticPool); + + SqlServer sqlServer = sqlServerManager.sqlServers().getByGroup(RG_NAME, SQL_SERVER_NAME); + validateSqlServer(sqlServer); + + // Get + validateSqlElasticPool(sqlServerManager.sqlElasticPools().getById(sqlElasticPool.id())); + validateSqlElasticPool(sqlServerManager.sqlElasticPools().getBySqlServer(sqlServer, SQL_ELASTIC_POOL_NAME)); + validateSqlElasticPool(sqlServerManager.sqlElasticPools().getBySqlServer(sqlServer.resourceGroupName(), sqlServer.name(), SQL_ELASTIC_POOL_NAME)); + + // List + validateListSqlElasticPool(sqlServerManager.sqlElasticPools().listBySqlServer(sqlServer.resourceGroupName(), sqlServer.name())); + validateListSqlElasticPool(sqlServerManager.sqlElasticPools().listBySqlServer(sqlServer)); + + sqlServerManager.sqlElasticPools().delete(sqlElasticPool.id()); + + // Add another database to the server + sqlElasticPool = sqlServerManager.sqlElasticPools() + .define("newElasticPool") + .withEdition(ElasticPoolEditions.STANDARD) + .withExistingSqlServer(sqlServer) + .createAsync().toBlocking().first(); + sqlServerManager.sqlElasticPools().delete(sqlElasticPool.resourceGroupName(), sqlElasticPool.sqlServerName(), sqlElasticPool.name()); + + sqlServerManager.sqlServers().delete(sqlServer.resourceGroupName(), sqlServer.name()); + try { + sqlServerManager.sqlServers().getById(sqlServer.id()); + Assert.assertTrue(false); + } + catch (CloudException exception) { + Assert.assertEquals(exception.getResponse().code(), 404); + } + } + + private void validateListSqlElasticPool(PagedList sqlElasticPools) { + boolean found = false; + for (SqlElasticPool elasticPool : sqlElasticPools) { + if (elasticPool.name().equals(SQL_ELASTIC_POOL_NAME)) { + found = true; + } + } + Assert.assertTrue(found); + } + + private static void validateSqlElasticPool(SqlElasticPool sqlElasticPool) { + Assert.assertNotNull(sqlElasticPool); + Assert.assertEquals(RG_NAME, sqlElasticPool.resourceGroupName()); + Assert.assertEquals(SQL_ELASTIC_POOL_NAME, sqlElasticPool.name()); + Assert.assertEquals(SQL_SERVER_NAME, sqlElasticPool.sqlServerName()); + Assert.assertEquals(ElasticPoolEditions.STANDARD, sqlElasticPool.edition()); + Assert.assertNotNull(sqlElasticPool.creationDate()); + Assert.assertNotEquals(0, sqlElasticPool.databaseDtuMax()); + Assert.assertNotEquals(0, sqlElasticPool.dtu()); + } + private static void validateListSqlDatabase(List sqlDatabases) { boolean found = false; for (SqlDatabase database : sqlDatabases) { @@ -132,7 +274,7 @@ private static void validateListSqlDatabase(List sqlDatabases) { Assert.assertTrue(found); } - static void validateSqlServer(SqlServer sqlServer) { + private static void validateSqlServer(SqlServer sqlServer) { Assert.assertNotNull(sqlServer); Assert.assertEquals(RG_NAME, sqlServer.resourceGroupName()); Assert.assertNotNull(sqlServer.fullyQualifiedDomainName()); @@ -140,10 +282,17 @@ static void validateSqlServer(SqlServer sqlServer) { Assert.assertEquals("userName", sqlServer.adminLogin()); } - static void validateSqlDatabase(SqlDatabase sqlDatabase) { + private static void validateSqlDatabase(SqlDatabase sqlDatabase) { Assert.assertNotNull(sqlDatabase); Assert.assertEquals(sqlDatabase.name(), SQL_DATABASE_NAME); + Assert.assertEquals(SQL_SERVER_NAME, sqlDatabase.sqlServerName()); Assert.assertEquals(sqlDatabase.collation(), COLLATION); - Assert.assertEquals(sqlDatabase.edition(), DatabaseEditions.FREE); + Assert.assertEquals(sqlDatabase.edition(), DatabaseEditions.STANDARD); + } + + + private static void validateSqlDatabaseWithElasticPool(SqlDatabase sqlDatabase) { + validateSqlDatabase(sqlDatabase); + Assert.assertEquals(SQL_ELASTIC_POOL_NAME, sqlDatabase.elasticPoolName()); } }