Skip to content

Commit

Permalink
Filter Windows instances by license or guest feature
Browse files Browse the repository at this point in the history
Old instances might not have a proper guest OS feature,
fall back to filtering by license in this case.

Change-Id: I5131b9b73fd08cefecbf77b988d720b33cdc34c0
  • Loading branch information
Johannes Passing authored and jpassing committed Nov 11, 2019
1 parent 9183d8d commit 6ad1c66
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace Google.Solutions.CloudIap.Plugin.Integration
/// </summary>
internal class ComputeEngineAdapter
{
private const string WindowsCloudLicenses = "https://www.googleapis.com/compute/v1/projects/windows-cloud/global/licenses";

private readonly ComputeService service;

public static ComputeEngineAdapter Create(ICredential credential)
Expand Down Expand Up @@ -139,14 +141,31 @@ public Task<NetworkCredential> ResetWindowsUserAsync(
return this.service.Instances.ResetWindowsUserAsync(instanceRef, username);
}

public static bool IsWindowsInstance(Instance instance)
private static bool IsWindowsInstanceByGuestOsFeature(Instance instance)
{
// For an instance to be a valid Windows instance, at least one of the disks
// (the boot disk) has to be marked as "WINDOWS".
// Note that older disks might lack this feature.
return instance.Disks
.Where(d => d.GuestOsFeatures != null)
.SelectMany(d => d.GuestOsFeatures)
.Any(f => f.Type == "WINDOWS");
}

private static bool IsWindowsInstanceByLicense(Instance instance)
{
// For an instance to be a valid Windows instance, at least one of the disks
// has to have an associated Windows license. This is also true for
// BYOL'ed instances.
return instance.Disks
.SelectMany(d => d.Licenses)
.Any(l => l.StartsWith(WindowsCloudLicenses));
}

public static bool IsWindowsInstance(Instance instance)
{
return IsWindowsInstanceByGuestOsFeature(instance) ||
IsWindowsInstanceByLicense(instance);
}
}
}

0 comments on commit 6ad1c66

Please sign in to comment.