Exemple #1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            OfferingClass offeringClass = (OfferingClass)value;

            switch (offeringClass)
            {
            case OfferingClass.STANDARD:
            {
                writer.WriteValue("Standard");
                break;
            }

            case OfferingClass.CONVERTIBLE:
            {
                writer.WriteValue("Convertible");
                break;
            }

            default:
            case OfferingClass.UNKNOWN:
            {
                writer.WriteValue("Unknown");
                break;
            }
            }
        }
Exemple #2
0
        public TermAttributes(
            int leaseContractLength,
            PurchaseOption purchaseOption,
            OfferingClass offeringClass
            )
        {
            if (leaseContractLength < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(leaseContractLength), "The lease contract length cannot be less than zero.");
            }

            this.LeaseContractLength = leaseContractLength;
            this.PurchaseOption      = purchaseOption;
            this.OfferingClass       = offeringClass;
        }
Exemple #3
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject obj = JObject.Load(reader);

            if (reader.TokenType == JsonToken.Null)
            {
                return(new TermAttributes(0, PurchaseOption.ON_DEMAND, OfferingClass.STANDARD));
            }

            if (reader.TokenType != JsonToken.Null)
            {
                if (!obj.HasValues)
                {
                    return(new TermAttributes(0, PurchaseOption.ON_DEMAND, OfferingClass.STANDARD));
                }
            }

            obj.TryGetValue("LeaseContractLength", StringComparison.OrdinalIgnoreCase, out JToken leaseToken);
            obj.TryGetValue("OfferingClass", StringComparison.OrdinalIgnoreCase, out JToken offeringClassToken);
            obj.TryGetValue("PurchaseOption", StringComparison.OrdinalIgnoreCase, out JToken purchaseOptionToken);

            OfferingClass  offeringClass = OfferingClass.STANDARD;
            PurchaseOption option        = PurchaseOption.ON_DEMAND;
            int            lease         = 0;

            if (offeringClassToken != null)
            {
                offeringClass = serializer.Deserialize <OfferingClass>(offeringClassToken.CreateReader());
            }

            if (purchaseOptionToken != null)
            {
                option = serializer.Deserialize <PurchaseOption>(purchaseOptionToken.CreateReader());
            }

            if (leaseToken != null)
            {
                JsonSerializer jsonSerializer = new JsonSerializer()
                {
                    Converters = { new LeaseContractLengthConverter() }
                };

                lease = jsonSerializer.Deserialize <int>(leaseToken.CreateReader());
            }

            return(new TermAttributes(lease, option, offeringClass));
        }
Exemple #4
0
        public CsvRowItem(
            string sku,
            string offerTermCode,
            Term termType,
            int leaseContractLength,
            double pricePerUnit,
            int vcpu,
            double memory,
            PurchaseOption purchaseOption,
            OfferingClass offeringClass,
            string tenancy,
            string instanceType,
            string platform,
            string operatingSystem,
            string operation,
            string usageType,
            string serviceCode,
            string region,
            string description
            )
        {
            this.Sku                 = sku;
            this.OfferTermCode       = offerTermCode;
            this.TermType            = termType;
            this.LeaseContractLength = leaseContractLength;
            this.PricePerUnit        = pricePerUnit;
            this.vCPU                = vcpu;
            this.Memory              = memory;
            this.PurchaseOption      = purchaseOption;
            this.OfferingClass       = offeringClass;
            this.Tenancy             = tenancy;
            this.InstanceType        = instanceType;
            this.Platform            = platform;
            this.OperatingSystem     = operatingSystem;
            this.Operation           = operation;
            this.UsageType           = usageType;
            this.ServiceCode         = serviceCode;
            this.Region              = region;
            this.Description         = description;

            this.Key = $"{this.LeaseContractLength}::{this.PurchaseOption}::{this.OfferingClass}";
        }
Exemple #5
0
        public ReservedInstancePricingTerm(
            string sku,
            string offerTermCode,
            string service,
            string platform,
            string operatingSystem,
            string instanceType,
            string operation,
            string usageType,
            string tenancy,
            string region,
            int vcpus,
            double memory,
            double onDemandHourlyCost,
            double adjustedPricePerUnit,
            double upfrontFee,
            int leaseTerm,
            PurchaseOption purchaseOption,
            OfferingClass offeringClass,
            Term termType
            )
        {
            this.Sku                  = sku;
            this.OfferTermCode        = offerTermCode;
            this.Service              = service;
            this.Platform             = platform;
            this.OperatingSystem      = operatingSystem;
            this.InstanceType         = instanceType;
            this.Operation            = operation;
            this.UsageType            = usageType;
            this.Tenancy              = tenancy;
            this.Region               = region;
            this.vCPU                 = vcpus;
            this.Memory               = memory;
            this.OnDemandHourlyCost   = onDemandHourlyCost;
            this.AdjustedPricePerUnit = adjustedPricePerUnit;
            this.UpfrontFee           = upfrontFee;
            this.LeaseTerm            = leaseTerm;
            this.PurchaseOption       = purchaseOption;
            this.OfferingClass        = offeringClass;
            this.TermType             = termType;

            this.BreakevenPercentage = (this.UpfrontFee + (365 * this.LeaseTerm * 24 * this.AdjustedPricePerUnit)) / (365 * this.LeaseTerm * 24 * this.OnDemandHourlyCost);

            if (termType == Term.ON_DEMAND)
            {
                this.Key = "OnDemand";
            }
            else
            {
                this.Key = $"{this.LeaseTerm}::{this.PurchaseOption.ToString()}::{this.OfferingClass.ToString()}";
            }

            // Calculated properties

            this.ReservedInstanceCost = this.UpfrontFee + (this.AdjustedPricePerUnit * 24 * 365 * this.LeaseTerm);
            this.OnDemandCostForTerm  = this.OnDemandHourlyCost * 24 * 365 * this.LeaseTerm;
            this.CostSavings          = OnDemandCostForTerm - ReservedInstanceCost;

            if (this.OnDemandCostForTerm > 0)
            {
                this.PercentSavings = Math.Round(((1 - (ReservedInstanceCost / OnDemandCostForTerm)) * 100), 3);
            }
            else
            {
                this.PercentSavings = 0;
            }
        }
Exemple #6
0
 /// <summary>
 /// Creates a new term attribute that defaults to a standard, on-demand term
 /// </summary>
 public TermAttributes()
 {
     this.LeaseContractLength = 0;
     this.PurchaseOption      = PurchaseOption.ON_DEMAND;
     this.OfferingClass       = OfferingClass.STANDARD;
 }
Exemple #7
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);
            }
        }