Skip to content

Commit 9da1b94

Browse files
committed
Merge pull request #181 from MabOneSdk/pragrawa-dev1
Set RS VaultContext cmdlet
2 parents 8fe40b3 + 31a9bf4 commit 9da1b94

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.Text;
18+
using System.Threading.Tasks;
19+
using System.Collections.Generic;
20+
using System.Management.Automation;
21+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
22+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel;
23+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.HydraAdapter;
24+
25+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
26+
{
27+
/// <summary>
28+
/// To set RecoveryServicesVaultContext
29+
/// </summary>
30+
[Cmdlet(VerbsCommon.Set, "AzureRmRecoveryServicesVaultContext")]
31+
public class SetAzureRmRecoveryServicesVaultContext : RecoveryServicesBackupCmdletBase
32+
{
33+
[Parameter(Mandatory = true, HelpMessage = "", ValueFromPipeline = true)]
34+
[ValidateNotNullOrEmpty]
35+
public ARSVault Vault { get; set; }
36+
37+
public override void ExecuteCmdlet()
38+
{
39+
ExecutionBlock(() =>
40+
{
41+
base.ExecuteCmdlet();
42+
43+
// Validate required parameters taken from the Vault.
44+
if (string.IsNullOrEmpty(Vault.Name))
45+
{
46+
throw new ArgumentException(
47+
Properties.Resources.ResourceNameNullOrEmpty,
48+
Vault.Name);
49+
}
50+
51+
if (string.IsNullOrEmpty(Vault.ResouceGroupName))
52+
{
53+
throw new ArgumentException(
54+
Properties.Resources.CloudServiceNameNullOrEmpty,
55+
Vault.ResouceGroupName);
56+
}
57+
58+
AzureRmRecoveryServicesVaultCreds vaultCreds = new AzureRmRecoveryServicesVaultCreds(Vault.Name,
59+
Vault.ResouceGroupName, Vault.Location);
60+
61+
ClientProxyBase.UpdateCurrentVaultContext(vaultCreds);
62+
63+
//Add validation to check vault exist or not (see if we can resuse existing ASR code).
64+
65+
this.WriteObject(vaultCreds);
66+
});
67+
68+
}
69+
}
70+
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.HydraAdapter/ClientProxyBase.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@
1919
using System.Text;
2020
using System.Threading;
2121
using System.Threading.Tasks;
22+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
2223

2324
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.HydraAdapter
2425
{
2526
public class ClientProxyBase
2627
{
28+
/// <summary>
29+
/// Recovery Services Vault Credentials.
30+
/// </summary>
31+
protected static AzureRmRecoveryServicesVaultCreds recoveryServicesVaultCreds = new AzureRmRecoveryServicesVaultCreds();
32+
2733
protected object[] Parameters;
2834

2935
/// <summary>
@@ -36,7 +42,7 @@ public class ClientProxyBase
3642
/// </summary>
3743
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
3844
public CancellationToken CmdletCancellationToken;
39-
45+
4046
public ClientProxyBase(params object[] parameters)
4147
{
4248
Parameters = parameters;
@@ -56,5 +62,24 @@ public string GetClientRequestId()
5662
{
5763
return ClientRequestId;
5864
}
65+
66+
public AzureRmRecoveryServicesVaultCreds GetVaultCredentials()
67+
{
68+
return recoveryServicesVaultCreds;
69+
}
70+
71+
public static void UpdateCurrentVaultContext(AzureRmRecoveryServicesVaultCreds vaultCreds)
72+
{
73+
object updateVaultContextOneAtATime = new object();
74+
lock (updateVaultContextOneAtATime)
75+
{
76+
recoveryServicesVaultCreds.ResourceName =
77+
vaultCreds.ResourceName;
78+
recoveryServicesVaultCreds.ResourceGroupName =
79+
vaultCreds.ResourceGroupName;
80+
recoveryServicesVaultCreds.Location =
81+
vaultCreds.Location;
82+
}
83+
}
5984
}
6085
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/BaseObjects.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public class AzureRmRecoveryServicesVaultCreds : AzureRmRecoveryServicesObjectBa
4949
/// </summary>
5050
public string Location { get; set; }
5151

52+
public AzureRmRecoveryServicesVaultCreds()
53+
{
54+
}
55+
5256
public AzureRmRecoveryServicesVaultCreds(string resourceName, string resourceGroupName, string location)
5357
{
5458
ResourceName = resourceName;

0 commit comments

Comments
 (0)