Skip to content

Refactor template deployment cmdlets. #10798

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 13 commits into from
Feb 4, 2020
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
1 change: 0 additions & 1 deletion src/Blueprint/Blueprint.Test/Blueprint.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
15 changes: 10 additions & 5 deletions src/Resources/ResourceManager/Components/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public static class Constants
/// </summary>
public static readonly string MicrosoftManagementNamespace = "Microsoft.Management";

/// <summary>
/// The string literal <c>ManagementGroups</c>
/// </summary>
public static readonly string ManagementGroups = "managementGroups";

/// <summary>
/// The management group id prefix.
/// </summary>
public static readonly string ManagementGroupIdPrefix = $"/providers/{Constants.MicrosoftManagementNamespace}/{Constants.ManagementGroups}/";

/// <summary>
/// The <c>Microsoft.Solutions</c> namespace.
/// </summary>
Expand Down Expand Up @@ -144,11 +154,6 @@ public static class Constants
/// </summary>
public static readonly string MicrosoftAuthorizationPolicyAssignmentType = Constants.MicrosoftAuthorizationNamespace + "/policyassignments";

/// <summary>
/// The management group resource type.
/// </summary>
public static readonly string MicrosoftManagementGroupDefinitionType = Constants.MicrosoftManagementNamespace + "/managementgroups";

/// <summary>
/// The application definition resource type.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions src/Resources/ResourceManager/Components/ResourceIdUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ public static string GetProviderNamespace(string resourceId)
return ResourceIdUtility.GetNextSegmentAfter(resourceId: resourceId, segmentName: Constants.Providers);
}

/// <summary>
/// Gets the management group id from the resource id.
/// </summary>
/// <param name="resourceId">The resource id.</param>
public static string GetManagementGroupId(string resourceId)
{
return resourceId.StartsWithInsensitively(Constants.ManagementGroupIdPrefix)
? ResourceIdUtility.GetNextSegmentAfter(resourceId: resourceId, segmentName: Constants.ManagementGroups)
: null;
}

/// <summary>
/// Gets the subscription id from the resource id.
/// </summary>
Expand Down Expand Up @@ -247,6 +258,15 @@ public static string GetResourceName(string resourceId)
return ResourceIdUtility.GetResourceTypeOrName(resourceId: resourceId, getResourceName: true);
}

/// <summary>
/// Gets the deployment name
/// </summary>
/// <param name="resourceId">The resource Id.</param>
public static string GetDeploymentName(string resourceId)
{
return ResourceIdUtility.GetResourceTypeOrName(resourceId: resourceId, getResourceName: true, useLastSegment: true);
}

/// <summary>
/// Gets the extension provider namespace from the resource id.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
using Microsoft.WindowsAzure.Commands.Utilities.Common;

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
Expand All @@ -39,7 +37,6 @@ public abstract class ResourceWithParameterCmdletBase : ResourceManagerCmdletBas
protected const string TemplateUriParameterFileParameterSetName = "ByTemplateUriAndParameterFile";
protected const string TemplateUriParameterUriParameterSetName = "ByTemplateUriAndParameterUri";

protected const string ParameterlessGalleryTemplateParameterSetName = "ByGalleryWithNoParameters";
protected const string ParameterlessTemplateObjectParameterSetName = "ByTemplateObjectWithNoParameters";
protected const string ParameterlessTemplateFileParameterSetName = "ByTemplateFileWithNoParameters";
protected const string ParameterlessTemplateUriParameterSetName = "ByTemplateUriWithNoParameters";
Expand Down Expand Up @@ -126,8 +123,7 @@ public object GetDynamicParameters()
{
if (!this.IsParameterBound(c => c.SkipTemplateParameterPrompt))
{
if (TemplateObject != null &&
TemplateObject != templateObject)
if (TemplateObject != null && TemplateObject != templateObject)
{
templateObject = TemplateObject;
if (string.IsNullOrEmpty(TemplateParameterUri))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// ----------------------------------------------------------------------------------
//
// 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.
// ----------------------------------------------------------------------------------

using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.Deployments;
using Microsoft.Azure.Commands.ResourceManager.Common;

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
/// <summary>
/// Get deployments.
/// </summary>
[Cmdlet(VerbsCommon.Get, AzureRMConstants.AzureRMPrefix + "ManagementGroupDeployment", DefaultParameterSetName = GetAzureManagementGroupDeploymentCmdlet.DeploymentNameParameterSet), OutputType(typeof(PSDeployment))]
public class GetAzureManagementGroupDeploymentCmdlet : ResourceManagerCmdletBase
{
/// <summary>
/// The deployment Id parameter set.
/// </summary>
internal const string DeploymentIdParameterSet = "GetByDeploymentId";

/// <summary>
/// The deployment name parameter set.
/// </summary>
internal const string DeploymentNameParameterSet = "GetByDeploymentName";

[Parameter(Position = 0, ParameterSetName = GetAzureManagementGroupDeploymentCmdlet.DeploymentNameParameterSet, Mandatory = true,
HelpMessage = "The management group id.")]
[ValidateNotNullOrEmpty]
public string ManagementGroupId { get; set; }

[Alias("DeploymentName")]
[Parameter(Position = 1, ParameterSetName = GetAzureManagementGroupDeploymentCmdlet.DeploymentNameParameterSet, Mandatory = false,
HelpMessage = "The name of deployment.")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Alias("DeploymentId", "ResourceId")]
[Parameter(ParameterSetName = GetAzureManagementGroupDeploymentCmdlet.DeploymentIdParameterSet, Mandatory = true,
HelpMessage = "The fully qualified resource Id of the deployment. example: /providers/Microsoft.Management/managementGroups/{managementGroupId}/providers/Microsoft.Resources/deployments/{deploymentName}")]
[ValidateNotNullOrEmpty]
public string Id { get; set; }

public override void ExecuteCmdlet()
{
FilterDeploymentOptions options = new FilterDeploymentOptions(DeploymentScopeType.ManagementGroup)
{
ManagementGroupId = this.ManagementGroupId ?? ResourceIdUtility.GetManagementGroupId(this.Id),
DeploymentName = this.Name ?? (string.IsNullOrEmpty(this.Id) ? null : ResourceIdUtility.GetDeploymentName(this.Id))
};

WriteObject(ResourceManagerSdkClient.FilterDeployments(options), true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// ----------------------------------------------------------------------------------
//
// 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 Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.Deployments;
using Microsoft.Azure.Commands.ResourceManager.Common;

/// <summary>
/// Gets the deployment operation.
/// </summary>
[Cmdlet(VerbsCommon.Get, AzureRMConstants.AzureRMPrefix + "ManagementGroupDeploymentOperation",
DefaultParameterSetName = GetAzureManagementGroupDeploymentOperationCmdlet.DeploymentNameParameterSet), OutputType(typeof(PSDeploymentOperation))]
public class GetAzureManagementGroupDeploymentOperationCmdlet : ResourceManagerCmdletBase
{
/// <summary>
/// The deployment name parameter set.
/// </summary>
internal const string DeploymentNameParameterSet = "GetByDeploymentName";

/// <summary>
/// The deployment object parameter set.
/// </summary>
internal const string DeploymentObjectParameterSet = "GetByDeploymentObject";

/// <summary>
/// Gets or sets the management group id parameter.
/// </summary>
[Parameter(ParameterSetName = GetAzureManagementGroupDeploymentOperationCmdlet.DeploymentNameParameterSet,
Mandatory = true, HelpMessage = "The management group id.")]
[ValidateNotNullOrEmpty]
public string ManagementGroupId { get; set; }

/// <summary>
/// Gets or sets the deployment name parameter.
/// </summary>
[Parameter(ParameterSetName = GetAzureManagementGroupDeploymentOperationCmdlet.DeploymentNameParameterSet,
Mandatory = true, HelpMessage = "The deployment name.")]
[ValidateNotNullOrEmpty]
public string DeploymentName { get; set; }

/// <summary>
/// Gets or sets the deployment operation Id.
/// </summary>
[Parameter(ParameterSetName = GetAzureManagementGroupDeploymentOperationCmdlet.DeploymentNameParameterSet,
Mandatory = false, HelpMessage = "The deployment operation Id.")]
public string OperationId { get; set; }

[Parameter(ParameterSetName = GetAzureManagementGroupDeploymentOperationCmdlet.DeploymentObjectParameterSet,
Mandatory = true, ValueFromPipeline = true, HelpMessage = "The deployment object.")]
public PSDeployment DeploymentObject { get; set; }

public override void ExecuteCmdlet()
{
var options = new FilterDeploymentOptions(DeploymentScopeType.ManagementGroup)
{
ManagementGroupId = !string.IsNullOrEmpty(this.ManagementGroupId) ? this.ManagementGroupId : this.DeploymentObject.ManagementGroupId,
DeploymentName = !string.IsNullOrEmpty(this.DeploymentName) ? this.DeploymentName : this.DeploymentObject.DeploymentName
};

var deploymentOperations = ResourceManagerSdkClient.ListDeploymentOperationsAtManagementGroup(
options.ManagementGroupId, options.DeploymentName, this.OperationId);

WriteObject(deploymentOperations, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.Deployments;
using Microsoft.Azure.Commands.ResourceManager.Common;

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
/// <summary>
/// Get deployments.
/// </summary>
[CmdletDeprecation(ReplacementCmdletName = "Get-AzSubscriptionDeployment")]
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Deployment", DefaultParameterSetName = GetAzureDeploymentCmdlet.DeploymentNameParameterSet), OutputType(typeof(PSDeployment))]
public class GetAzureDeploymentCmdlet : ResourceManagerCmdletBase
[Cmdlet(VerbsCommon.Get, AzureRMConstants.AzureRMPrefix + "Deployment", DefaultParameterSetName = GetAzureSubscriptionDeploymentCmdlet.DeploymentNameParameterSet), OutputType(typeof(PSDeployment))]
[Alias("Get-AzSubscriptionDeployment")]
public class GetAzureSubscriptionDeploymentCmdlet : ResourceManagerCmdletBase
{
/// <summary>
/// The deployment Id parameter set.
Expand All @@ -37,25 +38,25 @@ public class GetAzureDeploymentCmdlet : ResourceManagerCmdletBase
internal const string DeploymentNameParameterSet = "GetByDeploymentName";

[Alias("DeploymentName")]
[Parameter(Position = 0, ParameterSetName = GetAzureDeploymentCmdlet.DeploymentNameParameterSet, Mandatory = false,
[Parameter(Position = 0, ParameterSetName = GetAzureSubscriptionDeploymentCmdlet.DeploymentNameParameterSet, Mandatory = false,
HelpMessage = "The name of deployment.")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Alias("DeploymentId", "ResourceId")]
[Parameter(ParameterSetName = GetAzureDeploymentCmdlet.DeploymentIdParameterSet, Mandatory = false,
[Parameter(ParameterSetName = GetAzureSubscriptionDeploymentCmdlet.DeploymentIdParameterSet, Mandatory = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing an optional parameter to mandatory is a breaking change, and it cannot be checked in until next breaking change release which is ~May.
I agree that it should be mandatory, but my suggestion is to add a breaking change attribute and make the change later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also the cause to CI failure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be breaking. "Id" is the only parameter in this parameter set. If you don't specify the "id" parameter, you'll land with the default parameter set which is "GetByDeploymentName".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change it back to "mandatory" to false if that's the right thing..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.. then it's a false positive. Please read Debugging StaticAnalysis Errors to suppress the warning.

HelpMessage = "The fully qualified resource Id of the deployment. example: /subscriptions/{subId}/providers/Microsoft.Resources/deployments/{deploymentName}")]
[ValidateNotNullOrEmpty]
public string Id { get; set; }

public override void ExecuteCmdlet()
{
FilterDeploymentOptions options = new FilterDeploymentOptions()
FilterDeploymentOptions options = new FilterDeploymentOptions(DeploymentScopeType.Subscription)
{
DeploymentName = Name ?? (string.IsNullOrEmpty(Id) ? null : ResourceIdUtility.GetResourceName(Id))
DeploymentName = this.Name ?? (string.IsNullOrEmpty(this.Id) ? null : ResourceIdUtility.GetResourceName(this.Id))
};

WriteObject(ResourceManagerSdkClient.FilterDeploymentsAtSubscriptionScope(options), true);
WriteObject(ResourceManagerSdkClient.FilterDeployments(options), true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;

/// <summary>
/// Gets the deployment operation.
/// </summary>
[CmdletDeprecation(ReplacementCmdletName = "Get-AzSubscriptionDeploymentOperation")]
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "DeploymentOperation", DefaultParameterSetName = GetAzureDeploymentOperationCmdlet.DeploymentNameParameterSet), OutputType(typeof(PSDeploymentOperation))]
public class GetAzureDeploymentOperationCmdlet : ResourceManagerCmdletBase
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "DeploymentOperation", DefaultParameterSetName = GetAzureSubscriptionDeploymentOperationCmdlet.DeploymentNameParameterSet), OutputType(typeof(PSDeploymentOperation))]
[Alias("Get-AzSubscriptionDeploymentOperation")]
public class GetAzureSubscriptionDeploymentOperationCmdlet : ResourceManagerCmdletBase
{
/// <summary>
/// The deployment name parameter set.
Expand All @@ -38,17 +37,17 @@ public class GetAzureDeploymentOperationCmdlet : ResourceManagerCmdletBase
/// <summary>
/// Gets or sets the deployment name parameter.
/// </summary>
[Parameter(ParameterSetName = GetAzureDeploymentOperationCmdlet.DeploymentNameParameterSet, Mandatory = true, HelpMessage = "The deployment name.")]
[Parameter(ParameterSetName = GetAzureSubscriptionDeploymentOperationCmdlet.DeploymentNameParameterSet, Mandatory = true, HelpMessage = "The deployment name.")]
[ValidateNotNullOrEmpty]
public string DeploymentName { get; set; }

/// <summary>
/// Gets or sets the deployment operation Id.
/// </summary>
[Parameter(ParameterSetName = GetAzureDeploymentOperationCmdlet.DeploymentNameParameterSet, Mandatory = false, HelpMessage = "The deployment operation Id.")]
[Parameter(ParameterSetName = GetAzureSubscriptionDeploymentOperationCmdlet.DeploymentNameParameterSet, Mandatory = false, HelpMessage = "The deployment operation Id.")]
public string OperationId { get; set; }

[Parameter(ParameterSetName = GetAzureDeploymentOperationCmdlet.DeploymentObjectParameterSet, Mandatory = true,
[Parameter(ParameterSetName = GetAzureSubscriptionDeploymentOperationCmdlet.DeploymentObjectParameterSet, Mandatory = true,
ValueFromPipeline = true, HelpMessage = "The deployment object.")]
public PSDeployment DeploymentObject { get; set; }

Expand Down
Loading