/// <summary>
        /// Get the entities from the service
        /// </summary>
        /// <returns>The list of entities</returns>
        protected override IEnumerable <AzureSqlDatabaseCopyModel> GetEntity()
        {
            string copyResourceGroupName = string.IsNullOrWhiteSpace(this.CopyResourceGroupName) ? this.ResourceGroupName : this.CopyResourceGroupName;
            string copyServerName        = string.IsNullOrWhiteSpace(this.CopyServerName) ? this.ServerName : this.CopyServerName;

            // We try to get the database.  Since this is a create copy, we don't want the copy database to exist
            try
            {
                ModelAdapter.GetDatabase(copyResourceGroupName, copyServerName, this.CopyDatabaseName);
            }
            catch (CloudException ex)
            {
                if (ex.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    // This is what we want.  We looked and there is no database with this name.
                    return(null);
                }

                // Unexpected exception encountered
                throw;
            }

            // The database already exists
            throw new PSArgumentException(
                      string.Format(Resources.DatabaseNameExists, this.CopyDatabaseName, copyServerName),
                      "CopyDatabaseName");
        }
        /// <summary>
        /// Get the entities from the service
        /// </summary>
        /// <returns>The list of entities</returns>
        protected override AzureSqlDatabaseCreateOrUpdateModel GetEntity()
        {
            // We try to get the database.  Since this is a create, we don't want the database to exist
            try
            {
                ModelAdapter.GetDatabase(this.ResourceGroupName, this.ServerName, this.DatabaseName);
            }
            catch (Hyak.Common.CloudException ex) // when using Hyak SDK
            {
                if (ex.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    // This is what we want.  We looked and there is no database with this name.
                    return(null);
                }

                // Unexpected exception encountered
                throw;
            }
            catch (Microsoft.Rest.Azure.CloudException ex) // when using AutoRest SDK
            {
                if (ex.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    // This is what we want.  We looked and there is no database with this name.
                    return(null);
                }

                // Unexpected exception encountered
                throw;
            }

            // The database already exists
            throw new PSArgumentException(
                      string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.DatabaseNameExists, this.DatabaseName, this.ServerName),
                      "DatabaseName");
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Get the entities from the service
 /// </summary>
 /// <returns>The list of entities</returns>
 protected override IEnumerable <AzureSqlDatabaseModel> GetEntity()
 {
     return(new List <AzureSqlDatabaseModel>()
     {
         ModelAdapter.GetDatabase(this.ResourceGroupName, this.ServerName, this.DatabaseName)
     });
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Update the database
        /// </summary>
        /// <param name="entity">The output of apply user input to model</param>
        /// <returns>The input entity</returns>
        protected override IEnumerable <AzureSqlDatabaseModel> PersistChanges(IEnumerable <AzureSqlDatabaseModel> entity)
        {
            switch (this.ParameterSetName)
            {
            case UpdateParameterSetName:
                return(new List <AzureSqlDatabaseModel>
                {
                    ModelAdapter.UpsertDatabaseWithNewSdk(
                        this.ResourceGroupName,
                        this.ServerName,
                        new AzureSqlDatabaseCreateOrUpdateModel
                    {
                        Database = entity.First()
                    })
                });

            case RenameParameterSetName:
                ModelAdapter.RenameDatabase(
                    this.ResourceGroupName,
                    this.ServerName,
                    this.DatabaseName,
                    this.NewName);

                return(new List <AzureSqlDatabaseModel>
                {
                    ModelAdapter.GetDatabase(
                        this.ResourceGroupName,
                        this.ServerName,
                        this.NewName)
                });

            default:
                throw new ArgumentException(this.ParameterSetName);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create the model from user input
        /// </summary>
        /// <param name="model">Model retrieved from service</param>
        /// <returns>The model that was passed in</returns>
        protected override IEnumerable <AzureSqlDatabaseModel> ApplyUserInputToModel(IEnumerable <AzureSqlDatabaseModel> model)
        {
            List <Model.AzureSqlDatabaseModel> newEntity = new List <AzureSqlDatabaseModel>();
            AzureSqlDatabaseModel newDbModel             = new AzureSqlDatabaseModel()
            {
                ResourceGroupName = ResourceGroupName,
                ServerName        = ServerName,
                DatabaseName      = DatabaseName,
                MaxSizeBytes      = MaxSizeBytes,
                Tags                    = TagsConversionHelper.ReadOrFetchTags(this, model.FirstOrDefault().Tags),
                ElasticPoolName         = ElasticPoolName,
                Location                = model.FirstOrDefault().Location,
                ReadScale               = ReadScale,
                ZoneRedundant           = MyInvocation.BoundParameters.ContainsKey("ZoneRedundant") ? (bool?)ZoneRedundant.ToBool() : null,
                LicenseType             = LicenseType ?? model.FirstOrDefault().LicenseType, // set to original license type
                AutoPauseDelayInMinutes = this.IsParameterBound(p => p.AutoPauseDelayInMinutes) ? AutoPauseDelayInMinutes : (int?)null,
                MinimumCapacity         = this.IsParameterBound(p => p.MinimumCapacity) ? MinimumCapacity : (double?)null,
                ReadReplicaCount        = this.IsParameterBound(p => p.ReadReplicaCount) ? ReadReplicaCount : (int?)null,
                BackupStorageRedundancy = BackupStorageRedundancy,
            };

            var database = ModelAdapter.GetDatabase(ResourceGroupName, ServerName, DatabaseName);

            Management.Sql.Models.Sku databaseCurrentSku = new Management.Sql.Models.Sku()
            {
                Name     = database.SkuName,
                Tier     = database.Edition,
                Family   = database.Family,
                Capacity = database.Capacity
            };

            // check if current db is serverless
            string databaseCurrentComputeModel = database.CurrentServiceObjectiveName.Contains("_S_") ? DatabaseComputeModel.Serverless : DatabaseComputeModel.Provisioned;

            if (this.ParameterSetName == UpdateParameterSetName)
            {
                newDbModel.SkuName = string.IsNullOrWhiteSpace(RequestedServiceObjectiveName) ? AzureSqlDatabaseAdapter.GetDatabaseSkuName(Edition) : RequestedServiceObjectiveName;
                newDbModel.Edition = Edition;

                newEntity.Add(newDbModel);
            }
            else if (this.ParameterSetName == VcoreDatabaseParameterSet)
            {
                if (!string.IsNullOrWhiteSpace(Edition) ||
                    !string.IsNullOrWhiteSpace(ComputeGeneration) ||
                    this.IsParameterBound(p => p.VCore))
                {
                    string skuTier = string.IsNullOrWhiteSpace(Edition) ? databaseCurrentSku.Tier : Edition;
                    string requestedComputeModel = string.IsNullOrWhiteSpace(ComputeModel) ? databaseCurrentComputeModel : ComputeModel;
                    newDbModel.SkuName  = AzureSqlDatabaseAdapter.GetDatabaseSkuName(skuTier, requestedComputeModel == DatabaseComputeModel.Serverless);
                    newDbModel.Edition  = skuTier;
                    newDbModel.Family   = string.IsNullOrWhiteSpace(ComputeGeneration) ? databaseCurrentSku.Family : ComputeGeneration;
                    newDbModel.Capacity = this.IsParameterBound(p => p.VCore) ? VCore : databaseCurrentSku.Capacity;
                }

                newEntity.Add(newDbModel);
            }

            return(newEntity);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Create the model from user input
        /// </summary>
        /// <param name="model">Model retrieved from service</param>
        /// <returns>The model that was passed in</returns>
        protected override IEnumerable <AzureSqlDatabaseModel> ApplyUserInputToModel(IEnumerable <AzureSqlDatabaseModel> model)
        {
            List <Model.AzureSqlDatabaseModel> newEntity = new List <AzureSqlDatabaseModel>();
            AzureSqlDatabaseModel newDbModel             = new AzureSqlDatabaseModel()
            {
                ResourceGroupName = ResourceGroupName,
                ServerName        = ServerName,
                DatabaseName      = DatabaseName,
                MaxSizeBytes      = MaxSizeBytes,
                Tags            = TagsConversionHelper.ReadOrFetchTags(this, model.FirstOrDefault().Tags),
                ElasticPoolName = ElasticPoolName,
                Location        = model.FirstOrDefault().Location,
                ReadScale       = ReadScale,
                ZoneRedundant   =
                    ZoneRedundant != null
                           ? (bool?)ZoneRedundant.ToBool()
                           : null,
                LicenseType = LicenseType ?? model.FirstOrDefault().LicenseType // set to original license type
            };

            var database = ModelAdapter.GetDatabase(ResourceGroupName, ServerName, DatabaseName);

            Management.Sql.Models.Sku databaseCurrentSku = new Management.Sql.Models.Sku()
            {
                Name     = database.SkuName,
                Tier     = database.Edition,
                Family   = database.Family,
                Capacity = database.Capacity
            };

            if (this.ParameterSetName == UpdateParameterSetName)
            {
                newDbModel.SkuName = string.IsNullOrWhiteSpace(RequestedServiceObjectiveName) ? AzureSqlDatabaseAdapter.GetDatabaseSkuName(Edition) : RequestedServiceObjectiveName;
                newDbModel.Edition = Edition;

                newEntity.Add(newDbModel);
            }
            else if (this.ParameterSetName == VcoreDatabaseParameterSet)
            {
                if (!string.IsNullOrWhiteSpace(Edition) ||
                    !string.IsNullOrWhiteSpace(ComputeGeneration) ||
                    MyInvocation.BoundParameters.ContainsKey("VCore"))
                {
                    string skuTier = string.IsNullOrWhiteSpace(Edition) ? databaseCurrentSku.Tier : Edition;
                    newDbModel.SkuName  = AzureSqlDatabaseAdapter.GetDatabaseSkuName(skuTier);
                    newDbModel.Edition  = skuTier;
                    newDbModel.Family   = string.IsNullOrWhiteSpace(ComputeGeneration) ? databaseCurrentSku.Family : ComputeGeneration;
                    newDbModel.Capacity = MyInvocation.BoundParameters.ContainsKey("VCore") ? VCore : databaseCurrentSku.Capacity;
                }

                newEntity.Add(newDbModel);
            }

            return(newEntity);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Create the model from user input
        /// </summary>
        /// <param name="model">Model retrieved from service</param>
        /// <returns>The model that was passed in</returns>
        protected override IEnumerable <AzureSqlDatabaseCopyModel> ApplyUserInputToModel(IEnumerable <AzureSqlDatabaseCopyModel> model)
        {
            string copyResourceGroup = string.IsNullOrWhiteSpace(CopyResourceGroupName) ? ResourceGroupName : CopyResourceGroupName;
            string copyServer        = string.IsNullOrWhiteSpace(CopyServerName) ? ServerName : CopyServerName;

            string location     = ModelAdapter.GetServerLocation(ResourceGroupName, ServerName);
            string copyLocation = copyServer.Equals(ServerName) ? location : ModelAdapter.GetServerLocation(copyResourceGroup, copyServer);

            Database.Model.AzureSqlDatabaseModel   sourceDb  = ModelAdapter.GetDatabase(ResourceGroupName, ServerName, DatabaseName);
            List <Model.AzureSqlDatabaseCopyModel> newEntity = new List <AzureSqlDatabaseCopyModel>();

            AzureSqlDatabaseCopyModel copyModel = new AzureSqlDatabaseCopyModel()
            {
                Location              = location,
                ResourceGroupName     = ResourceGroupName,
                ServerName            = ServerName,
                DatabaseName          = DatabaseName,
                CopyResourceGroupName = copyResourceGroup,
                CopyServerName        = copyServer,
                CopyDatabaseName      = CopyDatabaseName,
                CopyLocation          = copyLocation,
                ServiceObjectiveName  = ServiceObjectiveName,
                ElasticPoolName       = ElasticPoolName,
                Tags        = TagsConversionHelper.CreateTagDictionary(Tags, validate: true),
                LicenseType = LicenseType, // note: default license type is LicenseIncluded
                RequestedBackupStorageRedundancy = BackupStorageRedundancy,
                ZoneRedundant = this.IsParameterBound(p => p.ZoneRedundant) ? ZoneRedundant.ToBool() : (bool?)null,
            };

            if (ParameterSetName == DtuDatabaseParameterSet)
            {
                if (!string.IsNullOrWhiteSpace(ServiceObjectiveName))
                {
                    copyModel.SkuName = ServiceObjectiveName;
                }
                else if (string.IsNullOrWhiteSpace(ElasticPoolName))
                {
                    copyModel.SkuName  = sourceDb.CurrentServiceObjectiveName;
                    copyModel.Edition  = sourceDb.Edition;
                    copyModel.Capacity = sourceDb.Capacity;
                    copyModel.Family   = sourceDb.Family;
                }
            }
            else
            {
                copyModel.SkuName  = AzureSqlDatabaseAdapter.GetDatabaseSkuName(sourceDb.Edition);
                copyModel.Edition  = sourceDb.Edition;
                copyModel.Capacity = VCore;
                copyModel.Family   = ComputeGeneration;
            }

            newEntity.Add(copyModel);
            return(newEntity);
        }
        /// <summary>
        /// Create the model from user input
        /// </summary>
        /// <param name="model">Model retrieved from service</param>
        /// <returns>The model that was passed in</returns>
        protected override IEnumerable <AzureSqlDatabaseCopyModel> ApplyUserInputToModel(IEnumerable <AzureSqlDatabaseCopyModel> model)
        {
            string copyResourceGroup = string.IsNullOrWhiteSpace(CopyResourceGroupName) ? ResourceGroupName : CopyResourceGroupName;
            string copyServer        = string.IsNullOrWhiteSpace(CopyServerName) ? ServerName : CopyServerName;

            string location     = ModelAdapter.GetServerLocation(ResourceGroupName, ServerName);
            string copyLocation = copyServer.Equals(ServerName) ? location : ModelAdapter.GetServerLocation(copyResourceGroup, copyServer);

            Database.Model.AzureSqlDatabaseModel   sourceDb  = ModelAdapter.GetDatabase(ResourceGroupName, ServerName, DatabaseName);
            List <Model.AzureSqlDatabaseCopyModel> newEntity = new List <AzureSqlDatabaseCopyModel>();

            AzureSqlDatabaseCopyModel copyModel = new AzureSqlDatabaseCopyModel()
            {
                Location              = location,
                ResourceGroupName     = ResourceGroupName,
                ServerName            = ServerName,
                DatabaseName          = DatabaseName,
                CopyResourceGroupName = copyResourceGroup,
                CopyServerName        = copyServer,
                CopyDatabaseName      = CopyDatabaseName,
                CopyLocation          = copyLocation,
                ServiceObjectiveName  = ServiceObjectiveName,
                ElasticPoolName       = ElasticPoolName,
                Tags = TagsConversionHelper.CreateTagDictionary(Tags, validate: true)
            };

            if (ParameterSetName == DtuDatabaseParameterSet)
            {
                if (!string.IsNullOrWhiteSpace(ServiceObjectiveName))
                {
                    copyModel.SkuName = ServiceObjectiveName;
                }
                else if (string.IsNullOrWhiteSpace(ElasticPoolName))
                {
                    copyModel.SkuName  = sourceDb.CurrentServiceObjectiveName;
                    copyModel.Edition  = sourceDb.Edition;
                    copyModel.Capacity = sourceDb.Capacity;
                    copyModel.Family   = sourceDb.Family;
                }
            }
            else
            {
                copyModel.SkuName  = AzureSqlDatabaseAdapter.GetDatabaseSkuName(sourceDb.Edition);
                copyModel.Edition  = sourceDb.Edition;
                copyModel.Capacity = VCore;
                copyModel.Family   = ComputeGeneration;
            }

            newEntity.Add(copyModel);
            return(newEntity);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Create the model from user input
        /// </summary>
        /// <param name="model">Model retrieved from service</param>
        /// <returns>The model that was passed in</returns>
        protected override IEnumerable <AzureReplicationLinkModel> ApplyUserInputToModel(IEnumerable <AzureReplicationLinkModel> model)
        {
            string location = ModelAdapter.GetServerLocation(this.PartnerResourceGroupName, this.PartnerServerName);
            List <Model.AzureReplicationLinkModel> newEntity = new List <AzureReplicationLinkModel>();

            Database.Model.AzureSqlDatabaseModel primaryDb = ModelAdapter.GetDatabase(ResourceGroupName, ServerName, DatabaseName);

            AzureReplicationLinkModel linkModel = new AzureReplicationLinkModel()
            {
                PartnerLocation          = location,
                ResourceGroupName        = this.ResourceGroupName,
                ServerName               = this.ServerName,
                DatabaseName             = this.DatabaseName,
                PartnerResourceGroupName = this.PartnerResourceGroupName,
                PartnerServerName        = this.PartnerServerName,
                PartnerDatabaseName      = GetEffectivePartnerDatabaseName(this.DatabaseName, this.PartnerDatabaseName),
                SecondaryElasticPoolName = this.SecondaryElasticPoolName,
                AllowConnections         = this.AllowConnections,
                Tags        = TagsConversionHelper.CreateTagDictionary(Tags, validate: true),
                LicenseType = LicenseType,
                RequestedBackupStorageRedundancy = this.BackupStorageRedundancy,
                SecondaryType = SecondaryType,
                HighAvailabilityReplicaCount = this.IsParameterBound(p => p.HighAvailabilityReplicaCount) ? HighAvailabilityReplicaCount : (int?)null,
                ZoneRedundant = this.IsParameterBound(p => p.ZoneRedundant) ? ZoneRedundant.ToBool() : (bool?)null,
            };

            if (ParameterSetName == DtuDatabaseParameterSet)
            {
                if (!string.IsNullOrWhiteSpace(SecondaryServiceObjectiveName))
                {
                    linkModel.SkuName = SecondaryServiceObjectiveName;
                }
                else if (string.IsNullOrWhiteSpace(SecondaryElasticPoolName))
                {
                    linkModel.SkuName  = primaryDb.CurrentServiceObjectiveName;
                    linkModel.Edition  = primaryDb.Edition;
                    linkModel.Capacity = primaryDb.Capacity;
                    linkModel.Family   = primaryDb.Family;
                }
            }
            else
            {
                linkModel.SkuName  = AzureSqlDatabaseAdapter.GetDatabaseSkuName(primaryDb.Edition);
                linkModel.Edition  = primaryDb.Edition;
                linkModel.Capacity = SecondaryVCore;
                linkModel.Family   = SecondaryComputeGeneration;
            }

            newEntity.Add(linkModel);
            return(newEntity);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Get the entities from the service
        /// </summary>
        /// <returns>The list of entities</returns>
        protected override IEnumerable <AzureSqlDatabaseModel> GetEntity()
        {
            ICollection <AzureSqlDatabaseModel> results;

            if (MyInvocation.BoundParameters.ContainsKey("DatabaseName"))
            {
                results = new List <AzureSqlDatabaseModel>();
                results.Add(ModelAdapter.GetDatabase(this.ResourceGroupName, this.ServerName, this.DatabaseName));
            }
            else
            {
                results = ModelAdapter.ListDatabases(this.ResourceGroupName, this.ServerName);
            }

            return(results);
        }
        /// <summary>
        /// Create the model from user input
        /// </summary>
        /// <param name="model">Model retrieved from service</param>
        /// <returns>The model that was passed in</returns>
        protected override IEnumerable <AzureReplicationLinkModel> ApplyUserInputToModel(IEnumerable <AzureReplicationLinkModel> model)
        {
            string location = ModelAdapter.GetServerLocation(this.PartnerResourceGroupName, this.PartnerServerName);
            List <Model.AzureReplicationLinkModel> newEntity = new List <AzureReplicationLinkModel>();

            Database.Model.AzureSqlDatabaseModel primaryDb = ModelAdapter.GetDatabase(ResourceGroupName, ServerName, DatabaseName);

            AzureReplicationLinkModel linkModel = new AzureReplicationLinkModel()
            {
                PartnerLocation          = location,
                ResourceGroupName        = this.ResourceGroupName,
                ServerName               = this.ServerName,
                DatabaseName             = this.DatabaseName,
                PartnerResourceGroupName = this.PartnerResourceGroupName,
                PartnerServerName        = this.PartnerServerName,
                PartnerDatabaseName      = string.IsNullOrWhiteSpace(this.PartnerDatabaseName) ? this.DatabaseName : this.PartnerDatabaseName,
                SecondaryElasticPoolName = this.SecondaryElasticPoolName,
                AllowConnections         = this.AllowConnections,
                Tags        = TagsConversionHelper.CreateTagDictionary(Tags, validate: true),
                LicenseType = LicenseType
            };

            if (ParameterSetName == DtuDatabaseParameterSet)
            {
                if (!string.IsNullOrWhiteSpace(SecondaryServiceObjectiveName))
                {
                    linkModel.SkuName = SecondaryServiceObjectiveName;
                }
                else if (string.IsNullOrWhiteSpace(SecondaryElasticPoolName))
                {
                    linkModel.SkuName  = primaryDb.CurrentServiceObjectiveName;
                    linkModel.Edition  = primaryDb.Edition;
                    linkModel.Capacity = primaryDb.Capacity;
                    linkModel.Family   = primaryDb.Family;
                }
            }
            else
            {
                linkModel.SkuName  = AzureSqlDatabaseAdapter.GetDatabaseSkuName(primaryDb.Edition);
                linkModel.Edition  = primaryDb.Edition;
                linkModel.Capacity = SecondaryVCore;
                linkModel.Family   = SecondaryComputeGeneration;
            }

            newEntity.Add(linkModel);
            return(newEntity);
        }
        /// <summary>
        /// Get the entities from the service
        /// </summary>
        /// <returns>The list of entities</returns>
        protected override IEnumerable <AzureReplicationLinkModel> GetEntity()
        {
            // We try to get the database.  Since this is a create secondary database operation, we don't want the secondary database to already exist
            try
            {
                ModelAdapter.GetDatabase(this.PartnerResourceGroupName, this.PartnerServerName, this.DatabaseName);
            }
            catch (CloudException ex)
            {
                if (ex.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    // This is what we want.  We looked and there is no database with this name.
                    return(null);
                }

                // Unexpected exception encountered
                throw;
            }

            // The database already exists
            throw new PSArgumentException(
                      string.Format(Resources.DatabaseNameExists, this.DatabaseName, this.PartnerServerName),
                      "DatabaseName");
        }