Skip to content

Commit 8c690f1

Browse files
author
Anil Kumar Yelam
committed
Retained old cmdlets for compatibility. Added deployment token. Ran cmdlets on SQL14 and 16 VMs.
1 parent 5235875 commit 8c690f1

11 files changed

+519
-89
lines changed

src/ResourceManager/Compute/AzureRM.Compute.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ CmdletsToExport = 'Remove-AzureRmAvailabilitySet', 'Get-AzureRmAvailabilitySet',
8989
'Set-AzureRmVMDiagnosticsExtension', 'Set-AzureRmVMExtension',
9090
'Remove-AzureRmVMExtension', 'Get-AzureRmVMExtension',
9191
'Get-AzureRmVMSqlServerExtension',
92+
'New-AzureVMSqlServerAutoBackupConfig',
93+
'New-AzureVMSqlServerAutoPatchingConfig',
94+
'New-AzureVMSqlServerKeyVaultCredentialConfig',
9295
'New-AzureRmVMSqlServerAutoBackupConfig',
9396
'New-AzureRmVMSqlServerAutoPatchingConfig',
9497
'New-AzureRmVMSqlServerKeyVaultCredentialConfig',

src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@
271271
<Compile Include="Extension\SqlServer\AzureVMSqlServerAutoBackupSettings.cs" />
272272
<Compile Include="Extension\SqlServer\AzureVMSqlServerAutoPatchingSettings.cs" />
273273
<Compile Include="Extension\SqlServer\AzureVMSqlServerAutoTelemetrySettings.cs" />
274+
<Compile Include="Extension\SqlServer\NewAzureRmVMSqlServerAutoBackupConfig.cs" />
275+
<Compile Include="Extension\SqlServer\NewAzureRmVMSqlServerAutoPatchingConfig.cs" />
276+
<Compile Include="Extension\SqlServer\NewAzureRmVMSqlServerKeyVaultCredentialConfig.cs" />
274277
<Compile Include="Extension\SqlServer\PrivateKeyVaultCredentialSettings.cs" />
275278
<Compile Include="Extension\SqlServer\AzureVMSqlServerPrivateSettings.cs" />
276279
<Compile Include="Extension\SqlServer\AzureVMSqlServerPublicSettings.cs" />

src/ResourceManager/Compute/Commands.Compute/Common/ConstantStringTypes.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public static class ValidateSetValues
6363
public const string ReadOnly = "ReadOnly";
6464
public const string ReadWrite = "ReadWrite";
6565
public const string None = "None";
66+
public const string Manual = "Manual";
67+
public const string Automated = "Automated";
68+
public const string Daily = "Daily";
69+
public const string Weekly = "Weekly";
6670
}
6771

6872
public static class ProfileNouns

src/ResourceManager/Compute/Commands.Compute/Extension/SqlServer/AzureVMSqlServerPublicSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@ public class SqlServerPublicSettings
3838
/// Auto-telemetry settings
3939
/// </summary>
4040
public AutoTelemetrySettings AutoTelemetrySettings { get; set; }
41+
42+
/// <summary>
43+
/// Deployment token, esp. to differentiate between two successive similar config updates.
44+
/// </summary>
45+
public int? DeploymentToken { get; set; }
4146
}
4247
}
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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 Microsoft.Azure.Commands.Compute.Common;
16+
using Microsoft.Azure.Commands.Common.Authentication;
17+
using Microsoft.Azure.Commands.Common.Authentication.Models;
18+
using Microsoft.Azure.Commands.ResourceManager.Common;
19+
using Microsoft.Azure.Management.Storage;
20+
using Microsoft.WindowsAzure.Commands.Common.Storage;
21+
using System;
22+
using System.Management.Automation;
23+
using System.Runtime.InteropServices;
24+
using System.Security;
25+
using System.Security.Permissions;
26+
27+
namespace Microsoft.Azure.Commands.Compute
28+
{
29+
/// <summary>
30+
/// Helper cmdlet to construct instance of AutoBackup settings class
31+
/// </summary>
32+
[Cmdlet(
33+
VerbsCommon.New,
34+
ProfileNouns.VirtualMachineSqlServerAutoBackupConfig,
35+
DefaultParameterSetName = StorageUriParamSetName),
36+
OutputType(
37+
typeof(AutoBackupSettings))]
38+
public class NewAzureRmVMSqlServerAutoBackupConfigCommand : AzureRMCmdlet
39+
{
40+
protected const string StorageContextParamSetName = "StorageContextSqlServerAutoBackup";
41+
protected const string StorageUriParamSetName = "StorageUriSqlServerAutoBackup";
42+
43+
[Parameter(
44+
Mandatory = true,
45+
Position = 0,
46+
ValueFromPipelineByPropertyName = true,
47+
HelpMessage = "The resource group name.")]
48+
[ValidateNotNullOrEmpty]
49+
public string ResourceGroupName { get; set; }
50+
51+
[Parameter(
52+
Mandatory = false,
53+
Position = 1,
54+
ValueFromPipelineByPropertyName = true,
55+
HelpMessage = "Enable Automatic Backup.")]
56+
[ValidateNotNullOrEmpty]
57+
public SwitchParameter Enable { get; set; }
58+
59+
[Parameter(
60+
Mandatory = false,
61+
Position = 2,
62+
ValueFromPipelineByPropertyName = true,
63+
HelpMessage = "Backup Retention period in days.")]
64+
[ValidateNotNullOrEmpty]
65+
public int RetentionPeriodInDays { get; set; }
66+
67+
[Parameter(
68+
Mandatory = false,
69+
Position = 3,
70+
ValueFromPipelineByPropertyName = true,
71+
HelpMessage = "Enable Backup Encryption.")]
72+
[ValidateNotNullOrEmpty]
73+
public SwitchParameter EnableEncryption { get; set; }
74+
75+
[Parameter(
76+
Mandatory = false,
77+
Position = 4,
78+
ValueFromPipelineByPropertyName = false,
79+
HelpMessage = "Certificate password.")]
80+
public SecureString CertificatePassword { get; set; }
81+
82+
[Parameter(
83+
ParameterSetName = StorageContextParamSetName,
84+
Mandatory = false,
85+
Position = 5,
86+
ValueFromPipelineByPropertyName = true,
87+
HelpMessage = "The storage connection context")]
88+
[ValidateNotNullOrEmpty]
89+
public AzureStorageContext StorageContext
90+
{
91+
get;
92+
set;
93+
}
94+
95+
[Parameter(
96+
Mandatory = false,
97+
Position = 4,
98+
ValueFromPipelineByPropertyName = true,
99+
HelpMessage = "The storage uri")]
100+
[ValidateNotNullOrEmpty]
101+
public Uri StorageUri
102+
{
103+
get;
104+
set;
105+
}
106+
107+
[Parameter(
108+
Mandatory = false,
109+
Position = 5,
110+
ValueFromPipelineByPropertyName = true,
111+
HelpMessage = "The storage access key")]
112+
[ValidateNotNullOrEmpty]
113+
public SecureString StorageKey
114+
{
115+
get;
116+
set;
117+
}
118+
119+
[Parameter(
120+
Mandatory = false,
121+
Position = 6,
122+
ValueFromPipelineByPropertyName = true,
123+
HelpMessage = "Backup system databases")]
124+
public SwitchParameter BackupSystemDbs
125+
{
126+
get;
127+
set;
128+
}
129+
130+
[Parameter(
131+
Mandatory = false,
132+
Position = 7,
133+
ValueFromPipelineByPropertyName = true,
134+
HelpMessage = "Backup schedule type, manual or automated")]
135+
[ValidateSet(ValidateSetValues.Manual, ValidateSetValues.Automated, IgnoreCase = true)]
136+
public string BackupScheduleType
137+
{
138+
get;
139+
set;
140+
}
141+
142+
[Parameter(
143+
Mandatory = false,
144+
Position = 8,
145+
ValueFromPipelineByPropertyName = true,
146+
HelpMessage = "Sql Server Full Backup frequency, daily or weekly")]
147+
[ValidateSet(ValidateSetValues.Daily, ValidateSetValues.Weekly, IgnoreCase = true)]
148+
public string FullBackupFrequency
149+
{
150+
get;
151+
set;
152+
}
153+
154+
[Parameter(
155+
Mandatory = false,
156+
Position = 9,
157+
ValueFromPipelineByPropertyName = true,
158+
HelpMessage = "Hour of the day (0-23) when the Sql Server Full Backup should start")]
159+
[ValidateRange(0,23)]
160+
public int FullBackupStartHour
161+
{
162+
get;
163+
set;
164+
}
165+
166+
[Parameter(
167+
Mandatory = false,
168+
Position = 10,
169+
ValueFromPipelineByPropertyName = true,
170+
HelpMessage = "Sql Server Full Backup window in hours")]
171+
[ValidateRange(1, 23)]
172+
public int FullBackupWindowInHours
173+
{
174+
get;
175+
set;
176+
}
177+
178+
[Parameter(
179+
Mandatory = false,
180+
Position = 11,
181+
ValueFromPipelineByPropertyName = true,
182+
HelpMessage = "Sql Server Log Backup frequency, once every 1-60 minutes")]
183+
[ValidateRange(1, 60)]
184+
public int LogBackupFrequencyInMinutes
185+
{
186+
get;
187+
set;
188+
}
189+
190+
/// <summary>
191+
/// Initialzies a new instance of the <see cref="NewAzureVMSqlServerAutoBackupConfigCommand"/> class.
192+
/// </summary>
193+
public NewAzureRmVMSqlServerAutoBackupConfigCommand()
194+
{
195+
}
196+
197+
/// <summary>
198+
/// Creates and returns <see cref="AutoBackupSettings"/> object.
199+
/// </summary>
200+
protected override void ProcessRecord()
201+
{
202+
AutoBackupSettings autoBackupSettings = new AutoBackupSettings();
203+
204+
autoBackupSettings.Enable = (Enable.IsPresent) ? Enable.ToBool() : false;
205+
autoBackupSettings.EnableEncryption = (EnableEncryption.IsPresent) ? EnableEncryption.ToBool() : false;
206+
autoBackupSettings.RetentionPeriod = RetentionPeriodInDays;
207+
208+
switch (ParameterSetName)
209+
{
210+
case StorageContextParamSetName:
211+
autoBackupSettings.StorageUrl = StorageContext.BlobEndPoint;
212+
autoBackupSettings.StorageAccessKey = this.GetStorageKey();
213+
break;
214+
215+
case StorageUriParamSetName:
216+
autoBackupSettings.StorageUrl = (StorageUri == null) ? null : StorageUri.ToString();
217+
autoBackupSettings.StorageAccessKey = (StorageKey == null) ? null : ConvertToUnsecureString(StorageKey);
218+
break;
219+
}
220+
221+
// Check if certificate password was set
222+
autoBackupSettings.Password = (CertificatePassword == null) ? null : ConvertToUnsecureString(CertificatePassword);
223+
224+
autoBackupSettings.BackupSystemDbs = BackupSystemDbs.IsPresent ? BackupSystemDbs.ToBool() : false;
225+
autoBackupSettings.BackupScheduleType = BackupScheduleType;
226+
227+
// Set other Backup schedule settings only if BackUpSchedule type is Manual.
228+
if (!string.IsNullOrEmpty(BackupScheduleType) && string.Equals(BackupScheduleType, ValidateSetValues.Manual, StringComparison.InvariantCultureIgnoreCase)) {
229+
autoBackupSettings.FullBackupFrequency = FullBackupFrequency;
230+
autoBackupSettings.FullBackupStartTime = FullBackupStartHour;
231+
autoBackupSettings.FullBackupWindowHours = FullBackupWindowInHours;
232+
autoBackupSettings.LogBackupFrequency = LogBackupFrequencyInMinutes;
233+
}
234+
235+
WriteObject(autoBackupSettings);
236+
}
237+
238+
protected string GetStorageKey()
239+
{
240+
string storageKey = string.Empty;
241+
string storageName = (this.StorageContext == null) ? null : this.StorageContext.StorageAccountName;
242+
243+
if (!string.IsNullOrEmpty(storageName))
244+
{
245+
var storageClient = AzureSession.ClientFactory.CreateArmClient<StorageManagementClient>(DefaultProfile.Context,
246+
AzureEnvironment.Endpoint.ResourceManager);
247+
248+
var storageAccount = storageClient.StorageAccounts.GetProperties(this.ResourceGroupName, storageName);
249+
250+
if (storageAccount != null)
251+
{
252+
var keys = storageClient.StorageAccounts.ListKeys(this.ResourceGroupName, storageName);
253+
254+
if (keys != null)
255+
{
256+
storageKey = !string.IsNullOrEmpty(keys.Key1) ?
257+
keys.Key1 :
258+
keys.Key2;
259+
}
260+
}
261+
}
262+
263+
return storageKey;
264+
}
265+
266+
/// <summary>
267+
/// convert secure string to regular string
268+
/// $Issue - for ARM cmdlets, check if there is a similair helper class library like Microsoft.WindowsAzure.Commands.ServiceManagement.Helpers
269+
/// </summary>
270+
/// <param name="securePassword"></param>
271+
/// <returns></returns>
272+
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
273+
private static string ConvertToUnsecureString(SecureString securePassword)
274+
{
275+
if (securePassword == null)
276+
{
277+
throw new ArgumentNullException("securePassword");
278+
}
279+
280+
IntPtr unmanagedString = IntPtr.Zero;
281+
try
282+
{
283+
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(securePassword);
284+
return Marshal.PtrToStringUni(unmanagedString);
285+
}
286+
finally
287+
{
288+
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
289+
}
290+
}
291+
}
292+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.Management.Automation;
16+
using Microsoft.Azure.Commands.Compute.Common;
17+
18+
namespace Microsoft.Azure.Commands.Compute
19+
{
20+
/// <summary>
21+
/// Helper cmdlet to construct instance of AutoPatching settings class
22+
/// </summary>
23+
[Cmdlet(
24+
VerbsCommon.New,
25+
ProfileNouns.VirtualMachineSqlServerAutoPatchingConfig),
26+
OutputType(
27+
typeof(AutoPatchingSettings))]
28+
public class NewAzureRmVMSqlServerAutoPatchingConfigCommand : PSCmdlet
29+
{
30+
[Parameter]
31+
public SwitchParameter Enable { get; set; }
32+
33+
[Parameter]
34+
[ValidateSetAttribute(new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Everyday" })]
35+
public string DayOfWeek { get; set; }
36+
37+
[Parameter]
38+
public int MaintenanceWindowStartingHour { get; set; }
39+
40+
[Parameter]
41+
public int MaintenanceWindowDuration { get; set; }
42+
43+
[Parameter]
44+
[ValidateSetAttribute(new string[] { "Important" })]
45+
public string PatchCategory { get; set; }
46+
47+
/// <summary>
48+
/// Creates and returns <see cref="AutoPatchingSettings"/> object.
49+
/// </summary>
50+
protected override void ProcessRecord()
51+
{
52+
AutoPatchingSettings autoPatchingSettings = new AutoPatchingSettings();
53+
54+
autoPatchingSettings.Enable = (Enable.IsPresent) ? Enable.ToBool() : false;
55+
autoPatchingSettings.DayOfWeek = DayOfWeek;
56+
autoPatchingSettings.MaintenanceWindowStartingHour = MaintenanceWindowStartingHour;
57+
autoPatchingSettings.MaintenanceWindowDuration = MaintenanceWindowDuration;
58+
autoPatchingSettings.PatchCategory = PatchCategory;
59+
60+
WriteObject(autoPatchingSettings);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)