Beispiel #1
0
        /// <summary>
        /// Get a customer's usage information for the last 1 month, calculates the total cost using RateCard API
        /// and Suspends the subscription if the total cost is more than the credit limit.
        /// </summary>
        /// <param name="defaultDomain">default domain of the reseller</param>
        /// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param>
        /// <param name="key">Key for this application in Azure Active Directory</param>
        /// <param name="customerMicrosoftId">Microsoft Id of the customer</param>
        /// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param>
        public static void GetRateCardAndUsage(string defaultDomain, string appId, string key,
                                               string customerMicrosoftId, string resellerMicrosoftId)
        {
            var correlationId = Guid.NewGuid().ToString();
            // Get Active Directory token first
            AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key);

            // Using the ADToken get the sales agent token
            AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);

            // Get the Reseller Cid, you can cache this value
            string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);

            // You can cache this value too
            var customerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId,
                                                      saAuthorizationToken.AccessToken);

            // Get Customer token
            AuthorizationToken customerAuthorizationToken = Customer.GetCustomer_Token(customerCid, adAuthorizationToken);

            // Gets the RateCard to get the prices
            var rateCard = RateCard.GetRateCard(resellerCid, saAuthorizationToken.AccessToken);

            var startTime = String.Format("{0:u}", DateTime.Today.AddDays(-30));
            var endTime   = String.Format("{0:u}", DateTime.Today.AddDays(-1));

            // Get all of a Customer's entitlements
            var entitlements = Usage.GetEntitlements(customerCid, customerAuthorizationToken.AccessToken);

            try
            {
                foreach (var entitlement in entitlements.items)
                {
                    // Get the usage for the given entitlement for the last 1 month
                    var usageRecords = Usage.GetUsageRecords(resellerCid, entitlement.id, saAuthorizationToken.AccessToken,
                                                             startTime, endTime);

                    if (usageRecords.items.Count > 0)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGreen;
                        Console.WriteLine("================================================================================");
                        Console.WriteLine("\nPrices for Entitlement: {0}", entitlement.id);
                        Console.WriteLine("================================================================================");

                        double totalCost = 0;
                        // Looping through the usage records to calculate the cost of each item
                        foreach (UsageType usageRecord in usageRecords.items)
                        {
                            string meterId = usageRecord.meter_id;

                            // Gets the price corresponding to the given meterId from the ratecard.
                            Console.WriteLine("\nMeter Name\t\t: {0}", usageRecord.meter_name);
                            double includedQty = Usage.GetIncludedQuantityByMeterID(rateCard, meterId);
                            Console.WriteLine("Included Quantity\t\t: {0}", includedQty);
                            double consumedQty = (double)usageRecord.quantity;
                            Console.WriteLine("Consumed Quantity\t\t: {0}", consumedQty);
                            double ratableUsage = Usage.GetRatableUsage(consumedQty, includedQty);
                            double cost         = Usage.computeRatedUsagePerMeter(Usage.GetRatesForMeterID(rateCard, meterId), ratableUsage);
                            Console.WriteLine("Cost\t\t: {0}", cost);
                            totalCost += cost;
                        }
                        Console.WriteLine("\nTOTAL COST:  {0}", totalCost);
                        // Setting the credit limit below the total cost for testing this scenario
                        double creditLimit = 100;
                        // Suspends the subscription if the total cost is above the credit limit.
                        if (totalCost > creditLimit)
                        {
                            var subscription = Subscription.GetSubscriptionByUri(entitlement.billing_subscription_uri,
                                                                                 saAuthorizationToken.AccessToken);
                            Subscription.SuspendSubscription(subscription.id, resellerCid, saAuthorizationToken.AccessToken);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }