Skip to content

Commit 81fafcc

Browse files
author
Kapil Borle
authored
Merge pull request #770 from PowerShell/kapilmb/fix-dsc-rule
Fix UseIdenticalMandatoryParametersForDSC rule logic
2 parents 1a15076 + 8171525 commit 81fafcc

33 files changed

+739
-141
lines changed

Engine/Extensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ public static Range ToRange(this IScriptExtent extent)
3030
extent.EndColumnNumber);
3131
}
3232

33+
/// <summary>
34+
/// Get the parameter Asts from a function definition Ast.
35+
///
36+
/// If not parameters are found, return null.
37+
/// </summary>
38+
public static IEnumerable<ParameterAst> GetParameterAsts(
39+
this FunctionDefinitionAst functionDefinitionAst)
40+
{
41+
ParamBlockAst paramBlockAst;
42+
return functionDefinitionAst.GetParameterAsts(out paramBlockAst);
43+
}
44+
3345
/// <summary>
3446
/// Get the parameter Asts from a function definition Ast.
3547
///
@@ -41,6 +53,8 @@ public static IEnumerable<ParameterAst> GetParameterAsts(
4153
this FunctionDefinitionAst functionDefinitionAst,
4254
out ParamBlockAst paramBlockAst)
4355
{
56+
// todo instead of returning null return an empty enumerator if no parameter is found
57+
// this removes the burden from the user for null checking.
4458
paramBlockAst = null;
4559
if (functionDefinitionAst.Parameters != null)
4660
{
@@ -114,6 +128,16 @@ public static NamedAttributeArgumentAst GetSupportsShouldProcessAst(this Attribu
114128
return null;
115129
}
116130

131+
132+
/// <summary>
133+
/// Return the boolean value of a named attribute argument.
134+
/// </summary>
135+
public static bool GetValue(this NamedAttributeArgumentAst attrAst)
136+
{
137+
ExpressionAst argumentAst;
138+
return attrAst.GetValue(out argumentAst);
139+
}
140+
117141
/// <summary>
118142
/// Return the boolean value of a named attribute argument.
119143
/// </summary>

RuleDocumentation/UseIdenticalMandatoryParametersForDSC.md

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,74 @@
44

55
## Description
66

7-
The `Get-TargetResource`, `Test-TargetResource` and `Set-TargetResource` functions of DSC Resource must have the same mandatory parameters.
7+
For script based DSC resources, if a property is declared with attributes `Key` of `Required` in a mof file, then is should be present as a mandatory parameter in the corresponding `Get-TargetResource`, `Set-TargetResource` and `Test-TargetResource` functions.
88

99
## How
1010

11-
Correct the mandatory parameters for the functions in DSC resource.
11+
Make sure all the properties with `Key` and `Required` attributes have equivalent mandatory parameters in the `Get/Set/Test` functions.
1212

1313
## Example
1414

15+
Consider the following `mof` file.
16+
17+
```powershell
18+
class WaitForAny : OMI_BaseResource
19+
{
20+
[key, Description("Name of Resource on remote machine")]
21+
string Name;
22+
23+
[required, Description("List of remote machines")]
24+
string NodeName[];
25+
};
26+
```
27+
1528
### Wrong
1629

1730
``` PowerShell
1831
function Get-TargetResource
1932
{
20-
[OutputType([Hashtable])]
33+
[CmdletBinding()]
2134
param
2235
(
23-
[parameter(Mandatory = $true)]
36+
[Parameter(Mandatory = $true)]
37+
[ValidateNotNullOrEmpty()]
2438
[String]
25-
$Name
39+
$Message
2640
)
27-
...
2841
}
2942
3043
function Set-TargetResource
3144
{
45+
[CmdletBinding()]
3246
param
3347
(
34-
[parameter(Mandatory = $true)]
48+
[Parameter(Mandatory = $true)]
49+
[ValidateNotNullOrEmpty()]
50+
[String]
51+
$Message,
52+
53+
[Parameter(Mandatory = $true)]
54+
[ValidateNotNullOrEmpty()]
3555
[String]
36-
$TargetName
56+
$Name
3757
)
38-
...
3958
}
4059
4160
function Test-TargetResource
4261
{
43-
[OutputType([System.Boolean])]
62+
[CmdletBinding()]
4463
param
4564
(
46-
[parameter(Mandatory = $true)]
65+
[Parameter(Mandatory = $true)]
66+
[ValidateNotNullOrEmpty()]
67+
[String]
68+
$Message,
69+
70+
[Parameter(Mandatory = $true)]
71+
[ValidateNotNullOrEmpty()]
4772
[String]
4873
$Name
4974
)
50-
...
5175
}
5276
```
5377

@@ -56,36 +80,52 @@ function Test-TargetResource
5680
``` PowerShell
5781
function Get-TargetResource
5882
{
59-
[OutputType([Hashtable])]
83+
[CmdletBinding()]
6084
param
6185
(
62-
[parameter(Mandatory = $true)]
86+
[Parameter(Mandatory = $true)]
87+
[ValidateNotNullOrEmpty()]
88+
[String]
89+
$Message,
90+
91+
[Parameter(Mandatory = $true)]
92+
[ValidateNotNullOrEmpty()]
6393
[String]
6494
$Name
6595
)
66-
...
6796
}
6897
6998
function Set-TargetResource
7099
{
100+
[CmdletBinding()]
71101
param
72102
(
73-
[parameter(Mandatory = $true)]
103+
[Parameter(Mandatory = $true)]
104+
[ValidateNotNullOrEmpty()]
105+
[String]
106+
$Message,
107+
108+
[Parameter(Mandatory = $true)]
109+
[ValidateNotNullOrEmpty()]
74110
[String]
75111
$Name
76112
)
77-
...
78113
}
79114
80115
function Test-TargetResource
81116
{
82-
[OutputType([System.Boolean])]
117+
[CmdletBinding()]
83118
param
84119
(
85-
[parameter(Mandatory = $true)]
120+
[Parameter(Mandatory = $true)]
121+
[ValidateNotNullOrEmpty()]
122+
[String]
123+
$Message,
124+
125+
[Parameter(Mandatory = $true)]
126+
[ValidateNotNullOrEmpty()]
86127
[String]
87128
$Name
88129
)
89-
...
90130
}
91131
```

Rules/Strings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@
619619
<value>The Get/Test/Set TargetResource functions of DSC resource must have the same mandatory parameters.</value>
620620
</data>
621621
<data name="UseIdenticalMandatoryParametersDSCError" xml:space="preserve">
622-
<value>The mandatory parameter '{0}' is not present in '{1}' DSC resource function(s).</value>
622+
<value>The '{0}' parameter '{1}' is not present in '{2}' DSC resource function(s).</value>
623623
</data>
624624
<data name="UseIdenticalMandatoryParametersDSCName" xml:space="preserve">
625625
<value>UseIdenticalMandatoryParametersForDSC</value>

0 commit comments

Comments
 (0)