예제 #1
0
        /// <summary>
        /// Gets the Meters for the Azure CSP Rate Card
        /// </summary>
        /// <param name="cspCreds">CSP Account credentials object. A token will be generated using these credentials and used for making the online Partner Center API call</param>
        /// <returns> Returns the list of Azure Meters</returns>
        public static List <Meter> GetRateCard(CSPAccountCreds cspCreds)
        {
            List <Meter> meterList = null;

            try
            {
                if (cspCreds == null)
                {
                    throw new Exception(ExceptionLogger.GenerateLoggerTextForInternalError("CSP Account Credentials is null"));
                }

                // Fetch the AzureAD Token
                string aadToken = AuthManager.GetAzureADTokenAppUser(cspCreds.CSPClientId, cspCreds.CSPAdminAgentUserName, cspCreds.CSPAdminAgentPassword, cspCreds.CSPResellerTenantID, true);

                // Create the HttpClient Object
                HttpClient client = new HttpClient();

                // Set the request header values
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + aadToken);
                client.DefaultRequestHeaders.Add("Accept", "application/json");
                client.DefaultRequestHeaders.Add("MS-CorrelationId", Guid.NewGuid().ToString());
                client.DefaultRequestHeaders.Add("MS-RequestId", Guid.NewGuid().ToString());
                client.DefaultRequestHeaders.Add("X-Locale", Constants.CSPLocale);
                client.Timeout = new TimeSpan(0, APIResponseTimeLimitConstants.RateCardFetchLimit, 0);

                // Set the path
                var path = string.Format("{0}/v1/ratecards/azure?currency={1}&region={2}", APIURLConstants.PCAPIUrl, cspCreds.CSPCurrency, cspCreds.CSPRegion);
                Uri uri  = new Uri(path);

                // Make the API Call to fetch the Rate Card
                HttpResponseMessage response = client.GetAsync(uri).Result;

                if (response.IsSuccessStatusCode)
                {
                    string   jsonResult = response.Content.ReadAsStringAsync().Result;
                    RateCard card       = JsonConvert.DeserializeObject <RateCard>(jsonResult);
                    meterList = card.Meters;
                }
                else
                {
                    string jsonResult = response.Content.ReadAsStringAsync().Result;
                    new Exception(ExceptionLogger.GenerateLoggerTextForOnlineHelperCall("CSP Rate Card", string.Format("Error while fetching the Rate Card: {0}", jsonResult)));
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(meterList);
        }
예제 #2
0
        /// <summary>
        /// Calculates the monthly estimates for the resource components
        /// </summary>
        /// <param name="components">List of Resource Components</param>
        /// <param name="location">Azure Location</param>
        /// <param name="meterList">List of Meters for Azure CSP Rate Card</param>
        /// <param name="log">The object that will contain the exception messages</param>
        /// <returns> Returns the list of resource components with associated meters and monthly estimates</returns>
        public static List <ResourceComponent> CalculateResourceComponentRates(List <ResourceComponent> components, string location, List <Meter> meterList, out StringBuilder log)
        {
            ResourceRateCalc.meterList = meterList;
            log = new StringBuilder(string.Empty);

            string locationAsPerPricingSpecs = null;

            try
            {
                // Fetch the Location as per Pricing Specs, Throw exception is cannot be mapped
                if (!LocationConstants.LocationAsPerPricingSpecsMap.TryGetValue(location, out locationAsPerPricingSpecs))
                {
                    throw new Exception(ExceptionLogger.GenerateLoggerTextForInternalError("Location could not be mapped as per pricing specs"));
                }

                // Loop thru the list of resource components
                foreach (ResourceComponent component in components)
                {
                    // Check if the component is chargeable
                    if (component.IsChargeable)
                    {
                        string meterRegion = locationAsPerPricingSpecs;

                        // If Current resource component if for Networking Bandwidth, Set the region to Zone by getting value from mapping, Throw exception is not found
                        if (NetworkingResourceConstants.NetworkingMeterNameForVMNetwork.Equals(component.MeterName, StringComparison.OrdinalIgnoreCase))
                        {
                            string zone = null;
                            if (LocationConstants.LocationZoneMap.TryGetValue(locationAsPerPricingSpecs, out zone))
                            {
                                meterRegion = zone;
                            }
                            else
                            {
                                log.AppendLine(ExceptionLogger.GenerateLoggerTextForInternalError("Location could not be mapped to a networking zone as per pricing specs"));
                                component.CostPerMonth = 0;
                                component.Rate         = 0;
                                continue;
                            }
                        }

                        double rate = 0;

                        // Get the Rate and Monthly Cost estimates for the current resource component
                        component.CostPerMonth = GetResourceRate(component.MeterCategory, component.MeterSubCategory, component.MeterName, meterRegion, component.Quantity, out rate, ref log, component.ResourceName);
                        component.Rate         = rate;
                    }
                    else
                    {
                        // If component is not chargeable, set to rate and monthly cost to zero
                        component.CostPerMonth = 0;
                        component.Rate         = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                components = null;
                log.AppendLine(ex.Message);
            }

            return(components);
        }