Esempio n. 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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }