Skip to content

Commit dc143bc

Browse files
committed
Taking in CR comments for vault credentials and also implementing commandlet to get ASR vaults.
1 parent 2f09b01 commit dc143bc

File tree

8 files changed

+443
-44
lines changed

8 files changed

+443
-44
lines changed

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/Commands.RecoveryServices.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<DesignTime>True</DesignTime>
118118
<DependentUpon>Resources.resx</DependentUpon>
119119
</Compile>
120+
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesCloudServiceClient.cs" />
120121
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesStorageClient.cs" />
121122
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesStorageMappingClient.cs" />
122123
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesNetworkMappingClient.cs" />
@@ -137,6 +138,7 @@
137138
<Compile Include="Properties\AssemblyInfo.cs" />
138139
<Compile Include="Service\GetAzureSiteRecoveryStorage.cs" />
139140
<Compile Include="Service\GetAzureSiteRecoveryStorageMapping.cs" />
141+
<Compile Include="Service\GetAzureSiteRecoveryVaults.cs" />
140142
<Compile Include="Service\NewAzureSiteRecoveryStorageMapping.cs" />
141143
<Compile Include="Service\RemoveAzureSiteRecoveryStorageMapping.cs" />
142144
<Compile Include="Service\RemoveAzureSiteRecoveryNetworkMapping.cs" />

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/PSRecoveryServicesClient/PSRecoveryServicesClient.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ public partial class PSRecoveryServicesClient
4747
/// </summary>
4848
public string ClientRequestId { get; set; }
4949

50+
/// <summary>
51+
/// Gets the value of recovery services management client.
52+
/// </summary>
53+
public RecoveryServicesManagementClient GetRecoveryServicesClient
54+
{
55+
get
56+
{
57+
return this.recoveryServicesClient;
58+
}
59+
}
60+
5061
/// <summary>
5162
/// Amount of time to sleep before fetching job details again.
5263
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.Collections.Generic;
16+
using Microsoft.WindowsAzure;
17+
using Microsoft.WindowsAzure.Management.RecoveryServices.Models;
18+
19+
namespace Microsoft.Azure.Commands.RecoveryServices
20+
{
21+
/// <summary>
22+
/// Recovery services convenience client.
23+
/// </summary>
24+
public partial class PSRecoveryServicesClient
25+
{
26+
/// <summary>
27+
/// Method to retrieve cloud services list for the current subscription
28+
/// </summary>
29+
/// <returns>list of cloud services.</returns>
30+
public IEnumerable<CloudService> GetCloudServices()
31+
{
32+
CloudServiceListResponse response = this.GetRecoveryServicesClient.CloudServices.List();
33+
34+
return response.CloudServices;
35+
}
36+
37+
/// <summary>
38+
/// Method to get Cloud Service object for a given vault
39+
/// </summary>
40+
/// <param name="vaultName">vault name</param>
41+
/// <param name="region">vault region</param>
42+
/// <returns>cloud service object.</returns>
43+
public CloudService GetCloudServiceForVault(string vaultName, string region)
44+
{
45+
IEnumerable<CloudService> cloudServiceList = this.GetCloudServices();
46+
CloudService cloudServiceToReturn = null;
47+
48+
foreach (var cloudService in cloudServiceList)
49+
{
50+
Vault selectedVault = null;
51+
if (cloudService.GeoRegion == region)
52+
{
53+
foreach (var vault in cloudService.Resources)
54+
{
55+
if (vault.Name == vaultName)
56+
{
57+
selectedVault = vault;
58+
break;
59+
}
60+
}
61+
}
62+
63+
if (selectedVault != null)
64+
{
65+
cloudServiceToReturn = cloudService;
66+
break;
67+
}
68+
}
69+
70+
return cloudServiceToReturn;
71+
}
72+
}
73+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.Collections.Generic;
17+
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.RecoveryServices.SiteRecovery;
19+
using Microsoft.WindowsAzure.Management.RecoveryServices.Models;
20+
21+
namespace Microsoft.Azure.Commands.RecoveryServices
22+
{
23+
/// <summary>
24+
/// Retrieves Azure Site Recovery Server.
25+
/// </summary>
26+
[Cmdlet(VerbsCommon.Get, "AzureSiteRecoveryVaults", DefaultParameterSetName = ASRParameterSets.Default)]
27+
[OutputType(typeof(List<ASRVault>))]
28+
public class GetAzureSiteRecoveryVaults : RecoveryServicesCmdletBase
29+
{
30+
/// <summary>
31+
/// ProcessRecord of the command.
32+
/// </summary>
33+
public override void ExecuteCmdlet()
34+
{
35+
try
36+
{
37+
IEnumerable<CloudService> cloudServiceList = RecoveryServicesClient.GetCloudServices();
38+
39+
List<ASRVault> vaultList = new List<ASRVault>();
40+
foreach (var cloudService in cloudServiceList)
41+
{
42+
foreach (var vault in cloudService.Resources)
43+
{
44+
if (vault.Type.Equals(Constants.ASRVaulType, StringComparison.InvariantCultureIgnoreCase))
45+
{
46+
vaultList.Add(new ASRVault(cloudService, vault));
47+
}
48+
}
49+
}
50+
51+
this.WriteVaults(vaultList);
52+
}
53+
catch (Exception exception)
54+
{
55+
this.HandleException(exception);
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Writes Virtual Machines.
61+
/// </summary>
62+
/// <param name="vaultList">List of Vaults</param>
63+
private void WriteVaults(IList<ASRVault> vaultList)
64+
{
65+
this.WriteObject(vaultList, true);
66+
}
67+
}
68+
}

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/Service/GetVaultCredentialsFile.cs

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using Microsoft.Azure.Portal.RecoveryServices.Models.Common;
2222
using Microsoft.WindowsAzure.Commands.Common;
2323
using Microsoft.WindowsAzure.Commands.Common.Models;
24+
using Microsoft.WindowsAzure.Management.RecoveryServices.Models;
2425
using Microsoft.WindowsAzure.Management.SiteRecovery.Models;
2526

2627
namespace Microsoft.Azure.Commands.RecoveryServices
@@ -47,19 +48,18 @@ public class GetVaultCredentialsFile : RecoveryServicesCmdletBase
4748
public string Name { get; set; }
4849

4950
/// <summary>
50-
/// Gets or sets the vault name
51+
/// Gets or sets the location of the vault
5152
/// </summary>
52-
[Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "Vault Name for which the cred file to be generated")]
53+
[Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "Geo Location Name to which the vault belongs")]
5354
[ValidateNotNullOrEmpty]
54-
//// TODO: devsri - Remove this.
55-
public string CloudServiceName { get; set; }
55+
public string Location { get; set; }
5656

5757
/// <summary>
58-
/// Gets or sets the location of the vault
58+
/// Gets or sets Job Object.
5959
/// </summary>
60-
[Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "Geo Name to which the vault belongs")]
60+
[Parameter(ParameterSetName = ASRParameterSets.ByObject, Mandatory = true, ValueFromPipeline = true)]
6161
[ValidateNotNullOrEmpty]
62-
public string Geo { get; set; }
62+
public ASRVault Vault { get; set; }
6363

6464
/// <summary>
6565
/// Gets or sets the path where the credential file is to be generated
@@ -78,48 +78,70 @@ public class GetVaultCredentialsFile : RecoveryServicesCmdletBase
7878
/// <summary>
7979
/// ProcessRecord of the command.
8080
/// </summary>
81-
public override async void ExecuteCmdlet()
81+
public override void ExecuteCmdlet()
8282
{
8383
try
8484
{
85-
AzureSubscription subscription = AzureSession.CurrentContext.Subscription;
86-
87-
// Generate certificate
88-
X509Certificate2 cert = CertUtils.CreateSelfSignedCertificate(VaultCertificateExpiryInHoursForHRM, subscription.Id.ToString(), this.Name);
89-
90-
Utilities.UpdateVaultSettings(new ASRVaultCreds()
85+
switch (this.ParameterSetName)
9186
{
92-
CloudServiceName = this.CloudServiceName,
93-
ResourceName = this.Name
94-
});
95-
96-
// Upload certificate
97-
UploadCertificateResponse acsDetails = await this.UpdateVaultCertificate(cert);
98-
99-
// Get Channel Integrity key
100-
string channelIntegrityKey = await this.GetChannelIntegrityKey();
101-
102-
// Generate file.
103-
ASRVaultCreds vaultCreds = this.GenerateCredential(
104-
subscription.Id.ToString(),
105-
this.Name,
106-
cert,
107-
acsDetails,
108-
channelIntegrityKey,
109-
this.CloudServiceName);
110-
111-
string filePath = string.IsNullOrEmpty(this.Path) ? Utilities.GetDefaultPath() : this.Path;
112-
string fileName = this.GenerateFileName();
113-
114-
// write the content to a file.
115-
Utilities.WriteToFile<ASRVaultCreds>(vaultCreds, filePath, fileName);
87+
case ASRParameterSets.ByObject:
88+
this.Name = this.Vault.Name;
89+
this.Location = this.Vault.Location;
90+
this.GetByParams();
91+
break;
92+
case ASRParameterSets.ByParam:
93+
default:
94+
this.GetByParams();
95+
break;
96+
}
11697
}
11798
catch (Exception exception)
11899
{
119100
this.HandleException(exception);
120101
}
121102
}
122103

104+
/// <summary>
105+
/// Method to execute the command
106+
/// </summary>
107+
private async void GetByParams()
108+
{
109+
AzureSubscription subscription = AzureSession.CurrentContext.Subscription;
110+
111+
CloudService cloudService = RecoveryServicesClient.GetCloudServiceForVault(this.Name, this.Location);
112+
string cloudServiceName = cloudService.Name;
113+
114+
// Generate certificate
115+
X509Certificate2 cert = CertUtils.CreateSelfSignedCertificate(VaultCertificateExpiryInHoursForHRM, subscription.Id.ToString(), this.Name);
116+
117+
Utilities.UpdateVaultSettings(new ASRVaultCreds()
118+
{
119+
CloudServiceName = cloudServiceName,
120+
ResourceName = this.Name
121+
});
122+
123+
// Upload certificate
124+
UploadCertificateResponse acsDetails = await this.UpdateVaultCertificate(cert);
125+
126+
// Get Channel Integrity key
127+
string channelIntegrityKey = await this.GetChannelIntegrityKey();
128+
129+
// Generate file.
130+
ASRVaultCreds vaultCreds = this.GenerateCredential(
131+
subscription.Id.ToString(),
132+
this.Name,
133+
cert,
134+
acsDetails,
135+
channelIntegrityKey,
136+
cloudServiceName);
137+
138+
string filePath = string.IsNullOrEmpty(this.Path) ? Utilities.GetDefaultPath() : this.Path;
139+
string fileName = this.GenerateFileName();
140+
141+
// write the content to a file.
142+
Utilities.WriteToFile<ASRVaultCreds>(vaultCreds, filePath, fileName);
143+
}
144+
123145
/// <summary>
124146
/// Method to update vault certificate
125147
/// </summary>

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/lib/PSContracts.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,42 @@ public enum CikSupportedHashFunctions
4343
HMACSHA512
4444
}
4545

46+
/// <summary>
47+
/// Vault Status ENUM values
48+
/// </summary>
49+
public enum VaultStatus
50+
{
51+
/// <summary>
52+
/// Activating the created resource.
53+
/// </summary>
54+
Activating,
55+
56+
/// <summary>
57+
/// Creating the resource.
58+
/// </summary>
59+
Creating,
60+
61+
/// <summary>
62+
/// Resource created
63+
/// </summary>
64+
Created,
65+
66+
/// <summary>
67+
/// Resource is active
68+
/// </summary>
69+
Active,
70+
71+
/// <summary>
72+
/// Resource is disabled
73+
/// </summary>
74+
Disabled,
75+
76+
/// <summary>
77+
/// Resource is being deleted
78+
/// </summary>
79+
Removing
80+
}
81+
4682
/// <summary>
4783
/// Constant definition
4884
/// </summary>
@@ -62,6 +98,31 @@ public class Constants
6298
/// extended information version.
6399
/// </summary>
64100
public const string VaultExtendedInfVersion = "V2014_09";
101+
102+
/// <summary>
103+
/// A valid value for the string field Microsoft.WindowsAzure.CloudServiceManagement.resource.OperationStatus.Type
104+
/// </summary>
105+
public const string RdfeOperationStatusTypeCreate = "Create";
106+
107+
/// <summary>
108+
/// A valid value for the string field Microsoft.WindowsAzure.CloudServiceManagement.resource.OperationStatus.Type
109+
/// </summary>
110+
public const string RdfeOperationStatusTypeDelete = "Delete";
111+
112+
/// <summary>
113+
/// A valid value for the string field Microsoft.WindowsAzure.CloudServiceManagement.resource.OperationStatus.Result
114+
/// </summary>
115+
public const string RdfeOperationStatusResultSucceeded = "Succeeded";
116+
117+
/// <summary>
118+
/// A valid value for the string field Microsoft.WindowsAzure.CloudServiceManagement.resource.OperationStatus.Failed
119+
/// </summary>
120+
public const string RdfeOperationStatusResultFailed = "Failed";
121+
122+
/// <summary>
123+
/// A valid value for the string field Microsoft.WindowsAzure.CloudServiceManagement.resource.OperationStatus.InProgress
124+
/// </summary>
125+
public const string RdfeOperationStatusResultInProgress = "InProgress";
65126
}
66127

67128
/// <summary>

0 commit comments

Comments
 (0)