Skip to content

Commit b8ab79b

Browse files
committed
Taking in site object, emitting the file path, calling upload certificate and get ACIk in parallel,
1 parent 9d9e9e7 commit b8ab79b

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

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

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,31 @@ public class GetVaultCredentialsFile : RecoveryServicesCmdletBase
5858
public string Location { get; set; }
5959

6060
/// <summary>
61-
/// Gets or sets Job Object.
61+
/// Gets or sets vault Object.
6262
/// </summary>
6363
[Parameter(ParameterSetName = ASRParameterSets.ByObject, Mandatory = true, ValueFromPipeline = true)]
6464
[ValidateNotNullOrEmpty]
6565
public ASRVault Vault { get; set; }
6666

6767
/// <summary>
68-
/// Gets or sets the path where the credential file is to be generated
68+
/// Gets or sets the site name
6969
/// </summary>
7070
[Parameter(ParameterSetName = ASRParameterSets.ByParam, Mandatory = false, HelpMessage = "The site name if the vault credentials to be downloaded for a Hyper-V sites.")]
7171
public string SiteName { get; set; }
7272

73+
/// <summary>
74+
/// Gets or sets the site id
75+
/// </summary>
76+
[Parameter(ParameterSetName = ASRParameterSets.ByParam, Mandatory = false, HelpMessage = "The site name if the vault credentials to be downloaded for a Hyper-V sites.")]
77+
public string SiteId { get; set; }
78+
79+
/// <summary>
80+
/// Gets or sets site object
81+
/// </summary>
82+
[Parameter(ParameterSetName = ASRParameterSets.ByObject, Mandatory = false, ValueFromPipeline = true)]
83+
[ValidateNotNullOrEmpty]
84+
public Site Site { get; set; }
85+
7386
/// <summary>
7487
/// Gets or sets the path where the credential file is to be generated
7588
/// </summary>
@@ -90,6 +103,12 @@ public override void ExecuteCmdlet()
90103
case ASRParameterSets.ByObject:
91104
this.Name = this.Vault.Name;
92105
this.Location = this.Vault.Location;
106+
if (this.Site != null)
107+
{
108+
this.SiteName = this.Site.Name;
109+
this.SiteId = this.Site.ID;
110+
}
111+
93112
this.GetByParams();
94113
break;
95114
case ASRParameterSets.ByParam:
@@ -107,7 +126,7 @@ public override void ExecuteCmdlet()
107126
/// <summary>
108127
/// Method to execute the command
109128
/// </summary>
110-
private async void GetByParams()
129+
private void GetByParams()
111130
{
112131
AzureSubscription subscription = AzureSession.CurrentContext.Subscription;
113132

@@ -124,15 +143,22 @@ private async void GetByParams()
124143
});
125144

126145
// Upload certificate
127-
UploadCertificateResponse acsDetails = await this.UpdateVaultCertificate(cert);
146+
UploadCertificateResponse acsDetails;
147+
Task<UploadCertificateResponse> uploadCertificate = this.UpdateVaultCertificate(cert);
128148

129149
// Get Channel Integrity key
130-
string channelIntegrityKey = await this.GetChannelIntegrityKey();
150+
string channelIntegrityKey;
151+
Task<string> getChannelIntegrityKey = this.GetChannelIntegrityKey();
152+
153+
uploadCertificate.Wait();
154+
getChannelIntegrityKey.Wait();
155+
156+
acsDetails = uploadCertificate.Result;
157+
channelIntegrityKey = getChannelIntegrityKey.Result;
131158

132159
// Generate file.
133160
ASRVaultCreds vaultCreds = this.GenerateCredential(
134161
subscription.Id.ToString(),
135-
this.Name,
136162
cert,
137163
acsDetails,
138164
channelIntegrityKey,
@@ -142,7 +168,10 @@ private async void GetByParams()
142168
string fileName = this.GenerateFileName();
143169

144170
// write the content to a file.
145-
Utilities.WriteToFile<ASRVaultCreds>(vaultCreds, filePath, fileName);
171+
string outputPath = Utilities.WriteToFile<ASRVaultCreds>(vaultCreds, filePath, fileName);
172+
173+
// print the path to the user.
174+
this.WriteObject(outputPath, true);
146175
}
147176

148177
/// <summary>
@@ -231,25 +260,26 @@ private ResourceExtendedInfo CreateVaultExtendedInformatino()
231260
/// Method to generate the credential file content
232261
/// </summary>
233262
/// <param name="subscriptionId">subscription id</param>
234-
/// <param name="resourceName">resource name</param>
235263
/// <param name="managementCert">management cert</param>
236264
/// <param name="acsDetails">ACS details</param>
237265
/// <param name="channelIntegrityKey">Integrity key</param>
238266
/// <param name="cloudServiceName">cloud service name</param>
239267
/// <returns>vault credential object</returns>
240-
private ASRVaultCreds GenerateCredential(string subscriptionId, string resourceName, X509Certificate2 managementCert, UploadCertificateResponse acsDetails, string channelIntegrityKey, string cloudServiceName)
268+
private ASRVaultCreds GenerateCredential(string subscriptionId, X509Certificate2 managementCert, UploadCertificateResponse acsDetails, string channelIntegrityKey, string cloudServiceName)
241269
{
242270
string serializedCertifivate = Convert.ToBase64String(managementCert.Export(X509ContentType.Pfx));
243271

244272
AcsNamespace acsNamespace = new AcsNamespace(acsDetails);
245273

246274
ASRVaultCreds vaultCreds = new ASRVaultCreds(
247275
subscriptionId,
248-
resourceName,
276+
this.Name,
249277
serializedCertifivate,
250278
acsNamespace,
251279
channelIntegrityKey,
252-
cloudServiceName);
280+
cloudServiceName,
281+
this.SiteId,
282+
this.SiteName);
253283

254284
return vaultCreds;
255285
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,18 +494,25 @@ public ASRVaultCreds()
494494
/// <param name="acsNamespace">authenticating service namespace</param>
495495
/// <param name="channelIntegrityKey">Agent Channel Integrity Key</param>
496496
/// <param name="cloudServiceName">cloud service name</param>
497+
/// <param name="siteId">custom site Id</param>
498+
/// <param name="siteName">custom site name</param>
497499
public ASRVaultCreds(
498500
string subscriptionId,
499501
string resourceName,
500502
string managementCert,
501503
AcsNamespace acsNamespace,
502504
string channelIntegrityKey,
503-
string cloudServiceName)
505+
string cloudServiceName,
506+
string siteId,
507+
string siteName)
504508
: base(subscriptionId, resourceName, managementCert, acsNamespace)
505509
{
506510
this.ChannelIntegrityKey = channelIntegrityKey;
507511
this.CloudServiceName = cloudServiceName;
508512
this.Version = Constants.VaultCredentialVersion;
513+
514+
this.SiteId = siteId != null ? siteId : string.Empty;
515+
this.SiteName = siteName != null ? siteName : string.Empty;
509516
}
510517

511518
#endregion
@@ -528,6 +535,18 @@ public ASRVaultCreds(
528535
/// </summary>
529536
[DataMember(Order = 2)]
530537
public string Version { get; set; }
538+
539+
/// <summary>
540+
/// Gets or sets the site Id
541+
/// </summary>
542+
[DataMember(Order = 3)]
543+
public string SiteId { get; set; }
544+
545+
/// <summary>
546+
/// Gets or sets the site name
547+
/// </summary>
548+
[DataMember(Order = 4)]
549+
public string SiteName { get; set; }
531550
#endregion
532551
}
533552

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,17 @@ public static T Deserialize<T>(string xml)
8282
/// <param name="fileContent">content to be written to the file</param>
8383
/// <param name="filePath">the path where the file is to be created</param>
8484
/// <param name="fileName">name of the file to be created</param>
85-
public static void WriteToFile<T>(T fileContent, string filePath, string fileName)
85+
/// <returns>file path with file name as string</returns>
86+
public static string WriteToFile<T>(T fileContent, string filePath, string fileName)
8687
{
8788
string fullFileName = Path.Combine(filePath, fileName);
88-
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@fullFileName, true))
89+
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@fullFileName, false))
8990
{
9091
string contentToWrite = Serialize<T>(fileContent);
9192
file.WriteLine(contentToWrite);
9293
}
94+
95+
return fullFileName;
9396
}
9497

9598
/// <summary>

0 commit comments

Comments
 (0)