Skip to content

Commit c4dac2f

Browse files
author
NimishaS
committed
Fetching latest extension version
1 parent d234d67 commit c4dac2f

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/ResourceManager/Compute/Commands.Compute/Extension/Chef/SetAzureVMChefExtension.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public class SetAzureVMChefExtensionCommand : VirtualMachineExtensionBaseCmdlet
5858

5959
[Alias("HandlerVersion", "Version")]
6060
[Parameter(
61-
Mandatory = true,
6261
Position = 9,
6362
ValueFromPipelineByPropertyName = true,
6463
HelpMessage = "The extension version.")]
@@ -224,7 +223,7 @@ private Hashtable PublicConfiguration
224223
bool IsBootstrapOptionsEmpty = string.IsNullOrEmpty(this.BootstrapOptions);
225224
string AutoUpdateChefClient = this.AutoUpdateChefClient.IsPresent ? "true" : "false";
226225
string DeleteChefConfig = this.DeleteChefConfig.IsPresent ? "true" : "false";
227-
string BootstrapVersion = this.BootstrapVersion;
226+
string BootstrapVersion = string.IsNullOrEmpty(this.BootstrapVersion) ? "" : this.BootstrapVersion;
228227
string UninstallChefClient = this.UninstallChefClient.IsPresent ? "true" : "false";
229228

230229
//Cases handled:
@@ -389,15 +388,30 @@ private void SetDefault()
389388
this.Name = ExtensionDefaultName;
390389
}
391390

392-
//Uncomment this when GetLatestChefExtensionVersion() is implemented
393-
//this.TypeHandlerVersion = this.TypeHandlerVersion ?? GetLatestChefExtensionVersion();
391+
this.TypeHandlerVersion = this.TypeHandlerVersion ?? GetLatestChefExtensionVersion();
394392
}
395393

396394
private string GetLatestChefExtensionVersion()
397-
{
398-
//Right now chef extension's major version is freezed as 1210.
399-
//Todo: Implement proper logic to fetch the current major.minor version number
400-
return "1210.12";
395+
{
396+
var result = ComputeClient.ComputeManagementClient.VirtualMachineExtensionImages.ListVersionsWithHttpMessagesAsync(this.Location, ExtensionDefaultPublisher, this.Name).GetAwaiter().GetResult();
397+
398+
var images = from r in result.Body
399+
select new PSVirtualMachineExtensionImage
400+
{
401+
RequestId = result.RequestId,
402+
StatusCode = result.Response.StatusCode,
403+
Id = r.Id,
404+
Location = r.Location,
405+
Version = r.Name,
406+
PublisherName = ExtensionDefaultPublisher,
407+
Type = this.Name
408+
};
409+
410+
var maxVersion = images.Max(extension => extension.Version);
411+
string[] separators = { "." };
412+
string[] splitVersion = maxVersion.Split(separators, StringSplitOptions.None);
413+
string majorMinorVersion = splitVersion[0] + "." + splitVersion[1];
414+
return majorMinorVersion;
401415
}
402416

403417
private void ValidateParameters()

0 commit comments

Comments
 (0)