Example #1
0
        public async Task <JArray> GetRegions()
        {
            excelAzureHelpers xhlp = new excelAzureHelpers(configuration);

            JArray cliRegions;

            // load local region list exported from CLI
            using (StreamReader r = new StreamReader("regionlist.json"))
            {
                string json = r.ReadToEnd();
                cliRegions = JArray.Parse(json.ToLower()); //JsonConvert.DeserializeObject<List<region>>(json);
            }
            //merge with calculator regions merge on displayname
            var response = await client.GetAsync(xhlp.GetVMURL(0, "usd", null));

            var result      = JsonConvert.DeserializeObject <dynamic>((await response.Content.ReadAsStringAsync()).ToLower());
            var calcRegions = (JArray)result["regions"];

            foreach (JObject item in calcRegions)
            {
                // find corresponding displayName in cli regions
                var selected = cliRegions.SelectToken("$[?(@.displayName == '" + item["displayName"] + "')]");
                //var selected = (JObject) cliRegions.Where(cliRegion => cliRegion["displayName"].Equals(item["displayName"]));

                item.Merge(selected, new JsonMergeSettings
                {
                    MergeArrayHandling = MergeArrayHandling.Union
                });
            }
            return(calcRegions);
        }
Example #2
0
        private async Task <Uri> storeBlob(string blob, DateTime?input_date, string type, string currency, int?ri = null)
        {
            excelAzureHelpers xhlp = new excelAzureHelpers(configuration);

            DateTime date;

            if (!input_date.HasValue)
            {
                date = DateTime.Now;
            }
            else
            {
                date = (DateTime)input_date;
            }
            int year  = date.Year;
            int month = date.Month;


            string storageConnectionString = configuration.GetValue <string>("BlobStorageSettings:blobConnectionString");
            string containerName           = configuration.GetValue <string>("BlobStorageSettings:containerName");


            CloudStorageAccount storageAccount;

            if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
            {
                // If the connection string is valid, proceed with operations against Blob storage here.
            }
            else
            {
                // Otherwise, let the user know that they need to define the environment variable.
                Debug.WriteLine(
                    "A connection string has not been defined in the system environment variables. " +
                    "Add a environment variable named 'storageconnectionstring' with your storage " +
                    "connection string as a value.");
            }

            // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account.
            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

            // Create a container called 'quickstartblobs' and append a GUID value to it to make the name unique.
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);

            // Set the permissions so the blobs are public.
            CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(xhlp.getBlobPath(date, currency, type, ri, false));

            if (!await cloudBlockBlob.ExistsAsync())
            {
                cloudBlockBlob.Properties.ContentType = "application/json";

                await cloudBlockBlob.UploadTextAsync(blob);
            }
            //           CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(xhlp.getBlobPath(date,currency,type,ri,false));

            return(cloudBlockBlob.Uri);
        }
Example #3
0
        public async Task <string> StoreMdisk(string region = "europe-west", string currency = "usd")
        {
            client = new HttpClient();

            mdisk             mdk  = new mdisk(configuration);
            excelAzureHelpers xhlp = new excelAzureHelpers(configuration);
            var url      = mdisk.mdiskUrl + currency;
            var response = await client.GetAsync(url);

            var result = await response.Content.ReadAsStringAsync();

            var uri = await storeBlob(result, DateTime.UtcNow, "mdisk", currency, null);

            return("Stored mdisks at " + uri.AbsoluteUri);
        }
Example #4
0
        public async Task <string> StoreVMs(int ri = 0, string format = "", string currency = "usd")
        {
            client = new HttpClient();
            VmList            vml  = new VmList(configuration);
            excelAzureHelpers xhlp = new excelAzureHelpers(configuration);
            //var taskList = new List<Task<HttpResponseMessage>>();
            var url      = xhlp.GetVMURL(ri, currency.ToLower(), null);
            var response = await client.GetAsync(url);

            var result = await response.Content.ReadAsStringAsync();

            var uri = await storeBlob(result, DateTime.UtcNow, "vm", currency, ri);

            return("Stored VM's at " + uri.AbsoluteUri);
        }
Example #5
0
        public async Task <List <UltraSSD> > GetUltraSSDs(string region = "us-east", string currency = "usd", DateTime?date = null)
        {
            List <UltraSSD>   usds = new List <UltraSSD>();
            excelAzureHelpers xhlp = new excelAzureHelpers(configuration);
            var response           = await client.GetAsync(xhlp.GetMdiskURL(currency, date));

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                var     result             = JsonConvert.DeserializeObject <JObject>(content);
                JObject maxIopsPerGb       = (JObject)result["maxIopsPerGb"];
                JObject maxThroughputPerGb = (JObject)result["maxThroughputPerGb"];
                int     minIopsPerGb       = result["minIops"].Value <int>();
                int     minThroughput      = result["minThroughput"].Value <int>();

                try
                {
                    foreach (JObject item in result["ultraSizes"])
                    {
                        UltraSSD disk = new UltraSSD();
                        disk.name          = "ultrassd-u" + item["slug"].Value <string>();
                        disk.currency      = currency;
                        disk.size          = item["slug"].Value <int>();
                        disk.displayName   = "Ultra SSD " + item["displayName"].Value <string>();
                        disk.minIops       = minIopsPerGb;
                        disk.minThroughput = minThroughput;

                        disk.maxIops       = maxIopsPerGb.SelectToken(disk.size.ToString()).Value <int>();
                        disk.maxThroughput = maxThroughputPerGb.SelectToken(disk.size.ToString()).Value <int>();
                        disk.priceIops     = result["offers"]["ultrassd-iops"]["prices"][region]["value"].Value <decimal>();

                        disk.priceThroughput = result["offers"]["ultrassd-throughput"]["prices"][region]["value"].Value <decimal>();
                        disk.priceVCpu       = result["offers"]["ultrassd-vcpu"]["prices"][region]["value"].Value <decimal>();
                        disk.priceSize       = result["offers"]["ultrassd-stored"]["prices"][region]["value"].Value <decimal>();

                        usds.Add(disk);
                    }
                }
                catch (Exception)
                {
                    // Ignore Exceptions
                    //throw;
                }
            }
            return(usds);
        }
Example #6
0
        public async Task <List <mdisk> > GetMdisks(string region = "europe-west", string currency = "usd", DateTime?date = null)
        {
            List <mdisk>      mds  = new List <mdisk>();
            excelAzureHelpers xhlp = new excelAzureHelpers(configuration);

            var response = await client.GetAsync(xhlp.GetMdiskURL(currency, date));

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                var result = JsonConvert.DeserializeObject <JObject>(content);

                decimal?snapshotPriceStandardHHD_LRS = (decimal?)result.SelectToken("offers.standardhdd-snapshot-lrs.prices." + region + ".value");
                decimal?snapshotPriceStandardHDD_ZRS = (decimal?)result.SelectToken("offers.standardhdd-snapshot-zrs.prices." + region + ".value");

                decimal?snapshotPriceStandardSSD = (decimal?)result.SelectToken("offers.standardssd-snapshot.prices." + region + ".value");
                decimal?snapshotPricePremium     = (decimal?)result.SelectToken("offers.premiumssd-snapshot.prices." + region + ".value");
                decimal?transactionPriceHDD      = (decimal?)result.SelectToken("offers.transactions-hdd.prices." + region + ".value");
                decimal?transactionPriceSSD      = (decimal?)result.SelectToken("offers.transactions-ssd.prices." + region + ".value");

                foreach (JProperty item in result["offers"])
                {
                    if (!item.Name.Contains("snapshot") && item.Name != "standardssd-snapshot" && item.Name != "premiumssd-snapshot" && !item.Name.Contains("transactions"))
                    {
                        // switch to take out non disk elements
                        var     productname     = item.Name.Split('-').Last().ToLower();
                        var     seriesname      = item.Name.Split('-').First().ToLower();
                        var     dispName        = mdiskSearchDispName(seriesname, result["tiers"]) + " " + mdiskSearchDispName(productname, result["sizes"]);
                        bool    isPre           = false;
                        decimal pricemultiplier = 1;

                        if (dispName.ToLower().Contains("preview") || dispName.ToLower().Contains("promo") || dispName.ToLower().Contains("offer"))
                        {
                            isPre           = true;
                            pricemultiplier = 2;
                        }


                        decimal?pricemonth;
                        decimal?priceyear;

                        try
                        {
                            pricemonth = (decimal?)item.Value.SelectToken("prices." + region + ".value") * pricemultiplier; // Value["prices"][region]["value"].Value<decimal>();
                            if (pricemonth != null)
                            {
                                priceyear = (decimal?)pricemonth * 12 * pricemultiplier;
                            }
                            else
                            {
                                priceyear = null;
                            }

                            if (item.Name.Contains("premiumssd"))
                            {
                                mds.Add(new mdisk(configuration)
                                {
                                    name = item.Name, displayName = dispName, isPreview = isPre, size = item.Value["size"].Value <int>(), iops = item.Value["iops"].Value <int>(), speed = item.Value["speed"].Value <int>(), pricemonth = pricemonth, priceyear = priceyear, snapshotPrice = snapshotPricePremium, transactionPrice = transactionPriceSSD
                                });
                            }
                            else if (item.Name.Contains("standardssd"))
                            {
                                mds.Add(new mdisk(configuration)
                                {
                                    name = item.Name, displayName = dispName, isPreview = isPre, size = item.Value["size"].Value <int>(), iops = item.Value["iops"].Value <int>(), speed = speed = item.Value["speed"].Value <int>(), pricemonth = pricemonth, priceyear = priceyear, snapshotPrice = snapshotPriceStandardSSD, transactionPrice = transactionPriceSSD
                                });
                            }

                            else if (item.Name.Contains("standardhdd"))
                            {
                                mds.Add(new mdisk(configuration)
                                {
                                    name = item.Name, displayName = dispName, isPreview = isPre, size = item.Value["size"].Value <int>(), iops = item.Value["iops"].Value <int>(), speed = speed = item.Value["speed"].Value <int>(), pricemonth = pricemonth, priceyear = priceyear, snapshotPrice = null, snapshotPriceLrs = snapshotPriceStandardHHD_LRS, snapshotPriceZrs = snapshotPriceStandardHDD_ZRS, transactionPrice = transactionPriceHDD
                                });
                            }
                        }
                        catch (Exception)
                        {
                            System.Diagnostics.Trace.TraceWarning("Issues with : " + item.ToString());
                            throw;
                        }
                    }
                }
            }
            else
            {
                System.Diagnostics.Trace.TraceError("endpoint unresponsive : " + mdiskUrl);
            }


            return(mds);
        }
Example #7
0
        /*public async Task<VmList> GetVmsAsync(string date,  int minCores = 0, int minMem = 0, int bindingPeriod = 0, string region = "europe-west", string currency = "usd") {
         *
         *          // TODO implement historic pricing
         *
         * }*/


        public async Task <VmList> GetVmsAsyncSoftware(int minCores = 0, int minMem = 0, int bindingPeriod = 0, string region = "europe-west", string currency = "usd", DateTime?date = null)
        {
            excelAzureHelpers xhlp = new excelAzureHelpers(configuration);

            var response = await client.GetAsync(GetVMURLSoftware(bindingPeriod, currency));


            // #retries
            for (int i = 0; i < 3; i++)
            {
                if (!response.IsSuccessStatusCode)
                {
                    response = await client.GetAsync(GetVMURLSoftware(bindingPeriod, currency));
                }
                else
                {
                    break;
                }
            }


            VmList vmlist = new VmList(configuration);

            vmlist.vms = new List <vm>();

            if (response.IsSuccessStatusCode)
            {
                var result = JsonConvert.DeserializeObject <JObject>(await response.Content.ReadAsStringAsync());

                foreach (JProperty item in result["offers"])
                {
                    try
                    {
                        if (item.Name != "transactions" && item.Value["baseOfferSlug"] != null) // transactions appearing in the offers list
                        {
                            var  dispName = item.Name;                                          //vmSearchDispName(item.Value["baseOfferSlug"].Value<string>(), result[GetVMDispNameToken(bindingPeriod)]);
                            bool isPre    = false;
                            if (dispName.ToLower().Contains("preview") || dispName.ToLower().Contains("promo") || dispName.ToLower().Contains("offer"))
                            {
                                isPre = true;
                            }
                            decimal?price;
                            decimal?pricemonth;
                            decimal?priceyear;
                            decimal pricemultiplier = 1;
                            if (isPre)
                            {
                                pricemultiplier = 2;
                            }


                            price = (decimal?)item.Value.SelectToken("prices." + region + ".value") * pricemultiplier; // item.Value["prices"][region]["value"].Value<decimal>();
                            if (price != null)
                            {
                                pricemonth = (decimal?)price * 730 * pricemultiplier;
                                priceyear  = (decimal?)pricemonth * 12 * pricemultiplier;
                            }
                            else
                            {
                                pricemonth = null;
                                priceyear  = null;
                            }

                            vmlist.vms.Add(new vm()
                            {
                                name = item.Name, currency = currency, displayName = "", isPreview = isPre, ri = bindingPeriod, cores = item.Value["cores"].Value <int>(), ram = item.Value["ram"].Value <int>(), diskSize = item.Value["diskSize"].Value <int>(), isVcpu = item.Value["isVcpu"].Value <bool>(), price = price, pricemonth = pricemonth, priceyear = priceyear
                            });
                        }
                    }
                    catch (Exception)
                    {
                        System.Diagnostics.Trace.TraceWarning("Issues with : " + item.ToString());

                        throw;
                    }
                }
            }
            else
            {
                System.Diagnostics.Trace.TraceError("endpoint unresponsive : " + xhlp.GetVMURL(bindingPeriod, currency, null));
            }
            return(vmlist);
        }