Skip to content

Commit 4951d0c

Browse files
committed
Fix bugs
1 parent 92b77e7 commit 4951d0c

File tree

8 files changed

+57
-13
lines changed

8 files changed

+57
-13
lines changed

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Entities/Policy/PolicyAssignment.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ public class PolicyAssignment
2626
/// </summary>
2727
[JsonProperty(Required = Required.Default)]
2828
public PolicyAssignmentProperties Properties { get; set; }
29+
30+
/// <summary>
31+
/// The policy assignment name.
32+
/// </summary>
33+
[JsonProperty(Required = Required.Default)]
34+
public string Name { get; set; }
2935
}
3036
}

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Entities/Policy/PolicyDefinition.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ public class PolicyDefinition
2626
/// </summary>
2727
[JsonProperty(Required = Required.Default)]
2828
public PolicyDefinitionProperties Properties { get; set; }
29+
30+
/// <summary>
31+
/// The policy definition name.
32+
/// </summary>
33+
[JsonProperty(Required = Required.Default)]
34+
public string Name { get; set; }
2935
}
3036
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
104104
.DetermineApiVersion(resourceId: resourceId)
105105
.ConfigureAwait(continueOnCapturedContext: false);
106106

107-
if (!string.IsNullOrEmpty(this.Name) && !string.IsNullOrEmpty(this.Scope))
107+
if (IsResourceGet(resourceId))
108108
{
109109
var resource = await this
110110
.GetResourcesClient()
@@ -119,7 +119,7 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
119119
? retVal
120120
: new ResponseWithContinuation<JObject[]> { Value = resource.AsArray() };
121121
}
122-
else if(!string.IsNullOrEmpty(this.Scope) && string.IsNullOrEmpty(this.Name))//If only scope is given, list assignments call
122+
else if (IsScopeLevelList(resourceId))//If only scope is given, list assignments call
123123
{
124124
string filter = "$filter=atScope()";
125125
return await this
@@ -148,6 +148,25 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
148148
}
149149
}
150150

151+
/// <summary>
152+
/// Returns true if it is scope level policy assignment list call
153+
/// </summary>
154+
private bool IsScopeLevelList(string resourceId)
155+
{
156+
return (!string.IsNullOrEmpty(this.Scope) && string.IsNullOrEmpty(this.Name))
157+
|| (!string.IsNullOrEmpty(this.Scope) && string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId)));
158+
}
159+
160+
/// <summary>
161+
/// Returns true if it is a single policy assignment get
162+
/// </summary>
163+
/// <param name="resourceId"></param>
164+
private bool IsResourceGet(string resourceId)
165+
{
166+
return (!string.IsNullOrEmpty(this.Name) && !string.IsNullOrEmpty(this.Scope))
167+
|| !string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId));
168+
}
169+
151170
/// <summary>
152171
/// Gets the resource Id
153172
/// </summary>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
8989
.DetermineApiVersion(resourceId: resourceId)
9090
.ConfigureAwait(continueOnCapturedContext: false);
9191

92-
if (!string.IsNullOrEmpty(this.Name))
92+
if (!string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId)))
9393
{
9494
var resource = await this
9595
.GetResourcesClient()

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public class NewAzurePolicyAssignmentCmdlet : PolicyAssignmentCmdletBase
6363
protected override void OnProcessRecord()
6464
{
6565
base.OnProcessRecord();
66+
if(this.PolicyDefinition.Properties["policyDefinitionId"] == null)
67+
{
68+
throw new PSInvalidOperationException("The supplied PolicyDefinition object is invalid.");
69+
}
6670
string resourceId = GetResourceId();
6771
var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;
6872

@@ -85,7 +89,7 @@ protected override void OnProcessRecord()
8589
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
8690
.WaitOnOperation(operationResult: operationResult);
8791

88-
this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
92+
this.WriteObject(this.GetOutputObjects(JObject.Parse(result)), enumerateCollection: true);
8993
}
9094

9195
/// <summary>
@@ -106,10 +110,11 @@ private JToken GetResource()
106110
{
107111
var policyassignmentObject = new PolicyAssignment
108112
{
113+
Name = this.Name,
109114
Properties = new PolicyAssignmentProperties
110115
{
111116
DisplayName = this.DisplayName ?? null,
112-
PolicyDefinitionId = this.PolicyDefinition.Properties["PolicyDefinitionId"].Value.ToString(),
117+
PolicyDefinitionId = this.PolicyDefinition.Properties["policyDefinitionId"].Value.ToString(),
113118
Scope = this.Scope
114119
}
115120
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ protected override void OnProcessRecord()
8585
var activity = string.Format("PUT {0}", managementUri.PathAndQuery);
8686
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
8787
.WaitOnOperation(operationResult: operationResult);
88-
89-
this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
88+
this.WriteObject(this.GetOutputObjects(JObject.Parse(result)), enumerateCollection: true);
9089
}
9190

9291
/// <summary>
@@ -108,6 +107,7 @@ private JToken GetResource()
108107
{
109108
var policyDefinitionObject = new PolicyDefinition
110109
{
110+
Name = this.Name,
111111
Properties = new PolicyDefinitionProperties
112112
{
113113
Description = this.Description ?? null,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected override void OnProcessRecord()
9797
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
9898
.WaitOnOperation(operationResult: operationResult);
9999

100-
this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
100+
this.WriteObject(this.GetOutputObjects(JObject.Parse(result)), enumerateCollection: true);
101101
}
102102

103103
/// <summary>
@@ -109,9 +109,12 @@ private JToken GetResource(string resourceId, string apiVersion)
109109

110110
var policyAssignmentObject = new PolicyAssignment
111111
{
112+
Name = this.Name ?? ResourceIdUtility.GetResourceName(this.Id),
112113
Properties = new PolicyAssignmentProperties
113114
{
114-
DisplayName = this.DisplayName ?? resource.Properties["DisplayName"].ToString(),
115+
DisplayName = this.DisplayName ?? (resource.Properties["displayName"] != null
116+
? resource.Properties["displayName"].ToString()
117+
: null)
115118
}
116119
};
117120

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected override void OnProcessRecord()
104104
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
105105
.WaitOnOperation(operationResult: operationResult);
106106

107-
this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
107+
this.WriteObject(this.GetOutputObjects(JObject.Parse(result)), enumerateCollection: true);
108108
}
109109

110110
/// <summary>
@@ -116,10 +116,15 @@ private JToken GetResource(string resourceId, string apiVersion)
116116

117117
var policyDefinitionObject = new PolicyDefinition
118118
{
119+
Name = this.Name ?? ResourceIdUtility.GetResourceName(this.Id),
119120
Properties = new PolicyDefinitionProperties
120121
{
121-
Description = this.Description ?? resource.Properties["Description"].ToString(),
122-
DisplayName = this.DisplayName ?? resource.Properties["DisplayName"].ToString(),
122+
Description = this.Description ?? (resource.Properties["description"] != null
123+
? resource.Properties["description"].ToString()
124+
: null),
125+
DisplayName = this.DisplayName ?? (resource.Properties["displayName"] != null
126+
? resource.Properties["displayName"].ToString()
127+
: null)
123128
}
124129
};
125130
if(!string.IsNullOrEmpty(this.Policy))
@@ -130,7 +135,7 @@ private JToken GetResource(string resourceId, string apiVersion)
130135
}
131136
else
132137
{
133-
policyDefinitionObject.Properties.PolicyRule = resource.Properties["PolicyRule"].ToString();
138+
policyDefinitionObject.Properties.PolicyRule = resource.Properties["policyRule"].ToString();
134139
}
135140

136141
return policyDefinitionObject.ToJToken();

0 commit comments

Comments
 (0)