Skip to content

Commit 29d5dc0

Browse files
author
Hovsep Mkrtchyan
committed
Removed PSObject from PolicyAssignment, PolicyDefinition and ResourceLock
1 parent dc34da6 commit 29d5dc0

14 files changed

+119
-29
lines changed

src/CLU/Commands.ResourceManager.Cmdlets/Entities/Resources/ResourceLock.cs renamed to src/CLU/Commands.ResourceManager.Cmdlets/Entities/Locks/ResourceLock.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources
1818
{
1919
public class ResourceLock<TProperties> : Resource<TProperties>
2020
{
21+
public ResourceLock(Resource<TProperties> r)
22+
: base(r)
23+
{
24+
}
25+
2126
/// <summary>
2227
/// Gets or sets the lock id for the resource.
2328
/// </summary>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
16+
using Newtonsoft.Json;
17+
18+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
19+
{
20+
public class ResourcePolicyAssignment<TProperties> : Resource<TProperties>
21+
{
22+
public ResourcePolicyAssignment(Resource<TProperties> r)
23+
: base(r)
24+
{
25+
}
26+
27+
/// <summary>
28+
/// Gets or sets the policy assignment id for the resource.
29+
/// </summary>
30+
[JsonProperty(Required = Required.Default)]
31+
public string PolicyAssignmentId { get; set; }
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
16+
using Newtonsoft.Json;
17+
18+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
19+
{
20+
public class ResourcePolicyDefinition<TProperties> : Resource<TProperties>
21+
{
22+
public ResourcePolicyDefinition(Resource<TProperties> r)
23+
: base(r)
24+
{
25+
}
26+
27+
/// <summary>
28+
/// Gets or sets the policy definition id for the resource.
29+
/// </summary>
30+
[JsonProperty(Required = Required.Default)]
31+
public string PolicyDefinitionId { get; set; }
32+
}
33+
}

src/CLU/Commands.ResourceManager.Cmdlets/Entities/Resources/Resource.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources
2323
/// </summary>
2424
public class Resource<TProperties>
2525
{
26+
public Resource()
27+
{
28+
29+
}
30+
31+
public Resource(Resource<TProperties> r)
32+
{
33+
this.ChangedTime = r.ChangedTime;
34+
this.CreatedTime = r.CreatedTime;
35+
this.ETag = r.ETag;
36+
this.Id = r.Id;
37+
this.Kind = r.Kind;
38+
this.Location = r.Location;
39+
this.Name = r.Name;
40+
this.Plan = r.Plan;
41+
this.Properties = r.Properties;
42+
this.Sku = r.Sku;
43+
this.Tags = r.Tags;
44+
this.Type = r.Type;
45+
}
46+
2647
/// <summary>
2748
/// Gets or sets the properties.
2849
/// </summary>

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/GetAzureResourceGroupDeploymentOperationCmdlet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2525
/// <summary>
2626
/// Gets the deployment operation.
2727
/// </summary>
28-
[Cmdlet(VerbsCommon.Get, "AzureRmResourceGroupDeploymentOperation"), OutputType(typeof(PSObject))]
28+
[Cmdlet(VerbsCommon.Get, "AzureRmResourceGroupDeploymentOperation"), OutputType(typeof(Resource<JToken>))]
2929
public class GetAzureResourceGroupDeploymentOperationCmdlet : ResourceManagerCmdletBase
3030
{
3131
/// <summary>
@@ -81,7 +81,7 @@ private void RunCmdlet()
8181
getFirstPage: () => this.GetResources(),
8282
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
8383
cancellationToken: this.CancellationToken,
84-
action: resources => this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(resource => resource.ToPsObject()), enumerateCollection: true));
84+
action: resources => this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(resource => resource.ToResource()), enumerateCollection: true));
8585
}
8686

8787
/// <summary>

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Lock/ResourceLockManagementCmdletBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ protected ResourceLock<JToken>[] GetOutputObjects(params JToken[] resources)
158158
.SelectArray(resource =>
159159
{
160160
var rc = resource.ToResource();
161-
var resourceLock = rc.Cast<ResourceLock<JToken>>();
162-
resourceLock.LockId = rc.Properties["ResourceId"].ToString();
161+
var resourceLock = new ResourceLock<JToken>(rc);
162+
resourceLock.LockId = rc.Id;
163163
return resourceLock;
164164
});
165165
}

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Policy/GetAzurePolicyAssignment.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
2020
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2121
using Newtonsoft.Json.Linq;
22-
22+
using Entities.Policy;
2323
/// <summary>
2424
/// Gets the policy assignment.
2525
/// </summary>
26-
[Cmdlet(VerbsCommon.Get, "AzureRmPolicyAssignment", DefaultParameterSetName = GetAzurePolicyAssignmentCmdlet.ParameterlessSet), OutputType(typeof(PSObject))]
26+
[Cmdlet(VerbsCommon.Get, "AzureRmPolicyAssignment", DefaultParameterSetName = GetAzurePolicyAssignmentCmdlet.ParameterlessSet), OutputType(typeof(ResourcePolicyAssignment<JToken>))]
2727
public class GetAzurePolicyAssignmentCmdlet : PolicyAssignmentCmdletBase
2828
{
2929
/// <summary>

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Policy/GetAzurePolicyDefinition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
2020
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2121
using Newtonsoft.Json.Linq;
22-
22+
using Entities.Policy;
2323
/// <summary>
2424
/// Gets the policy definition.
2525
/// </summary>
26-
[Cmdlet(VerbsCommon.Get, "AzureRmPolicyDefinition", DefaultParameterSetName = GetAzurePolicyDefinitionCmdlet.ParameterlessSet), OutputType(typeof(PSObject))]
26+
[Cmdlet(VerbsCommon.Get, "AzureRmPolicyDefinition", DefaultParameterSetName = GetAzurePolicyDefinitionCmdlet.ParameterlessSet), OutputType(typeof(ResourcePolicyDefinition<JToken>))]
2727
public class GetAzurePolicyDefinitionCmdlet : PolicyDefinitionCmdletBase
2828
{
2929
/// <summary>

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Policy/NewAzurePolicyAssignment.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2323
/// <summary>
2424
/// Creates a policy assignment.
2525
/// </summary>
26-
[Cmdlet(VerbsCommon.New, "AzureRmPolicyAssignment"), OutputType(typeof(PSObject))]
26+
[Cmdlet(VerbsCommon.New, "AzureRmPolicyAssignment"), OutputType(typeof(ResourcePolicyAssignment<JToken>))]
2727
public class NewAzurePolicyAssignmentCmdlet : PolicyAssignmentCmdletBase
2828
{
2929
/// <summary>
@@ -51,19 +51,15 @@ public class NewAzurePolicyAssignmentCmdlet : PolicyAssignmentCmdletBase
5151
/// <summary>
5252
/// Gets or sets the policy assignment policy definition parameter.
5353
/// </summary>
54-
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The pollicy definition object.")]
55-
public PSObject PolicyDefinition { get; set; }
54+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The policy definition id object.")]
55+
public string PolicyDefinitionId { get; set; }
5656

5757
/// <summary>
5858
/// Executes the cmdlet.
5959
/// </summary>
6060
protected override void OnProcessRecord()
6161
{
6262
base.OnProcessRecord();
63-
if(this.PolicyDefinition.Properties["policyDefinitionId"] == null)
64-
{
65-
throw new PSInvalidOperationException("The supplied PolicyDefinition object is invalid.");
66-
}
6763
string resourceId = GetResourceId();
6864
var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;
6965

@@ -111,7 +107,7 @@ private JToken GetResource()
111107
Properties = new PolicyAssignmentProperties
112108
{
113109
DisplayName = this.DisplayName ?? null,
114-
PolicyDefinitionId = this.PolicyDefinition.Properties["policyDefinitionId"].Value.ToString(),
110+
PolicyDefinitionId = PolicyDefinitionId,
115111
Scope = this.Scope
116112
}
117113
};

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Policy/NewAzurePolicyDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2525
/// <summary>
2626
/// Creates the policy definition.
2727
/// </summary>
28-
[Cmdlet(VerbsCommon.New, "AzureRmPolicyDefinition"), OutputType(typeof(PSObject))]
28+
[Cmdlet(VerbsCommon.New, "AzureRmPolicyDefinition"), OutputType(typeof(ResourcePolicyDefinition<JToken>))]
2929
public class NewAzurePolicyDefinitionCmdlet : PolicyDefinitionCmdletBase
3030
{
3131
/// <summary>

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Policy/PolicyAssignmentCmdletBase.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2121
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2222
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2323
using Newtonsoft.Json.Linq;
24-
24+
using Entities.Policy;
2525
/// <summary>
2626
/// Base class for policy assignment cmdlets.
2727
/// </summary>
@@ -42,16 +42,17 @@ protected Task<ResponseWithContinuation<TType[]>> GetNextLink<TType>(string next
4242
/// Converts the resource object to policy definition object.
4343
/// </summary>
4444
/// <param name="resources">The policy definition resource object.</param>
45-
protected PSObject[] GetOutputObjects(params JToken[] resources)
45+
protected ResourcePolicyAssignment<JToken>[] GetOutputObjects(params JToken[] resources)
4646
{
4747
return resources
4848
.CoalesceEnumerable()
4949
.Where(resource => resource != null)
5050
.SelectArray(resource =>
5151
{
52-
var psobject = resource.ToResource().ToPsObject();
53-
psobject.Properties.Add(new PSNoteProperty("PolicyAssignmentId", psobject.Properties["ResourceId"].Value));
54-
return psobject;
52+
var rc = resource.ToResource();
53+
var res = new ResourcePolicyAssignment<JToken>(rc);
54+
res.PolicyAssignmentId = rc.Id;
55+
return res;
5556
});
5657
}
5758
}

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Policy/PolicyDefinitionCmdletBase.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2121
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2222
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2323
using Newtonsoft.Json.Linq;
24-
24+
using Entities.Policy;
2525
/// <summary>
2626
/// Base class for policy definition cmdlets.
2727
/// </summary>
@@ -42,16 +42,17 @@ protected Task<ResponseWithContinuation<TType[]>> GetNextLink<TType>(string next
4242
/// Converts the resource object to policy definition object.
4343
/// </summary>
4444
/// <param name="resources">The policy definition resource object.</param>
45-
protected PSObject[] GetOutputObjects(params JToken[] resources)
45+
protected ResourcePolicyDefinition<JToken>[] GetOutputObjects(params JToken[] resources)
4646
{
4747
return resources
4848
.CoalesceEnumerable()
4949
.Where(resource => resource != null)
5050
.SelectArray(resource =>
5151
{
52-
var psobject = resource.ToResource().ToPsObject();
53-
psobject.Properties.Add(new PSNoteProperty("PolicyDefinitionId", psobject.Properties["ResourceId"].Value));
54-
return psobject;
52+
var rc = resource.ToResource();
53+
var res = new ResourcePolicyDefinition<JToken>(rc);
54+
res.PolicyDefinitionId = rc.Id;
55+
return res;
5556
});
5657
}
5758
}

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Policy/SetAzurePolicyAssignment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2424
/// <summary>
2525
/// Sets the policy assignment.
2626
/// </summary>
27-
[Cmdlet(VerbsCommon.Set, "AzureRmPolicyAssignment", DefaultParameterSetName = SetAzurePolicyAssignmentCmdlet.PolicyAssignmentNameParameterSet), OutputType(typeof(PSObject))]
27+
[Cmdlet(VerbsCommon.Set, "AzureRmPolicyAssignment", DefaultParameterSetName = SetAzurePolicyAssignmentCmdlet.PolicyAssignmentNameParameterSet), OutputType(typeof(ResourcePolicyAssignment<JToken>))]
2828
public class SetAzurePolicyAssignmentCmdlet : PolicyAssignmentCmdletBase
2929
{
3030
/// <summary>

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Policy/SetAzurePolicyDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2626
/// <summary>
2727
/// Sets the policy definition.
2828
/// </summary>
29-
[Cmdlet(VerbsCommon.Set, "AzureRmPolicyDefinition", DefaultParameterSetName = SetAzurePolicyDefinitionCmdlet.PolicyDefinitionNameParameterSet), OutputType(typeof(PSObject))]
29+
[Cmdlet(VerbsCommon.Set, "AzureRmPolicyDefinition", DefaultParameterSetName = SetAzurePolicyDefinitionCmdlet.PolicyDefinitionNameParameterSet), OutputType(typeof(ResourcePolicyDefinition<JToken>))]
3030
public class SetAzurePolicyDefinitionCmdlet : PolicyDefinitionCmdletBase
3131
{
3232
/// <summary>

0 commit comments

Comments
 (0)