Skip to content

Commit 280bbf4

Browse files
committed
Model changes
1 parent 80e54c4 commit 280bbf4

File tree

8 files changed

+168
-18
lines changed

8 files changed

+168
-18
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
16+
{
17+
using Newtonsoft.Json;
18+
19+
/// <summary>
20+
/// The policy assignment non compliance message used to describe why a resource is non-compliant with a policy.
21+
/// </summary>
22+
public class NonComplianceMessage
23+
{
24+
/// <summary>
25+
/// The message that describes why a resource is non-compliant with the policy. This is shown in 'deny' error messages and
26+
/// on resource's non-compliant compliance results.
27+
/// </summary>
28+
[JsonProperty(Required = Required.Default)]
29+
public string Message { get; set; }
30+
31+
/// <summary>
32+
/// The policy definition reference ID within a policy set definition that the message is intended for. This is only applicable if
33+
/// the policy assignment assigns a policy set definition. If this is not provided the message applies to all policies assigned by this
34+
/// policy assignment.
35+
/// </summary>
36+
[JsonProperty(Required = Required.Default)]
37+
public string PolicyDefinitionReferenceId { get; set; }
38+
}
39+
}

src/Resources/ResourceManager/Entities/Policy/PolicyAssignmentProperties.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,11 @@ public class PolicyAssignmentProperties
6969
/// </summary>
7070
[JsonProperty(Required = Required.Default)]
7171
public JObject Parameters { get; set; }
72+
73+
/// <summary>
74+
/// The non-compliance messages used to describe why a resource is non-compliant with the policy.
75+
/// </summary>
76+
[JsonProperty(Required = Required.Default)]
77+
public NonComplianceMessage[] NonComplianceMessages { get; set; }
7278
}
7379
}

src/Resources/ResourceManager/Implementation/Policy/NewAzurePolicyAssignment.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2727

2828
using Newtonsoft.Json.Linq;
2929
using Policy;
30+
using System.Linq;
3031

3132
/// <summary>
3233
/// Creates a policy assignment.
@@ -133,6 +134,13 @@ public class NewAzurePolicyAssignmentCmdlet : PolicyCmdletBase, IDynamicParamete
133134
[LocationCompleter("Microsoft.ManagedIdentity/userAssignedIdentities")]
134135
public string Location { get; set; }
135136

137+
/// <summary>
138+
/// Gets or sets the messages that describe why a resource is non-compliant with the policy.
139+
/// </summary>
140+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.PolicyAssignmentNonComplianceMessageHelp)]
141+
[ValidateNotNull]
142+
public PsNonComplianceMessage[] NonComplianceMessage { get; set; }
143+
136144
/// <summary>
137145
/// Executes the cmdlet.
138146
/// </summary>
@@ -206,7 +214,8 @@ private JToken GetResource()
206214
NotScopes = this.NotScope ?? null,
207215
Metadata = this.Metadata == null ? null : this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)),
208216
EnforcementMode = EnforcementMode ?? PolicyAssignmentEnforcementMode.Default,
209-
Parameters = this.GetParameters(this.PolicyParameter, this.PolicyParameterObject)
217+
Parameters = this.GetParameters(this.PolicyParameter, this.PolicyParameterObject),
218+
NonComplianceMessages = this.NonComplianceMessage?.Where(message => message != null).SelectArray(message => message.ToModel())
210219
}
211220
};
212221

src/Resources/ResourceManager/Implementation/Policy/PolicyHelpStrings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static class PolicyHelpStrings
5454
public const string SetPolicyAssignmentInputObjectHelp = "The policy assignment object to update that was output from another cmdlet.";
5555
public const string PolicyAssignmentAssignIdentityHelp = "Generate and assign an Azure Active Directory Identity for this policy assignment. The identity will be used when executing deployments for 'deployIfNotExists' and 'modify' policies. Location is required when assigning an identity.";
5656
public const string PolicyAssignmentLocationHelp = "The location of the policy assignment's resource identity. This is required when the -AssignIdentity switch is used.";
57+
public const string PolicyAssignmentNonComplianceMessageHelp = "The non-compliance messages that describe why a resource is non-compliant with the policy.";
5758

5859
/// <summary>
5960
/// Policy definition cmdlet parameter help strings
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Policy
2+
{
3+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
4+
using Newtonsoft.Json.Linq;
5+
6+
/// <summary>
7+
/// The policy assignment non compliance message used to describe why a resource is non-compliant with a policy.
8+
/// </summary>
9+
public class PsNonComplianceMessage
10+
{
11+
/// <summary>
12+
/// Creates an instance of <see cref="PsNonComplianceMessage"/> from the equivalent model representation.
13+
/// </summary>
14+
/// <param name="messageModel">The non-compliance message model.</param>
15+
public PsNonComplianceMessage(NonComplianceMessage messageModel)
16+
{
17+
this.Message = messageModel.Message;
18+
this.PolicyDefinitionReferenceId = messageModel.PolicyDefinitionReferenceId;
19+
}
20+
21+
/// <summary>
22+
/// The message that describes why a resource is non-compliant with the policy. This is shown in 'deny' error messages and
23+
/// on resource's non-compliant compliance results.
24+
/// </summary>
25+
public string Message { get; set; }
26+
27+
/// <summary>
28+
/// The policy definition reference ID within a policy set definition that the message is intended for. This is only applicable if
29+
/// the policy assignment assigns a policy set definition. If this is not provided the message applies to all policies assigned by this
30+
/// policy assignment.
31+
/// </summary>
32+
public string PolicyDefinitionReferenceId { get; set; }
33+
34+
/// <summary>
35+
/// Convert to JSON
36+
/// </summary>
37+
/// <returns>JSON representatnion of the non-compliance message.</returns>
38+
public JToken ToJToken()
39+
{
40+
var returnValue = new JObject();
41+
if (this.Message != null)
42+
{
43+
returnValue["message"] = this.Message;
44+
}
45+
46+
if (this.PolicyDefinitionReferenceId != null)
47+
{
48+
returnValue["policyDefinitionReferenceId"] = this.PolicyDefinitionReferenceId;
49+
}
50+
51+
return returnValue;
52+
}
53+
54+
/// <summary>
55+
/// Converts the powershell model to the entity model.
56+
/// </summary>
57+
public NonComplianceMessage ToModel()
58+
{
59+
return new NonComplianceMessage
60+
{
61+
Message = this.Message,
62+
PolicyDefinitionReferenceId = this.PolicyDefinitionReferenceId
63+
};
64+
}
65+
}
66+
}

src/Resources/ResourceManager/Implementation/Policy/PsPolicyAssignment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using Newtonsoft.Json.Linq;
99

1010
/// <summary>
11-
/// Class that wraps a policy definition PSObject
11+
/// Class that wraps a policy assignment PSObject
1212
/// </summary>
1313
public class PsPolicyAssignment
1414
{

src/Resources/ResourceManager/Implementation/Policy/PsPolicyAssignmentProperties.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.Policy
22
{
3+
using System.Linq;
34
using System.Management.Automation;
4-
55
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
66
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
77

@@ -23,6 +23,10 @@ public PsPolicyAssignmentProperties(JToken input)
2323
EnforcementMode = (PsPolicyAssignmentEnforcementMode)properties.EnforcementMode;
2424
PolicyDefinitionId = properties.PolicyDefinitionId;
2525
Parameters = properties.Parameters.ToPsObject();
26+
NonComplianceMessages = properties
27+
.NonComplianceMessages?
28+
.Where(message => message != null)
29+
.SelectArray(message => new PsNonComplianceMessage(message));
2630
}
2731

2832
/// <summary>
@@ -65,6 +69,11 @@ public PsPolicyAssignmentProperties(JToken input)
6569
/// </summary>
6670
public PSObject Parameters { get; set; }
6771

72+
/// <summary>
73+
/// The non-compliance messages used to describe why a resource is non-compliant with the policy.
74+
/// </summary>
75+
public PsNonComplianceMessage[] NonComplianceMessages { get; set; }
76+
6877
/// <summary>
6978
/// Convert to JSON
7079
/// </summary>
@@ -109,6 +118,11 @@ public JToken ToJToken()
109118
returnValue["parameters"] = this.Parameters.ToJToken();
110119
}
111120

121+
if (this.NonComplianceMessages != null)
122+
{
123+
returnValue["nonComplianceMessages"] = this.NonComplianceMessages.ToJToken();
124+
}
125+
112126
return returnValue;
113127
}
114128
}

src/Resources/ResourceManager/Implementation/Policy/SetAzurePolicyAssignment.cs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
1717
using System;
1818
using System.Collections;
19+
using System.Linq;
1920
using System.Management.Automation;
2021

2122
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
@@ -130,6 +131,13 @@ public class SetAzurePolicyAssignmentCmdlet : PolicyCmdletBase
130131
[Parameter(ParameterSetName = PolicyCmdletBase.InputObjectParameterSet, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.SetPolicyAssignmentInputObjectHelp)]
131132
public PsPolicyAssignment InputObject { get; set; }
132133

134+
/// <summary>
135+
/// Gets or sets the messages that describe why a resource is non-compliant with the policy.
136+
/// </summary>
137+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.PolicyAssignmentNonComplianceMessageHelp)]
138+
[ValidateNotNull]
139+
public PsNonComplianceMessage[] NonComplianceMessage { get; set; }
140+
133141
/// <summary>
134142
/// Executes the cmdlet.
135143
/// </summary>
@@ -166,10 +174,11 @@ protected override void OnProcessRecord()
166174
/// </summary>
167175
private JToken GetResource(string resourceId, string apiVersion)
168176
{
169-
var resource = this.GetExistingResource(resourceId, apiVersion).Result.ToResource();
177+
var resource = this.GetExistingResource(resourceId, apiVersion).Result.ToResource<PolicyAssignmentProperties>();
170178

171179
// get incoming object properties if present
172180
JObject inputMetadata = null;
181+
173182
if (this.InputObject != null)
174183
{
175184
var newProperties = this.InputObject.Properties.ToJToken();
@@ -178,36 +187,42 @@ private JToken GetResource(string resourceId, string apiVersion)
178187

179188
var parameterMetadata = this.Metadata != null ? this.GetObjectFromParameter(this.Metadata, nameof(this.Metadata)) : null;
180189

181-
PolicyAssignmentEnforcementMode? existingMode = null;
182-
if (Enum.TryParse(resource.Properties["enforcementMode"]?.ToString(), true, out PolicyAssignmentEnforcementMode tempMode))
183-
{
184-
existingMode = tempMode;
185-
}
186-
187190
PolicyAssignmentEnforcementMode? inputMode = null;
188191
if (Enum.TryParse(this.InputObject?.Properties?.EnforcementMode?.ToString(), true, out PolicyAssignmentEnforcementMode tempMode1))
189192
{
190193
inputMode = tempMode1;
191194
}
192195

196+
// Grab the non-compliance messages from the parameter or input object or existing resource
197+
var nonComplianceMessages = this.NonComplianceMessage?.Where(message => message != null).SelectArray(message => message.ToModel());
198+
if (nonComplianceMessages == null && this.InputObject?.Properties.NonComplianceMessages != null)
199+
{
200+
nonComplianceMessages = this.InputObject.Properties.NonComplianceMessages.Where(message => message != null).SelectArray(message => message.ToModel());
201+
}
202+
else if (nonComplianceMessages == null)
203+
{
204+
nonComplianceMessages = resource.Properties.NonComplianceMessages;
205+
}
206+
193207
var policyAssignmentObject = new PolicyAssignment
194208
{
195209
Name = this.Name ?? this.InputObject?.Name ?? resource.Name,
196210
Identity = this.AssignIdentity.IsPresent ? new ResourceIdentity { Type = ResourceIdentityType.SystemAssigned } : null,
197211
Location = this.Location ?? this.InputObject?.Location ?? resource.Location,
198212
Properties = new PolicyAssignmentProperties
199213
{
200-
DisplayName = this.DisplayName ?? this.InputObject?.Properties?.DisplayName ?? resource.Properties["displayName"]?.ToString(),
201-
Description = this.Description ?? this.InputObject?.Properties?.Description ?? resource.Properties["description"]?.ToString(),
202-
Scope = resource.Properties["scope"]?.ToString(),
203-
NotScopes = this.NotScope ?? this.InputObject?.Properties?.NotScopes ?? resource.Properties["NotScopes"]?.ToString()?.Split(','),
204-
PolicyDefinitionId = resource.Properties["policyDefinitionId"]?.ToString(),
205-
Metadata = parameterMetadata ?? inputMetadata ?? resource.Properties["metadata"] as JObject,
206-
EnforcementMode = this.EnforcementMode ?? inputMode ?? existingMode,
214+
DisplayName = this.DisplayName ?? this.InputObject?.Properties?.DisplayName ?? resource.Properties.DisplayName,
215+
Description = this.Description ?? this.InputObject?.Properties?.Description ?? resource.Properties.Description,
216+
Scope = resource.Properties.Scope,
217+
NotScopes = this.NotScope ?? this.InputObject?.Properties?.NotScopes ?? resource.Properties.NotScopes,
218+
PolicyDefinitionId = resource.Properties.PolicyDefinitionId,
219+
Metadata = parameterMetadata ?? inputMetadata ?? resource.Properties.Metadata,
220+
EnforcementMode = this.EnforcementMode ?? inputMode ?? resource.Properties.EnforcementMode,
221+
NonComplianceMessages = nonComplianceMessages,
207222
Parameters =
208223
this.GetParameters(this.PolicyParameter, this.PolicyParameterObject)
209224
?? this.InputObject?.Properties?.Parameters?.ToResourcePropertiesBody() as JObject
210-
?? resource.Properties["parameters"] as JObject
225+
?? resource.Properties.Parameters
211226
}
212227
};
213228

0 commit comments

Comments
 (0)