/// <summary> /// Gets the Meter SubCategory String for the specified Azure VM Image having Software Cost /// </summary> /// <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="publisher">Publisher of the Azure VM Image</param> /// <param name="offer">Offer of the Azure VM Image</param> /// <param name="sku">SKU of the Azure VM Image</param> /// <param name="vmSize">VM Size String</param> /// <param name="location">Azure Location</param> /// <returns> Returns the Meter SubCategory for the Software Cost associated with the Azure VM Image</returns> public static string GetMeterSubCategoryForVMImageWithSoftwareCost(CSPAccountCreds cspCreds, string publisher, string offer, string sku, string vmSize, string location) { string meterSubCategory = null; try { // Get Filtered List by Publisher List <VMImageHavingSoftwareCost> vmImageFilteredList = vmImageHavingSoftwareCostList.FindAll(x => x.VMImagePublisherName.Equals(publisher, StringComparison.OrdinalIgnoreCase)); // Get Filtered List by Offer Names, SKU Names VMImageHavingSoftwareCost vmImageHavingSoftwareCost = GetVMImageFilteredListbyOfferSKUNames(offer, sku, vmImageHavingSoftwareCostList); if (vmImageHavingSoftwareCost != null) { int cores = -1, coresListIndex = -1; cores = VMOnlineHelper.GetCoresForVmSize(cspCreds, vmSize, location); if (cores != -1) { coresListIndex = GetLowestIndexForItemInSortedArray(vmImageHavingSoftwareCost.MeterSubCategoryCoresList, cores); } if (coresListIndex == -1) { throw new Exception(ExceptionLogger.GenerateLoggerTextForInternalError("Unsupported number of cores while calculating software cost for the VM Image")); } else { string[] templateParams = null; templateParams = SetValuesForMeterSubCategoryTemplateParams(vmImageHavingSoftwareCost, coresListIndex, sku, cores); if (templateParams != null) { meterSubCategory = coresListIndex == 0 ? string.Format(vmImageHavingSoftwareCost.VMMeterSubCategoryStrTemplateForFirst, templateParams) : string.Format(vmImageHavingSoftwareCost.VMMeterSubCategoryStrTemplate, templateParams); } else { meterSubCategory = coresListIndex == 0 ? vmImageHavingSoftwareCost.VMMeterSubCategoryStrTemplateForFirst : vmImageHavingSoftwareCost.VMMeterSubCategoryStrTemplate; } } } } catch (Exception) { throw; } return(meterSubCategory); }
/// <summary> /// Gets the resource components for the compute hours of the Virtual machine resource. This includes components for software cost if present. /// </summary> /// <returns> Returns the list of resource components </returns> private List <ResourceComponent> GetResourceComponentForComputeHours() { List <ResourceComponent> componentList = new List <ResourceComponent>(); // Create the compute hours component, Meter SubCategory value will be set later ResourceComponent computeHoursComponent = new ResourceComponent { ResourceType = this.resource.Type, MeterCategory = VMResourceConstants.VMMeterCategory, MeterSubCategory = null, MeterName = VMResourceConstants.VMMeterName, Quantity = Constants.HoursinaMonth, IsChargeable = true }; // Create the software cost component, Meter SubCategory value will be set later ResourceComponent softwareCostComponent = new ResourceComponent { ResourceType = this.resource.Type, MeterCategory = VMResourceConstants.VMMeterCategory, MeterSubCategory = null, MeterName = VMResourceConstants.VMMeterName, Quantity = Constants.HoursinaMonth, IsChargeable = true }; string vmSize = null, osType = null, createOption = null; if (this.prop.HardwareProfile != null && this.prop.HardwareProfile.VmSize != null) { // Fetch the VM Size vmSize = PropertyHelper.GetValueIfVariableOrParam(this.prop.HardwareProfile.VmSize, this.template.Variables, this.template.Parameters, this.paramValue.Parameters); } else { throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("properties.hardwareProfile.vmSize", this.nameOfResource)); } if (vmSize.Equals(string.Empty)) { throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("properties.hardwareProfile.vmSize", this.nameOfResource)); } if (this.prop.StorageProfile != null && this.prop.StorageProfile.OsDisk != null) { // Fetch the OS Disk related info osType = PropertyHelper.GetValueIfVariableOrParam(this.prop.StorageProfile.OsDisk.OsType, this.template.Variables, this.template.Parameters, this.paramValue.Parameters); createOption = PropertyHelper.GetValueIfVariableOrParam(this.prop.StorageProfile.OsDisk.CreateOption, this.template.Variables, this.template.Parameters, this.paramValue.Parameters); } else { throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("properties.storageProfile.osDisk", this.nameOfResource)); } if (createOption == null) { throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("properties.storageProfile.osDisk.createOption", this.nameOfResource)); } if (osType == null) { if (createOption != null && createOption.Equals("FromImage", StringComparison.OrdinalIgnoreCase)) { string vmImagePublisher = null, vmImageOffer = null, vmImageSKU = null; // Fetch the VM Image info - Publisher, Offer and SKU if (this.prop.StorageProfile != null && this.prop.StorageProfile.ImageReference != null) { vmImagePublisher = PropertyHelper.GetValueIfVariableOrParam(this.prop.StorageProfile.ImageReference.Publisher, this.template.Variables, this.template.Parameters, this.paramValue.Parameters); vmImageOffer = PropertyHelper.GetValueIfVariableOrParam(this.prop.StorageProfile.ImageReference.Offer, this.template.Variables, this.template.Parameters, this.paramValue.Parameters); vmImageSKU = PropertyHelper.GetValueIfVariableOrParam(this.prop.StorageProfile.ImageReference.Sku, this.template.Variables, this.template.Parameters, this.paramValue.Parameters); } else { throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("properties.storageProfile.imageReference", this.nameOfResource)); } if (vmImagePublisher != null && vmImageOffer != null && vmImageSKU != null) { // Get the OS Type of the VM Image from the Online Helper method osType = VMOnlineHelper.GetVMImageOSType(this.cspCreds, vmImagePublisher, vmImageOffer, vmImageSKU, this.location); string meterSubCategoryForVMImageWithSoftwareCost = null; // Get the Meter SubCategory for software cost of the VM Image is applicable meterSubCategoryForVMImageWithSoftwareCost = VMImageHelper.GetMeterSubCategoryForVMImageWithSoftwareCost(this.cspCreds, vmImagePublisher, vmImageOffer, vmImageSKU, vmSize, this.location); if (meterSubCategoryForVMImageWithSoftwareCost != null) { // Set the Meter SubCategory for Software Cost component, Add to the List of resource components softwareCostComponent.MeterSubCategory = meterSubCategoryForVMImageWithSoftwareCost; componentList.Add(softwareCostComponent); } } else { throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("properties.storageProfile.publisher/offer/sku", this.nameOfResource)); } } else { throw new Exception(ExceptionLogger.GenerateLoggerTextForInvalidField("storageProfile.osDisk.createOption", createOption, this.nameOfResource)); } } string meterSubCategory = null; string modifiedVMSizeString = VMHelper.ModifyVMSizeStringAsPerPricingSpecs(vmSize); if (osType != null) { // Fetch the Meter SubCategory as per the OS Type switch (osType.ToUpper()) { case "WINDOWS": meterSubCategory = string.Format(VMResourceConstants.VMMeterSubCategoryWindowsString, modifiedVMSizeString, osType); break; case "LINUX": meterSubCategory = string.Format(VMResourceConstants.VMMeterSubCategoryLinuxString, modifiedVMSizeString); break; default: throw new Exception(ExceptionLogger.GenerateLoggerTextForInvalidField("OSType", osType, this.nameOfResource)); } } else { throw new Exception(ExceptionLogger.GenerateLoggerTextForInvalidField("OSType", string.Empty, this.nameOfResource)); } // Set the Meter SubCategory for Compute Hours Cost component, Add to the List of resource components computeHoursComponent.MeterSubCategory = meterSubCategory; componentList.Add(computeHoursComponent); return(componentList); }