Example #1
0
        public ActionResult Index()
        {
            HomeIndexViewModel model = null;

            if (ClaimsPrincipal.Current.Identity.IsAuthenticated)
            {
                model = new HomeIndexViewModel();
                model.UserOrganizations = new Dictionary <string, Organization>();
                model.UserSubscriptions = new Dictionary <string, Subscription>();
                model.UserCanManageAccessForSubscriptions = new List <string>();
                model.DisconnectedUserOrganizations       = new List <string>();

                var orgnaizations = AzureResourceManagerUtil.GetUserOrganizations();
                foreach (Organization org in orgnaizations)
                {
                    model.UserOrganizations.Add(org.Id, org);
                    var subscriptions = AzureResourceManagerUtil.GetUserSubscriptions(org.Id);

                    if (subscriptions != null)
                    {
                        foreach (var subscription in subscriptions)
                        {
                            Subscription s = db.Subscriptions.Find(subscription.Id);
                            if (s != null)
                            {
                                subscription.IsConnected = true;
                                subscription.ConnectedOn = s.ConnectedOn;
                                subscription.ConnectedBy = s.ConnectedBy;
                                subscription.AzureAccessNeedsToBeRepaired = AzureResourceManagerUtil.ServicePrincipalHasReadAccessToSubscription(subscription.Id, org.Id);
                                subscription.UsageString = AzureResourceManagerUtil.GetUsage(s.Id, org.Id);
                                //Deserialize the usage response into the usagePayload object
                                subscription.usagePayload = JsonConvert.DeserializeObject <UsagePayload>(subscription.UsageString);
                                List <UsageAggregate> UsageAggregateList = subscription.usagePayload.value;
                            }
                            else
                            {
                                subscription.IsConnected = false;
                            }
                            model.UserSubscriptions.Add(subscription.Id, subscription);

                            if (AzureResourceManagerUtil.UserCanManageAccessForSubscription(subscription.Id, org.Id))
                            {
                                model.UserCanManageAccessForSubscriptions.Add(subscription.Id);
                            }
                        }
                    }
                    else
                    {
                        model.DisconnectedUserOrganizations.Add(org.Id);
                    }
                }
            }
            return(View(model));
        }
Example #2
0
        /// <summary>
        /// Method to collect azure usage records from Azure APIs.
        /// </summary>
        /// <returns>Collection of Usage records.</returns>
        private IEnumerable <UsageInfoModel> GetUsageInfo()
        {
            // Loop for every subscription
            foreach (var subscription in this.subscriptions)
            {
                // Get usage details for subscription
                RateCardPayload rates = AzureResourceManagerUtil.GetRates(subscription.Id, org.Id, authResult);

                List <UsageAggregate> usageList = AzureResourceManagerUtil.GetUsage(
                    subscription.Id,
                    org.Id,
                    authResult,
                    this.startDate,
                    this.endDate);

                IEnumerable <string> projects = AzureResourceManagerUtil.GetUniqueProjects(usageList);
                subscription.Projects = projects;

                IEnumerable <ProjectUsage> projectTotals =
                    AzureResourceManagerUtil.GetProjectIDTotals(usageList, projects);
                subscription.ProjectTotals = projectTotals;

                IEnumerable <ProjectRate> projectRates = AzureResourceManagerUtil.GetProjectRates(
                    rates,
                    projectTotals);
                subscription.ProjectRates = projectRates;

                IEnumerable <ProjectEstimate> projectEstimateTotals =
                    AzureResourceManagerUtil.RatedEstimateForProject(projectRates);
                subscription.ProjectEstimateTotals = projectEstimateTotals;

                IEnumerable <string> resourceList = AzureResourceManagerUtil.GetResources(usageList);

                IEnumerable <ResourceUsage> resourceUsages =
                    AzureResourceManagerUtil.GetResourceUsage(usageList, resourceList);
                subscription.ResourceTotals = AzureResourceManagerUtil.GetResourceTotals(resourceList, usageList);

                IEnumerable <ResourceRate> resourceRates =
                    AzureResourceManagerUtil.GetResourceRates(rates, resourceUsages);

                IEnumerable <ResourceEstimate> resourceEstimates =
                    AzureResourceManagerUtil.RatedEstimateForResource(resourceRates);
                subscription.ResourceEstimates = resourceEstimates;

                subscription.MeterIdTotals = AzureResourceManagerUtil.GetMeterIdTotals(usageList);
                subscription.ServiceTotals = AzureResourceManagerUtil.GetServiceTotals(usageList);

                var result = JsonConvert.SerializeObject(subscription.MeterIdTotals); // Input to graphing solution
                subscription.UsageDetails = result;

                IEnumerable <RatedUsage> ratedUsageList =
                    AzureResourceManagerUtil.GetMeterIDRates(
                        AzureResourceManagerUtil.GetMeterIdTotals(usageList), rates);
                subscription.ratedUsage    = ratedUsageList;
                subscription.RatedEstimate = AzureResourceManagerUtil.RatedEstimate(ratedUsageList);

                foreach (var usg in usageList)
                {
                    double usgtot =
                        AzureResourceManagerUtil.ComputeRatedUsagePerMeter(
                            AzureResourceManagerUtil.GetRatesForMeterID(rates, usg.properties.meterId),
                            usg.properties.quantity);

                    this.usageInfo.Add(new UsageInfoModel()
                    {
                        OrganizationId     = org.Id,
                        SubceriptionId     = subscription.Id,
                        UsageStartTime     = usg.properties.usageStartTime,
                        UsageEndTime       = usg.properties.usageEndTime,
                        MeteredRegion      = usg.properties.infoFields.MeteredRegion,
                        MeterName          = usg.properties.meterName,
                        MeteredService     = usg.properties.infoFields.MeteredService,
                        MeteredServiceType = usg.properties.infoFields.MeteredServiceType,
                        MeterCategory      = usg.properties.meterCategory,
                        MeterSubCategory   = usg.properties.meterSubCategory,
                        UserProject        = usg.properties.infoFields.Project,
                        Quantity           = usg.properties.quantity,
                        ItemTotal          = usgtot
                    });
                }
            }

            return(this.usageInfo);
        }