Skip to content

Commit 26bf2c5

Browse files
committed
Create Vault commands
1 parent b8ab79b commit 26bf2c5

File tree

9 files changed

+188
-11
lines changed

9 files changed

+188
-11
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesVMClient.cs" />
137137
<Compile Include="RecoveryServicesCmdletBase.cs" />
138138
<Compile Include="Properties\AssemblyInfo.cs" />
139+
<Compile Include="Service\CreateAzureSiteRecoveryVault.cs" />
139140
<Compile Include="Service\GetAzureSiteRecoveryStorage.cs" />
140141
<Compile Include="Service\GetAzureSiteRecoveryStorageMapping.cs" />
141142
<Compile Include="Service\GetAzureSiteRecoveryVaults.cs" />
@@ -147,7 +148,7 @@
147148
<Compile Include="Service\GetAzureSiteRecoveryNetwork.cs" />
148149
<Compile Include="Service\CreateAzureSiteRecoveryRecoveryPlan.cs" />
149150
<Compile Include="Service\GetAzureSiteRecoveryRecoveryPlanFile.cs" />
150-
<Compile Include="Service\GetVaultCredentialsFile.cs" />
151+
<Compile Include="Service\GetAzureSiteRecoveryVaultCredential.cs" />
151152
<Compile Include="Service\RemoveAzureSiteRecoveryRecoveryPlan.cs" />
152153
<Compile Include="Service\GetAzureSiteRecoveryProtectionEntity.cs" />
153154
<Compile Include="Service\RestartAzureSiteRecoveryJob.cs" />
@@ -176,9 +177,7 @@
176177
</ProjectReference>
177178
</ItemGroup>
178179
<ItemGroup>
179-
<Content Include="lib\Microsoft.Azure.RecoveryServices.dll">
180-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
181-
</Content>
180+
<Content Include="lib\Microsoft.Azure.RecoveryServices.dll" />
182181
<Content Include="lib\Security.Cryptography.dll">
183182
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
184183
</Content>

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
1516
using System.Collections.Generic;
17+
using System.Net;
18+
using System.Threading.Tasks;
1619
using Microsoft.WindowsAzure;
1720
using Microsoft.WindowsAzure.Management.RecoveryServices.Models;
1821

@@ -69,5 +72,42 @@ public CloudService GetCloudServiceForVault(string vaultName, string region)
6972

7073
return cloudServiceToReturn;
7174
}
75+
76+
/// <summary>
77+
/// Method to Either find or create the cloud service.
78+
/// </summary>
79+
/// <param name="cloudServiceName">name of the cloud service to be created</param>
80+
/// <param name="cloudServiceInput">cloud service input to create the service.</param>
81+
public void FindOrCreateCloudService(string cloudServiceName, CloudServiceCreateArgs cloudServiceInput)
82+
{
83+
bool cloudServicePresent = this.DoesCloudServiceExits(cloudServiceName);
84+
85+
if (!cloudServicePresent)
86+
{
87+
this.GetRecoveryServicesClient.CloudServices.Create(cloudServiceName, cloudServiceInput);
88+
}
89+
}
90+
91+
/// <summary>
92+
/// Checks whether a cloud service is present or not.
93+
/// </summary>
94+
/// <param name="cloudServiceName">name of the cloud service to be created</param>
95+
/// <returns>returns true in case the cloud service exits and false otherwise.</returns>
96+
private bool DoesCloudServiceExits(string cloudServiceName)
97+
{
98+
IEnumerable<CloudService> cloudServiceList = this.GetCloudServices();
99+
bool cloudServicePresent = false;
100+
101+
foreach (var cloudService in cloudServiceList)
102+
{
103+
if (cloudServiceName.Equals(cloudService.Name, StringComparison.InvariantCultureIgnoreCase))
104+
{
105+
cloudServicePresent = true;
106+
break;
107+
}
108+
}
109+
110+
return cloudServicePresent;
111+
}
72112
}
73113
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System.Threading.Tasks;
1615
using Microsoft.WindowsAzure;
17-
using Microsoft.WindowsAzure.Management.SiteRecovery.Models;
16+
using Microsoft.WindowsAzure.Management.RecoveryServices.Models;
1817

1918
namespace Microsoft.Azure.Commands.RecoveryServices
2019
{
@@ -24,13 +23,15 @@ namespace Microsoft.Azure.Commands.RecoveryServices
2423
public partial class PSRecoveryServicesClient
2524
{
2625
/// <summary>
27-
/// Updates the vault certificate
26+
/// Method to create Azure Site Recovery Vault
2827
/// </summary>
29-
/// <param name="args">the certificate update arguments</param>
30-
/// <returns>Upload Certificate Response</returns>
31-
public async Task<UploadCertificateResponse> UpdateVaultCertificate(CertificateArgs args)
28+
/// <param name="cloudServiceName">name of the cloud service</param>
29+
/// <param name="vaultName">name of the vault</param>
30+
/// <param name="vaultCreateInput">vault creation input object</param>
31+
/// <returns>creation response object.</returns>
32+
public VaultCreateResponse CreateVault(string cloudServiceName, string vaultName, VaultCreateArgs vaultCreateInput)
3233
{
33-
return await this.GetSiteRecoveryClient().Vaults.UploadCertificateAsync(args, this.GetRequestHeaders(false));
34+
return this.GetRecoveryServicesClient.Vaults.Create(cloudServiceName, vaultName, vaultCreateInput);
3435
}
3536
}
3637
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,15 @@ public OperationResponse CreateExtendedInfo(ResourceExtendedInformationArgs exte
4343
{
4444
return this.GetSiteRecoveryClient().VaultExtendedInfo.CreateExtendedInfo(extendedInfoArgs, this.GetRequestHeaders(false));
4545
}
46+
47+
/// <summary>
48+
/// Updates the vault certificate
49+
/// </summary>
50+
/// <param name="args">the certificate update arguments</param>
51+
/// <returns>Upload Certificate Response</returns>
52+
public async Task<UploadCertificateResponse> UpdateVaultCertificate(CertificateArgs args)
53+
{
54+
return await this.GetSiteRecoveryClient().VaultExtendedInfo.UploadCertificateAsync(args, this.GetRequestHeaders(false));
55+
}
4656
}
4757
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.Management.Automation;
17+
using Microsoft.Azure.Commands.RecoveryServices.SiteRecovery;
18+
using Microsoft.Azure.Portal.RecoveryServices.Models.Common;
19+
using Microsoft.WindowsAzure.Management.RecoveryServices.Models;
20+
using Microsoft.WindowsAzure.Management.SiteRecovery.Models;
21+
22+
namespace Microsoft.Azure.Commands.RecoveryServices
23+
{
24+
/// <summary>
25+
/// Used to initiate a vault create operation.
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.New, "AzureSiteRecoveryVault")]
28+
[OutputType(typeof(string))]
29+
public class CreateAzureSiteRecoveryVault : RecoveryServicesCmdletBase
30+
{
31+
#region Parameters
32+
33+
/// <summary>
34+
/// Gets or sets the vault name
35+
/// </summary>
36+
[Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "Vault Name for which the cred file to be generated")]
37+
[ValidateNotNullOrEmpty]
38+
public string Name { get; set; }
39+
40+
/// <summary>
41+
/// Gets or sets the location of the vault
42+
/// </summary>
43+
[Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "Geo Location Name")]
44+
[ValidateNotNullOrEmpty]
45+
public string Location { get; set; }
46+
47+
#endregion
48+
49+
/// <summary>
50+
/// ProcessRecord of the command.
51+
/// </summary>
52+
public override void ExecuteCmdlet()
53+
{
54+
try
55+
{
56+
string cloudServiceName = Utilities.GenerateCloudServiceName(this.Location);
57+
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(cloudServiceName);
58+
string base64Label = Convert.ToBase64String(bytes);
59+
60+
CloudServiceCreateArgs cloudServiceCreateArgs = new CloudServiceCreateArgs()
61+
{
62+
GeoRegion = this.Location,
63+
Label = base64Label,
64+
Description = base64Label
65+
};
66+
67+
RecoveryServicesClient.FindOrCreateCloudService(cloudServiceName, cloudServiceCreateArgs);
68+
69+
VaultCreateArgs vaultCreateArgs = new VaultCreateArgs()
70+
{
71+
Name = this.Name,
72+
Plan = string.Empty,
73+
ResourceProviderNamespace = "WAHyperVRecoveryManager", // TODO:devsri - do not hard code, find a good place to keep it.
74+
Type = Constants.ASRVaulType,
75+
ETag = Guid.NewGuid().ToString(),
76+
SchemaVersion = Constants.RpSchemaVersion
77+
};
78+
79+
Utilities.UpdateVaultSettings(new ASRVaultCreds()
80+
{
81+
CloudServiceName = cloudServiceName,
82+
ResourceName = this.Name
83+
});
84+
85+
VaultCreateResponse response = RecoveryServicesClient.CreateVault(cloudServiceName, this.Name, vaultCreateArgs);
86+
87+
this.WriteObject(response.RequestId, true);
88+
}
89+
catch (Exception exception)
90+
{
91+
this.HandleException(exception);
92+
}
93+
}
94+
}
95+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ public class Constants
170170
/// A valid value for the string field Microsoft.WindowsAzure.CloudServiceManagement.resource.OperationStatus.InProgress
171171
/// </summary>
172172
public const string RdfeOperationStatusResultInProgress = "InProgress";
173+
174+
/// <summary>
175+
/// Cloud service name prefix
176+
/// </summary>
177+
public const string CloudServiceNameExtensionPrefix = "CS-";
178+
179+
/// <summary>
180+
/// Cloud service name suffix
181+
/// </summary>
182+
public const string CloudServiceNameExtensionSuffix = "-RecoveryServices";
183+
184+
/// <summary>
185+
/// Schema Version of RP
186+
/// </summary>
187+
public const string RpSchemaVersion = "1.1";
173188
}
174189

175190
/// <summary>

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Globalization;
1617
using System.IO;
1718
using System.Runtime.Serialization;
1819
using System.Security.Cryptography;
20+
using System.Text;
1921
using Microsoft.Azure.Portal.RecoveryServices.Models.Common;
2022

2123
namespace Microsoft.Azure.Commands.RecoveryServices
@@ -138,5 +140,20 @@ public static string GenerateRandomKey(int size)
138140
crypto.GetBytes(key);
139141
return Convert.ToBase64String(key);
140142
}
143+
144+
/// <summary>
145+
/// Method to generate the cloud service name
146+
/// </summary>
147+
/// <param name="region">region name</param>
148+
/// <returns>cloud service name as string</returns>
149+
public static string GenerateCloudServiceName(string region)
150+
{
151+
return string.Format(
152+
CultureInfo.InvariantCulture,
153+
"{0}{1}{2}",
154+
Constants.CloudServiceNameExtensionPrefix,
155+
region.Replace(' ', '-'),
156+
Constants.CloudServiceNameExtensionSuffix);
157+
}
141158
}
142159
}

0 commit comments

Comments
 (0)