Skip to content

Add policy definition cmdlets #907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@
<Compile Include="Entities\Locks\LockLevel.cs" />
<Compile Include="Entities\Locks\LockProperties.cs" />
<Compile Include="Entities\Operations\AzureAsyncOperationResource.cs" />
<Compile Include="Entities\Policy\PolicyDefinition.cs" />
<Compile Include="Entities\Policy\PolicyDefinitionProperties.cs" />
<Compile Include="Entities\Policy\PolicyRule.cs" />
<Compile Include="Entities\Providers\ResourceProviderDefinition.cs" />
<Compile Include="Entities\Providers\ResourceTypeDefinition.cs" />
<Compile Include="Entities\ResourceGroup\ResourceBatchMoveParameters.cs" />
Expand All @@ -136,6 +139,11 @@
<Compile Include="Implementation\InvokeAzureResourceActionCmdlet.cs" />
<Compile Include="Implementation\MoveAzureResourceCmdlet.cs" />
<Compile Include="Implementation\NewAzureResourceLockCmdlet.cs" />
<Compile Include="Implementation\Policy\GetAzurePolicyDefinition.cs" />
<Compile Include="Implementation\Policy\NewAzurePolicyDefinition.cs" />
<Compile Include="Implementation\Policy\PolicyDefinitionCmdletBase.cs" />
<Compile Include="Implementation\Policy\RemoveAzurePolicyDefinition.cs" />
<Compile Include="Implementation\Policy\SetAzurePolicyDefinition.cs" />
<Compile Include="Implementation\RemoveAzureResourceCmdlet.cs" />
<Compile Include="Implementation\RemoveAzureResourceLockCmdlet.cs" />
<Compile Include="Implementation\ResourceLockManagementCmdletBase.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static class Constants
/// </summary>
public static readonly string MicrosoftResourceNamesapce = "Microsoft.Resources";

/// <summary>
/// The <c>Microsoft.Authorization</c> namespace.
/// </summary>
public static readonly string MicrosoftAuthorizationNamespace = "Microsoft.Authorization";

/// <summary>
/// The string literal <c>ResourceGroups</c>
/// </summary>
Expand Down Expand Up @@ -64,6 +69,11 @@ public static class Constants
/// </summary>
public static readonly string MicrosoftResourcesDeploymentOperationsType = Constants.MicrosoftResourceNamesapce + "/deployments/operations";

/// <summary>
/// The policy definition resource type.
/// </summary>
public static readonly string MicrosoftAuthorizationPolicyDefinitionType = Constants.MicrosoftAuthorizationNamespace + "/policydefinitions";

/// <summary>
/// The type name of the generic resource.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
{
using Newtonsoft.Json;

/// <summary>
/// The policy definition object.
/// </summary>
public class PolicyDefinition
{
/// <summary>
/// The policy definition properties.
/// </summary>
[JsonProperty(Required = Required.Default)]
public PolicyDefinitionProperties Properties { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
{
using Newtonsoft.Json;

/// <summary>
/// The policy definition properties.
/// </summary>
public class PolicyDefinitionProperties
{
/// <summary>
/// The description.
/// </summary>
[JsonProperty(Required = Required.Default)]
public string Description { get; set; }

/// <summary>
/// The display name.
/// </summary>
[JsonProperty(Required = Required.Default)]
public string DisplayName { get; set; }

/// <summary>
/// The policy rule.
/// </summary>
[JsonProperty(Required = Required.Always)]
public PolicyRule PolicyRule { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
{
using Newtonsoft.Json;

/// <summary>
/// The policy rule object.
/// </summary>
public class PolicyRule
{
/// <summary>
/// The policy rule
/// </summary>
[JsonProperty(Required = Required.Always)]
public string Rule { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using System.Management.Automation;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Newtonsoft.Json.Linq;

/// <summary>
/// Gets the policy definition.
/// </summary>
[Cmdlet(VerbsCommon.Get, "AzureRMPolicyDefinition", DefaultParameterSetName = GetAzurePolicyDefinitionCmdlet.ParameterlessSet), OutputType(typeof(PSObject))]
public class GetAzurePolicyDefinitionCmdlet : PolicyDefinitionCmdletBase
{
/// <summary>
/// The policy Id parameter set.
/// </summary>
internal const string PolicyDefinitionIdParameterSet = "The policy definition Id parameter set.";

/// <summary>
/// The policy name parameter set.
/// </summary>
internal const string PolicyDefinitionNameParameterSet = "The policy definition name parameter set.";

/// <summary>
/// The list all policy parameter set.
/// </summary>
internal const string ParameterlessSet = "The list all policy definitions parameter set.";

/// <summary>
/// Gets or sets the policy definition name parameter.
/// </summary>
[Parameter(ParameterSetName = GetAzurePolicyDefinitionCmdlet.PolicyDefinitionNameParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The policy definition name.")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

/// <summary>
/// Gets or sets the policy definition id parameter
/// </summary>
[Alias("ResourceId")]
[Parameter(ParameterSetName = GetAzurePolicyDefinitionCmdlet.PolicyDefinitionIdParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The fully qualified policy definition Id, including the subscription. e.g. /subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}")]
[ValidateNotNullOrEmpty]
public string Id { get; set; }

/// <summary>
/// Executes the cmdlet.
/// </summary>
protected override void OnProcessRecord()
{
base.OnProcessRecord();

this.RunCmdlet();
}

/// <summary>
/// Contains the cmdlet's execution logic.
/// </summary>
private void RunCmdlet()
{
PaginatedResponseHelper.ForEach(
getFirstPage: () => this.GetResources(),
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
cancellationToken: this.CancellationToken,
action: resources => this.WriteObject(sendToPipeline: this.GetOutputObjects(resources), enumerateCollection: true));
}

/// <summary>
/// Queries the ARM cache and returns the cached resource that match the query specified.
/// </summary>
private async Task<ResponseWithContinuation<JObject[]>> GetResources()
{
string resourceId = this.Id ?? this.GetResourceId();

var apiVersion = await this
.DetermineApiVersion(resourceId: resourceId)
.ConfigureAwait(continueOnCapturedContext: false);

if (!string.IsNullOrEmpty(ResourceIdUtility.GetResourceGroupName(resourceId)))
{
var resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
ResponseWithContinuation<JObject[]> retVal;
return resource.TryConvertTo(out retVal) && retVal.Value != null
? retVal
: new ResponseWithContinuation<JObject[]> { Value = resource.AsArray() };
}
else
{
return await this
.GetResourcesClient()
.ListObjectColleciton<JObject>(
resourceCollectionId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
}
}

/// <summary>
/// Gets the resource Id
/// </summary>
private string GetResourceId()
{
var subscriptionId = DefaultContext.Subscription.Id;
if(string.IsNullOrEmpty(this.Name))
{
return string.Format("/subscriptions/{0}/providers/{1}",
subscriptionId.ToString(),
Constants.MicrosoftAuthorizationPolicyDefinitionType);
}
else
{
return string.Format("/subscriptions/{0}/providers/{1}/{2}",
subscriptionId.ToString(),
Constants.MicrosoftAuthorizationPolicyDefinitionType,
this.Name);
}
}
}
}
Loading