Beispiel #1
0
        public async Task <KeyValuePair <string, string>[]> GetKeysAsync(string accountName, IEnumerable <string> keyNames)
        {
            var tenant = await TenantCacheClient.GetTenantAsync(accountName);

            if (tenant == null)
            {
                return(new KeyValuePair <string, string>[] { });
            }

            if ((tenant.IsDisabled != null && tenant.IsDisabled.Value) ||
                tenant.State != TenantCache.Contract.TenantState.Active)
            {
                throw new AccountDisabledException($"Account {accountName} is disabled");
            }

            return(tenant.TenantDescription.AuthenticationRules
                   .Where(r => keyNames.Contains(r.KeyName, StringComparer.OrdinalIgnoreCase))
                   .SelectMany(r => new KeyValuePair <string, string>[]
            {
                new KeyValuePair <string, string>(r.KeyName, r.PrimaryKey),
                new KeyValuePair <string, string>(r.KeyName, r.SecondaryKey)
            })
                   .Where(pair => !string.IsNullOrWhiteSpace(pair.Value))
                   .ToArray());
        }
        protected override async Task PushToTargetAsync(IEnumerable <ResourceUsageRecord> records, Guid batchId, CancellationToken cancellationToken)
        {
            try
            {
                var batchOperation = new TableBatchOperation();
                foreach (var record in records)
                {
                    // Fill in record with Tenant info
                    var tenant = await TenantCacheClient.GetTenantAsync(record.EngagementAccount);

                    if (tenant == null)
                    {
                        BillingEventSource.Current.Warning(BillingEventSource.EmptyTrackingId, this, nameof(this.PushToTargetAsync), OperationStates.FailedMatch, $"Unable to get tenant for account '{record.EngagementAccount}'");
                    }
                    else if (this.pushUsageForWhitelistedSubscriptionsOnly)
                    {
                        if (Guid.TryParse(tenant.SubscriptionId, out Guid subId) && this.whitelistedSubscriptionIds.Contains(subId))
                        {
                            batchOperation.InsertOrReplace(new UsageRecord(record, tenant, batchId));
                        }
                        else
                        {
                            BillingEventSource.Current.Info(BillingEventSource.EmptyTrackingId, this, nameof(this.PushToTargetAsync), OperationStates.Dropped, $"Drop usage for subscription '{tenant.SubscriptionId}' as it's not in whitelist");
                        }
                    }
                    else
                    {
                        batchOperation.InsertOrReplace(new UsageRecord(record, tenant, batchId));
                    }
                }

                if (batchOperation.Count > 0)
                {
                    var results = await this.table.ExecuteBatchAsync(batchOperation, cancellationToken);

                    BillingEventSource.Current.Info(BillingEventSource.EmptyTrackingId, this, nameof(this.PushToTargetAsync), OperationStates.Succeeded, $"Push usage with PartitionId = {batchId}, count = {records.Count()}");
                }
            }
            catch (Exception ex)
            {
                BillingEventSource.Current.ErrorException(BillingEventSource.EmptyTrackingId, this, nameof(this.PushToTargetAsync), OperationStates.Failed, string.Empty, ex);
                throw ex;
            }
        }
Beispiel #3
0
        // Get credential from tenant service
        private async Task <string> GetCredentialAsync(string account, string channel, string platform)
        {
            var tenant = await TenantCacheClient.GetTenantAsync(account);

            Validator.ArgumentNotNull(tenant, nameof(tenant));
            if (tenant?.TenantDescription?.ChannelSettings != null)
            {
                var setting = tenant.TenantDescription.ChannelSettings.SingleOrDefault(s => s.Type.Equals(channel, StringComparison.InvariantCultureIgnoreCase));

                if (setting != null)
                {
                    string platformKey = platform + "AppId";
                    string appid;
                    setting.Credentials.TryGetValue(platformKey, out appid);
                    Validator.IsTrue <ArgumentException>(appid != null, nameof(appid), "Unexpected social login platform '{0}' for account '{1}' and channel '{2}'", platform, account, channel);
                    return(appid);
                }
            }

            throw new ArgumentException($"Unexpected social login channel '{channel}' for account '{account}'");
        }