예제 #1
0
        /// <summary>
        /// Creates a consolidated pricing term from a regularly formatted pricing term
        /// that comes directly from the price list API. Used when you've already identified the on
        /// demand pricing term and have separated out just the reserved pricing terms.
        /// </summary>
        /// <param name="product"></param>
        /// <param name="ondemand"></param>
        /// <param name="reservedterms"></param>
        /// <returns></returns>
        public static IEnumerable <ReservedInstancePricingTerm> Build(Product product, PricingTerm ondemand, IEnumerable <PricingTerm> reservedterms)
        {
            if (product == null)
            {
                throw new ArgumentNullException("product");
            }

            if (ondemand == null)
            {
                throw new ArgumentNullException("ondemand");
            }

            if (reservedterms == null)
            {
                throw new ArgumentNullException("reservedterms");
            }

            if (!reservedterms.Any())
            {
                throw new ArgumentException("You must supply at least 1 reserved term.");
            }

            double onDemandCost = -1;

            // Get the on demand hourly cost
            // DynamoDB has free tier price dimensions in the same on demand object, so make
            // sure we pick the first that does not have free tier in the description
            KeyValuePair <string, PriceDimension> dimension = ondemand.PriceDimensions.FirstOrDefault(x => !x.Value.Description.Contains(FREE_TIER, StringComparison.OrdinalIgnoreCase));

            if (dimension.Value == null)
            {
                onDemandCost = 0;
            }
            else if (!Double.TryParse(dimension.Value.PricePerUnit.First().Value, out onDemandCost))
            {
                throw new FormatException($"Could not parse the on demand price {dimension.Value.PricePerUnit.First().Value} for sku: {product.Sku}.");
            }

            string platform = GetPlatform(product);
            string region   = RegionMapper.GetRegionFromUsageType(product.Attributes.GetValueOrDefault("usagetype"));

            // Only EC2 has tenancy
            if (!product.Attributes.TryGetValue("tenancy", out string tenancy))
            {
                tenancy = "Shared";
            }

            // Each pricing term will have the price dimensions for the upfront and recurring costs
            foreach (PricingTerm term in reservedterms)
            {
                PriceDimension upfront = term.PriceDimensions
                                         .Select(x => x.Value)
                                         .FirstOrDefault(x => !String.IsNullOrEmpty(x.Description) && x.Description.Equals("upfront fee", StringComparison.OrdinalIgnoreCase));

                PriceDimension recurring = term.PriceDimensions
                                           .Select(x => x.Value)
                                           .FirstOrDefault(x => !String.IsNullOrEmpty(x.Description) && !x.Description.Equals("upfront fee", StringComparison.OrdinalIgnoreCase));

                double hourlyRecurring = 0;

                // Only check for recurring, since some may have no upfront
                if (recurring == null)
                {
                    // This should never happen
                    throw new KeyNotFoundException($"The pricing term in {product.Attributes.GetValueOrDefault("servicecode")} for sku {term.Sku} and offer term code {term.OfferTermCode} did not contain a price dimension for hourly usage charges.");
                }
                else
                {
                    // Parse out the rate
                    if (!Double.TryParse(recurring.PricePerUnit.First().Value, out hourlyRecurring))
                    {
                        throw new FormatException($"Could not parse the recurring price per unit of {recurring.PricePerUnit.First().Value} for sku {term.Sku}, offer term code {term.OfferTermCode}, in service {product.Attributes.GetValueOrDefault("servicecode")}.");
                    }
                }

                double upfrontFee = 0;

                if (upfront != null)
                {
                    // Parse out upfront fee
                    if (!Double.TryParse(upfront.PricePerUnit.First().Value, out upfrontFee))
                    {
                        throw new FormatException($"Could not parse the upfront cost of {upfront.PricePerUnit.First().Value} for sku {term.Sku}, offer term code {term.OfferTermCode}, in service {product.Attributes.GetValueOrDefault("servicecode")}.");
                    }
                }

                string operatingSystem = String.Empty;

                if (product.Attributes.ContainsKey("operatingsystem"))
                {
                    operatingSystem = product.Attributes.GetValueOrDefault("operatingsystem");
                }
                else
                {
                    operatingSystem = platform;
                }

                int vCPU = 0;

                if (product.Attributes.ContainsKey("vcpu"))
                {
                    Int32.TryParse(product.Attributes.GetValueOrDefault("vcpu"), out vCPU);
                }

                double memory = 0;

                if (product.Attributes.ContainsKey("memory"))
                {
                    string memoryString = product.Attributes.GetValueOrDefault("memory").Replace(",", "");

                    Match memoryMatch = _MemoryRegex.Match(memoryString);

                    if (memoryMatch.Success)
                    {
                        Double.TryParse(memoryMatch.Groups[1].Value, out memory);
                    }
                }

                string usageType = product.Attributes.GetValueOrDefault("usagetype");

                string instanceType = usageType;

                if (product.Attributes.ContainsKey("instancetype"))
                {
                    instanceType = product.Attributes.GetValueOrDefault("usagetype");
                }

                yield return(new ReservedInstancePricingTerm(
                                 term.Sku,
                                 term.OfferTermCode,
                                 product.Attributes.GetValueOrDefault("servicecode"),
                                 platform,
                                 operatingSystem,
                                 instanceType,
                                 product.Attributes.GetValueOrDefault("operation"),
                                 usageType,
                                 tenancy,
                                 region,
                                 vCPU,
                                 memory,
                                 onDemandCost,
                                 hourlyRecurring,
                                 upfrontFee,
                                 term.TermAttributes.LeaseContractLength,
                                 term.TermAttributes.PurchaseOption,
                                 term.TermAttributes.OfferingClass,
                                 AWSPriceListApi.Model.Term.RESERVED
                                 ));
            }
        }
예제 #2
0
        /// <summary>
        /// Builds a row item from the current line of the csv reader, this method
        /// does not change the position of the csv reader. If the row item
        /// does not describe a "reservable" charge, then null is returned
        /// </summary>
        /// <param name="reader">The csv reader to read from</param>
        /// <returns></returns>
        public static CsvRowItem Build(CsvReader reader)
        {
            string instanceType = String.Empty;

            // The field names are case sensitive
            if (reader.TryGetField <string>("operation", out string operation) &&
                !String.IsNullOrEmpty(operation) &&
                reader.TryGetField <string>("usagetype", out string usageType) &&
                allUsageTypes.IsMatch(usageType) &&
                reader.TryGetField <string>("servicecode", out string serviceCode) &&
                !String.IsNullOrEmpty(serviceCode) &&
                (Constants.InstanceBasedReservableServices.Contains(serviceCode) ? reader.TryGetField("instance type", out instanceType) : true)
                )
            {
                reader.TryGetField <string>("sku", out string sku);
                reader.TryGetField <double>("priceperunit", out double pricePerUnit);
                reader.TryGetField <string>("leasecontractlength", out string leaseContractLength);
                reader.TryGetField <string>("pricedescription", out string priceDescription);
                reader.TryGetField <string>("offertermcode", out string offerTermCode);
                reader.TryGetField <int>("vcpu", out int vCPU);
                reader.TryGetField <string>("memory", out string memoryString);

                double memory = 0;

                if (!String.IsNullOrEmpty(memoryString))
                {
                    memoryString = memoryString.Replace(",", "");

                    Match memoryMatch = memoryRegex.Match(memoryString);

                    if (memoryMatch.Success)
                    {
                        Double.TryParse(memoryMatch.Groups[1].Value, out memory);
                    }
                }

                Term termType = Term.ON_DEMAND;

                if (reader.TryGetField <string>("termtype", out string termString))
                {
                    termType = EnumConverters.ConvertToTerm(termString);
                }

                if (String.IsNullOrEmpty(instanceType))
                {
                    // This will probably only happen for DynamoDB
                    instanceType = usageType;
                }

                PurchaseOption purchaseOption = PurchaseOption.ON_DEMAND;

                if (reader.TryGetField <string>("purchaseoption", out string PurchaseOptionString))
                {
                    purchaseOption = EnumConverters.ConvertToPurchaseOption(PurchaseOptionString);
                }

                OfferingClass offeringClass = OfferingClass.STANDARD;

                if (reader.TryGetField <string>("offeringclass", out string offeringClassString))
                {
                    offeringClass = EnumConverters.ConvertToOfferingClass(offeringClassString);
                }

                // Only EC2 has tenancy
                if (!reader.TryGetField <string>("tenancy", out string tenancy))
                {
                    tenancy = "Shared";
                }

                int lease = String.IsNullOrEmpty(leaseContractLength) ? 0 : Int32.Parse(Regex.Match(leaseContractLength, "^([0-9]+)").Groups[1].Value);

                string platform = GetPlatform(reader);

                if (!reader.TryGetField <string>("operating system", out string operatingSystem))
                {
                    if (!String.IsNullOrEmpty(platform))
                    {
                        operatingSystem = platform;
                    }
                    else
                    {
                        operatingSystem = serviceCode;
                    }
                }

                return(new CsvRowItem(
                           sku,
                           offerTermCode,
                           termType,
                           lease,
                           pricePerUnit,
                           vCPU,
                           memory,
                           purchaseOption,
                           offeringClass,
                           tenancy,
                           instanceType,
                           platform,
                           operatingSystem,
                           operation,
                           usageType,
                           serviceCode,
                           RegionMapper.GetRegionFromUsageType(usageType),
                           priceDescription
                           ));
            }
            else
            {
                return(null);
            }
        }