public static bool TryGetTenantRelocationStateByObjectId(ADObjectId identity, out TenantRelocationState state, out bool isSourceTenant)
        {
            ArgumentValidator.ThrowIfNull("identity", identity);
            state          = null;
            isSourceTenant = true;
            PartitionId partitionId = identity.GetPartitionId();

            if (!ForestTenantRelocationsCache.IsTenantRelocationAllowed(partitionId.ForestFQDN))
            {
                return(false);
            }
            ITenantConfigurationSession tenantConfigurationSession = DirectorySessionFactory.Default.CreateTenantConfigurationSession(ConsistencyMode.PartiallyConsistent, ADSessionSettings.FromAllTenantsPartitionId(partitionId), 595, "TryGetTenantRelocationStateByObjectId", "f:\\15.00.1497\\sources\\dev\\data\\src\\directory\\RelocationCache\\TenantRelocationStateCache.cs");
            ADObjectId configurationUnitsRoot = tenantConfigurationSession.GetConfigurationUnitsRoot();

            if (configurationUnitsRoot.Equals(identity))
            {
                return(false);
            }
            ADObjectId adobjectId = null;

            if (identity.IsDescendantOf(configurationUnitsRoot))
            {
                adobjectId = identity.GetFirstGenerationDecendantOf(configurationUnitsRoot);
            }
            else
            {
                ADObjectId hostedOrganizationsRoot = tenantConfigurationSession.GetHostedOrganizationsRoot();
                if (hostedOrganizationsRoot.Equals(identity))
                {
                    return(false);
                }
                if (identity.IsDescendantOf(hostedOrganizationsRoot))
                {
                    adobjectId = identity.GetFirstGenerationDecendantOf(hostedOrganizationsRoot);
                }
            }
            if (adobjectId == null)
            {
                return(false);
            }
            try
            {
                state = TenantRelocationStateCache.GetTenantRelocationState(adobjectId.Name, partitionId, out isSourceTenant, false);
            }
            catch (CannotResolveTenantNameException)
            {
                return(false);
            }
            return(true);
        }
        public static TenantRelocationState GetTenantRelocationState(string tenantName, PartitionId partitionId, out bool isSourceTenant, bool readThrough = false)
        {
            if (string.IsNullOrEmpty(tenantName))
            {
                throw new ArgumentNullException("tenantName");
            }
            if (!ForestTenantRelocationsCache.IsTenantRelocationAllowed(partitionId.ForestFQDN))
            {
                isSourceTenant = true;
                return(new TenantRelocationState(partitionId.ForestFQDN, TenantRelocationStatus.NotStarted));
            }
            TenantRelocationStateCache         tenantRelocationStateCache = TenantRelocationStateCache.GetInstance();
            ExpiringTenantRelocationStateValue expiringTenantRelocationStateValue;

            if (!tenantRelocationStateCache.tenantRelocationStates.TryGetValue(tenantName, out expiringTenantRelocationStateValue) || expiringTenantRelocationStateValue.Expired || readThrough || TenantRelocationStateCache.IgnoreRelocationTimeConstraints())
            {
                TenantRelocationState tenantRelocationState = TenantRelocationStateCache.LoadTenantRelocationState(tenantName, partitionId);
                ExTraceGlobals.TenantRelocationTracer.TraceDebug(0L, "TenantRelocationStateCache::GetTenantRelocationState(): Inserting new TenantRelocationState value for tenant {0} to the cache: SourceForestFQDN={1}, SourceForestState={2}, TargetForestFQDN={3}, TargetForestState={4}, TargetOrganizationId={5}", new object[]
                {
                    tenantName,
                    tenantRelocationState.SourceForestFQDN,
                    tenantRelocationState.SourceForestState,
                    tenantRelocationState.TargetForestFQDN,
                    tenantRelocationState.TargetForestState,
                    (tenantRelocationState.TargetOrganizationId != null) ? tenantRelocationState.TargetOrganizationId.ToString() : "<null>"
                });
                expiringTenantRelocationStateValue = new ExpiringTenantRelocationStateValue(tenantRelocationState);
                tenantRelocationStateCache.AddOrUpdate(tenantName, expiringTenantRelocationStateValue);
            }
            if (expiringTenantRelocationStateValue.Value.TargetOrganizationId == null || expiringTenantRelocationStateValue.Value.TargetOrganizationId.OrganizationalUnit == null)
            {
                isSourceTenant = true;
            }
            else if (expiringTenantRelocationStateValue.Value.TargetOrganizationId.PartitionId != partitionId)
            {
                isSourceTenant = true;
            }
            else if (expiringTenantRelocationStateValue.Value.SourceForestFQDN == expiringTenantRelocationStateValue.Value.TargetForestFQDN)
            {
                isSourceTenant = (tenantName != expiringTenantRelocationStateValue.Value.TargetOrganizationId.OrganizationalUnit.Name);
            }
            else
            {
                isSourceTenant = false;
            }
            return(expiringTenantRelocationStateValue.Value);
        }
Example #3
0
        private static TimeSpan CalculateCacheItemExpirationWindow(bool dataFromOfflineService, string tenantName, Guid externalOrgId, PartitionId accountPartitionId)
        {
            if (dataFromOfflineService)
            {
                return(TimeSpan.FromMinutes((double)ConfigBase <AdDriverConfigSchema> .GetConfig <int>("OfflineDataCacheExpirationTimeInMinutes")));
            }
            if (!ForestTenantRelocationsCache.IsTenantRelocationAllowed(accountPartitionId.ForestFQDN))
            {
                return(ExpiringTenantRelocationStateValue.TenantRelocationStateExpirationWindowProvider.DefaultExpirationWindow);
            }
            if (!string.IsNullOrEmpty(tenantName))
            {
                return(TenantRelocationStateCache.GetTenantCacheExpirationWindow(tenantName, accountPartitionId));
            }
            ITenantConfigurationSession tenantConfigurationSession            = DirectorySessionFactory.Default.CreateTenantConfigurationSession(ConsistencyMode.PartiallyConsistent, ADSessionSettings.FromAllTenantsPartitionId(accountPartitionId), 110, "CalculateCacheItemExpirationWindow", "f:\\15.00.1497\\sources\\dev\\data\\src\\directory\\TenantPartitionCacheItem.cs");
            ExchangeConfigurationUnit   exchangeConfigurationUnitByExternalId = tenantConfigurationSession.GetExchangeConfigurationUnitByExternalId(externalOrgId);

            if (exchangeConfigurationUnitByExternalId != null)
            {
                tenantName = ((ADObjectId)exchangeConfigurationUnitByExternalId.Identity).Parent.Name;
                return(TenantRelocationStateCache.GetTenantCacheExpirationWindow(tenantName, accountPartitionId));
            }
            return(ExpiringTenantRelocationStateValue.TenantRelocationStateExpirationWindowProvider.DefaultExpirationWindow);
        }