Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
vivsriaus committed Sep 17, 2015
1 parent 92b77e7 commit 4951d0c
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ public class PolicyAssignment
/// </summary>
[JsonProperty(Required = Required.Default)]
public PolicyAssignmentProperties Properties { get; set; }

/// <summary>
/// The policy assignment name.
/// </summary>
[JsonProperty(Required = Required.Default)]
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ public class PolicyDefinition
/// </summary>
[JsonProperty(Required = Required.Default)]
public PolicyDefinitionProperties Properties { get; set; }

/// <summary>
/// The policy definition name.
/// </summary>
[JsonProperty(Required = Required.Default)]
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
.DetermineApiVersion(resourceId: resourceId)
.ConfigureAwait(continueOnCapturedContext: false);

if (!string.IsNullOrEmpty(this.Name) && !string.IsNullOrEmpty(this.Scope))
if (IsResourceGet(resourceId))
{
var resource = await this
.GetResourcesClient()
Expand All @@ -119,7 +119,7 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
? retVal
: new ResponseWithContinuation<JObject[]> { Value = resource.AsArray() };
}
else if(!string.IsNullOrEmpty(this.Scope) && string.IsNullOrEmpty(this.Name))//If only scope is given, list assignments call
else if (IsScopeLevelList(resourceId))//If only scope is given, list assignments call
{
string filter = "$filter=atScope()";
return await this
Expand Down Expand Up @@ -148,6 +148,25 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
}
}

/// <summary>
/// Returns true if it is scope level policy assignment list call
/// </summary>
private bool IsScopeLevelList(string resourceId)
{
return (!string.IsNullOrEmpty(this.Scope) && string.IsNullOrEmpty(this.Name))
|| (!string.IsNullOrEmpty(this.Scope) && string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId)));
}

/// <summary>
/// Returns true if it is a single policy assignment get
/// </summary>
/// <param name="resourceId"></param>
private bool IsResourceGet(string resourceId)
{
return (!string.IsNullOrEmpty(this.Name) && !string.IsNullOrEmpty(this.Scope))
|| !string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId));
}

/// <summary>
/// Gets the resource Id
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()
.DetermineApiVersion(resourceId: resourceId)
.ConfigureAwait(continueOnCapturedContext: false);

if (!string.IsNullOrEmpty(this.Name))
if (!string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId)))
{
var resource = await this
.GetResourcesClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public class NewAzurePolicyAssignmentCmdlet : PolicyAssignmentCmdletBase
protected override void OnProcessRecord()
{
base.OnProcessRecord();
if(this.PolicyDefinition.Properties["policyDefinitionId"] == null)
{
throw new PSInvalidOperationException("The supplied PolicyDefinition object is invalid.");
}
string resourceId = GetResourceId();
var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;

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

this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
this.WriteObject(this.GetOutputObjects(JObject.Parse(result)), enumerateCollection: true);
}

/// <summary>
Expand All @@ -106,10 +110,11 @@ private JToken GetResource()
{
var policyassignmentObject = new PolicyAssignment
{
Name = this.Name,
Properties = new PolicyAssignmentProperties
{
DisplayName = this.DisplayName ?? null,
PolicyDefinitionId = this.PolicyDefinition.Properties["PolicyDefinitionId"].Value.ToString(),
PolicyDefinitionId = this.PolicyDefinition.Properties["policyDefinitionId"].Value.ToString(),
Scope = this.Scope
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ protected override void OnProcessRecord()
var activity = string.Format("PUT {0}", managementUri.PathAndQuery);
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
.WaitOnOperation(operationResult: operationResult);

this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
this.WriteObject(this.GetOutputObjects(JObject.Parse(result)), enumerateCollection: true);
}

/// <summary>
Expand All @@ -108,6 +107,7 @@ private JToken GetResource()
{
var policyDefinitionObject = new PolicyDefinition
{
Name = this.Name,
Properties = new PolicyDefinitionProperties
{
Description = this.Description ?? null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected override void OnProcessRecord()
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
.WaitOnOperation(operationResult: operationResult);

this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
this.WriteObject(this.GetOutputObjects(JObject.Parse(result)), enumerateCollection: true);
}

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

var policyAssignmentObject = new PolicyAssignment
{
Name = this.Name ?? ResourceIdUtility.GetResourceName(this.Id),
Properties = new PolicyAssignmentProperties
{
DisplayName = this.DisplayName ?? resource.Properties["DisplayName"].ToString(),
DisplayName = this.DisplayName ?? (resource.Properties["displayName"] != null
? resource.Properties["displayName"].ToString()
: null)
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected override void OnProcessRecord()
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
.WaitOnOperation(operationResult: operationResult);

this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
this.WriteObject(this.GetOutputObjects(JObject.Parse(result)), enumerateCollection: true);
}

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

var policyDefinitionObject = new PolicyDefinition
{
Name = this.Name ?? ResourceIdUtility.GetResourceName(this.Id),
Properties = new PolicyDefinitionProperties
{
Description = this.Description ?? resource.Properties["Description"].ToString(),
DisplayName = this.DisplayName ?? resource.Properties["DisplayName"].ToString(),
Description = this.Description ?? (resource.Properties["description"] != null
? resource.Properties["description"].ToString()
: null),
DisplayName = this.DisplayName ?? (resource.Properties["displayName"] != null
? resource.Properties["displayName"].ToString()
: null)
}
};
if(!string.IsNullOrEmpty(this.Policy))
Expand All @@ -130,7 +135,7 @@ private JToken GetResource(string resourceId, string apiVersion)
}
else
{
policyDefinitionObject.Properties.PolicyRule = resource.Properties["PolicyRule"].ToString();
policyDefinitionObject.Properties.PolicyRule = resource.Properties["policyRule"].ToString();
}

return policyDefinitionObject.ToJToken();
Expand Down

0 comments on commit 4951d0c

Please sign in to comment.