public async Task <Image> GetImage(ImageLocator image, CancellationToken cancellationToken) { try { if (image.Name.StartsWith("family/")) { return(await this.service.Images .GetFromFamily(image.ProjectId, image.Name.Substring(7)) .ExecuteAsync(cancellationToken).ConfigureAwait(false)); } else { return(await this.service.Images .Get(image.ProjectId, image.Name) .ExecuteAsync(cancellationToken).ConfigureAwait(false)); } } catch (Exception e) when( e.Unwrap() is GoogleApiException apiEx && apiEx.Error != null && apiEx.Error.Code == 404) { throw new ResourceNotFoundException($"Image {image} not found", e); } catch (Exception e) when( e.Unwrap() is GoogleApiException apiEx && apiEx.Error != null && (apiEx.Error.Code == 403)) { throw new ResourceAccessDeniedException($"Access to {image} denied", e); } }
public void WhenReferencesAreEquivalent_ThenGetHasCodeIsSame() { var ref1 = new ImageLocator("proj", "image-1"); var ref2 = new ImageLocator("proj", "image-1"); Assert.AreEqual(ref1.GetHashCode(), ref2.GetHashCode()); }
internal InsertInstanceEvent(LogRecord logRecord) : base(logRecord) { Debug.Assert(IsInsertInstanceEvent(logRecord)); var request = logRecord.ProtoPayload.Request; if (request != null) { var disks = request["disks"]; if (disks != null) { foreach (var disk in ((JArray)disks)) { if (disk["boot"] != null && (bool)disk["boot"]) { var initializeParams = disk["initializeParams"]; if (initializeParams != null) { this.Image = ImageLocator.FromString( initializeParams["sourceImage"].Value <string>()); } } } } } }
//--------------------------------------------------------------------- // Ctor //--------------------------------------------------------------------- private InstanceHistoryBuilder( ulong instanceId, InstanceLocator reference, ImageLocator image, InstanceState state, DateTime?lastSeen, Tenancies tenancy) { Debug.Assert(!tenancy.IsFlagCombination()); if (instanceId == 0) { throw new ArgumentException("Instance ID cannot be 0"); } this.InstanceId = instanceId; this.reference = reference; this.image = image; this.lastStoppedOn = lastSeen; if (state == InstanceState.Running) { Debug.Assert(tenancy != Tenancies.Unknown); Debug.Assert(lastSeen != null); // Add a synthetic placement. AddPlacement(new InstancePlacement(tenancy, null, lastSeen.Value, lastSeen.Value)); } }
public void AddLicenseAnnotation( ImageLocator image, OperatingSystemTypes osType, LicenseTypes licenseType) { this.LicenseAnnotations[image.ToString()] = new ImageAnnotation(osType, licenseType); }
public void WhenCreatedFromPath_ThenToStringReturnsPath() { var path = "projects/project-1/global/images/image-1"; Assert.AreEqual( path, ImageLocator.FromString(path).ToString()); }
public void WhenUsingBetaApi_FromStringReturnsObject() { var ref1 = ImageLocator.FromString( "https://compute.googleapis.com/compute/beta/projects/eip-images/global/images/debian-9-drawfork-v20191004"); Assert.AreEqual("images", ref1.ResourceType); Assert.AreEqual("debian-9-drawfork-v20191004", ref1.Name); Assert.AreEqual("eip-images", ref1.ProjectId); }
public void WhenQualifiedByComputeGoogleapisHost_FromStringReturnsObject() { var ref1 = ImageLocator.FromString( "https://compute.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-9"); Assert.AreEqual("images", ref1.ResourceType); Assert.AreEqual("family/debian-9", ref1.Name); Assert.AreEqual("debian-cloud", ref1.ProjectId); }
public void WhenQualifiedByGoogleapisHost_FromStringReturnsObject() { var ref1 = ImageLocator.FromString( "https://www.googleapis.com/compute/v1/projects/windows-cloud/global/images/windows-server-core"); Assert.AreEqual("images", ref1.ResourceType); Assert.AreEqual("windows-server-core", ref1.Name); Assert.AreEqual("windows-cloud", ref1.ProjectId); }
public void WhenResourceNameCotainsSlash_FromStringReturnsObject() { var ref1 = ImageLocator.FromString( "projects/debian-cloud/global/images/family/debian-9"); Assert.AreEqual("images", ref1.ResourceType); Assert.AreEqual("family/debian-9", ref1.Name); Assert.AreEqual("debian-cloud", ref1.ProjectId); }
public void WhenPathIsValid_FromStringReturnsObject() { var ref1 = ImageLocator.FromString( "projects/project-1/global/images/image-1"); Assert.AreEqual("images", ref1.ResourceType); Assert.AreEqual("image-1", ref1.Name); Assert.AreEqual("project-1", ref1.ProjectId); }
public void WhenCreatedFromUrl_ThenToStringReturnsPath() { var path = "projects/project-1/global/images/image-1"; Assert.AreEqual( path, ImageLocator.FromString( "https://www.googleapis.com/compute/v1/" + path).ToString()); }
public void WhenPathInvalid_FromStringThrowsArgumentException() { Assert.Throws <ArgumentException>(() => ImageLocator.FromString( "projects/project-1/notglobal/images/image-1")); Assert.Throws <ArgumentException>(() => ImageLocator.FromString( "/project-1/global/images/image-1")); Assert.Throws <ArgumentException>(() => ImageLocator.FromString( "/")); }
public void WhenReferencesAreNotEquivalent_ThenEqualsReturnsFalse() { var ref1 = new ImageLocator("proj-1", "image-1"); var ref2 = new ImageLocator("proj-2", "image-1"); Assert.IsFalse(ref1.Equals(ref2)); Assert.IsFalse(ref1.Equals((object)ref2)); Assert.IsFalse(ref1 == ref2); Assert.IsTrue(ref1 != ref2); }
public void WhenReferencesAreSame_ThenEqualsReturnsTrue() { var ref1 = new ImageLocator("proj", "image-1"); var ref2 = ref1; Assert.IsTrue(ref1.Equals(ref2)); Assert.IsTrue(ref1.Equals((object)ref2)); Assert.IsTrue(ref1 == ref2); Assert.IsFalse(ref1 != ref2); }
public void AddExistingInstances( IEnumerable <Instance> instances, IEnumerable <Disk> disks, string projectId) { using (TraceSources.IapDesktop.TraceMethod().WithParameters(projectId)) { // // NB. Instances.list returns the disks associated with each // instance, but lacks the information about the source image. // Therefore, we load disks first and then join the data. // var sourceImagesByDisk = disks .EnsureNotNull() .ToDictionary(d => d.SelfLink, d => d.SourceImage); TraceSources.IapDesktop.TraceVerbose("Found {0} existing disks", sourceImagesByDisk.Count()); TraceSources.IapDesktop.TraceVerbose("Found {0} existing instances", instances.Count()); foreach (var instance in instances) { TraceSources.IapDesktop.TraceVerbose("Adding {0}", instance.Id); var bootDiskUrl = instance.Disks .EnsureNotNull() .Where(d => d.Boot != null && d.Boot.Value) .EnsureNotNull() .Select(d => d.Source) .EnsureNotNull() .FirstOrDefault(); ImageLocator image = null; if (bootDiskUrl != null && sourceImagesByDisk.TryGetValue(bootDiskUrl, out string imageUrl) && imageUrl != null) { image = ImageLocator.FromString(imageUrl); } AddExistingInstance( (ulong)instance.Id.Value, new InstanceLocator( projectId, ShortZoneIdFromUrl(instance.Zone), instance.Name), image, instance.Status == "RUNNING" ? InstanceState.Running : InstanceState.Terminated, DateTime.Now, instance.Scheduling.NodeAffinities != null && instance.Scheduling.NodeAffinities.Any() ? Tenancies.SoleTenant : Tenancies.Fleet); } } }
public void TestEqualsNull() { var ref1 = new ImageLocator("proj", "image-1"); Assert.IsFalse(ref1.Equals(null)); Assert.IsFalse(ref1.Equals((object)null)); Assert.IsFalse(ref1 == null); Assert.IsFalse(null == ref1); Assert.IsTrue(ref1 != null); Assert.IsTrue(null != ref1); }
internal InstanceHistory( [JsonProperty("id")] ulong instanceId, [JsonProperty("vm")] InstanceLocator reference, [JsonProperty("state")] InstanceHistoryState state, [JsonProperty("image")] ImageLocator image, [JsonProperty("placements")] IEnumerable <InstancePlacement> placements ) { this.InstanceId = instanceId; this.Reference = reference; this.State = state; this.Image = image; this.Placements = placements; }
private ReportArchive CreateSet(ImageLocator image) { return(new ReportArchive( new InstanceSetHistory( new DateTime(2019, 12, 1, 0, 0, 0, DateTimeKind.Utc), new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc), new[] { new InstanceHistory( 188550847350222232, new InstanceLocator("project-1", "us-central1-a", "instance-1"), InstanceHistoryState.Complete, image, Enumerable.Empty <InstancePlacement>()) }))); }
public void AddExistingInstance( ulong instanceId, InstanceLocator reference, ImageLocator image, InstanceState state, DateTime lastSeen, Tenancies tenancy) { Debug.Assert(!tenancy.IsFlagCombination()); this.instanceBuilders[instanceId] = InstanceHistoryBuilder.ForExistingInstance( instanceId, reference, image, state, lastSeen, tenancy); }
internal static InstanceHistoryBuilder ForExistingInstance( ulong instanceId, InstanceLocator reference, ImageLocator image, InstanceState state, DateTime lastSeen, Tenancies tenancy) { Debug.Assert(!tenancy.IsFlagCombination()); Debug.Assert(state != InstanceState.Deleted); return(new InstanceHistoryBuilder( instanceId, reference, image, state, lastSeen, tenancy)); }
//--------------------------------------------------------------------- // Lifecycle events that construct the history. //--------------------------------------------------------------------- public void OnInsert(DateTime date, InstanceLocator reference, ImageLocator image) { Debug.Assert(date <= this.lastEventDate); this.lastEventDate = date; // NB. We might get multiple calls for a single instance, each providing some, but // potentially not all information. if (this.reference == null) { this.reference = reference; } if (this.image == null) { this.image = image; } // Register Fleet placement - this might be merged with an existing // SoleTenant placement if there has been one registerd before. AddPlacement(Tenancies.Fleet, null, date); }
//--------------------------------------------------------------------- // Ctor //--------------------------------------------------------------------- private InstanceHistoryBuilder( ulong instanceId, InstanceLocator reference, ImageLocator image, InstanceState state, DateTime?lastSeen, Tenancies tenancy, string serverId, NodeTypeLocator nodeType) { Debug.Assert(!tenancy.IsFlagCombination()); Debug.Assert(tenancy == Tenancies.SoleTenant || (serverId == null && nodeType == null)); if (instanceId == 0) { throw new ArgumentException("Instance ID cannot be 0"); } this.InstanceId = instanceId; this.reference = reference; this.image = image; this.lastStoppedOn = lastSeen; if (state == InstanceState.Running) { Debug.Assert(tenancy != Tenancies.Unknown); Debug.Assert(lastSeen != null); AddPlacement(new InstancePlacement( tenancy, serverId, nodeType, lastSeen.Value, lastSeen.Value)); } }
public void WhenReadingSample3_ThenHistoryIsRestored() { var set = BuildHistoryFromResource("instance-3.json"); Assert.AreEqual(1, set.Instances.Count()); var instance = set.Instances.First(); Assert.AreEqual( ImageLocator.FromString("projects/project-1/global/images/windows-server"), instance.Image); Assert.AreEqual(2, instance.Placements.Count()); var firstPlacement = instance.Placements.First(); Assert.AreEqual("413db7b32a208e7ccb4ee62acedee725", firstPlacement.ServerId); Assert.AreEqual(Tenancies.SoleTenant, firstPlacement.Tenancy); // Insert.. Assert.AreEqual(DateTime.Parse("2020-05-05T08:31:40.864Z").ToUniversalTime(), firstPlacement.From); // ..till TerminateOnHostMaintenance. Assert.AreEqual(DateTime.Parse("2020-05-06T16:10:46.781Z").ToUniversalTime(), firstPlacement.To); var secondPlacement = instance.Placements.Last(); Assert.AreEqual("413db7b32a208e7ccb4ee62acedee725", secondPlacement.ServerId); Assert.AreEqual(Tenancies.SoleTenant, secondPlacement.Tenancy); // Insert.. Assert.AreEqual(DateTime.Parse("2020-05-06T16:36:01.441Z").ToUniversalTime(), secondPlacement.From); // ..till GuestTerminate. Assert.AreEqual(DateTime.Parse("2020-05-06T17:39:34.635Z").ToUniversalTime(), secondPlacement.To); }
public void WhenReadingSample2_ThenHistoryIsRestored() { var set = BuildHistoryFromResource("instance-2.json"); Assert.AreEqual(1, set.Instances.Count()); var instance = set.Instances.First(); Assert.AreEqual( ImageLocator.FromString("projects/windows-cloud/global/images/windows-server"), instance.Image); Assert.AreEqual(1, instance.Placements.Count()); var placement = instance.Placements.First(); Assert.AreEqual("15934ff9aee7d8c5719fad1053b7fc7d", placement.ServerId); Assert.AreEqual(Tenancies.SoleTenant, placement.Tenancy); // Insert.. Assert.AreEqual(DateTime.Parse("2020-05-06T14:57:46.557Z").ToUniversalTime(), placement.From); // ..till GuestTerminate. Assert.AreEqual(DateTime.Parse("2020-05-06T16:03:06.484Z").ToUniversalTime(), placement.To); }
public void AddLicenseAnnotation( ImageLocator image, LicenseLocator license) { this.LicenseAnnotations[image.ToString()] = ImageAnnotation.FromLicense(license); }
public void AddExistingInstances( IEnumerable <Instance> instances, IEnumerable <NodeGroupNode> nodes, IEnumerable <Disk> disks, string projectId) { using (ApplicationTraceSources.Default.TraceMethod().WithParameters(projectId)) { // // NB. Instances.list returns the disks associated with each // instance, but lacks the information about the source image. // Therefore, we load disks first and then join the data. // var sourceImagesByDisk = disks .EnsureNotNull() .ToDictionary(d => d.SelfLink, d => d.SourceImage); ApplicationTraceSources.Default.TraceVerbose("Found {0} existing disks", sourceImagesByDisk.Count()); ApplicationTraceSources.Default.TraceVerbose("Found {0} existing instances", instances.Count()); foreach (var instance in instances) { ApplicationTraceSources.Default.TraceVerbose("Adding {0}", instance.Id); var bootDiskUrl = instance.Disks .EnsureNotNull() .Where(d => d.Boot != null && d.Boot.Value) .EnsureNotNull() .Select(d => d.Source) .EnsureNotNull() .FirstOrDefault(); ImageLocator image = null; if (bootDiskUrl != null && sourceImagesByDisk.TryGetValue(bootDiskUrl, out string imageUrl) && imageUrl != null) { image = ImageLocator.FromString(imageUrl); } var instanceLocator = new InstanceLocator( projectId, ShortZoneIdFromUrl(instance.Zone), instance.Name); if (instance.Scheduling.NodeAffinities != null && instance.Scheduling.NodeAffinities.Any()) { // This VM runs on a sole-tenant node. var node = nodes.FirstOrDefault(n => n.Instances .EnsureNotNull() .Select(uri => InstanceLocator.FromString(uri)) .Any(locator => locator == instanceLocator)); if (node == null) { ApplicationTraceSources.Default.TraceWarning( "Could not identify node {0} is scheduled on", instanceLocator); } AddExistingInstance( (ulong)instance.Id.Value, instanceLocator, image, instance.Status == "RUNNING" ? InstanceState.Running : InstanceState.Terminated, this.EndDate, Tenancies.SoleTenant, node?.ServerId, node?.NodeType != null ? NodeTypeLocator.FromString(node.NodeType) : null); } else { // Fleet VM. AddExistingInstance( (ulong)instance.Id.Value, instanceLocator, image, instance.Status == "RUNNING" ? InstanceState.Running : InstanceState.Terminated, this.EndDate, Tenancies.Fleet, null, null); } } } }
public void WhenPathLacksProject_FromStringThrowsArgumentException() { Assert.Throws <ArgumentException>(() => ImageLocator.FromString( "/project-1/project-1/global/images/image-1")); }