Beispiel #1
0
        /// <summary>
        /// Gets the resource components for the Virtual machine resource in an ARM template
        /// </summary>
        /// <param name="resource">The object containing the resource</param>
        /// <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. This is not used in current version.</param>
        /// <param name="log">The object that will contain the exception messages</param>
        /// <returns> Returns the list of resource components</returns>
        public List <ResourceComponent> GetResourceComponents(Resource resource, ARMTemplate template, ARMParamValue paramValue, string location, CSPAccountCreds cspCreds, out StringBuilder log)
        {
            this.resource   = resource;
            this.template   = template;
            this.paramValue = paramValue;
            this.cspCreds   = cspCreds;
            this.location   = location;

            List <ResourceComponent> componentList = new List <ResourceComponent>();

            log = new StringBuilder(string.Empty);

            try
            {
                if (resource != null && resource.Name != null)
                {
                    // Get the name of the resource
                    this.nameOfResource = PropertyHelper.GetValueIfVariableOrParam(resource.Name, template.Variables, template.Parameters, paramValue.Parameters);
                }

                // Convert Resource Properties to VMProperties
                if (resource != null && resource.Properties != null)
                {
                    this.prop = resource.Properties.ToObject <VMProperties>();

                    if (this.prop != null)
                    {
                        // Fetch resource components for Compute Hours
                        componentList.AddRange(this.GetResourceComponentForComputeHours());

                        // Fetch resource components for Storage
                        componentList.AddRange(this.GetResourceComponentForStorage());

                        // Fetch resource components for Network
                        componentList.AddRange(this.GetResourceComponentForNetwork());

                        // Fetch resource components for VM Diagnostics
                        componentList.AddRange(this.GetResourceComponentForDiagnostics());
                    }
                    else
                    {
                        throw new Exception(ExceptionLogger.GenerateLoggerTextForFailedReadProperties(this.nameOfResource));
                    }
                }
                else
                {
                    throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("Properties", this.nameOfResource));
                }
            }
            catch (Exception ex)
            {
                componentList = null;
                log.AppendLine(ex.Message);
            }

            return(componentList);
        }
        /// <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);
        }
Beispiel #3
0
        /// <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>
        /// Gets the resource components for the resources 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 ARM API call</param>
        /// <param name="log">The object that will contain the exception messages</param>
        /// <returns> Returns the list of resource components</returns>
        public static List <ResourceComponent> GetResourceComponentsForTemplate(ARMTemplate template, ARMParamValue paramvalue, string location, CSPAccountCreds cspCreds, out StringBuilder log)
        {
            List <ResourceComponent> components = new List <ResourceComponent>();

            log = new StringBuilder(string.Empty);
            string locationAsPerARMSpecs = null;

            try
            {
                // Fetch the location as per ARM Specs from the mapping
                if (!LocationConstants.LocationAsPerARMSpecsMap.TryGetValue(location, out locationAsPerARMSpecs))
                {
                    throw new Exception(ExceptionLogger.GenerateLoggerTextForInvalidField("Location", location, "ARMTemplate"));
                }

                if (template.Resources != null && template.Resources.Count > 0)
                {
                    // Loop thru each resource in the ARM Template
                    foreach (Resource res in template.Resources)
                    {
                        ARMResourceType resType        = null;
                        string          nameOfResource = string.Empty;
                        if (res != null && res.Name != null)
                        {
                            // Fetch the name of the current resource
                            nameOfResource = PropertyHelper.GetValueIfVariableOrParam(res.Name, template.Variables, template.Parameters, paramvalue.Parameters);
                        }

                        // Check if resource or resource type is not null
                        if (res != null && res.Type != null)
                        {
                            // Check if resource type is in supported by this application
                            resType = ResourceTypeHelper.ResourceTypeList.Find(x => res.Type.Equals(x.ARMResourceTypeText, StringComparison.OrdinalIgnoreCase));

                            if (resType != null)
                            {
                                List <ResourceComponent> currentResourceComponents = new List <ResourceComponent>();

                                // Check if the resource type of the current resource does not have chargeable components
                                if (!resType.HasChargableComponents)
                                {
                                    currentResourceComponents.Add(new ResourceComponent()
                                    {
                                        ResourceType = res.Type,
                                        Quantity     = 0,
                                        IsChargeable = false
                                    });
                                }
                                else
                                {
                                    IComponentFetcher resCompFetcher = null;

                                    // Create the appropriate object to fetch the components of the resource
                                    switch (resType.ARMResourceTypeText)
                                    {
                                    // Public IP Resource
                                    case ARMResourceTypeConstants.ARMPublicIPResourceType:
                                        resCompFetcher = new PublicIPComponentFetcher();
                                        break;

                                    // Virtual Machine Resource
                                    case ARMResourceTypeConstants.ARMVMResourceType:
                                        resCompFetcher = new VMComponentFetcher();
                                        break;

                                    default:

                                        // Has Chargable Components but not yet supported
                                        log.AppendLine(ExceptionLogger.GenerateLoggerTextForUnSupportedResource(res.Type));
                                        break;
                                    }

                                    StringBuilder resLog = new StringBuilder(string.Empty);

                                    // Call the method to fetch the resource components
                                    List <ResourceComponent> resComp = resCompFetcher.GetResourceComponents(res, template, paramvalue, locationAsPerARMSpecs, cspCreds, out resLog);
                                    if (resLog != null)
                                    {
                                        log.Append(resLog);
                                    }

                                    if (resComp != null && resComp.Count > 0)
                                    {
                                        currentResourceComponents.AddRange(resComp);
                                    }
                                    else
                                    {
                                        log.AppendLine(ExceptionLogger.GenerateLoggerTextForNoResourceOutput(nameOfResource));
                                    }
                                }

                                foreach (ResourceComponent component in currentResourceComponents)
                                {
                                    component.ResourceName = nameOfResource;
                                }

                                components.AddRange(currentResourceComponents);
                            }
                            else
                            {
                                log.AppendLine(ExceptionLogger.GenerateLoggerTextForUnSupportedResource(res.Type));
                            }
                        }
                        else
                        {
                            if (res != null)
                            {
                                // Type of the resourse is missing/null, generate the message
                                log.AppendLine(ExceptionLogger.GenerateLoggerTextForMissingField("Type", nameOfResource));
                            }
                            else
                            {
                                // Resourse is missing/null, generate the message
                                log.AppendLine(ExceptionLogger.GenerateLoggerTextForMissingField("Resource", "ARMTemplate"));
                            }
                        }
                    }
                }
                else
                {
                    // Resources section in ARM template is missing/null, generate a message
                    throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("RESOURCES", "ARMTemplate"));
                }
            }
            catch (Exception ex)
            {
                // Catch any exception and log the message
                components = null;
                log.AppendLine(ex.Message);
            }

            // Return the list of components obtained for the resources in the ARM template
            return(components);
        }