Skip to content

Commit 82132a1

Browse files
committed
Merge pull request #500 from vivsriaus/dev
Add Test-AzureResource and Test-AzureResourceGroup cmdlets
2 parents 8de0b9d + fe2b0bb commit 82132a1

File tree

3 files changed

+298
-0
lines changed

3 files changed

+298
-0
lines changed

src/ResourceManager/ResourceManager/Commands.ResourceManager/Cmdlets/Commands.ResourceManager.Cmdlets.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@
143143
<Compile Include="Extensions\JTokenExtensions.cs" />
144144
<Compile Include="Implementation\ResourceManagerCmdletBase.cs" />
145145
<Compile Include="Implementation\SetAzureResourceLockCmdlet.cs" />
146+
<Compile Include="Implementation\TestAzureResourceCmdlet.cs" />
147+
<Compile Include="Implementation\TestAzureResourceGroupCmdlet.cs" />
146148
<Compile Include="Json\CamelCasePropertyNamesWithOverridesContractResolver.cs" />
147149
<Compile Include="Json\JsonPreserveCaseDictionaryAttribute.cs" />
148150
<Compile Include="Json\TimeSpanConverter.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
16+
{
17+
using System;
18+
using System.Management.Automation;
19+
using System.Net;
20+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
21+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
22+
using Newtonsoft.Json.Linq;
23+
24+
/// <summary>
25+
/// Cmdlet to check if a resource exists or not
26+
/// </summary>
27+
[Cmdlet(VerbsDiagnostic.Test, "AzureResource", DefaultParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet), OutputType(typeof(bool))]
28+
public sealed class TestAzureResoruceCmdlet : ResourceManagerCmdletBase
29+
{
30+
/// <summary>
31+
/// The get resource parameter set.
32+
/// </summary>
33+
internal const string GetResourceGroupResourceParameterSet = "Tests for the existance of a single resource in a resource group.";
34+
35+
/// <summary>
36+
/// The get tenant resource parameter set.
37+
/// </summary>
38+
internal const string GetTenantResourceParameterSet = "Tests for the existance of a single resource at the tenant level.";
39+
40+
/// <summary>
41+
/// The get tenant resource parameter set.
42+
/// </summary>
43+
internal const string GetResourceByIdParameterSet = "Tests for the existance of a single resource by its Id.";
44+
45+
/// <summary>
46+
/// The get tenant resource parameter set.
47+
/// </summary>
48+
internal const string GetSubscriptionResourcesParameterSet = "Tests for the existance of a single resource at the subscription level.";
49+
50+
/// <summary>
51+
/// Gets or sets the resource name parameter.
52+
/// </summary>
53+
[Alias("Id")]
54+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetResourceByIdParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = false, HelpMessage = "The resource's Id.")]
55+
[ValidateNotNullOrEmpty]
56+
public string ResourceId { get; set; }
57+
58+
/// <summary>
59+
/// Gets or sets the resource name parameter.
60+
/// </summary>
61+
[Alias("Name")]
62+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
63+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
64+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name. e.g. to specify a database MyServer/MyDatabase.")]
65+
[ValidateNotNullOrEmpty]
66+
public string ResourceName { get; set; }
67+
68+
/// <summary>
69+
/// Gets or sets the resource type parameter.
70+
/// </summary>
71+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
72+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
73+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource type. e.g. Microsoft.Sql/Servers/Databases.")]
74+
[ValidateNotNullOrEmpty]
75+
public string ResourceType { get; set; }
76+
77+
/// <summary>
78+
/// Gets or sets the extension resource name parameter.
79+
/// </summary>
80+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource name. e.g. to specify a database MyServer/MyDatabase.")]
81+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource name. e.g. to specify a database MyServer/MyDatabase.")]
82+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource type. e.g. Microsoft.Sql/Servers/Databases.")]
83+
[ValidateNotNullOrEmpty]
84+
public string ExtensionResourceName { get; set; }
85+
86+
/// <summary>
87+
/// Gets or sets the extension resource type.
88+
/// </summary>
89+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource type. e.g. Microsoft.Sql/Servers/Databases.")]
90+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource type. e.g. Microsoft.Sql/Servers/Databases.")]
91+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The extension resource type. e.g. Microsoft.Sql/Servers/Databases.")]
92+
[ValidateNotNullOrEmpty]
93+
public string ExtensionResourceType { get; set; }
94+
/// <summary>
95+
/// Gets or sets the subscription ids.
96+
/// </summary>
97+
[Parameter(Mandatory = false, ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
98+
[Parameter(Mandatory = false, ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
99+
[ValidateNotNullOrEmpty]
100+
public Guid? SubscriptionId { get; set; }
101+
102+
/// <summary>
103+
/// Gets or sets the resource group name.
104+
/// </summary>
105+
[Parameter(Mandatory = true, ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")]
106+
[ValidateNotNullOrEmpty]
107+
public string ResourceGroupName { get; set; }
108+
109+
/// <summary>
110+
/// Gets or sets the tenant level parameter.
111+
/// </summary>
112+
[Parameter(ParameterSetName = TestAzureResoruceCmdlet.GetTenantResourceParameterSet, Mandatory = true, HelpMessage = "Indicates that this is a tenant level operation.")]
113+
public SwitchParameter TenantLevel { get; set; }
114+
115+
/// <summary>
116+
/// Collects subscription ids from the pipeline.
117+
/// </summary>
118+
protected override void OnProcessRecord()
119+
{
120+
base.OnProcessRecord();
121+
if (!this.TenantLevel)
122+
{
123+
this.SubscriptionId = this.Profile.Context.Subscription.Id;
124+
}
125+
126+
this.RunCmdlet();
127+
}
128+
129+
/// <summary>
130+
/// Contains the cmdlet's execution logic.
131+
/// </summary>
132+
private void RunCmdlet()
133+
{
134+
this.WriteObject(this.TestResource());
135+
}
136+
137+
/// <summary>
138+
/// Tests if a resource exists or not.
139+
/// </summary>
140+
private bool TestResource()
141+
{
142+
var resourceId = this.GetResourceId();
143+
144+
var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;
145+
146+
try
147+
{
148+
this.GetResourcesClient().GetResource<JObject>(resourceId: resourceId, apiVersion: apiVersion, cancellationToken: this.CancellationToken.Value).Wait();
149+
return true;
150+
}
151+
catch (Exception ex)
152+
{
153+
if(ex.InnerException != null && ex.InnerException is ErrorResponseMessageException)
154+
{
155+
var exception = ex.InnerException as ErrorResponseMessageException;
156+
if (exception.HttpStatus.Equals(HttpStatusCode.NotFound))
157+
{
158+
return false;
159+
}
160+
else
161+
{
162+
throw ex.InnerException;
163+
}
164+
}
165+
else
166+
{
167+
throw ex;
168+
}
169+
}
170+
}
171+
172+
/// <summary>
173+
/// Gets the resource Id from the supplied PowerShell parameters.
174+
/// </summary>
175+
private string GetResourceId()
176+
{
177+
return !string.IsNullOrWhiteSpace(this.ResourceId)
178+
? this.ResourceId
179+
: ResourceIdUtility.GetResourceId(
180+
subscriptionId: this.SubscriptionId,
181+
resourceGroupName: this.ResourceGroupName,
182+
resourceType: this.ResourceType,
183+
resourceName: this.ResourceName,
184+
extensionResourceType: this.ExtensionResourceType,
185+
extensionResourceName: this.ExtensionResourceName);
186+
}
187+
}
188+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
16+
{
17+
using System;
18+
using System.Management.Automation;
19+
using System.Net;
20+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
21+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
22+
using Newtonsoft.Json.Linq;
23+
24+
/// <summary>
25+
/// Cmdlet to check if a resource group exists or not
26+
/// </summary>
27+
[Cmdlet(VerbsDiagnostic.Test, "AzureResourceGroup"), OutputType(typeof(bool))]
28+
public sealed class TestAzureResoruceGroupCmdlet : ResourceManagerCmdletBase
29+
{
30+
/// <summary>
31+
/// Gets or sets the subscription ids.
32+
/// </summary>
33+
[Parameter(Mandatory = false, ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
34+
[Parameter(Mandatory = false, ParameterSetName = TestAzureResoruceCmdlet.GetSubscriptionResourcesParameterSet, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The subscription to use.")]
35+
[ValidateNotNullOrEmpty]
36+
public Guid? SubscriptionId { get; set; }
37+
38+
/// <summary>
39+
/// Gets or sets the resource group name.
40+
/// </summary>
41+
[Parameter(Mandatory = true, ParameterSetName = TestAzureResoruceCmdlet.GetResourceGroupResourceParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name.")]
42+
[ValidateNotNullOrEmpty]
43+
public string ResourceGroupName { get; set; }
44+
45+
/// <summary>
46+
/// Collects subscription ids from the pipeline.
47+
/// </summary>
48+
protected override void OnProcessRecord()
49+
{
50+
base.OnProcessRecord();
51+
this.SubscriptionId = this.Profile.Context.Subscription.Id;
52+
this.RunCmdlet();
53+
}
54+
55+
/// <summary>
56+
/// Contains the cmdlet's execution logic.
57+
/// </summary>
58+
private void RunCmdlet()
59+
{
60+
this.WriteObject(this.TestResourceGroup());
61+
}
62+
63+
/// <summary>
64+
/// Tests if a resource group exists or not.
65+
/// </summary>
66+
private bool TestResourceGroup()
67+
{
68+
var resourceGroupId = this.GetResourceGroupId();
69+
var apiVersion = this.DetermineApiVersion(resourceId: resourceGroupId).Result;
70+
try
71+
{
72+
this.GetResourcesClient().GetResource<JObject>(resourceId: resourceGroupId, apiVersion: apiVersion, cancellationToken: this.CancellationToken.Value).Wait();
73+
return true;
74+
}
75+
catch (Exception ex)
76+
{
77+
if (ex.InnerException != null && ex.InnerException is ErrorResponseMessageException)
78+
{
79+
var exception = ex.InnerException as ErrorResponseMessageException;
80+
if (exception.HttpStatus.Equals(HttpStatusCode.NotFound))
81+
{
82+
return false;
83+
}
84+
else
85+
{
86+
throw ex.InnerException;
87+
}
88+
}
89+
else
90+
{
91+
throw ex;
92+
}
93+
}
94+
}
95+
96+
/// <summary>
97+
/// Gets the resource Id from the supplied PowerShell parameters.
98+
/// </summary>
99+
private string GetResourceGroupId()
100+
{
101+
return ResourceIdUtility.GetResourceId(
102+
subscriptionId: this.SubscriptionId,
103+
resourceGroupName: this.ResourceGroupName,
104+
resourceType: null,
105+
resourceName: null);
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)