Skip to content

Commit 8171525

Browse files
author
Kapil Borle
committed
Update UseIdenticalParametersForDSC rule logic
This removes the constraint that a parameter needs to be declared `mandatory` in the `Get/Test/Set` functions if any of them declares the parameter as `mandatory`. These DSC functions are supposed to take in parameters that declared in the corresponding mof file and only the parameters that are declared as `key` or `required` should be mandatory in all the three functions.
1 parent d851011 commit 8171525

File tree

3 files changed

+10
-47
lines changed

3 files changed

+10
-47
lines changed

RuleDocumentation/UseIdenticalMandatoryParametersForDSC.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44

55
## Description
66

7-
For script based DSC resources the `Get-TargetResource`, `Test-TargetResource` and `Set-TargetResource` functions:
8-
9-
1. If a parameter is declared as `mandatory` in any of the `Get/Set/Test` functions, then it should be a mandatory parameter in all the three functions.
10-
1. 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 `Get/Set/Test` functions of the corresponding resource file.
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.
118

129
## How
1310

14-
1. Make sure `Get/Set/Test` declare identical mandatory parameters.
15-
1. Make sure all the properties with `Key` and `Required` attributes have equivalent mandatory parameters in the `Get/Set/Test` functions.
11+
Make sure all the properties with `Key` and `Required` attributes have equivalent mandatory parameters in the `Get/Set/Test` functions.
1612

1713
## Example
1814

Rules/UseIdenticalMandatoryParametersDSC.cs

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,6 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
6767
.Cast<FunctionDefinitionAst>()
6868
.ToArray();
6969

70-
// Loop through Set/Test/Get TargetResource DSC cmdlets
71-
foreach (var functionDefinitionAst in resourceFunctions)
72-
{
73-
var manParams = GetMandatoryParameters(functionDefinitionAst)
74-
.Select(p => p.Name.VariablePath.UserPath);
75-
76-
foreach (var key in propAttrDict.Keys.Except(manParams))
77-
{
78-
yield return new DiagnosticRecord(
79-
string.Format(
80-
CultureInfo.InvariantCulture,
81-
Strings.UseIdenticalMandatoryParametersDSCError,
82-
propAttrDict[key],
83-
key,
84-
functionDefinitionAst.Name),
85-
Helper.Instance.GetScriptExtentForFunctionName(functionDefinitionAst),
86-
GetName(),
87-
DiagnosticSeverity.Error,
88-
fileName);
89-
}
90-
}
91-
9270
var funcManParamMap = resourceFunctions
9371
.ToDictionary(
9472
f => f.Name,
@@ -97,31 +75,20 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
9775
GetMandatoryParameters(f)
9876
.Select(p => p.Name.VariablePath.UserPath)
9977
.ToArray()));
100-
var paramFuncMap = new Dictionary<string, List<string>>();
101-
foreach (var kvp in funcManParamMap)
102-
{
103-
foreach (var param in kvp.Value.Item2)
104-
{
105-
if (!paramFuncMap.ContainsKey(param))
106-
{
107-
paramFuncMap.Add(param, new List<string>());
108-
}
10978

110-
paramFuncMap[param].Add(kvp.Key);
111-
}
112-
}
113-
114-
foreach(var param in paramFuncMap.Keys.Except(propAttrDict.Keys))
79+
// Loop through Set/Test/Get TargetResource DSC cmdlets
80+
foreach (var kvp in funcManParamMap)
11581
{
116-
foreach(var func in funcManParamMap.Keys.Except(paramFuncMap[param]))
82+
var functionDefinitionAst = kvp.Value.Item1;
83+
var manParams = kvp.Value.Item2;
84+
foreach (var key in propAttrDict.Keys.Except(manParams))
11785
{
118-
var functionDefinitionAst = funcManParamMap[func].Item1;
11986
yield return new DiagnosticRecord(
12087
string.Format(
12188
CultureInfo.InvariantCulture,
12289
Strings.UseIdenticalMandatoryParametersDSCError,
123-
"mandatory",
124-
param,
90+
propAttrDict[key],
91+
key,
12592
functionDefinitionAst.Name),
12693
Helper.Instance.GetScriptExtentForFunctionName(functionDefinitionAst),
12794
GetName(),

Tests/Rules/UseIdenticalMandatoryParametersForDSC.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Describe "UseIdenticalMandatoryParametersForDSC" {
1616
}
1717

1818
It "Should find a violations" {
19-
$violations.Count | Should Be 11
19+
$violations.Count | Should Be 5
2020
}
2121

2222
It "Should mark only the function name" {

0 commit comments

Comments
 (0)