winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

DscResourceInfoInternal.cs (6449B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="DscResourceInfoInternal.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.Management.Configuration.Processor.PowerShell.DscResourcesInfo
      8 {
      9     using System;
     10     using System.Collections.Generic;
     11     using System.Management.Automation;
     12     using Microsoft.Management.Configuration.Processor.Helpers;
     13 
     14     /// <summary>
     15     /// Contains a DSC resource information.
     16     /// This object should be initialized with a Microsoft.PowerShell.DesiredStateConfiguration.DscResourceInfo.
     17     /// That object is the result of Get-DscResource and is not the same as System.Management.Automation.DscResourceInfo.
     18     /// The type is defined in a psd1 in the PSDesiredStateConfiguration. This file is based on that.
     19     /// If Invoke-DscResource gets support for passing the DscResourceInfo, we could keep the original object as member
     20     /// to then pass it in code.
     21     /// </summary>
     22     internal class DscResourceInfoInternal
     23     {
     24         private const string DscResourceInfoFullName = "Microsoft.PowerShell.DesiredStateConfiguration.DscResourceInfo";
     25 
     26         /// <summary>
     27         /// Initializes a new instance of the <see cref="DscResourceInfoInternal"/> class.
     28         /// </summary>
     29         /// <param name="info">Dynamic object. Expected DscResourceInfo.</param>
     30         public DscResourceInfoInternal(dynamic info)
     31         {
     32             if (info.GetType().FullName != DscResourceInfoFullName)
     33             {
     34                 throw new ArgumentException();
     35             }
     36 
     37             this.ResourceType = info.ResourceType;
     38             this.Name = info.Name;
     39             this.FriendlyName = info.FriendlyName;
     40             this.Module = info.Module;
     41             this.Path = info.Path;
     42             this.ParentPath = info.ParentPath;
     43             this.ImplementedAs = Enum.Parse<ImplementedAsTypeInternal>(info.ImplementedAs.ToString());
     44             this.CompanyName = info.CompanyName;
     45 
     46             if (this.Module is not null)
     47             {
     48                 this.ModuleName = this.Module.Name;
     49                 this.Version = this.Module.Version;
     50             }
     51 
     52             foreach (object property in info.Properties)
     53             {
     54                 this.Properties.Add(new DscResourcePropertyInfoInternal(property));
     55             }
     56         }
     57 
     58         /// <summary>
     59         /// Initializes a new instance of the <see cref="DscResourceInfoInternal"/> class.
     60         /// Used for unit tests.
     61         /// </summary>
     62         /// <param name="name">Resource name.</param>
     63         /// <param name="moduleName">Module name.</param>
     64         /// <param name="version">Version.</param>
     65         internal DscResourceInfoInternal(string name, string? moduleName, Version? version)
     66         {
     67             this.Name = name;
     68             this.ModuleName = moduleName;
     69             this.Version = version;
     70         }
     71 
     72         /// <summary>
     73         /// Gets or sets resource type name.
     74         /// </summary>
     75         public string? ResourceType { get; set; }
     76 
     77         /// <summary>
     78         /// Gets or sets Name of the resource. This name is used to access the resource.
     79         /// </summary>
     80         public string Name { get; set; }
     81 
     82         /// <summary>
     83         /// Gets the normalized resource name.
     84         /// </summary>
     85         public string NormalizedName
     86         {
     87             get { return StringHelpers.Normalize(this.Name); }
     88         }
     89 
     90         /// <summary>
     91         /// Gets or sets friendly name defined for the resource.
     92         /// </summary>
     93         public string? FriendlyName { get; set; }
     94 
     95         /// <summary>
     96         /// Gets or sets module which implements the resource. This could point to parent module, if the DSC resource is implemented.
     97         /// by one of nested modules.
     98         /// </summary>
     99         public PSModuleInfo? Module { get; set; }
    100 
    101         /// <summary>
    102         /// Gets or sets name of the module which implements the resource.
    103         /// </summary>
    104         public string? ModuleName { get; set; }
    105 
    106         /// <summary>
    107         /// Gets the normalized module name.
    108         /// </summary>
    109         public string? NormalizedModuleName
    110         {
    111             get { return this.ModuleName is not null ? StringHelpers.Normalize(this.ModuleName) : null; }
    112         }
    113 
    114         /// <summary>
    115         /// Gets or sets version of the module which implements the resource.
    116         /// </summary>
    117         public Version? Version { get; set; }
    118 
    119         /// <summary>
    120         /// Gets or sets of the file which implements the resource. For the resources which are defined using
    121         /// MOF file, this will be path to a module which resides in the same folder where schema.mof file is present.
    122         /// For composite resources, this will be the module which implements the resource.
    123         /// </summary>
    124         public string? Path { get; set; }
    125 
    126         /// <summary>
    127         /// Gets or sets parent folder, where the resource is defined
    128         /// It is the folder containing either the implementing module(=Path) or folder containing ".schema.mof".
    129         /// For native providers, Path will be null and only ParentPath will be present.
    130         /// </summary>
    131         public string? ParentPath { get; set; }
    132 
    133         /// <summary>
    134         /// Gets or sets a value which indicate how DSC resource is implemented.
    135         /// </summary>
    136         public ImplementedAsTypeInternal? ImplementedAs { get; set; }
    137 
    138         /// <summary>
    139         /// Gets or sets company which owns this resource.
    140         /// </summary>
    141         public string? CompanyName { get; set; }
    142 
    143         /// <summary>
    144         /// Gets properties of the resource.
    145         /// </summary>
    146         public List<DscResourcePropertyInfoInternal> Properties { get; private set; } = new List<DscResourcePropertyInfoInternal>();
    147 
    148         /// <summary>
    149         /// Updates properties of the resource.
    150         /// </summary>
    151         /// <param name="properties">Updated properties.</param>
    152         public void UpdateProperties(List<DscResourcePropertyInfoInternal> properties)
    153         {
    154             this.Properties = properties;
    155         }
    156     }
    157 }