1515using Microsoft . Azure . Commands . Compute . Common ;
1616using Microsoft . Azure . Commands . Compute . Models ;
1717using Microsoft . Azure . Management . Compute ;
18- using Microsoft . Azure . Management . Compute . Models ;
1918using System ;
2019using System . Management . Automation ;
20+ using Microsoft . Azure . Management . Compute . Models ;
21+ using System . Globalization ;
2122
2223namespace Microsoft . Azure . Commands . Compute . Extension . AzureDiskEncryption
2324{
@@ -44,16 +45,42 @@ public class GetAzureDiskEncryptionStatusCommand : VirtualMachineExtensionBaseCm
4445 [ ValidateNotNullOrEmpty ]
4546 public string VMName { get ; set ; }
4647
47- private bool IsOsVolumeEncrypted ( VirtualMachine vmParameters )
48+ private OSType GetOSType ( VirtualMachine vmParameters )
4849 {
49- var osVolumeEncryptionSettings = GetOsVolumeEncryptionSettings ( vmParameters ) ;
50- if ( osVolumeEncryptionSettings != null )
50+ if ( vmParameters == null || vmParameters . StorageProfile == null || vmParameters . StorageProfile . OsDisk == null )
5151 {
52- return ( osVolumeEncryptionSettings . Enabled == true
53- && ! string . IsNullOrWhiteSpace ( osVolumeEncryptionSettings . DiskEncryptionKey . SecretUrl ) ) ;
52+ return OSType . Unknown ;
53+ }
54+ else
55+ {
56+ if ( OperatingSystemTypes . Linux == vmParameters . StorageProfile . OsDisk . OsType )
57+ {
58+ return OSType . Linux ;
59+ }
60+ if ( OperatingSystemTypes . Windows == vmParameters . StorageProfile . OsDisk . OsType )
61+ {
62+ return OSType . Windows ;
63+ }
64+ return OSType . Unknown ;
65+ }
66+ }
67+ private EncryptionStatus IsOsVolumeEncrypted ( VirtualMachine vmParameters )
68+ {
69+ OSType osType = this . GetOSType ( vmParameters ) ;
70+ switch ( osType )
71+ {
72+ case OSType . Windows :
73+ if ( GetOsVolumeEncryptionSettings ( vmParameters ) != null )
74+ {
75+ return EncryptionStatus . Encrypted ;
76+ }
77+ else
78+ {
79+ return EncryptionStatus . NotEncrypted ;
80+ }
81+ default :
82+ return EncryptionStatus . Unknown ;
5483 }
55-
56- return false ;
5784 }
5885
5986 private DiskEncryptionSettings GetOsVolumeEncryptionSettings ( VirtualMachine vmParameters )
@@ -66,18 +93,38 @@ private DiskEncryptionSettings GetOsVolumeEncryptionSettings(VirtualMachine vmPa
6693 }
6794 return null ;
6895 }
69- private bool IsAzureDiskEncryptionExtension ( VirtualMachineExtension vmExtension )
96+
97+ private bool IsAzureDiskEncryptionExtension ( OSType osType , VirtualMachineExtension vmExtension )
7098 {
71- if ( ( vmExtension != null ) &&
72- ( vmExtension . Publisher != null ) &&
73- ( vmExtension . VirtualMachineExtensionType != null ) &&
74- ( vmExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) ) &&
75- ( vmExtension . VirtualMachineExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) ) )
99+ switch ( osType )
76100 {
77- return true ;
78- }
101+ case OSType . Windows :
102+ if ( ( vmExtension != null ) &&
103+ ( vmExtension . Publisher != null ) &&
104+ ( vmExtension . VirtualMachineExtensionType != null ) &&
105+ ( vmExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) ) &&
106+ ( vmExtension . VirtualMachineExtensionType . Equals ( AzureDiskEncryptionExtensionContext . ExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) ) )
107+ {
108+ return true ;
109+ }
79110
80- return false ;
111+ return false ;
112+ case OSType . Linux :
113+ if ( ( vmExtension != null ) &&
114+ ( vmExtension . Publisher != null ) &&
115+ ( vmExtension . VirtualMachineExtensionType != null ) &&
116+ ( vmExtension . Publisher . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultPublisher , StringComparison . InvariantCultureIgnoreCase ) ) &&
117+ ( vmExtension . VirtualMachineExtensionType . Equals ( AzureDiskEncryptionExtensionContext . LinuxExtensionDefaultName , StringComparison . InvariantCultureIgnoreCase ) ) )
118+ {
119+ return true ;
120+ }
121+
122+ return false ;
123+ case OSType . Unknown :
124+ return false ;
125+ default :
126+ return false ;
127+ }
81128 }
82129
83130 private bool DataVolumeInExtensionConfig ( AzureDiskEncryptionExtensionContext adeExtension )
@@ -108,32 +155,39 @@ private bool ExtensionProvisioningSucceeded(AzureDiskEncryptionExtensionContext
108155 return false ;
109156 }
110157
111- private bool AreDataVolumesEncrypted ( VirtualMachine vmParameters )
158+ private EncryptionStatus AreDataVolumesEncrypted ( VirtualMachine vmParameters )
112159 {
113160 if ( vmParameters == null || vmParameters . Resources == null )
114161 {
115- return false ;
162+ return EncryptionStatus . Unknown ;
116163 }
117164
165+ OSType osType = this . GetOSType ( vmParameters ) ;
118166 foreach ( VirtualMachineExtension vmExtension in vmParameters . Resources )
119167 {
120- if ( IsAzureDiskEncryptionExtension ( vmExtension ) )
168+ switch ( osType )
121169 {
122- AzureDiskEncryptionExtensionContext adeExtension = new AzureDiskEncryptionExtensionContext ( vmExtension . ToPSVirtualMachineExtension ( this . ResourceGroupName ) ) ;
123- if ( DataVolumeInExtensionConfig ( adeExtension ) )
124- {
125- if ( adeExtension . EncryptionOperation . Equals ( AzureDiskEncryptionExtensionConstants . enableEncryptionOperation , StringComparison . InvariantCultureIgnoreCase ) )
170+ case OSType . Windows :
171+ case OSType . Linux :
172+ if ( IsAzureDiskEncryptionExtension ( osType , vmExtension ) )
126173 {
127- if ( ExtensionProvisioningSucceeded ( adeExtension ) )
174+ AzureDiskEncryptionExtensionContext adeExtension = new AzureDiskEncryptionExtensionContext ( vmExtension . ToPSVirtualMachineExtension ( this . ResourceGroupName ) ) ;
175+ if ( DataVolumeInExtensionConfig ( adeExtension ) )
128176 {
129- return true ;
177+ if ( ExtensionProvisioningSucceeded ( adeExtension ) )
178+ {
179+ return EncryptionStatus . Encrypted ;
180+ }
130181 }
131182 }
132- }
183+ break ;
184+ case OSType . Unknown :
185+ return EncryptionStatus . Unknown ;
186+ default :
187+ return EncryptionStatus . Unknown ;
133188 }
134189 }
135-
136- return false ;
190+ return EncryptionStatus . NotEncrypted ;
137191 }
138192
139193 public override void ExecuteCmdlet ( )
@@ -144,19 +198,40 @@ public override void ExecuteCmdlet()
144198 {
145199 VirtualMachine vmParameters = ( this . ComputeClient . ComputeManagementClient . VirtualMachines . Get ( this . ResourceGroupName , this . VMName ) ) ;
146200
147- bool osVolumeEncrypted = IsOsVolumeEncrypted ( vmParameters ) ;
201+ EncryptionStatus osVolumeEncrypted = IsOsVolumeEncrypted ( vmParameters ) ;
148202 DiskEncryptionSettings osVolumeEncryptionSettings = GetOsVolumeEncryptionSettings ( vmParameters ) ;
149- bool dataVolumesEncrypted = AreDataVolumesEncrypted ( vmParameters ) ;
203+ EncryptionStatus dataVolumesEncrypted = AreDataVolumesEncrypted ( vmParameters ) ;
150204
151- AzureDiskEncryptionStatusContext encryptionStatus = new AzureDiskEncryptionStatusContext
205+ OSType osType = GetOSType ( vmParameters ) ;
206+ switch ( osType )
152207 {
153- OsVolumeEncrypted = osVolumeEncrypted ,
154- OsVolumeEncryptionSettings = osVolumeEncryptionSettings ,
155- DataVolumesEncrypted = dataVolumesEncrypted
156- } ;
157- WriteObject ( encryptionStatus ) ;
208+ case OSType . Windows :
209+ AzureDiskEncryptionStatusContext encryptionStatus = new AzureDiskEncryptionStatusContext
210+ {
211+ OsVolumeEncrypted = osVolumeEncrypted ,
212+ OsVolumeEncryptionSettings = osVolumeEncryptionSettings ,
213+ DataVolumesEncrypted = dataVolumesEncrypted
214+ } ;
215+ WriteObject ( encryptionStatus ) ;
216+ break ;
217+ case OSType . Linux :
218+ AzureDiskEncryptionStatusLinuxContext encryptionStatusLinux = new AzureDiskEncryptionStatusLinuxContext
219+ {
220+ OsVolumeEncrypted = osVolumeEncrypted ,
221+ OsVolumeEncryptionSettings = null ,
222+ DataVolumesEncrypted = dataVolumesEncrypted ,
223+ DataVolumeEncryptionSettings = osVolumeEncryptionSettings
224+ } ;
225+ WriteObject ( encryptionStatusLinux ) ;
226+ break ;
227+ case OSType . Unknown :
228+ ThrowTerminatingError ( new ErrorRecord ( new ApplicationException ( string . Format ( CultureInfo . CurrentUICulture , "OS type unknown." ) ) ,
229+ "InvalidResult" ,
230+ ErrorCategory . InvalidResult ,
231+ null ) ) ;
232+ break ;
233+ }
158234 } ) ;
159-
160235 }
161236 }
162237}
0 commit comments