Example #1
0
        /// <summary>
        /// Upserts a managed instance
        /// </summary>
        /// <param name="model">The managed instance to upsert</param>
        /// <returns>The updated managed instance model</returns>
        public AzureSqlManagedInstanceModel UpsertManagedInstance(AzureSqlManagedInstanceModel model)
        {
            var resp = Communicator.CreateOrUpdate(model.ResourceGroupName, model.FullyQualifiedDomainName, new Management.Sql.Models.ManagedInstance()
            {
                Location                   = model.Location,
                Tags                       = model.Tags,
                AdministratorLogin         = model.AdministratorLogin,
                AdministratorLoginPassword = model.AdministratorPassword != null ? ConversionUtilities.SecureStringToString(model.AdministratorPassword) : null,
                Sku                       = model.Sku != null ? new Management.Sql.Models.Sku(model.Sku.Name, model.Sku.Tier) : null,
                LicenseType               = model.LicenseType,
                StorageSizeInGB           = model.StorageSizeInGB,
                SubnetId                  = model.SubnetId,
                VCores                    = model.VCores,
                Identity                  = model.Identity,
                Collation                 = model.Collation,
                PublicDataEndpointEnabled = model.PublicDataEndpointEnabled,
                ProxyOverride             = model.ProxyOverride,
                TimezoneId                = model.TimezoneId,
                DnsZonePartner            = model.DnsZonePartner,
                InstancePoolId            = model.InstancePoolName != null ?
                                            string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/instancePools/{2}",
                                                          Context.Subscription.Id, model.ResourceGroupName, model.InstancePoolName) : null,
                MinimalTlsVersion = model.MinimalTlsVersion,
                RequestedBackupStorageRedundancy = model.RequestedBackupStorageRedundancy,
                MaintenanceConfigurationId       = MaintenanceConfigurationHelper.ConvertMaintenanceConfigurationIdArgument(model.MaintenanceConfigurationId, Context.Subscription.Id),
                Administrators = GetActiveDirectoryInformation(model.Administrators),
                PrimaryUserAssignedIdentityId = model.PrimaryUserAssignedIdentityId,
                KeyId            = model.KeyId,
                ZoneRedundant    = model.ZoneRedundant,
                ServicePrincipal = ResourceServicePrincipalHelper.UnwrapServicePrincipalObject(model.ServicePrincipal)
            });

            return(CreateManagedInstanceModelFromResponse(resp));
        }
Example #2
0
        /// <summary>
        /// Generates the model from user input.
        /// </summary>
        /// <param name="model">This is null since the instance doesn't exist yet</param>
        /// <returns>The generated model from user input</returns>
        protected override IEnumerable <Model.AzureSqlManagedInstanceModel> ApplyUserInputToModel(IEnumerable <Model.AzureSqlManagedInstanceModel> model)
        {
            List <Model.AzureSqlManagedInstanceModel> newEntity = new List <Model.AzureSqlManagedInstanceModel>();

            Management.Internal.Resources.Models.Sku Sku = new Management.Internal.Resources.Models.Sku();

            if (string.Equals(this.ParameterSetName, NewBySkuNameParameterSet, System.StringComparison.OrdinalIgnoreCase))
            {
                Sku.Name = SkuName;
            }
            else if (string.Equals(this.ParameterSetName, NewByEditionAndComputeGenerationParameterSet, System.StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(this.ParameterSetName, NewByInstancePoolParentObjectParameterSet, System.StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(this.ParameterSetName, NewByInstancePoolResourceIdParameterSet, System.StringComparison.OrdinalIgnoreCase))
            {
                string editionShort = AzureSqlManagedInstanceAdapter.GetInstanceSkuPrefix(Edition);
                Sku.Name = editionShort + "_" + ComputeGeneration;
            }

            newEntity.Add(new AzureSqlManagedInstanceModel()
            {
                Location                 = this.Location,
                ResourceGroupName        = this.ResourceGroupName,
                FullyQualifiedDomainName = this.Name,
                AdministratorPassword    = (this.AdministratorCredential != null) ? this.AdministratorCredential.Password : null,
                AdministratorLogin       = (this.AdministratorCredential != null) ? this.AdministratorCredential.UserName : null,
                Tags        = TagsConversionHelper.CreateTagDictionary(Tag, validate: true),
                Identity    = ResourceIdentityHelper.GetIdentityObjectFromType(this.AssignIdentity.IsPresent, this.IdentityType ?? null, UserAssignedIdentityId, null),
                LicenseType = this.LicenseType,
                // `-StorageSizeInGB 0` as a parameter to this cmdlet means "use default".
                // For non-MI database, we can just pass in 0 and the server will treat 0 as default.
                // However this is (currently) not the case for MI. We need to convert the 0 to null
                // here in client before sending to the server.
                StorageSizeInGB                  = SqlSkuUtils.ValueIfNonZero(this.StorageSizeInGB),
                SubnetId                         = this.SubnetId,
                VCores                           = this.VCore,
                Sku                              = Sku,
                Collation                        = this.Collation,
                PublicDataEndpointEnabled        = this.PublicDataEndpointEnabled,
                ProxyOverride                    = this.ProxyOverride,
                TimezoneId                       = this.TimezoneId,
                DnsZonePartner                   = this.DnsZonePartner,
                InstancePoolName                 = this.InstancePoolName,
                MinimalTlsVersion                = this.MinimalTlsVersion,
                RequestedBackupStorageRedundancy = this.BackupStorageRedundancy,
                MaintenanceConfigurationId       = this.MaintenanceConfigurationId,
                PrimaryUserAssignedIdentityId    = this.PrimaryUserAssignedIdentityId,
                KeyId                            = this.KeyId,
                Administrators                   = new Management.Sql.Models.ManagedInstanceExternalAdministrator()
                {
                    AzureADOnlyAuthentication = (this.EnableActiveDirectoryOnlyAuthentication.IsPresent) ? (bool?)true : null,
                    Login = this.ExternalAdminName,
                    Sid   = this.ExternalAdminSID
                },
                ZoneRedundant    = this.ZoneRedundant.IsPresent ? this.ZoneRedundant.ToBool() : (bool?)null,
                ServicePrincipal = ResourceServicePrincipalHelper.GetServicePrincipalObjectFromType(this.ServicePrincipalType ?? null)
            });;
            return(newEntity);
        }
Example #3
0
        /// <summary>
        /// Convert a Management.Sql.LegacySdk.Models.ManagedInstance to AzureSqlDatabaseManagedInstanceModel
        /// </summary>
        /// <param name="resourceGroupName">The resource group the managed instance is in</param>
        /// <param name="resp">The management client managed instance response to convert</param>
        /// <returns>The converted managed instance model</returns>
        private static AzureSqlManagedInstanceModel CreateManagedInstanceModelFromResponse(Management.Sql.Models.ManagedInstance resp)
        {
            AzureSqlManagedInstanceModel managedInstance = new AzureSqlManagedInstanceModel();

            // Extract the resource group name from the ID.
            // ID is in the form:
            // /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rgName/providers/Microsoft.Sql/managedInstances/managedInstanceName
            string[] segments = resp.Id.Split('/');
            managedInstance.ResourceGroupName = segments[4];

            managedInstance.ManagedInstanceName = resp.Name;
            managedInstance.Id = resp.Id;
            managedInstance.FullyQualifiedDomainName = resp.FullyQualifiedDomainName;
            managedInstance.AdministratorLogin       = resp.AdministratorLogin;
            managedInstance.Location = resp.Location;
            managedInstance.Tags     = TagsConversionHelper.CreateTagDictionary(TagsConversionHelper.CreateTagHashtable(resp.Tags), false);
            managedInstance.Identity = resp.Identity;
            managedInstance.FullyQualifiedDomainName = resp.FullyQualifiedDomainName;
            managedInstance.SubnetId                  = resp.SubnetId;
            managedInstance.LicenseType               = resp.LicenseType;
            managedInstance.VCores                    = resp.VCores;
            managedInstance.StorageSizeInGB           = resp.StorageSizeInGB;
            managedInstance.Collation                 = resp.Collation;
            managedInstance.PublicDataEndpointEnabled = resp.PublicDataEndpointEnabled;
            managedInstance.ProxyOverride             = resp.ProxyOverride;
            managedInstance.TimezoneId                = resp.TimezoneId;
            managedInstance.DnsZone                   = resp.DnsZone;
            managedInstance.InstancePoolName          = resp.InstancePoolId != null ?
                                                        new ResourceIdentifier(resp.InstancePoolId).ResourceName : null;
            managedInstance.MinimalTlsVersion                = resp.MinimalTlsVersion;
            managedInstance.BackupStorageRedundancy          = resp.CurrentBackupStorageRedundancy;
            managedInstance.RequestedBackupStorageRedundancy = resp.RequestedBackupStorageRedundancy;
            managedInstance.CurrentBackupStorageRedundancy   = resp.CurrentBackupStorageRedundancy;
            managedInstance.MaintenanceConfigurationId       = resp.MaintenanceConfigurationId;
            managedInstance.ServicePrincipal = ResourceServicePrincipalHelper.WrapServicePrincipalObject(resp.ServicePrincipal);

            Management.Internal.Resources.Models.Sku sku = new Management.Internal.Resources.Models.Sku();
            sku.Name   = resp.Sku.Name;
            sku.Tier   = resp.Sku.Tier;
            sku.Family = resp.Sku.Family;

            managedInstance.Sku            = sku;
            managedInstance.Administrators = resp.Administrators;

            if (managedInstance.Administrators != null && managedInstance.Administrators.AdministratorType == null)
            {
                managedInstance.Administrators.AdministratorType = "ActiveDirectory";
            }
            managedInstance.PrimaryUserAssignedIdentityId = resp.PrimaryUserAssignedIdentityId;
            managedInstance.KeyId         = resp.KeyId;
            managedInstance.ZoneRedundant = resp.ZoneRedundant;

            return(managedInstance);
        }
Example #4
0
        /// <summary>
        /// Constructs the model to send to the update API
        /// </summary>
        /// <param name="model">The result of the get operation</param>
        /// <returns>The model to send to the update</returns>
        protected override IEnumerable <AzureSqlManagedInstanceModel> ApplyUserInputToModel(IEnumerable <AzureSqlManagedInstanceModel> model)
        {
            AzureSqlManagedInstanceModel existingInstance = ModelAdapter.GetManagedInstance(this.ResourceGroupName, this.Name, "administrators/activedirectory");

            Management.Internal.Resources.Models.Sku Sku = new Management.Internal.Resources.Models.Sku();

            // Get current edition and family
            string currentEdition           = existingInstance.Sku.Tier;
            string currentComputeGeneration = existingInstance.Sku.Family;

            // If either edition or compute generation are set, get the new sku
            if (this.Edition != null || this.ComputeGeneration != null)
            {
                string editionShort = AzureSqlManagedInstanceAdapter.GetInstanceSkuPrefix(!string.IsNullOrWhiteSpace(Edition) ? this.Edition : currentEdition);
                Sku.Name   = editionShort + "_" + (!string.IsNullOrWhiteSpace(this.ComputeGeneration) ? this.ComputeGeneration : currentComputeGeneration);
                Sku.Tier   = !string.IsNullOrWhiteSpace(this.Edition) ? this.Edition : null;
                Sku.Family = !string.IsNullOrWhiteSpace(this.ComputeGeneration) ? this.ComputeGeneration : currentComputeGeneration;
            }
            else
            {
                Sku = existingInstance.Sku;
            }

            // Construct a new entity so we only send the relevant data to the Managed instance
            List <AzureSqlManagedInstanceModel> updateData = new List <AzureSqlManagedInstanceModel>();

            updateData.Add(model.FirstOrDefault());
            updateData[0].ResourceGroupName        = this.ResourceGroupName;
            updateData[0].ManagedInstanceName      = this.Name;
            updateData[0].FullyQualifiedDomainName = this.Name;
            updateData[0].Location = model.FirstOrDefault().Location;
            updateData[0].Sku      = Sku;
            updateData[0].AdministratorPassword = this.AdministratorPassword;
            updateData[0].LicenseType           = this.LicenseType ?? updateData[0].LicenseType;
            updateData[0].StorageSizeInGB       = this.StorageSizeInGB ?? model.FirstOrDefault().StorageSizeInGB;
            updateData[0].VCores = this.VCore ?? updateData[0].VCores;
            updateData[0].PublicDataEndpointEnabled = this.PublicDataEndpointEnabled ?? updateData[0].PublicDataEndpointEnabled;
            updateData[0].ProxyOverride             = this.ProxyOverride ?? this.ProxyOverride;
            updateData[0].Tags                             = TagsConversionHelper.CreateTagDictionary(Tag, validate: true);
            updateData[0].Identity                         = ResourceIdentityHelper.GetIdentityObjectFromType(this.AssignIdentity.IsPresent, this.IdentityType ?? null, UserAssignedIdentityId, model.FirstOrDefault().Identity);
            updateData[0].InstancePoolName                 = this.InstancePoolName ?? updateData[0].InstancePoolName;
            updateData[0].MinimalTlsVersion                = this.MinimalTlsVersion ?? updateData[0].MinimalTlsVersion;
            updateData[0].MaintenanceConfigurationId       = this.MaintenanceConfigurationId ?? updateData[0].MaintenanceConfigurationId;
            updateData[0].AdministratorLogin               = model.FirstOrDefault().AdministratorLogin;
            updateData[0].PrimaryUserAssignedIdentityId    = this.PrimaryUserAssignedIdentityId ?? model.FirstOrDefault().PrimaryUserAssignedIdentityId;
            updateData[0].KeyId                            = this.KeyId ?? updateData[0].KeyId;
            updateData[0].SubnetId                         = this.SubnetId ?? model.FirstOrDefault().SubnetId;
            updateData[0].ZoneRedundant                    = this.ZoneRedundant.IsPresent ? this.ZoneRedundant.ToBool() : (bool?)null;
            updateData[0].RequestedBackupStorageRedundancy = this.BackupStorageRedundancy ?? updateData[0].CurrentBackupStorageRedundancy;
            updateData[0].ServicePrincipal                 = ResourceServicePrincipalHelper.GetServicePrincipalObjectFromType(this.ServicePrincipalType ?? null);
            return(updateData);
        }