/// <summary> /// Background worker function /// </summary> /// <param name="sender">sender object</param> /// <param name="e">e (DoWorkEventArgs)</param> private void BackGrndWorker_DoWork(object sender, DoWorkEventArgs e) { // Create a background worker object BackgroundWorker worker = sender as BackgroundWorker; // Set the input data OperationData opData = (OperationData)e.Argument; // Switch to choose the operation to be performed switch (opData.Operation) { // Rate Card Fetch Operation case OperationType.FetchRateCard: FetchRateCardInput rateCardInput = (FetchRateCardInput)opData.Data; this.FetchRateCard(rateCardInput.CSPARMPricingCalc, rateCardInput.CSPCreds); e.Result = new OperationData() { Operation = OperationType.FetchRateCard, Data = null }; break; // CSP ARM Pricing Calculation Operation case OperationType.CalculateCSPARMPricing: CalculateCSPARMPricingInput calculateCSPARMPricingInput = (CalculateCSPARMPricingInput)opData.Data; CSPARMPricingInfo info = this.CalculateCSPARMPricing(calculateCSPARMPricingInput.CSPARMPricingCalc, calculateCSPARMPricingInput.ARMTemplateFilePath, calculateCSPARMPricingInput.ARMParamValueFilePath, calculateCSPARMPricingInput.Location, calculateCSPARMPricingInput.CSPCreds); e.Result = new OperationData() { Operation = OperationType.CalculateCSPARMPricing, Data = info }; break; default: break; } }
/// <summary> /// Writes the Result of the ARM Pricing Calculation to a file /// </summary> /// <param name="fileName">Path to the file</param> /// <param name="cspARMPricingInfoOutput">Object containing the result of the ARM Pricing Calculation</param> /// <param name="totalCost">Total Cost</param> public static void ExportCSPARMPricingToCSV(string fileName, CSPARMPricingInfo cspARMPricingInfoOutput, double totalCost) { List <ResourceComponent> cspARMPricingList = cspARMPricingInfoOutput.CSPARMPricingList; StringBuilder sb = new StringBuilder(); // Add header row string line = "Resource Name,Resource Type,Meter Name,Meter Category,Meter SubCategory,Is Chargeable?,Quantity,Rate,Cost Per Month"; sb.AppendLine(line); // Add Resource Component rows with pricing for (int i = 0; i < cspARMPricingList.Count; i++) { line = string.Format("\"{0}\",{1},\"{2}\",\"{3}\",\"{4}\",{5},{6},{7},{8}", cspARMPricingList[i].ResourceName, cspARMPricingList[i].ResourceType, cspARMPricingList[i].MeterName, cspARMPricingList[i].MeterCategory, cspARMPricingList[i].MeterSubCategory, cspARMPricingList[i].IsChargeable.ToString(), cspARMPricingList[i].Quantity.ToString(), cspARMPricingList[i].Rate, cspARMPricingList[i].CostPerMonth.ToString()); sb.AppendLine(line); } // Add Total row line = string.Format("Total Cost:,,,,,,,,{0}", totalCost.ToString()); sb.AppendLine(line); try { // Write to File File.WriteAllText(fileName, sb.ToString()); } catch (Exception) { throw; } }
/// <summary> /// Function that will be asynchronously called - that calls the CSP ARM Pricing Calculation module /// </summary> /// <param name="cspARMPricingCalc">Object of the class whose method will be called to perform CSP ARM Pricing Calculation</param> /// <param name="armTemplateFilePath">Path to the ARM Template File</param> /// <param name="armParamValueFilePath">Path to the ARM Parameter File</param> /// <param name="location">Azure Location selection</param> /// <param name="cspCreds">CSP Account credentials object. A token will be generated using these credentials and used for making the online ARM API call</param> /// <returns> Returns the Result of the CSP ARM Pricing Calculation with resource components and monthly cost estimates</returns> private CSPARMPricingInfo CalculateCSPARMPricing(CSPARMPricingCalculator cspARMPricingCalc, string armTemplateFilePath, string armParamValueFilePath, string location, CSPAccountCreds cspCreds) { CSPARMPricingInfo info = null; try { // Read the ARM Template file and get the object ARMTemplate template = FileUtil.GetResourceList(armTemplateFilePath); // Read the ARM Template file and get the object, Parameter file is optional ARMParamValue paramValue = new ARMParamValue() { ContentVersion = null, Parameters = null }; if (armParamValueFilePath != null && !armParamValueFilePath.Equals(string.Empty)) { paramValue = FileUtil.GetParamValueList(armParamValueFilePath); } // Report progress message this.BackGrndWorker.ReportProgress(50, UIMessageConstants.CSPARMPricingCalculationProgressFileReadCompleteMsg); // Calculate info = cspARMPricingCalc.CalculateCSPARMPricing(template, paramValue, location, cspCreds); // Report progress message this.BackGrndWorker.ReportProgress(100, UIMessageConstants.CSPARMPricingCalculationProgressAllCompleteMsg); } catch (JsonSerializationException ex) { info = new CSPARMPricingInfo(); info.Log.Append(string.Format(UIMessageConstants.CSPARMPricingCalculationInvalidJSONMsg, ex.Message)); } catch (Exception ex) { info = new CSPARMPricingInfo(); info.Log.Append(string.Format("{0}", ex.Message)); } return(info); }
/// <summary> /// Gets the resource components for the Virtual machine resource in an ARM template /// </summary> /// <param name="template">The object containing the ARM Template</param> /// <param name="paramValue">The object containing the values in the Parameter file</param> /// <param name="location">The Azure Location</param> /// <param name="cspCreds">CSP Account credentials object. A token will be generated using these credentials and used for making the online API call.</param> /// <returns> Returns the result of the CSP ARM Pricing Calculation</returns> public CSPARMPricingInfo CalculateCSPARMPricing(ARMTemplate template, ARMParamValue paramValue, string location, CSPAccountCreds cspCreds) { CSPARMPricingInfo info = new CSPARMPricingInfo(); StringBuilder log = new StringBuilder(string.Empty); info.Log = new StringBuilder(string.Empty); this.template = template; // Fetch the list of resource components for the ARM Template List <ResourceComponent> components = ComponentFetcher.GetResourceComponentsForTemplate(template, paramValue, location, cspCreds, out log); if (log != null) { // Append log of exception messages if any info.Log.Append(log.ToString()); } // Fetch the Rates for the Resource components and Calculate monthly Estimates List <ResourceComponent> ratedComponents = null; if (components != null && components.Count > 0) { ratedComponents = ResourceRateCalc.CalculateResourceComponentRates(components, location, this.meterlist, out log); } if (log != null) { // Append log of exception messages if any info.Log.Append(log.ToString()); } // Set currency and object of resource component with pricing and estimation details info.Currency = this.currency; info.CSPARMPricingList = ratedComponents; return(info); }
/// <summary> /// Background worker run completed function /// </summary> /// <param name="sender">sender object</param> /// <param name="e">e (RunWorkerCompletedEventArgs)</param> private void BackGrndWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // Enable the status of the buttons this.ToggleButtonStatus(true); // Check if error occured while processing operation or if user cancelled, Display log messages accordingly if (e.Error != null) { this.AddOperationStatusMessage(string.Format(UIMessageConstants.BackgroundOperationFailedMsg, e.Error.Message)); } else if (e.Cancelled) { this.AddOperationStatusMessage(UIMessageConstants.BackgroundOperationCancelledMsg); } else { // Background Operation complete and successful OperationData data = (OperationData)e.Result; switch (data.Operation) { case OperationType.FetchRateCard: StringBuilder str = new StringBuilder(); this.AddOperationStatusMessage(UIMessageConstants.RateCardFetchCompleteMsg); break; case OperationType.CalculateCSPARMPricing: // CSP ARM Pricing Calculation Complete OperationData cspARMPricingOutput = (OperationData)e.Result; CSPARMPricingInfo info = (CSPARMPricingInfo)cspARMPricingOutput.Data; this.cspARMPricingInfoOutput = info; if (info != null && info.Log != null) { this.AddOperationStatusMessage(info.Log.ToString()); } this.totalCost = 0; if (info != null & info.CSPARMPricingList != null) { // Load grid with result this.AddOperationStatusMessage(UIMessageConstants.GridShowingResultsMsg); this.GridVwCSPARMPricing.DataSource = info.CSPARMPricingList; this.GridVwCSPARMPricing.Refresh(); // Get the total cost and Display foreach (ResourceComponent component in (List <ResourceComponent>)info.CSPARMPricingList) { this.totalCost = this.totalCost + component.CostPerMonth; } this.txtTotalRate.Text = string.Format(UIMessageConstants.TotalCostStringMsg, this.totalCost, info.Currency); } break; default: break; } } }