/// <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> /// Gets the resource components for the Storage of the Virtual machine resource. /// </summary> /// <returns> Returns the list of resource components </returns> private List <ResourceComponent> GetResourceComponentForStorage() { List <ResourceComponent> storageComponentList = new List <ResourceComponent>(); // Get list of all storage resources in template List <Resource> storageResourceList = this.template.Resources.FindAll(x => ARMResourceTypeConstants.ARMStorageResourceType.Equals(x.Type, StringComparison.OrdinalIgnoreCase)); // OS Disk Components string diskURI = null; if (this.prop.StorageProfile != null && this.prop.StorageProfile.OsDisk != null && this.prop.StorageProfile.OsDisk.Vhd != null) { // Fetch the OS Disk URI diskURI = this.prop.StorageProfile.OsDisk.Vhd.Uri; } if (diskURI != null && !diskURI.Equals(string.Empty)) { // Fetch the list of resource components for OS Disk and Add to list storageComponentList.AddRange(this.GetResourceComponentForDiskStorage(diskURI, null, storageResourceList, true)); } else { throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("properties.storageProfile.osDisk.vhd.uri", this.nameOfResource)); } // Data Disk Components if (this.prop.StorageProfile != null && this.prop.StorageProfile.DataDisks != null && this.prop.StorageProfile.DataDisks.Count != 0) { for (int i = 0; i < this.prop.StorageProfile.DataDisks.Count; i++) { string dataDiskSize = null; if (this.prop.StorageProfile.DataDisks[i] != null && this.prop.StorageProfile.DataDisks[i].Vhd != null) { // Fetch the Data Disk URI diskURI = this.prop.StorageProfile.DataDisks[i].Vhd.Uri; // Fetch the Data Disk Size dataDiskSize = PropertyHelper.GetValueIfVariableOrParam(this.prop.StorageProfile.DataDisks[i].DiskSizeGB, this.template.Variables, this.template.Parameters, this.paramValue.Parameters); } if (diskURI != null && dataDiskSize != null) { // Fetch the list of resource components for Data Disk and Add to list storageComponentList.AddRange(this.GetResourceComponentForDiskStorage(diskURI, dataDiskSize, storageResourceList, false)); } } } return(storageComponentList); }
/// <summary> /// Gets the resource components for the Public IP resource by checking if the Public IP Allocation method is static /// </summary> /// <returns> Returns the list of resource components</returns> private List <ResourceComponent> GetResourceComponentForPublicIP() { List <ResourceComponent> componentList = new List <ResourceComponent>(); ResourceComponent publicIPComponent = new ResourceComponent() { ResourceType = this.resource.Type, Quantity = 0, IsChargeable = false, ResourceName = this.nameOfResource }; string publicIPAllocationMethod = null; // Get the Allocation method publicIPAllocationMethod = PropertyHelper.GetValueIfVariableOrParam(this.prop.PublicIPAllocationMethod, this.template.Variables, this.template.Parameters, this.paramValue.Parameters); if (publicIPAllocationMethod != null) { // Check if Static Allocation, Add resource component with associated meter if (publicIPAllocationMethod.Equals(PublicIPResourceConstants.PublicPublicIPAllocationMethodStatic, StringComparison.OrdinalIgnoreCase)) { publicIPComponent.MeterCategory = NetworkingResourceConstants.NetworkingMeterCategory; publicIPComponent.MeterSubCategory = PublicIPResourceConstants.NetworkingMeterSubCategoryForPublicIP; publicIPComponent.MeterName = PublicIPResourceConstants.NetworkingMeterNameForPublicIP; publicIPComponent.IsChargeable = true; publicIPComponent.Quantity = Constants.HoursinaMonth; } } else { throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("publicIPAllocationMethod", this.nameOfResource)); } componentList.Add(publicIPComponent); return(componentList); }
/// <summary> /// Gets the resource components for the Network bandwidth of the Virtual machine resource. /// </summary> /// <returns> Returns the list of resource components </returns> private List <ResourceComponent> GetResourceComponentForNetwork() { List <ResourceComponent> componentList = new List <ResourceComponent>(); List <NetworkInterface> vmNetworkInterfaces = null; if (this.prop.NetworkProfile != null && this.prop.NetworkProfile.NetworkInterfaces != null) { vmNetworkInterfaces = this.prop.NetworkProfile.NetworkInterfaces; } else { throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("properties.networkProfile.networkInterfaces", this.nameOfResource)); } if (vmNetworkInterfaces != null && (vmNetworkInterfaces.Count >= 1)) { // Create the resource component for network bandwidth of the VM and add to the list ResourceComponent networkComponent = new ResourceComponent { ResourceType = this.resource.Type, MeterCategory = NetworkingResourceConstants.NetworkingMeterCategory, MeterSubCategory = NetworkingResourceConstants.NetworkingMeterSubCategoryForVMNetwork, MeterName = NetworkingResourceConstants.NetworkingMeterNameForVMNetwork, Quantity = VMResourceConstants.NetworkingVMNetwork, IsChargeable = true }; componentList.Add(networkComponent); } else { throw new Exception(ExceptionLogger.GenerateLoggerTextForMissingField("properties.networkProfile.networkInterfaces", this.nameOfResource)); } return(componentList); }
/// <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); }
/// <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); }