Skip to content

Commit 67a8387

Browse files
committed
Set RS VaultContext cmdlet
1 parent 189b3f8 commit 67a8387

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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
{
@@ -37,6 +38,8 @@ public class ClientProxyBase
3738
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
3839
public CancellationToken CmdletCancellationToken;
3940

41+
public static AzureRmRecoveryServicesVaultCreds recoveryServicesVaultCreds = new AzureRmRecoveryServicesVaultCreds();
42+
4043
public ClientProxyBase(params object[] parameters)
4144
{
4245
Parameters = parameters;
@@ -56,5 +59,19 @@ public string GetClientRequestId()
5659
{
5760
return ClientRequestId;
5861
}
62+
63+
public static void UpdateCurrentVaultContext(AzureRmRecoveryServicesVaultCreds vaultCreds)
64+
{
65+
object updateVaultContextOneAtATime = new object();
66+
lock (updateVaultContextOneAtATime)
67+
{
68+
recoveryServicesVaultCreds.ResourceName =
69+
vaultCreds.ResourceName;
70+
recoveryServicesVaultCreds.ResourceGroupName =
71+
vaultCreds.ResourceGroupName;
72+
recoveryServicesVaultCreds.Location =
73+
vaultCreds.Location;
74+
}
75+
}
5976
}
6077
}

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)