Skip to content

Commit 4a6eb97

Browse files
committed
Merge pull request #190 from MabOneSdk/pragrawa-dev1
Set-RS VaultContext changes
2 parents bf79f3f + 01213a3 commit 4a6eb97

File tree

7 files changed

+119
-1
lines changed

7 files changed

+119
-1
lines changed

src/ResourceManager/RecoveryServices/Commands.RecoveryServices/Commands.RecoveryServices.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<Compile Include="Vault\GetAzureRmRecoveryServicesVaultSettingsFile.cs" />
134134
<Compile Include="Vault\NewAzureRmRecoveryServicesVault.cs" />
135135
<Compile Include="Vault\RemoveAzureRmRecoveryServicesVault.cs" />
136+
<Compile Include="Vault\SetAzureRmRecoveryServicesVaultContext.cs" />
136137
</ItemGroup>
137138
<ItemGroup>
138139
<EmbeddedResource Include="Properties\Resources.resx">

src/ResourceManager/RecoveryServices/Commands.RecoveryServices/Common/PSRecoveryServicesClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public RecoveryServicesManagementClient GetRecoveryServicesClient
6666
Justification = "For Resource Credentials.")]
6767
public static ASRVaultCreds asrVaultCreds = new ASRVaultCreds();
6868

69+
/// <summary>
70+
/// Resource credentials holds vault, resource group name, location and other details.
71+
/// </summary>
72+
[SuppressMessage(
73+
"Microsoft.StyleCop.CSharp.MaintainabilityRules",
74+
"SA1401:FieldsMustBePrivate",
75+
Justification = "For Resource Credentials.")]
76+
public static ARSVault arsVaultCreds = new ARSVault();
6977
/// <summary>
7078
/// Recovery Services client.
7179
/// </summary>

src/ResourceManager/RecoveryServices/Commands.RecoveryServices/Common/PSRecoveryServicesVaultClient.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ public VaultListResponse GetVaultsInResouceGroup(string resouceGroupName)
3232
return this.GetRecoveryServicesClient.Vaults.List(resouceGroupName, this.GetRequestHeaders());
3333
}
3434

35+
/// <summary>
36+
/// Method to get Azure Recovery Services Vault
37+
/// </summary>
38+
/// <param name="resouceGroupName">Name of the resouce group</param>
39+
/// <param name="resourceName">Name of the resource</param>
40+
/// <returns>vault response object.</returns>
41+
public VaultResponse GetVault(string resouceGroupName, string resourceName)
42+
{
43+
return this.GetRecoveryServicesClient.Vaults.Get(resouceGroupName, resourceName, this.GetRequestHeaders());
44+
}
45+
3546
/// <summary>
3647
/// Method to create Azure Recovery Services Vault
3748
/// </summary>

src/ResourceManager/RecoveryServices/Commands.RecoveryServices/Properties/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/RecoveryServices/Commands.RecoveryServices/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,7 @@ Please provide a storage account with the same location as that of the vault.</v
277277
<data name="SiteNotFound" xml:space="preserve">
278278
<value>Site {0} is not associated with the Vault {1}</value>
279279
</data>
280+
<data name="ResourceGroupNameNullOrEmpty" xml:space="preserve">
281+
<value>Resource Group name mentioned is either null or empty</value>
282+
</data>
280283
</root>

src/ResourceManager/RecoveryServices/Commands.RecoveryServices/Utilities/Utilities.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.Security.Cryptography;
2020
using Microsoft.Azure.Portal.RecoveryServices.Models.Common;
2121
using System.Collections.Generic;
22+
using Microsoft.Azure.Management.RecoveryServices.Models;
2223

2324
namespace Microsoft.Azure.Commands.RecoveryServices
2425
{
@@ -121,6 +122,19 @@ public static void UpdateCurrentVaultContext(ASRVaultCreds asrVaultCreds)
121122
}
122123
}
123124

125+
/// <summary>
126+
/// Updates current Vault context.
127+
/// </summary>
128+
/// <param name="arsVaultCreds">ARS Vault credentials</param>
129+
public static void UpdateCurrentVaultContext(ARSVault arsVaultCreds)
130+
{
131+
object updateVaultContextOneAtATime = new object();
132+
lock (updateVaultContextOneAtATime)
133+
{
134+
PSRecoveryServicesClient.arsVaultCreds = arsVaultCreds;
135+
}
136+
}
137+
124138
/// <summary>
125139
/// method to return the Downloads path for the current user.
126140
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
using System;
16+
using System.Linq;
17+
using System.Management.Automation;
18+
using System.Security.Cryptography.X509Certificates;
19+
using Microsoft.Azure.Common.Authentication.Models;
20+
using Microsoft.Azure.Portal.RecoveryServices.Models.Common;
21+
22+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
23+
{
24+
/// <summary>
25+
/// Used to set RecoveryServices Vault Context
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Set, "AzureRmRecoveryServicesVaultContext")]
28+
public class SetAzureRmRecoveryServicesVaultContext : RecoveryServicesCmdletBase
29+
{
30+
[Parameter(Mandatory = true, ValueFromPipeline = true)]
31+
[ValidateNotNullOrEmpty]
32+
public ARSVault Vault { get; set; }
33+
34+
public override void ExecuteCmdlet()
35+
{
36+
try
37+
{
38+
base.ExecuteCmdlet();
39+
40+
// Validate required parameters taken from the Vault.
41+
if (string.IsNullOrEmpty(Vault.Name))
42+
{
43+
throw new ArgumentException(
44+
Properties.Resources.ResourceNameNullOrEmpty,
45+
Vault.Name);
46+
}
47+
48+
if (string.IsNullOrEmpty(Vault.ResouceGroupName))
49+
{
50+
throw new ArgumentException(
51+
Properties.Resources.ResourceGroupNameNullOrEmpty,
52+
Vault.ResouceGroupName);
53+
}
54+
55+
var vault = RecoveryServicesClient.GetVault(Vault.ResouceGroupName, Vault.Name);
56+
if(vault == null)
57+
{
58+
throw new ArgumentException(
59+
string.Format(Properties.Resources.VaultNotFound, Vault.Name),
60+
Vault.ResouceGroupName);
61+
}
62+
63+
Utilities.UpdateCurrentVaultContext(Vault);
64+
}
65+
catch (Exception exception)
66+
{
67+
this.HandleException(exception);
68+
}
69+
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)