public void WhenCreatedFromPath_ThenToStringReturnsPath()
        {
            var path = "projects/project-1/zones/us-central1-a/nodeTypes/c2-node-60-240";

            Assert.AreEqual(
                path,
                NodeTypeLocator.FromString(path).ToString());
        }
 public void WhenPathInvalid_FromStringThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => NodeTypeLocator.FromString(
                                           "projects/project-1/zones/us-central1-a/nodeTypes/"));
     Assert.Throws <ArgumentException>(() => NodeTypeLocator.FromString(
                                           "/zones/us-central1-a/nodeTypes/c2-node-60-240 "));
     Assert.Throws <ArgumentException>(() => NodeTypeLocator.FromString(
                                           "/"));
 }
        public void WhenCreatedFromUrl_ThenToStringReturnsPath()
        {
            var path = "projects/project-1/zones/us-central1-a/nodeTypes/c2-node-60-240";

            Assert.AreEqual(
                path,
                NodeTypeLocator.FromString(
                    "https://www.googleapis.com/compute/v1/" + path).ToString());
        }
        public void WhenUsingBetaApi_FromStringReturnsObject()
        {
            var ref1 = NodeTypeLocator.FromString(
                "https://compute.googleapis.com/compute/beta/projects/project-1/zones/us-central1-a/nodeTypes/c2-node-60-240");

            Assert.AreEqual("nodeTypes", ref1.ResourceType);
            Assert.AreEqual("c2-node-60-240", ref1.Name);
            Assert.AreEqual("us-central1-a", ref1.Zone);
            Assert.AreEqual("project-1", ref1.ProjectId);
        }
        public void WhenPathIsValid_FromStringReturnsObject()
        {
            var ref1 = NodeTypeLocator.FromString(
                "projects/project-1/zones/us-central1-a/nodeTypes/c2-node-60-240");

            Assert.AreEqual("nodeTypes", ref1.ResourceType);
            Assert.AreEqual("c2-node-60-240", ref1.Name);
            Assert.AreEqual("us-central1-a", ref1.Zone);
            Assert.AreEqual("project-1", ref1.ProjectId);
        }
        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);
                    }
                }
            }
        }