Skip to content

Commit

Permalink
Add public commands for class-based resource (#96)
Browse files Browse the repository at this point in the history
- Added public command `Get-DscProperty` that returns a hashtable of available
  properties for a class-based resource. See comment-based help for more
  information.
- Added public command `Test-DscProperty` that returns a true or false
  whether a property exist in a class-based resource. See comment-based help
  for more information.
- Added private function `Test-DscPropertyIsAssigned` that returns a true
  or false whether a property in a class-based resource has a non-null value.
  • Loading branch information
johlju authored Dec 31, 2022
1 parent efc0408 commit c5ef7be
Show file tree
Hide file tree
Showing 8 changed files with 1,569 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added public function `Test-AccountRequirePassword` that returns true or
false whether an account need a password to be passed - [Issue #93](https://github.com/dsccommunity/DscResource.Common/issues/93)
- Related to SqlServerDsc [Issue #1794](https://github.com/dsccommunity/SqlServerDsc/issues/1794).
- Added public command `Get-DscProperty` that returns a hashtable of available
properties for a class-based resource. See comment-based help for more
information.
- Added public command `Test-DscProperty` that returns a true or false
whether a property exist in a class-based resource. See comment-based help
for more information.
- Added private function `Test-DscPropertyIsAssigned` that returns a true
or false whether a property in a class-based resource has a non-null value.

### Changed

Expand Down
115 changes: 114 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,62 @@ None.
$computerName = Get-ComputerName
```

### `Get-DscProperty`

Returns DSC resource properties that is part of a class-based DSC resource.
The properties can be filtered using name, attribute, or if it has been
assigned a non-null value.

#### Syntax

<!-- markdownlint-disable MD013 - Line length -->
```plaintext
Get-DscProperty [-InputObject] <PSObject> [[-Name] <String[]>] [[-ExcludeName] <String[]>] [[-Attribute] <String[]>] [-HasValue] [<CommonParameters>]
```
<!-- markdownlint-enable MD013 - Line length -->

#### Outputs

**System.Collections.Hashtable**

#### Notes

This command only works with nullable data types, if using a non-nullable
type make sure to make it nullable, e.g. `[Nullable[System.Int32]]`.

#### Example

```powershell
Get-DscProperty -InputObject $this
```

Returns all DSC resource properties of the DSC resource.

```powershell
$this | Get-DscProperty
```

Returns all DSC resource properties of the DSC resource.

```powershell
Get-DscProperty -InputObject $this -Name @('MyProperty1', 'MyProperty2')
```

Returns the DSC resource properties with the specified names.

```powershell
Get-DscProperty -InputObject $this -Attribute @('Mandatory', 'Optional')
```

Returns the DSC resource properties that has the specified attributes.

```powershell
Get-DscProperty -InputObject $this -Attribute @('Optional') -HasValue
```

Returns the DSC resource properties that has the specified attributes and
has a non-null value assigned.

### `Get-LocalizedData`

Gets language-specific data into scripts and functions based on the UI culture
Expand Down Expand Up @@ -887,7 +943,7 @@ Test-AccountRequirePassword [-Name] <string> [<CommonParameters>]

#### Outputs

None.
**System.Boolean**

#### Example

Expand Down Expand Up @@ -989,6 +1045,63 @@ This compares the values in the current state against the desires state.
The function `Get-TargetResource` is called using just the required parameters
to get the values in the current state.

### `Test-DscProperty`

Tests whether the class-based resource has the specified property, and
can optionally tests if the property has a certain attribute or whether
it is assigned a non-null value.

#### Syntax

<!-- markdownlint-disable MD013 - Line length -->
```plaintext
Test-DscProperty [-InputObject] <psobject> [-Name] <string> [[-Attribute] {Key | Mandatory | NotConfigurable | Optional}] [-HasValue] [<CommonParameters>]
```
<!-- markdownlint-enable MD013 - Line length -->

#### Outputs

**System.Boolean**

#### Notes

This command only works with nullable data types, if using a non-nullable
type make sure to make it nullable, e.g. `[Nullable[System.Int32]]`.

#### Example

```powershell
Test-DscProperty -InputObject $this -Name 'MyDscProperty'
```

Returns `$true` or `$false` whether the property exist or not.

```powershell
$this | Test-DscProperty -Name 'MyDscProperty'
```

Returns `$true` or `$false` whether the property exist or not.

```powershell
Test-DscProperty -InputObject $this -Name 'MyDscProperty' -HasValue
```

Returns `$true` if the property exist and is assigned a non-null value, if not
`$false` is returned.

```powershell
Test-DscProperty -InputObject $this -Name 'MyDscProperty' -Attribute 'Optional'
```

Returns `$true` if the property exist and is an optional property.

```powershell
Test-DscProperty -InputObject $this -Name 'MyDscProperty' -Attribute 'Optional' -HasValue
```

Returns `$true` if the property exist, is an optional property, and is
assigned a non-null value.

### `Test-IsNanoServer`

This function tests if the current OS is a Nano server.
Expand Down
55 changes: 55 additions & 0 deletions source/Private/Test-DscPropertyIsAssigned.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<#
.SYNOPSIS
Tests whether the class-based resource property is assigned a non-null value.
.DESCRIPTION
Tests whether the class-based resource property is assigned a non-null value.
.PARAMETER InputObject
Specifies the object that contain the property.
.PARAMETER Name
Specifies the name of the property.
.EXAMPLE
Test-DscPropertyIsAssigned -InputObject $this -Name 'MyDscProperty'
Returns $true or $false whether the property is assigned or not.
.OUTPUTS
[System.Boolean]
.NOTES
This command only works with nullable data types, if using a non-nullable
type make sure to make it nullable, e.g. [nullable[System.Int32]].
#>
function Test-DscPropertyIsAssigned
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[PSObject]
$InputObject,

[Parameter(Mandatory = $true)]
[System.String]
$Name
)

begin
{
$isAssigned = $false
}

process
{
$isAssigned = -not ($null -eq $InputObject.$Name)
}

end
{
return $isAssigned
}
}
170 changes: 170 additions & 0 deletions source/Public/Get-DscProperty.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@

<#
.SYNOPSIS
Returns DSC resource properties that is part of a class-based DSC resource.
.DESCRIPTION
Returns DSC resource properties that is part of a class-based DSC resource.
The properties can be filtered using name, attribute, or if it has been
assigned a value.
.PARAMETER InputObject
The object that contain one or more key properties.
.PARAMETER Name
Specifies one or more property names to return. If left out all properties
are returned.
.PARAMETER ExcludeName
Specifies one or more property names to exclude.
.PARAMETER Attribute
Specifies one or more property attributes to return. If left out all property
types are returned.
.PARAMETER HasValue
Specifies to return only properties that has been assigned a non-null value.
If left out all properties are returned regardless if there is a value
assigned or not.
.EXAMPLE
Get-DscProperty -InputObject $this
Returns all DSC resource properties of the DSC resource.
.EXAMPLE
$this | Get-DscProperty
Returns all DSC resource properties of the DSC resource.
.EXAMPLE
Get-DscProperty -InputObject $this -Name @('MyProperty1', 'MyProperty2')
Returns the DSC resource properties with the specified names.
.EXAMPLE
Get-DscProperty -InputObject $this -Attribute @('Mandatory', 'Optional')
Returns the DSC resource properties that has the specified attributes.
.EXAMPLE
Get-DscProperty -InputObject $this -Attribute @('Optional') -HasValue
Returns the DSC resource properties that has the specified attributes and
has a non-null value assigned.
.OUTPUTS
[System.Collections.Hashtable]
.NOTES
This command only works with nullable data types, if using a non-nullable
type make sure to make it nullable, e.g. [Nullable[System.Int32]].
#>
function Get-DscProperty
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[PSObject]
$InputObject,

[Parameter()]
[System.String[]]
$Name,

[Parameter()]
[System.String[]]
$ExcludeName,

[Parameter()]
[ValidateSet('Key', 'Mandatory', 'NotConfigurable', 'Optional')]
[Alias('Type')]
[System.String[]]
$Attribute,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$HasValue
)

process
{
$property = $InputObject.PSObject.Properties.Name |
Where-Object -FilterScript {
<#
Return all properties if $Name is not assigned, or if assigned
just those properties.
#>
(-not $Name -or $_ -in $Name) -and

<#
Return all properties if $ExcludeName is not assigned. Skip
property if it is included in $ExcludeName.
#>
(-not $ExcludeName -or ($_ -notin $ExcludeName)) -and

# Only return the property if it is a DSC property.
$InputObject.GetType().GetMember($_).CustomAttributes.Where(
{
$_.AttributeType.Name -eq 'DscPropertyAttribute'
}
)
}

if (-not [System.String]::IsNullOrEmpty($property))
{
if ($PSBoundParameters.ContainsKey('Attribute'))
{
$propertiesOfAttribute = @()

$propertiesOfAttribute += $property | Where-Object -FilterScript {
$InputObject.GetType().GetMember($_).CustomAttributes.Where(
{
<#
To simplify the code, ignoring that this will compare
MemberNAme against type 'Optional' which does not exist.
#>
$_.NamedArguments.MemberName -in $Attribute
}
).NamedArguments.TypedValue.Value -eq $true
}

# Include all optional parameter if it was requested.
if ($Attribute -contains 'Optional')
{
$propertiesOfAttribute += $property | Where-Object -FilterScript {
$InputObject.GetType().GetMember($_).CustomAttributes.Where(
{
$_.NamedArguments.MemberName -notin @('Key', 'Mandatory', 'NotConfigurable')
}
)
}
}

$property = $propertiesOfAttribute
}
}

# Return a hashtable containing each key property and its value.
$getPropertyResult = @{}

foreach ($currentProperty in $property)
{
if ($HasValue.IsPresent)
{
$isAssigned = Test-DscPropertyIsAssigned -Name $currentProperty -InputObject $InputObject

if (-not $isAssigned)
{
continue
}
}

$getPropertyResult.$currentProperty = $InputObject.$currentProperty
}

return $getPropertyResult
}
}
Loading

0 comments on commit c5ef7be

Please sign in to comment.