Skip to content

SQLVM extension Powershell cmdlet updates - December 2014 #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
<Compile Include="IaaS\Extensions\MicrosoftAntimalware\SetAzureVMMicrosoftAntimalwareExtension.cs" />
<Compile Include="IaaS\Extensions\MicrosoftAntimalware\VirtualMachineMicrosoftAntimalwareExtensionCmdletBase.cs" />
<Compile Include="IaaS\Extensions\MicrosoftAntimalware\VirtualMachineMicrosoftAntimalwareExtensionContext.cs" />
<Compile Include="IaaS\Extensions\SqlServer\AzureVMSqlServerAutoTelemetrySettings.cs" />
<Compile Include="IaaS\Extensions\SqlServer\GetAzureVMSqlServerExtension.cs" />
<Compile Include="IaaS\Extensions\SqlServer\NewAzureVMSqlServerAutoBackupConfig.cs" />
<Compile Include="IaaS\Extensions\SqlServer\NewAzureVMSqlServerAutoPatchingConfig.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public enum AzureVMSqlServerAutoPatchingPatchCategoryEnum
/// <summary>
/// Important patches
/// </summary>
Important = 0,

/// <summary>
/// Optional patches
/// </summary>
Optional = 1
Important = 1,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions
/// </summary>
public class AutoPatchingSettings
{
private const string Important = "important";
private const string Optional = "optional";

/// <summary>
/// Enable / Disable auto patching
/// </summary>
Expand All @@ -36,53 +33,15 @@ public class AutoPatchingSettings
/// Maintainance Windows Start hour ( 0 to 23 )
/// </summary>
public int MaintenanceWindowStartingHour { get; set; }

/// <summary>
/// Maintainance window duration in minutes
/// </summary>
public int MaintenanceWindowDuration { get; set; }

/// <summary>
/// pathc category returned as string
/// </summary>
public string PatchCategory
{
get
{
return this.patchCategory.ToString("G");
}
}

private AzureVMSqlServerAutoPatchingPatchCategoryEnum patchCategory = AzureVMSqlServerAutoPatchingPatchCategoryEnum.Unknown;

public void UpdatePatchingCategory(AzureVMSqlServerAutoPatchingPatchCategoryEnum category)
{
this.patchCategory = category;
}

/// <summary>
/// Update patching category enum
/// Patch category returned as string
/// </summary>
/// <param name="category"></param>
public void UpdatePatchingCategory(string category)
{
if (!string.IsNullOrEmpty(category))
{
switch (category.ToLower())
{
case Important:
this.patchCategory = AzureVMSqlServerAutoPatchingPatchCategoryEnum.Important;
break;

case Optional:
this.patchCategory = AzureVMSqlServerAutoPatchingPatchCategoryEnum.Optional;
break;

default:
this.patchCategory = AzureVMSqlServerAutoPatchingPatchCategoryEnum.Unknown;
break;
}
}
}
public string PatchCategory { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Security;

namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions
{
/// <summary>
/// AutoTelemetry settings to configure telemetry collection on SQL VM
/// </summary>
public class AutoTelemetrySettings
{
/// <summary>
/// The name of the region the VM is running in.
/// </summary>
public string Region { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ public class SqlServerPublicSettings
/// Auto-backup settings
/// </summary>
public AutoBackupSettings AutoBackupSettings { get; set; }

/// <summary>
/// Auto-telemetry settings
/// </summary>
public AutoTelemetrySettings AutoTelemetrySettings { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private VirtualMachineSqlServerExtensionContext GetExtensionContext(ResourceExte
PrivateConfiguration = SecureStringHelper.GetSecureString(PrivateConfiguration),
RoleName = VM.GetInstance().RoleName,
};

// gather extension status messages
List<string> statusMessageList = new List<string>();

Expand Down Expand Up @@ -155,7 +155,7 @@ private VirtualMachineSqlServerExtensionContext GetExtensionContext(ResourceExte
NSM.DeploymentSlot.Production);
}
catch (CloudException e)
{
{
if (e.Response.StatusCode != HttpStatusCode.NotFound)
{
throw;
Expand Down Expand Up @@ -188,13 +188,13 @@ private VirtualMachineSqlServerExtensionContext GetExtensionContext(ResourceExte
private AutoPatchingSettings DeSerializeAutoPatchingSettings(string category, string input)
{
AutoPatchingSettings aps = new AutoPatchingSettings();

if (!string.IsNullOrEmpty(input))
{
try
{
aps = JsonConvert.DeserializeObject<AutoPatchingSettings>(input);
aps.UpdatePatchingCategory(this.ResolvePatchCategoryStringforPowerShell(aps.PatchCategory));
aps.PatchCategory = this.ResolvePatchCategoryStringforPowerShell(aps.PatchCategory);
}
catch (JsonReaderException jre)
{
Expand Down Expand Up @@ -231,28 +231,22 @@ private AutoBackupSettings DeSerializeAutoBackupSettings(string category, string
/// <summary>
/// Map strings Auto-patching public settings -> Powershell API
/// "WindowsMandatoryUpdates" -> "Important"
/// "MicrosoftOptionalUpdates" -> "Optional"
/// </summary>
/// <param name="patchCategory"></param>
/// <returns></returns>
private AzureVMSqlServerAutoPatchingPatchCategoryEnum ResolvePatchCategoryStringforPowerShell(string category)
private string ResolvePatchCategoryStringforPowerShell(string category)
{
AzureVMSqlServerAutoPatchingPatchCategoryEnum patchCategory = AzureVMSqlServerAutoPatchingPatchCategoryEnum.Important;
string patchCategory = string.Empty;

if (!string.IsNullOrEmpty(category))
{
switch (category.ToLower())
{
case "windowsmandatoryupdates":
patchCategory = AzureVMSqlServerAutoPatchingPatchCategoryEnum.Important;
break;

case "microsoftoptionalupdates":
patchCategory = AzureVMSqlServerAutoPatchingPatchCategoryEnum.Optional;
patchCategory = AzureVMSqlServerAutoPatchingPatchCategoryEnum.Important.ToString("G");
break;

default:
patchCategory = AzureVMSqlServerAutoPatchingPatchCategoryEnum.Unknown;
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions
/// <summary>
/// Helper cmdlet to construct instance of AutoPatching settings class
/// </summary>
[Cmdlet(
VerbsCommon.New,
AzureVMSqlServerAutoPatchingConfigNoun),
OutputType(
typeof(AutoPatchingSettings))]
[Cmdlet(
VerbsCommon.New,
AzureVMSqlServerAutoPatchingConfigNoun),
OutputType(
typeof(AutoPatchingSettings))]
public class NewAzureVMSqlServerAutoPatchingConfigCommand : PSCmdlet
{
protected const string AzureVMSqlServerAutoPatchingConfigNoun = "AzureVMSqlServerAutoPatchingConfig";
Expand All @@ -47,7 +47,7 @@ public class NewAzureVMSqlServerAutoPatchingConfigCommand : PSCmdlet
public int MaintenanceWindowDuration { get; set; }

[Parameter]
[ValidateSetAttribute(new string[] { "Important", "Optional" })]
[ValidateSetAttribute(new string[] { "Important" })]
public string PatchCategory { get; set; }

/// <summary>
Expand All @@ -68,7 +68,7 @@ protected override void ProcessRecord()
autoPatchingSettings.DayOfWeek = DayOfWeek;
autoPatchingSettings.MaintenanceWindowStartingHour = MaintenanceWindowStartingHour;
autoPatchingSettings.MaintenanceWindowDuration = MaintenanceWindowDuration;
autoPatchingSettings.UpdatePatchingCategory(PatchCategory);
autoPatchingSettings.PatchCategory = PatchCategory;

WriteObject(autoPatchingSettings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.WindowsAzure.Commands.Common.Storage;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Properties;
using System.Net;

namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions
{
Expand All @@ -33,8 +34,8 @@ namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions
typeof(IPersistentVM))]
public class SetAzureVMSqlServerExtensionCommand : VirtualMachineSqlServerExtensionCmdletBase
{
protected const string EnableExtensionParamSetName = "EnableSqlServerExtension";
protected const string DisableSqlServerExtensionParamSetName = "DisableSqlServerExtension";
protected const string EnableExtensionParamSetName = "EnableSqlServerExtension";
protected const string DisableSqlServerExtensionParamSetName = "DisableSqlServerExtension";
protected const string UninstallSqlServerExtensionParamSetName = "UninstallSqlServerExtension";

[Parameter(
Expand Down Expand Up @@ -105,10 +106,58 @@ protected override void ValidateParameters()
{
base.ValidateParameters();
this.ReferenceName = string.IsNullOrEmpty(this.ReferenceName) ? ExtensionDefaultName : this.ReferenceName;

this.SetupAutoTelemetrySettings();
this.PublicConfiguration = GetPublicConfiguration();
this.PrivateConfiguration = GetPrivateConfiguration();
this.Version = this.Version ?? ExtensionDefaultVersion;
}

private void SetupAutoTelemetrySettings()
{
if (this.AutoTelemetrySettings == null || string.IsNullOrEmpty(this.AutoTelemetrySettings.Region))
{
foreach (var hs in this.ComputeClient.HostedServices.List().HostedServices)
{
try
{
var deployment = this.ComputeClient.Deployments.GetBySlot(hs.ServiceName, Management.Compute.Models.DeploymentSlot.Production);
if (deployment != null)
{
var role = deployment.RoleInstances.FirstOrDefault(r => r.RoleName == VM.GetInstance().RoleName);

string location = String.Empty;
if (role != null)
{
if (null != hs.Properties)
{
if (!string.IsNullOrEmpty(hs.Properties.Location))
{
location = hs.Properties.Location;
}
else
{
if (!string.IsNullOrEmpty(hs.Properties.AffinityGroup))
{
location = this.ManagementClient.AffinityGroups.Get(hs.Properties.AffinityGroup).Location;
}
}
}

this.AutoTelemetrySettings = new AutoTelemetrySettings() { Region = location };
WriteVerboseWithTimestamp("VM Location:" + location);
break;
}
}
}
catch (CloudException e)
{
if (e.Response.StatusCode != HttpStatusCode.NotFound)
{
throw;
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class VirtualMachineSqlServerExtensionCmdletBase : VirtualMachineExtensio
/// <summary>
/// Extension's default version
/// </summary>
protected const string ExtensionDefaultVersion = "1.0";
protected const string ExtensionDefaultVersion = "1.*";

/// <summary>
/// value of Auto-patching settings object that can be set by derived classes
Expand All @@ -49,6 +49,11 @@ public class VirtualMachineSqlServerExtensionCmdletBase : VirtualMachineExtensio
/// </summary>
public virtual AutoBackupSettings AutoBackupSettings { get; set; }

/// <summary>
/// value of Auto-telemetry settings object that can be set by derived classes
/// </summary>
public virtual AutoTelemetrySettings AutoTelemetrySettings { get; set; }

/// <summary>
/// Sets extension's publisher and name
/// </summary>
Expand All @@ -68,7 +73,8 @@ protected string GetPublicConfiguration()
new SqlServerPublicSettings
{
AutoPatchingSettings = this.AutoPatchingSettings,
AutoBackupSettings = this.AutoBackupSettings
AutoBackupSettings = this.AutoBackupSettings,
AutoTelemetrySettings = this.AutoTelemetrySettings
}));
}

Expand All @@ -81,7 +87,7 @@ protected string GetPrivateConfiguration()
return JsonUtilities.TryFormatJson(JsonConvert.SerializeObject(
new SqlServerPrivateSettings
{
StorageUrl = (this.AutoBackupSettings == null)? string.Empty: this.AutoBackupSettings.StorageUrl,
StorageUrl = (this.AutoBackupSettings == null) ? string.Empty : this.AutoBackupSettings.StorageUrl,
StorageAccessKey = (this.AutoBackupSettings == null) ? string.Empty : this.AutoBackupSettings.StorageAccessKey,
Password = (this.AutoBackupSettings == null) ? string.Empty : this.AutoBackupSettings.Password
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33136,7 +33136,7 @@ PS C:\&gt; Get-AzureVM -ServiceName &quot;ContosoService03&quot; -Name &quot;Con
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="named">
<maml:name>PatchCategory</maml:name>
<maml:description>
<maml:para>Defines if recommended updates should be included.</maml:para>
<maml:para>Defines if Important updates should be included.</maml:para>
</maml:description>
<command:parameterValue required="true" variableLength="false">string</command:parameterValue>
</command:parameter>
Expand Down Expand Up @@ -33195,7 +33195,7 @@ PS C:\&gt; Get-AzureVM -ServiceName &quot;ContosoService03&quot; -Name &quot;Con
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="named">
<maml:name>PatchCategory</maml:name>
<maml:description>
<maml:para>Defines if recommended updates should be included.</maml:para>
<maml:para>Defines if important updates should be included.</maml:para>
</maml:description>
<command:parameterValue required="true" variableLength="false">string</command:parameterValue>
<dev:type>
Expand Down Expand Up @@ -33261,14 +33261,14 @@ PS C:\&gt; Get-AzureVM -ServiceName &quot;ContosoService03&quot; -Name &quot;Con
<maml:para></maml:para>
</maml:introduction>
<dev:code>
C:\PS&gt; $aps = New-AzureVMSqlServerAutoPatchingConfig -Enable -DayOfWeek &quot;Thursday&quot; -MaintenanceWindowStartingHour 11 -MaintenanceWindowDuration 120 -PatchCategory &quot;Optional&quot;
C:\PS&gt; $aps = New-AzureVMSqlServerAutoPatchingConfig -Enable -DayOfWeek &quot;Thursday&quot; -MaintenanceWindowStartingHour 11 -MaintenanceWindowDuration 120 -PatchCategory &quot;Important&quot;

PS C:\&gt; $aps
Enable : True
DayOfWeek : Thursday
MaintenanceWindowStartingHour : 11
MaintenanceWindowDuration : 120
PatchCategory : Optional
PatchCategory : Important
</dev:code>
<dev:remarks>
<maml:para>This command creates configuration object that can be used to configure automatic patching using Set-AzureVMSqlServerExtension</maml:para>
Expand Down