示例#1
0
 internal ManagedInstanceData(ResourceIdentifier id, string name, ResourceType resourceType, SystemData systemData, IDictionary <string, string> tags, AzureLocation location, ManagedServiceIdentity identity, SqlSku sku, ManagedInstancePropertiesProvisioningState?provisioningState, ManagedServerCreateMode?managedInstanceCreateMode, string fullyQualifiedDomainName, string administratorLogin, string administratorLoginPassword, string subnetId, string state, ManagedInstanceLicenseType?licenseType, int?vCores, int?storageSizeInGB, string collation, string dnsZone, string dnsZonePartner, bool?publicDataEndpointEnabled, string sourceManagedInstanceId, DateTimeOffset?restorePointInTime, ManagedInstanceProxyOverride?proxyOverride, string timezoneId, string instancePoolId, string maintenanceConfigurationId, IReadOnlyList <ManagedInstancePecProperty> privateEndpointConnections, string minimalTlsVersion, StorageAccountType?storageAccountType, bool?zoneRedundant, string primaryUserAssignedIdentityId, string keyId, ManagedInstanceExternalAdministrator administrators) : base(id, name, resourceType, systemData, tags, location)
 {
     Identity                      = identity;
     Sku                           = sku;
     ProvisioningState             = provisioningState;
     ManagedInstanceCreateMode     = managedInstanceCreateMode;
     FullyQualifiedDomainName      = fullyQualifiedDomainName;
     AdministratorLogin            = administratorLogin;
     AdministratorLoginPassword    = administratorLoginPassword;
     SubnetId                      = subnetId;
     State                         = state;
     LicenseType                   = licenseType;
     VCores                        = vCores;
     StorageSizeInGB               = storageSizeInGB;
     Collation                     = collation;
     DnsZone                       = dnsZone;
     DnsZonePartner                = dnsZonePartner;
     PublicDataEndpointEnabled     = publicDataEndpointEnabled;
     SourceManagedInstanceId       = sourceManagedInstanceId;
     RestorePointInTime            = restorePointInTime;
     ProxyOverride                 = proxyOverride;
     TimezoneId                    = timezoneId;
     InstancePoolId                = instancePoolId;
     MaintenanceConfigurationId    = maintenanceConfigurationId;
     PrivateEndpointConnections    = privateEndpointConnections;
     MinimalTlsVersion             = minimalTlsVersion;
     StorageAccountType            = storageAccountType;
     ZoneRedundant                 = zoneRedundant;
     PrimaryUserAssignedIdentityId = primaryUserAssignedIdentityId;
     KeyId                         = keyId;
     Administrators                = administrators;
 }
示例#2
0
        /// <summary>
        /// Verifies that the Azure Active Directory user or group exists, and will get the object id if it is not set.
        /// </summary>
        /// <param name="displayName">Azure Active Directory user or group display name</param>
        /// <param name="objectId">Azure Active Directory user or group object id</param>
        /// <returns></returns>
        protected ManagedInstanceExternalAdministrator GetActiveDirectoryInformation(ManagedInstanceExternalAdministrator input)
        {
            if (input == null || string.IsNullOrEmpty(input.Login))
            {
                return(null);
            }

            Guid?  objectId    = input.Sid;
            string displayName = input.Login;
            bool?  adOnlyAuth  = input.AzureADOnlyAuthentication;

            // Gets the default Tenant id for the subscriptions
            Guid tenantId = GetTenantId();

            // Check for a Azure Active Directory group. Recommended to always use group.
            IEnumerable <PSADGroup> groupList = null;
            PSADGroup group = null;

            var filter = new ADObjectFilterOptions()
            {
                Id           = (objectId != null && objectId != Guid.Empty) ? objectId.ToString() : null,
                SearchString = displayName,
                Paging       = true,
            };

            // Get a list of groups from Azure Active Directory
            groupList = ActiveDirectoryClient.FilterGroups(filter).Where(gr => string.Equals(gr.DisplayName, displayName, StringComparison.OrdinalIgnoreCase));

            if (groupList != null && groupList.Count() > 1)
            {
                // More than one group was found with that display name.
                throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ADGroupMoreThanOneFound, displayName));
            }
            else if (groupList != null && groupList.Count() == 1)
            {
                // Only one group was found. Get the group display name and object id
                group = groupList.First();

                // Only support Security Groups
                if (group.SecurityEnabled.HasValue && !group.SecurityEnabled.Value)
                {
                    throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.InvalidADGroupNotSecurity, displayName));
                }
            }

            // Lookup for serviceprincipals
            ODataQuery <ServicePrincipal> odataQueryFilter;

            if ((objectId != null && objectId != Guid.Empty))
            {
                var applicationIdString = objectId.ToString();
                odataQueryFilter = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(a => a.AppId == applicationIdString);
            }
            else
            {
                odataQueryFilter = new Rest.Azure.OData.ODataQuery <ServicePrincipal>(a => a.DisplayName == displayName);
            }

            var servicePrincipalList = ActiveDirectoryClient.FilterServicePrincipals(odataQueryFilter);

            if (servicePrincipalList != null && servicePrincipalList.Count() > 1)
            {
                // More than one service principal was found.
                throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ADApplicationMoreThanOneFound, displayName));
            }
            else if (servicePrincipalList != null && servicePrincipalList.Count() == 1)
            {
                // Only one user was found. Get the user display name and object id
                PSADServicePrincipal app = servicePrincipalList.First();

                if (displayName != null && string.CompareOrdinal(displayName, app.DisplayName) != 0)
                {
                    throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ADApplicationDisplayNameMismatch, displayName, app.DisplayName));
                }

                if (group != null)
                {
                    throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ADDuplicateGroupAndApplicationFound, displayName));
                }

                return(new ManagedInstanceExternalAdministrator()
                {
                    Login = displayName,
                    Sid = app.ApplicationId,
                    TenantId = tenantId,
                    PrincipalType = "Application",
                    AzureADOnlyAuthentication = adOnlyAuth
                });
            }

            if (group != null)
            {
                return(new ManagedInstanceExternalAdministrator()
                {
                    Login = group.DisplayName,
                    Sid = group.Id,
                    TenantId = tenantId,
                    PrincipalType = "Group",
                    AzureADOnlyAuthentication = adOnlyAuth
                });
            }

            // No group or service principal was found. Check for a user
            filter = new ADObjectFilterOptions()
            {
                Id           = (objectId != null && objectId != Guid.Empty) ? objectId.ToString() : null,
                SearchString = displayName,
                Paging       = true,
            };

            // Get a list of user from Azure Active Directory
            var userList = ActiveDirectoryClient.FilterUsers(filter).Where(gr => string.Equals(gr.DisplayName, displayName, StringComparison.OrdinalIgnoreCase));

            // No user was found. Check if the display name is a UPN
            if (userList == null || userList.Count() == 0)
            {
                // Check if the display name is the UPN
                filter = new ADObjectFilterOptions()
                {
                    Id     = (objectId != null && objectId != Guid.Empty) ? objectId.ToString() : null,
                    UPN    = displayName,
                    Paging = true,
                };

                userList = ActiveDirectoryClient.FilterUsers(filter).Where(gr => string.Equals(gr.UserPrincipalName, displayName, StringComparison.OrdinalIgnoreCase));
            }

            // No user was found. Check if the display name is a guest user.
            if (userList == null || userList.Count() == 0)
            {
                // Check if the display name is the UPN
                filter = new ADObjectFilterOptions()
                {
                    Id     = (objectId != null && objectId != Guid.Empty) ? objectId.ToString() : null,
                    Mail   = displayName,
                    Paging = true,
                };

                userList = ActiveDirectoryClient.FilterUsers(filter);
            }

            // No user was found
            if (userList == null || userList.Count() == 0)
            {
                throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ADObjectNotFound, displayName));
            }
            else if (userList.Count() > 1)
            {
                // More than one user was found.
                throw new ArgumentException(string.Format(Microsoft.Azure.Commands.Sql.Properties.Resources.ADUserMoreThanOneFound, displayName));
            }
            else
            {
                // Only one user was found. Get the user display name and object id
                var obj = userList.First();

                return(new ManagedInstanceExternalAdministrator()
                {
                    Login = displayName,
                    Sid = obj.Id,
                    TenantId = tenantId,
                    PrincipalType = "User",
                    AzureADOnlyAuthentication = adOnlyAuth
                });
            }
        }
示例#3
0
        internal static ManagedInstanceData DeserializeManagedInstanceData(JsonElement element)
        {
            Optional <ManagedServiceIdentity> identity = default;
            Optional <SqlSku>            sku           = default;
            IDictionary <string, string> tags          = default;
            AzureLocation      location   = default;
            ResourceIdentifier id         = default;
            string             name       = default;
            ResourceType       type       = default;
            SystemData         systemData = default;
            Optional <ManagedInstancePropertiesProvisioningState> provisioningState = default;
            Optional <ManagedServerCreateMode> managedInstanceCreateMode            = default;
            Optional <string> fullyQualifiedDomainName   = default;
            Optional <string> administratorLogin         = default;
            Optional <string> administratorLoginPassword = default;
            Optional <string> subnetId = default;
            Optional <string> state    = default;
            Optional <ManagedInstanceLicenseType> licenseType = default;
            Optional <int>            vCores                      = default;
            Optional <int>            storageSizeInGB             = default;
            Optional <string>         collation                   = default;
            Optional <string>         dnsZone                     = default;
            Optional <string>         dnsZonePartner              = default;
            Optional <bool>           publicDataEndpointEnabled   = default;
            Optional <string>         sourceManagedInstanceId     = default;
            Optional <DateTimeOffset> restorePointInTime          = default;
            Optional <ManagedInstanceProxyOverride> proxyOverride = default;
            Optional <string> timezoneId                 = default;
            Optional <string> instancePoolId             = default;
            Optional <string> maintenanceConfigurationId = default;
            Optional <IReadOnlyList <ManagedInstancePecProperty> > privateEndpointConnections = default;
            Optional <string>             minimalTlsVersion  = default;
            Optional <StorageAccountType> storageAccountType = default;
            Optional <bool>   zoneRedundant = default;
            Optional <string> primaryUserAssignedIdentityId = default;
            Optional <string> keyId = default;
            Optional <ManagedInstanceExternalAdministrator> administrators = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("identity"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    var serializeOptions = new JsonSerializerOptions {
                        Converters = { new ManagedServiceIdentityTypeV3Converter() }
                    };
                    identity = JsonSerializer.Deserialize <ManagedServiceIdentity>(property.Value.ToString(), serializeOptions);
                    continue;
                }
                if (property.NameEquals("sku"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    sku = SqlSku.DeserializeSqlSku(property.Value);
                    continue;
                }
                if (property.NameEquals("tags"))
                {
                    Dictionary <string, string> dictionary = new Dictionary <string, string>();
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        dictionary.Add(property0.Name, property0.Value.GetString());
                    }
                    tags = dictionary;
                    continue;
                }
                if (property.NameEquals("location"))
                {
                    location = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("id"))
                {
                    id = new ResourceIdentifier(property.Value.GetString());
                    continue;
                }
                if (property.NameEquals("name"))
                {
                    name = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("type"))
                {
                    type = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("systemData"))
                {
                    systemData = JsonSerializer.Deserialize <SystemData>(property.Value.ToString());
                    continue;
                }
                if (property.NameEquals("properties"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    foreach (var property0 in property.Value.EnumerateObject())
                    {
                        if (property0.NameEquals("provisioningState"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            provisioningState = new ManagedInstancePropertiesProvisioningState(property0.Value.GetString());
                            continue;
                        }
                        if (property0.NameEquals("managedInstanceCreateMode"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            managedInstanceCreateMode = new ManagedServerCreateMode(property0.Value.GetString());
                            continue;
                        }
                        if (property0.NameEquals("fullyQualifiedDomainName"))
                        {
                            fullyQualifiedDomainName = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("administratorLogin"))
                        {
                            administratorLogin = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("administratorLoginPassword"))
                        {
                            administratorLoginPassword = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("subnetId"))
                        {
                            subnetId = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("state"))
                        {
                            state = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("licenseType"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            licenseType = new ManagedInstanceLicenseType(property0.Value.GetString());
                            continue;
                        }
                        if (property0.NameEquals("vCores"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            vCores = property0.Value.GetInt32();
                            continue;
                        }
                        if (property0.NameEquals("storageSizeInGB"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            storageSizeInGB = property0.Value.GetInt32();
                            continue;
                        }
                        if (property0.NameEquals("collation"))
                        {
                            collation = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("dnsZone"))
                        {
                            dnsZone = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("dnsZonePartner"))
                        {
                            dnsZonePartner = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("publicDataEndpointEnabled"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            publicDataEndpointEnabled = property0.Value.GetBoolean();
                            continue;
                        }
                        if (property0.NameEquals("sourceManagedInstanceId"))
                        {
                            sourceManagedInstanceId = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("restorePointInTime"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            restorePointInTime = property0.Value.GetDateTimeOffset("O");
                            continue;
                        }
                        if (property0.NameEquals("proxyOverride"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            proxyOverride = new ManagedInstanceProxyOverride(property0.Value.GetString());
                            continue;
                        }
                        if (property0.NameEquals("timezoneId"))
                        {
                            timezoneId = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("instancePoolId"))
                        {
                            instancePoolId = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("maintenanceConfigurationId"))
                        {
                            maintenanceConfigurationId = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("privateEndpointConnections"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            List <ManagedInstancePecProperty> array = new List <ManagedInstancePecProperty>();
                            foreach (var item in property0.Value.EnumerateArray())
                            {
                                array.Add(ManagedInstancePecProperty.DeserializeManagedInstancePecProperty(item));
                            }
                            privateEndpointConnections = array;
                            continue;
                        }
                        if (property0.NameEquals("minimalTlsVersion"))
                        {
                            minimalTlsVersion = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("storageAccountType"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            storageAccountType = new StorageAccountType(property0.Value.GetString());
                            continue;
                        }
                        if (property0.NameEquals("zoneRedundant"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            zoneRedundant = property0.Value.GetBoolean();
                            continue;
                        }
                        if (property0.NameEquals("primaryUserAssignedIdentityId"))
                        {
                            primaryUserAssignedIdentityId = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("keyId"))
                        {
                            keyId = property0.Value.GetString();
                            continue;
                        }
                        if (property0.NameEquals("administrators"))
                        {
                            if (property0.Value.ValueKind == JsonValueKind.Null)
                            {
                                property0.ThrowNonNullablePropertyIsNull();
                                continue;
                            }
                            administrators = ManagedInstanceExternalAdministrator.DeserializeManagedInstanceExternalAdministrator(property0.Value);
                            continue;
                        }
                    }
                    continue;
                }
            }
            return(new ManagedInstanceData(id, name, type, systemData, tags, location, identity, sku.Value, Optional.ToNullable(provisioningState), Optional.ToNullable(managedInstanceCreateMode), fullyQualifiedDomainName.Value, administratorLogin.Value, administratorLoginPassword.Value, subnetId.Value, state.Value, Optional.ToNullable(licenseType), Optional.ToNullable(vCores), Optional.ToNullable(storageSizeInGB), collation.Value, dnsZone.Value, dnsZonePartner.Value, Optional.ToNullable(publicDataEndpointEnabled), sourceManagedInstanceId.Value, Optional.ToNullable(restorePointInTime), Optional.ToNullable(proxyOverride), timezoneId.Value, instancePoolId.Value, maintenanceConfigurationId.Value, Optional.ToList(privateEndpointConnections), minimalTlsVersion.Value, Optional.ToNullable(storageAccountType), Optional.ToNullable(zoneRedundant), primaryUserAssignedIdentityId.Value, keyId.Value, administrators.Value));
        }