Skip to content

Commit

Permalink
split out module from config unit internal
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMcPMS committed Feb 10, 2025
1 parent c8ea68a commit ffd256c
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace Microsoft.Management.Configuration.Processor.Helpers
using System.IO;
using Microsoft.Management.Configuration.Processor.Constants;
using Microsoft.Management.Configuration.Processor.Exceptions;
using Microsoft.Management.Configuration.Processor.PowerShell.Helpers;
using Microsoft.PowerShell.Commands;
using Windows.Foundation.Collections;

/// <summary>
Expand All @@ -40,21 +38,6 @@ public ConfigurationUnitInternal(
this.InitializeDirectives();
this.InitializeNames();

string? moduleName = this.GetDirective<string>(DirectiveConstants.Module);
if (string.IsNullOrEmpty(moduleName))
{
this.Module = null;
}
else
{
this.Module = PowerShellHelpers.CreateModuleSpecification(
moduleName,
this.GetDirective<string>(DirectiveConstants.Version),
this.GetDirective<string>(DirectiveConstants.MinVersion),
this.GetDirective<string>(DirectiveConstants.MaxVersion),
this.GetDirective<string>(DirectiveConstants.ModuleGuid));
}

if (!string.IsNullOrEmpty(configurationFilePath))
{
if (!File.Exists(configurationFilePath))
Expand All @@ -76,11 +59,6 @@ public ConfigurationUnitInternal(
/// </summary>
public bool UnitTypeIsResourceName { get; init; } = false;

/// <summary>
/// Gets the module specification.
/// </summary>
public ModuleSpecification? Module { get; }

/// <summary>
/// Gets the resource name *only*. For example, "Resource".
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="PowerShell\Unit\" />
<Folder Include="PowerShell\Set\" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// -----------------------------------------------------------------------------
// <copyright file="ConfigurationUnitAndModule.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------

namespace Microsoft.Management.Configuration.Processor.PowerShell.Helpers
{
using System;
using Microsoft.Management.Configuration;
using Microsoft.Management.Configuration.Processor.Constants;
using Microsoft.Management.Configuration.Processor.Helpers;
using Microsoft.PowerShell.Commands;

/// <summary>
/// Contains information about the unit and the DSC resource that applies to it.
/// </summary>
internal class ConfigurationUnitAndModule : ConfigurationUnitInternal
{
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationUnitAndModule"/> class.
/// </summary>
/// <param name="unit">Configuration unit.</param>
/// <param name="configurationFilePath">The configuration file path.</param>
public ConfigurationUnitAndModule(ConfigurationUnit unit, string? configurationFilePath)
: base(unit, configurationFilePath)
{
string? moduleName = this.GetDirective<string>(DirectiveConstants.Module);
if (string.IsNullOrEmpty(moduleName))
{
this.Module = null;
}
else
{
this.Module = PowerShellHelpers.CreateModuleSpecification(
moduleName,
this.GetDirective<string>(DirectiveConstants.Version),
this.GetDirective<string>(DirectiveConstants.MinVersion),
this.GetDirective<string>(DirectiveConstants.MaxVersion),
this.GetDirective<string>(DirectiveConstants.ModuleGuid));
}
}

/// <summary>
/// Gets the module specification.
/// </summary>
public ModuleSpecification? Module { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class ConfigurationUnitAndResource
/// <param name="configurationUnitInternal">Configuration unit internal.</param>
/// <param name="dscResourceInfoInternal">DscResourceInfoInternal.</param>
public ConfigurationUnitAndResource(
ConfigurationUnitInternal configurationUnitInternal,
ConfigurationUnitAndModule configurationUnitInternal,
DscResourceInfoInternal dscResourceInfoInternal)
{
if (!configurationUnitInternal.ResourceName.Equals(dscResourceInfoInternal.Name, StringComparison.OrdinalIgnoreCase))
Expand All @@ -41,7 +41,7 @@ public ConfigurationUnitAndResource(
/// <summary>
/// Gets or initializes the internal unit.
/// </summary>
public ConfigurationUnitInternal UnitInternal { get; private init; }
public ConfigurationUnitAndModule UnitInternal { get; private init; }

/// <summary>
/// Gets the configuration unit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public IReadOnlyList<DscResourceInfoInternal> GetDscResourcesInModule(ModuleSpec
}

/// <inheritdoc/>
public DscResourceInfoInternal? GetDscResource(ConfigurationUnitInternal unitInternal)
public DscResourceInfoInternal? GetDscResource(ConfigurationUnitAndModule unitInternal)
{
using PowerShell pwsh = PowerShell.Create(this.Runspace);
var result = this.DscModule.GetDscResource(pwsh, unitInternal.ResourceName, unitInternal.Module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Management.Configuration.Processor.PowerShell.ProcessorEnvir
using System.Management.Automation;
using Microsoft.Management.Configuration.Processor.Helpers;
using Microsoft.Management.Configuration.Processor.PowerShell.DscResourcesInfo;
using Microsoft.Management.Configuration.Processor.PowerShell.Helpers;
using Microsoft.PowerShell.Commands;
using Windows.Foundation.Collections;
using Windows.Security.Cryptography.Certificates;
Expand Down Expand Up @@ -47,7 +48,7 @@ internal interface IProcessorEnvironment
/// </summary>
/// <param name="unitInternal">Configuration unit internal.</param>
/// <returns>DSC Resource.</returns>
DscResourceInfoInternal? GetDscResource(ConfigurationUnitInternal unitInternal);
DscResourceInfoInternal? GetDscResource(ConfigurationUnitAndModule unitInternal);

/// <summary>
/// Calls Invoke-DscResource -Method Get from this module.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace Microsoft.Management.Configuration.Processor.PowerShell.Set
using System.IO;
using System.Management.Automation;
using Microsoft.Management.Configuration.Processor.Exceptions;
using Microsoft.Management.Configuration.Processor.Helpers;
using Microsoft.Management.Configuration.Processor.PowerShell.DscResourcesInfo;
using Microsoft.Management.Configuration.Processor.PowerShell.Helpers;
using Microsoft.Management.Configuration.Processor.PowerShell.ProcessorEnvironments;
using Microsoft.Management.Configuration.Processor.PowerShell.Unit;
using Microsoft.Management.Configuration.Processor.Set;
using Microsoft.Management.Configuration.Processor.Unit;
using Windows.Security.Cryptography.Certificates;
Expand Down Expand Up @@ -42,15 +42,15 @@ public PowerShellConfigurationSetProcessor(IProcessorEnvironment processorEnviro
internal IProcessorEnvironment ProcessorEnvironment { get; }

/// <inheritdoc />
protected override ConfigurationUnitProcessor CreateUnitProcessorInternal(ConfigurationUnit unit)
protected override IConfigurationUnitProcessor CreateUnitProcessorInternal(ConfigurationUnit unit)
{
var configurationUnitInternal = new ConfigurationUnitInternal(unit, this.ConfigurationSet?.Path) { UnitTypeIsResourceName = IsUnitTypeResourceName(this.ConfigurationSet?.SchemaVersion) };
var configurationUnitInternal = new ConfigurationUnitAndModule(unit, this.ConfigurationSet?.Path) { UnitTypeIsResourceName = IsUnitTypeResourceName(this.ConfigurationSet?.SchemaVersion) };
this.OnDiagnostics(DiagnosticLevel.Verbose, $"Creating unit processor for: {configurationUnitInternal.QualifiedName}...");

var dscResourceInfo = this.PrepareUnitForProcessing(configurationUnitInternal);

this.OnDiagnostics(DiagnosticLevel.Verbose, $"Using unit from location: {dscResourceInfo.Path}");
return new ConfigurationUnitProcessor(
return new PowerShellConfigurationUnitProcessor(
this.ProcessorEnvironment,
new ConfigurationUnitAndResource(configurationUnitInternal, dscResourceInfo),
this.IsLimitMode)
Expand All @@ -60,7 +60,7 @@ protected override ConfigurationUnitProcessor CreateUnitProcessorInternal(Config
/// <inheritdoc />
protected override IConfigurationUnitProcessorDetails? GetUnitProcessorDetailsInternal(ConfigurationUnit unit, ConfigurationUnitDetailFlags detailFlags)
{
var unitInternal = new ConfigurationUnitInternal(unit, this.ConfigurationSet?.Path);
var unitInternal = new ConfigurationUnitAndModule(unit, this.ConfigurationSet?.Path);
this.OnDiagnostics(DiagnosticLevel.Verbose, $"Getting unit details [{detailFlags}] for: {unitInternal.QualifiedName}");

// (Local | Download | Load) will all work off of local files, so if any one is an option just use the local module info if found.
Expand Down Expand Up @@ -153,7 +153,7 @@ private static bool IsUnitTypeResourceName(string? schemaVersion)
/// </summary>
/// <param name="unitInternal">The internal configuration unit.</param>
/// <returns>A tuple containing the module info and preferred resource name, or null if not found.</returns>
private (PSObject Module, string ResourceName)? FindUnitModule(ConfigurationUnitInternal unitInternal)
private (PSObject Module, string ResourceName)? FindUnitModule(ConfigurationUnitAndModule unitInternal)
{
PSObject? foundModule = null;
string resourceName = string.Empty;
Expand Down Expand Up @@ -190,7 +190,7 @@ private static bool IsUnitTypeResourceName(string? schemaVersion)
return null;
}

private DscResourceInfoInternal PrepareUnitForProcessing(ConfigurationUnitInternal unitInternal)
private DscResourceInfoInternal PrepareUnitForProcessing(ConfigurationUnitAndModule unitInternal)
{
// Invoke-DscResource makes a call to Get-DscResource which looks at the entire PSModulePath
// to see if a resource exists. DscResourcesMap is an attempt to try to optimize Get-DscResource
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// -----------------------------------------------------------------------------
// <copyright file="PowerShellConfigurationUnitProcessor.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------

namespace Microsoft.Management.Configuration.Processor.PowerShell.Unit
{
using Microsoft.Management.Configuration;
using Microsoft.Management.Configuration.Processor.PowerShell.Helpers;
using Microsoft.Management.Configuration.Processor.PowerShell.ProcessorEnvironments;
using Microsoft.Management.Configuration.Processor.Unit;
using Windows.Foundation.Collections;

/// <summary>
/// Provides access to a specific configuration unit within the runtime.
/// </summary>
internal sealed partial class PowerShellConfigurationUnitProcessor : ConfigurationUnitProcessorBase, IConfigurationUnitProcessor
{
private readonly IProcessorEnvironment processorEnvironment;
private readonly ConfigurationUnitAndResource unitResource;

/// <summary>
/// Initializes a new instance of the <see cref="PowerShellConfigurationUnitProcessor"/> class.
/// </summary>
/// <param name="processorEnvironment">Processor environment.</param>
/// <param name="unitResource">UnitResource.</param>
/// <param name="isLimitMode">Whether it is under limit mode.</param>
internal PowerShellConfigurationUnitProcessor(IProcessorEnvironment processorEnvironment, ConfigurationUnitAndResource unitResource, bool isLimitMode = false)
: base(unitResource.UnitInternal, isLimitMode)
{
this.processorEnvironment = processorEnvironment;
this.unitResource = unitResource;
}

/// <inheritdoc />
protected override ValueSet GetSettingsInternal()
{
return this.processorEnvironment.InvokeGetResource(
this.unitResource.GetSettings(),
this.unitResource.ResourceName,
this.unitResource.Module);
}

/// <inheritdoc />
protected override bool TestSettingsInternal()
{
return this.processorEnvironment.InvokeTestResource(
this.unitResource.GetSettings(),
this.unitResource.ResourceName,
this.unitResource.Module);
}

/// <inheritdoc />
protected override bool ApplySettingsInternal()
{
return this.processorEnvironment.InvokeSetResource(
this.unitResource.GetSettings(),
this.unitResource.ResourceName,
this.unitResource.Module);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public IConfigurationUnitProcessor CreateUnitProcessor(ConfigurationUnit incomin
// CreateUnitProcessor can only be called once on each configuration unit in limit mode.
var unit = this.GetConfigurationUnit(incomingUnit, true);

ConfigurationUnitProcessor result = this.CreateUnitProcessorInternal(unit);
IConfigurationUnitProcessor result = this.CreateUnitProcessorInternal(unit);

this.OnDiagnostics(DiagnosticLevel.Verbose, "... done creating unit processor.");

Expand Down Expand Up @@ -120,7 +120,7 @@ public IConfigurationUnitProcessor CreateUnitProcessor(ConfigurationUnit incomin
/// </summary>
/// <param name="unit">Configuration unit.</param>
/// <returns>A configuration unit processor.</returns>
protected abstract ConfigurationUnitProcessor CreateUnitProcessorInternal(ConfigurationUnit unit);
protected abstract IConfigurationUnitProcessor CreateUnitProcessorInternal(ConfigurationUnit unit);

/// <summary>
/// Gets the configuration unit processor details for the given unit.
Expand Down
Loading

0 comments on commit ffd256c

Please sign in to comment.