Skip to content

Commit

Permalink
Merge pull request #338 from SteveL-MSFT/kind
Browse files Browse the repository at this point in the history
add `kind` to dscresource
  • Loading branch information
SteveL-MSFT authored Mar 3, 2024
2 parents 35fe00d + 5a023c1 commit 7a4b647
Show file tree
Hide file tree
Showing 31 changed files with 89 additions and 52 deletions.
5 changes: 2 additions & 3 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,12 @@ $projects = @(
"file_lib",
"dsc",
"osinfo",
"powershellgroup",
"powershell-adapter",
"process",
"tools/dsctest",
"tools/test_group_resource",
"y2j",
"powershellgroup",
"wmigroup",
"wmi-adapter",
"resources/brew"
)
$pedantic_unclean_projects = @("ntreg")
Expand Down
1 change: 1 addition & 0 deletions dsc/assertion.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"type": "DSC/AssertionGroup",
"version": "0.1.0",
"description": "`test` will be invoked for all resources in the supplied configuration.",
"kind": "Group",
"get": {
"executable": "dsc",
"args": [
Expand Down
2 changes: 1 addition & 1 deletion dsc/examples/powershell.dsc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json
resources:
- name: Use class PowerShell resources
type: DSC/PowerShellGroup
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: OpenSSH service
Expand Down
1 change: 1 addition & 0 deletions dsc/group.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"type": "DSC/Group",
"version": "0.1.0",
"description": "All resources in the supplied configuration is treated as a group.",
"kind": "Group",
"get": {
"executable": "dsc",
"args": [
Expand Down
1 change: 1 addition & 0 deletions dsc/parallel.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"type": "DSC/ParallelGroup",
"version": "0.1.0",
"description": "All resources in the supplied configuration run concurrently.",
"kind": "Group",
"get": {
"executable": "dsc",
"args": [
Expand Down
3 changes: 2 additions & 1 deletion dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ pub fn resource(subcommand: &ResourceSubCommand, stdin: &Option<String>) {

let mut write_table = false;
let mut methods: Vec<String> = Vec::new();
let mut table = Table::new(&["Type", "Version", "Methods", "Requires", "Description"]);
let mut table = Table::new(&["Type", "Kind", "Version", "Methods", "Requires", "Description"]);
if format.is_none() && atty::is(Stream::Stdout) {
// write as table if format is not specified and interactive
write_table = true;
Expand Down Expand Up @@ -445,6 +445,7 @@ pub fn resource(subcommand: &ResourceSubCommand, stdin: &Option<String>) {
if write_table {
table.add_row(vec![
resource.type_name,
format!("{:?}", resource.kind),
resource.version,
methods.join(", "),
resource.requires.unwrap_or_default(),
Expand Down
5 changes: 4 additions & 1 deletion dsc/tests/dsc_resource_list.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

Describe 'Tests for listing resources' {
It 'dsc resource list' {
$resources = dsc resource list | ConvertFrom-Json
$resources = dsc resource list | ConvertFrom-Json -Depth 10
$LASTEXITCODE | Should -Be 0
$resources | Should -Not -BeNullOrEmpty
$resources.Count | Should -BeGreaterThan 0
$resources.type | Should -Contain 'DSC/AssertionGroup'
$resources.type | Should -Contain 'DSC/Group'
$resources.type | Should -Contain 'DSC/ParallelGroup'
$resources.type | Should -Contain 'Microsoft/OSInfo'
($resources | Where-Object { $_.type -eq 'DSC/Group' }).Kind | Should -BeExactly 'Group'
($resources | Where-Object { $_.type -eq 'Microsoft/OSInfo' }).Kind | Should -BeExactly 'Resource'
($resources | Where-Object { $_.type -eq 'Microsoft.DSC/PowerShell' }).Kind | Should -BeExactly 'Adapter'
}

It 'dsc resource list --tags "<tags>" and --description "<description> work' -TestCases @(
Expand Down
11 changes: 10 additions & 1 deletion dsc_lib/src/discovery/command_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::discovery::discovery_trait::ResourceDiscovery;
use crate::dscresources::dscresource::{DscResource, ImplementedAs};
use crate::dscresources::resource_manifest::{ResourceManifest, import_manifest};
use crate::dscresources::resource_manifest::{import_manifest, Kind, ResourceManifest};
use crate::dscresources::command_resource::invoke_command;
use crate::dscerror::DscError;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
Expand Down Expand Up @@ -255,8 +255,17 @@ fn load_manifest(path: &Path) -> Result<DscResource, DscError> {
}
};

let kind = if let Some(kind) = manifest.kind.clone() {
kind
} else if manifest.adapter.is_some() {
Kind::Adapter
} else {
Kind::Resource
};

let resource = DscResource {
type_name: manifest.resource_type.clone(),
kind,
implemented_as: ImplementedAs::Command,
description: manifest.description.clone(),
version: manifest.version.clone(),
Expand Down
4 changes: 4 additions & 0 deletions dsc_lib/src/dscresources/dscresource.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::dscresources::resource_manifest::Kind;
use dscerror::DscError;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -16,6 +17,8 @@ pub struct DscResource {
/// The namespaced name of the resource.
#[serde(rename="type")]
pub type_name: String,
/// The kind of resource.
pub kind: Kind,
/// The version of the resource.
pub version: String,
/// The file path to the resource.
Expand Down Expand Up @@ -51,6 +54,7 @@ impl DscResource {
pub fn new() -> Self {
Self {
type_name: String::new(),
kind: Kind::Resource,
version: String::new(),
description: None,
path: String::new(),
Expand Down
11 changes: 11 additions & 0 deletions dsc_lib/src/dscresources/resource_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ use std::collections::HashMap;

use crate::dscerror::DscError;

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
pub enum Kind {
Adapter,
Group,
Resource,
}


#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ResourceManifest {
Expand All @@ -17,6 +25,9 @@ pub struct ResourceManifest {
/// The namespaced name of the resource.
#[serde(rename = "type")]
pub resource_type: String,
/// The kind of resource.
#[serde(skip_serializing_if = "Option::is_none")]
pub kind: Option<Kind>,
/// The version of the resource.
pub version: String,
/// The description of the resource.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
PowerShellVersion = '5.0'
DscResourcesToExport = 'TestClassResource'
FunctionsToExport = @(
'Hello-World')
'Test-World')
VariablesToExport = '@()'
AliasesToExport = @()
PrivateData = @{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ class TestClassResource
$obj.Prop1 = "Property of object$_"
$resultList.Add($obj)
}

return $resultList.ToArray()
}
}

function Hello-World()
function Test-World()
{
"Hello world from PSTestModule!"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Working with classic DSC resources
type: DSC/PowerShellGroup
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Repository Info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Get info from classic DSC resources
type: DSC/PowerShellGroup
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Get PS Repository Info
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'PowerShellGroup resource tests' {
Describe 'PowerShell adapter resource tests' {

BeforeAll {
$OldPSModulePath = $env:PSModulePath
Expand Down Expand Up @@ -46,7 +46,7 @@ Describe 'PowerShellGroup resource tests' {
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Working with class-based resources
type: DSC/PowerShellGroup
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
Expand All @@ -67,13 +67,13 @@ Describe 'PowerShellGroup resource tests' {
$OldPSModulePath = $env:PSModulePath
Copy-Item -Recurse -Force -Path "$PSScriptRoot/PSTestModule" -Destination $TestDrive
Rename-Item -Path "$PSScriptRoot/PSTestModule" -NewName "_PSTestModule"

try {
$yaml = @"
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Working with class-based resources
type: DSC/PowerShellGroup
type: Microsoft.DSC/PowerShell
properties:
psmodulepath: `$env:PSModulePath;$TestDrive
resources:
Expand Down Expand Up @@ -101,7 +101,7 @@ Describe 'PowerShellGroup resource tests' {
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Working with class-based resources
type: DSC/PowerShellGroup
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
Expand All @@ -126,7 +126,7 @@ Describe 'PowerShellGroup resource tests' {
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Working with class-based resources
type: DSC/PowerShellGroup
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'PowerShellGroup resource tests' {
Describe 'PowerShell adapter resource tests' {

BeforeAll {
$OldPSModulePath = $env:PSModulePath
$env:PSModulePath += ";" + $PSScriptRoot
$env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot
}
AfterAll {
$env:PSModulePath = $OldPSModulePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Get info from classic DSC resources
type: DSC/WindowsPowerShellGroup
type: Microsoft.Windows/WindowsPowerShell
properties:
resources:
- name: Get Info
Expand Down
1 change: 1 addition & 0 deletions powershell-adapter/copy_files.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
powershell.resource.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json",
"type": "DSC/PowerShellGroup",
"type": "Microsoft.DSC/PowerShell",
"version": "0.1.0",
"description": "Resource adapter to classic DSC Powershell resources.",
"tags": [
Expand All @@ -14,7 +14,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"./powershellgroup.resource.ps1 List"
"./powershell.resource.ps1 List"
]
},
"config": "full"
Expand All @@ -26,7 +26,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./powershellgroup.resource.ps1 Get"
"$Input | ./powershell.resource.ps1 Get"
],
"input": "stdin"
},
Expand All @@ -37,7 +37,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./powershellgroup.resource.ps1 Set"
"$Input | ./powershell.resource.ps1 Set"
],
"input": "stdin",
"implementsPretest": true,
Expand All @@ -50,7 +50,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./powershellgroup.resource.ps1 Test"
"$Input | ./powershell.resource.ps1 Test"
],
"input": "stdin",
"return": "state"
Expand All @@ -62,7 +62,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./powershellgroup.resource.ps1 Export"
"$Input | ./powershell.resource.ps1 Export"
],
"input": "stdin",
"return": "state"
Expand All @@ -74,7 +74,7 @@
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./powershellgroup.resource.ps1 Validate"
"$Input | ./powershell.resource.ps1 Validate"
]
},
"exitCodes": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function RefreshCache
if (($PSVersionTable.PSVersion.Major -eq 7) -and ($PSVersionTable.PSVersion.Minor -eq 4) `
-and ($PSVersionTable.PSVersion.PreReleaseLabel.StartsWith("preview")))
{
throw "PowerShell 7.4-previews are not supported by PowerShellGroup resource; please use PS 7.4.0-rc.1 or newer."
throw "PowerShell 7.4-previews are not supported by PowerShell adapter resource; please use PS 7.4.0-rc.1 or newer."
}

$inputobj_pscustomobj = $null
Expand Down Expand Up @@ -98,10 +98,11 @@ if ($Operation -eq 'List')

$fullResourceTypeName = "$moduleName/$($r.ResourceType)"
$script:ResourceCache[$fullResourceTypeName] = $r
if ($WinPS) {$requiresString = "DSC/WindowsPowerShellGroup"} else {$requiresString = "DSC/PowerShellGroup"}
if ($WinPS) {$requiresString = "Microsoft.Windows/WindowsPowerShell"} else {$requiresString = "Microsoft.DSC/PowerShell"}

$z = [pscustomobject]@{
type = $fullResourceTypeName;
kind = 'Resource';
version = $version_string;
path = $r.Path;
directory = $r.ParentPath;
Expand Down Expand Up @@ -395,5 +396,5 @@ elseif ($Operation -eq 'Validate')
}
else
{
"ERROR: Unsupported operation requested from powershellgroup.resource.ps1"
"ERROR: Unsupported operation requested from powershell.resource.ps1"
}
Loading

0 comments on commit 7a4b647

Please sign in to comment.