-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
ResourceManipulationCmdletBase.cs
157 lines (137 loc) · 7.75 KB
/
ResourceManipulationCmdletBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// ----------------------------------------------------------------------------------
//
// 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 Commands.Common.Authentication.Abstractions;
using Common.ArgumentCompleters;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using System;
using System.Management.Automation;
/// <summary>
/// The base class for manipulating resources.
/// </summary>
public abstract class ResourceManipulationCmdletBase : ResourceManagerCmdletBase
{
/// <summary>
/// The subscription level parameter set.
/// </summary>
internal const string SubscriptionLevelResoruceParameterSet = "BySubscriptionLevel";
/// <summary>
/// The tenant level parameter set.
/// </summary>
internal const string TenantLevelResoruceParameterSet = "ByTenantLevel";
/// <summary>
/// The tenant level parameter set.
/// </summary>
internal const string ResourceIdParameterSet = "ByResourceId";
/// <summary>
/// Gets or sets the resource Id parameter.
/// </summary>
[Alias("Id")]
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The fully qualified resource Id, including the subscription. e.g. /subscriptions/{subscriptionId}/providers/Microsoft.Sql/servers/myServer/databases/myDatabase")]
[ValidateNotNullOrEmpty]
public string ResourceId { get; set; }
/// <summary>
/// Gets or sets the extension resource name parameter.
/// </summary>
[Alias("Name")]
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.SubscriptionLevelResoruceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.TenantLevelResoruceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
[ValidateNotNullOrEmpty]
public string ResourceName { get; set; }
/// <summary>
/// Gets or sets the resource type parameter.
/// </summary>
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.SubscriptionLevelResoruceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.TenantLevelResoruceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
[ValidateNotNullOrEmpty]
public string ResourceType { get; set; }
/// <summary>
/// Gets or sets the extension resource name parameter.
/// </summary>
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.SubscriptionLevelResoruceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource name. e.g. to specify a database MyServer/MyDatabase.")]
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.TenantLevelResoruceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource name. e.g. to specify a database MyServer/MyDatabase.")]
[ValidateNotNullOrEmpty]
public string ExtensionResourceName { get; set; }
/// <summary>
/// Gets or sets the extension resource type parameter.
/// </summary>
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.SubscriptionLevelResoruceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource type. e.g. Microsoft.Sql/Servers/Databases.")]
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.TenantLevelResoruceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource type. e.g. Microsoft.Sql/Servers/Databases.")]
[ValidateNotNullOrEmpty]
public string ExtensionResourceType { get; set; }
/// <summary>
/// Gets or sets the OData query parameter.
/// </summary>
[Parameter(Mandatory = false, HelpMessage = "An OData style filter which will be appended to the request in addition to any other filters.")]
[ValidateNotNullOrEmpty]
public string ODataQuery { get; set; }
/// <summary>
/// Gets or sets the resource group name parameter.
/// </summary>
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.SubscriptionLevelResoruceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")]
[ResourceGroupCompleter]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }
/// <summary>
/// Gets or sets the tenant level parameter.
/// </summary>
[Parameter(ParameterSetName = ResourceManipulationCmdletBase.TenantLevelResoruceParameterSet, Mandatory = true, HelpMessage = "Indicates that this is a tenant level operation.")]
public SwitchParameter TenantLevel { get; set; }
/// <summary>
/// Gets or sets the force parameter.
/// </summary>
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
public SwitchParameter Force { get; set; }
/// <summary>
/// Gets or sets the subscription id.
/// </summary>
public Guid SubscriptionId { get; set; }
/// <summary>
/// Initializes the default subscription id if needed.
/// </summary>
public ResourceManipulationCmdletBase() {}
protected override void OnProcessRecord()
{
if (string.IsNullOrEmpty(this.ResourceId))
{
this.SubscriptionId = DefaultContext.Subscription.GetId();
}
}
/// <summary>
/// Gets the resource Id from the supplied PowerShell parameters.
/// </summary>
protected string GetResourceId()
{
#pragma warning disable 618
return !string.IsNullOrWhiteSpace(this.ResourceId)
? this.ResourceId
: this.GetResourceIdWithoutParentResource();
#pragma warning restore 618
}
/// <summary>
/// Gets the resource Id from the supplied PowerShell parameters.
/// </summary>
private string GetResourceIdWithoutParentResource()
{
return ResourceIdUtility.GetResourceId(
subscriptionId: this.SubscriptionId,
resourceGroupName: this.ResourceGroupName,
resourceType: this.ResourceType,
resourceName: this.ResourceName,
extensionResourceType: this.ExtensionResourceType,
extensionResourceName: this.ExtensionResourceName);
}
}
}