Exemplo n.º 1
0
        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 = GlobalResourceReference.FromString(
                                    initializeParams["sourceImage"].Value <string>());
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        //---------------------------------------------------------------------
        // Ctor
        //---------------------------------------------------------------------

        private InstanceHistoryBuilder(
            ulong instanceId,
            VmInstanceReference reference,
            GlobalResourceReference image,
            InstanceState state,
            DateTime?lastSeen,
            Tenancy tenancy)
        {
            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 != Tenancy.Unknown);
                Debug.Assert(lastSeen != null);

                // Add a synthetic placement.
                AddPlacement(new InstancePlacement(tenancy, null, lastSeen.Value, lastSeen.Value));
            }
        }
        public void WhenReferencesAreEquivalent_ThenGetHasCodeIsSame()
        {
            var ref1 = new GlobalResourceReference("proj", "images", "inst");
            var ref2 = new GlobalResourceReference("proj", "images", "inst");

            Assert.AreEqual(ref1.GetHashCode(), ref2.GetHashCode());
        }
        public void WhenCreatedFromPath_ThenToStringReturnsPath()
        {
            var path = "projects/project-1/global/images/image-1";

            Assert.AreEqual(
                path,
                GlobalResourceReference.FromString(path).ToString());
        }
 public void WhenPathInvalid_FromStringThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => GlobalResourceReference.FromString(
                                           "projects/project-1/notglobal/images/image-1"));
     Assert.Throws <ArgumentException>(() => GlobalResourceReference.FromString(
                                           "/project-1/global/images/image-1"));
     Assert.Throws <ArgumentException>(() => GlobalResourceReference.FromString(
                                           "/"));
 }
        public void WhenQualifiedByGoogleapisHost_FromStringReturnsObject()
        {
            var ref1 = GlobalResourceReference.FromString(
                "https://www.googleapis.com/compute/v1/projects/windows-cloud/global/licenses/windows-server-core");

            Assert.AreEqual("licenses", ref1.ResourceType);
            Assert.AreEqual("windows-server-core", ref1.ResourceName);
            Assert.AreEqual("windows-cloud", ref1.ProjectId);
        }
        public void WhenQualifiedByComputeGoogleapisHost_FromStringReturnsObject()
        {
            var ref1 = GlobalResourceReference.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.ResourceName);
            Assert.AreEqual("debian-cloud", ref1.ProjectId);
        }
        public void WhenResourceNameCotainsSlash_FromStringReturnsObject()
        {
            var ref1 = GlobalResourceReference.FromString(
                "projects/debian-cloud/global/images/family/debian-9");

            Assert.AreEqual("images", ref1.ResourceType);
            Assert.AreEqual("family/debian-9", ref1.ResourceName);
            Assert.AreEqual("debian-cloud", ref1.ProjectId);
        }
        public void WhenPathIsValid_FromStringReturnsObject()
        {
            var ref1 = GlobalResourceReference.FromString(
                "projects/project-1/global/images/image-1");

            Assert.AreEqual("images", ref1.ResourceType);
            Assert.AreEqual("image-1", ref1.ResourceName);
            Assert.AreEqual("project-1", ref1.ProjectId);
        }
        public void WhenCreatedFromUrl_ThenToStringReturnsPath()
        {
            var path = "projects/project-1/global/images/image-1";

            Assert.AreEqual(
                path,
                GlobalResourceReference.FromString(
                    "https://www.googleapis.com/compute/v1/" + path).ToString());
        }
        public void WhenReferencesAreEquivalent_ThenEqualsReturnsTrue()
        {
            var ref1 = new GlobalResourceReference("proj", "images", "inst");
            var ref2 = new GlobalResourceReference("proj", "images", "inst");

            Assert.IsTrue(ref1.Equals(ref2));
            Assert.IsTrue(ref1.Equals((object)ref2));
            Assert.IsTrue(ref1 == ref2);
            Assert.IsFalse(ref1 != ref2);
        }
        public void TestEqualsNull()
        {
            var ref1 = new GlobalResourceReference("proj", "machineTypes", "inst");

            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);
        }
Exemplo n.º 13
0
 internal InstanceHistory(
     [JsonProperty("id")] ulong instanceId,
     [JsonProperty("vm")] VmInstanceReference reference,
     [JsonProperty("state")] InstanceHistoryState state,
     [JsonProperty("image")] GlobalResourceReference image,
     [JsonProperty("placements")] IEnumerable <InstancePlacement> placements
     )
 {
     this.InstanceId = instanceId;
     this.Reference  = reference;
     this.State      = state;
     this.Image      = image;
     this.Placements = placements;
 }
 public void AddExistingInstance(
     ulong instanceId,
     VmInstanceReference reference,
     GlobalResourceReference image,
     InstanceState state,
     DateTime lastSeen,
     Tenancy tenancy)
 {
     this.instanceBuilders[instanceId] = InstanceHistoryBuilder.ForExistingInstance(
         instanceId,
         reference,
         image,
         state,
         lastSeen,
         tenancy);
 }
Exemplo n.º 15
0
        internal static InstanceHistoryBuilder ForExistingInstance(
            ulong instanceId,
            VmInstanceReference reference,
            GlobalResourceReference image,
            InstanceState state,
            DateTime lastSeen,
            Tenancy tenancy)
        {
            Debug.Assert(state != InstanceState.Deleted);

            return(new InstanceHistoryBuilder(
                       instanceId,
                       reference,
                       image,
                       state,
                       lastSeen,
                       tenancy));
        }
Exemplo n.º 16
0
        //---------------------------------------------------------------------
        // Lifecycle events that construct the history.
        //---------------------------------------------------------------------

        public void OnInsert(DateTime date, VmInstanceReference reference, GlobalResourceReference 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(Tenancy.Fleet, null, date);
        }
Exemplo n.º 17
0
        public void WhenReadingSample3_ThenHistoryIsRestored()
        {
            var set = BuildHistoryFromResource("instance-3.json");

            Assert.AreEqual(1, set.Instances.Count());

            var instance = set.Instances.First();

            Assert.AreEqual(
                GlobalResourceReference.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(Tenancy.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(Tenancy.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);
        }
Exemplo n.º 18
0
        public void WhenReadingSample2_ThenHistoryIsRestored()
        {
            var set = BuildHistoryFromResource("instance-2.json");

            Assert.AreEqual(1, set.Instances.Count());

            var instance = set.Instances.First();

            Assert.AreEqual(
                GlobalResourceReference.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(Tenancy.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 WhenPathLacksProject_FromStringThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => GlobalResourceReference.FromString(
                                           "/project-1/project-1/global/images/image-1"));
 }
        public async Task AddExistingInstances(
            InstancesResource instancesResource,
            DisksResource disksResource,
            string projectId)
        {
            // NB. Instances.list returns the disks associated with each
            // instance, but lacks the information about the source image.
            // Therefore, load disks separately.

            using (TraceSources.LogAnalysis.TraceMethod().WithParameters(projectId))
            {
                // TODO: Paging
                var disks = await disksResource.AggregatedList(projectId).ExecuteAsync();

                var sourceImagaesByDisk = disks.Items == null
                    ? new Dictionary <string, string>()
                    : disks.Items.Values
                                          .Where(v => v.Disks != null)
                                          .EnsureNotNull()
                                          .SelectMany(v => v.Disks)
                                          .EnsureNotNull()
                                          .ToDictionary(d => d.SelfLink, d => d.SourceImage);

                // TODO: Paging
                var instances = await instancesResource.AggregatedList(projectId).ExecuteAsync();

                if (instances.Items != null)
                {
                    foreach (var list in instances.Items.Values)
                    {
                        if (list.Instances == null)
                        {
                            continue;
                        }

                        foreach (var instance in list.Instances)
                        {
                            TraceSources.LogAnalysis.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();
                            GlobalResourceReference image = null;
                            if (bootDiskUrl != null &&
                                sourceImagaesByDisk.TryGetValue(bootDiskUrl, out string imageUrl) &&
                                imageUrl != null)
                            {
                                image = GlobalResourceReference.FromString(imageUrl);
                            }

                            AddExistingInstance(
                                (ulong)instance.Id.Value,
                                new VmInstanceReference(
                                    projectId,
                                    ShortZoneIdFromUrl(instance.Zone),
                                    instance.Name),
                                image,
                                instance.Status == "RUNNING"
                                    ? InstanceState.Running
                                    : InstanceState.Terminated,
                                DateTime.Now,
                                instance.Scheduling.NodeAffinities != null && instance.Scheduling.NodeAffinities.Any()
                                    ? Tenancy.SoleTenant
                                    : Tenancy.Fleet);
                        }
                    }
                }
            }
        }