Example #1
0
 public void WhenPathInvalid_FromStringThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => ZoneLocator.FromString(
                                           "projects/project-1/zone/us-central1-a"));
     Assert.Throws <ArgumentException>(() => ZoneLocator.FromString(
                                           "projects/project-1/zones"));
 }
Example #2
0
        public void OpenInstanceList(ZoneLocator zone)
        {
            var query = "[{\"k\":\"zoneForFilter\",\"v\":\"" + zone.Name + "\"}]";

            OpenUrl("https://console.cloud.google.com/compute/instances" +
                    $"?project={zone.ProjectId}&instancesquery={WebUtility.UrlEncode(query)}");
        }
        public async Task <IEnumerable <NodeGroupNode> > ListNodesAsync(
            string projectId,
            CancellationToken cancellationToken)
        {
            using (TraceSources.IapDesktop.TraceMethod().WithParameters(projectId))
            {
                var nodeGroups = await ListNodeGroupsAsync(
                    projectId,
                    cancellationToken)
                                 .ConfigureAwait(false);

                var nodesAcrossGroups = Enumerable.Empty <NodeGroupNode>();

                foreach (var nodeGroup in nodeGroups)
                {
                    nodesAcrossGroups = nodesAcrossGroups.Concat(await ListNodesAsync(
                                                                     ZoneLocator.FromString(nodeGroup.Zone),
                                                                     nodeGroup.Name,
                                                                     cancellationToken)
                                                                 .ConfigureAwait(false));
                }

                return(nodesAcrossGroups);
            }
        }
        public async Task <IEnumerable <Instance> > ListInstancesAsync(
            ZoneLocator zoneLocator,
            CancellationToken cancellationToken)
        {
            using (TraceSources.IapDesktop.TraceMethod().WithParameters(zoneLocator))
            {
                try
                {
                    var result = await new PageStreamer <
                        Instance,
                        InstancesResource.ListRequest,
                        InstanceList,
                        string>(
                        (req, token) => req.PageToken = token,
                        response => response.NextPageToken,
                        response => response.Items)
                                 .FetchAllAsync(
                        this.service.Instances.List(zoneLocator.ProjectId, zoneLocator.Name),
                        cancellationToken)
                                 .ConfigureAwait(false);

                    TraceSources.IapDesktop.TraceVerbose("Found {0} instances", result.Count());

                    return(result);
                }
                catch (GoogleApiException e) when(e.Error != null && e.Error.Code == 403)
                {
                    throw new ResourceAccessDeniedException(
                              $"Access to VM instances in project {zoneLocator.ProjectId} has been denied", e);
                }
            }
        }
 public async Task <IEnumerable <NodeGroupNode> > ListNodesAsync(
     ZoneLocator zone,
     string nodeGroup,
     CancellationToken cancellationToken)
 {
     using (TraceSources.IapDesktop.TraceMethod().WithParameters(zone, nodeGroup))
     {
         try
         {
             return(await new PageStreamer <
                        NodeGroupNode,
                        NodeGroupsResource.ListNodesRequest,
                        NodeGroupsListNodes,
                        string>(
                        (req, token) => req.PageToken = token,
                        response => response.NextPageToken,
                        response => response.Items)
                    .FetchAllAsync(
                        this.service.NodeGroups.ListNodes(zone.ProjectId, zone.Name, nodeGroup),
                        cancellationToken)
                    .ConfigureAwait(false));
         }
         catch (GoogleApiException e) when(e.Error != null && e.Error.Code == 403)
         {
             throw new ResourceAccessDeniedException(
                       $"Access to nodes in project {zone.ProjectId} has been denied", e);
         }
     }
 }
        //---------------------------------------------------------------------
        // Ctor.
        //---------------------------------------------------------------------

        public ZoneNode(
            ZoneLocator locator,
            IEnumerable <IProjectModelInstanceNode> instances)
        {
            this.Zone      = locator;
            this.Instances = instances;
        }
Example #7
0
        public void WhenReferencesAreEquivalent_ThenGetHasCodeIsSame()
        {
            var ref1 = new ZoneLocator("proj", "us-central1-a");
            var ref2 = new ZoneLocator("proj", "us-central1-a");

            Assert.AreEqual(ref1.GetHashCode(), ref2.GetHashCode());
        }
Example #8
0
        public void WhenCreatedFromPath_ThenToStringReturnsPath()
        {
            var path = "projects/project-1/zones/us-central1-a";

            Assert.AreEqual(
                path,
                ZoneLocator.FromString(path).ToString());
        }
Example #9
0
        public void WhenCreatedFromUrl_ThenToStringReturnsPath()
        {
            var path = "projects/project-1/zones/us-central1-a";

            Assert.AreEqual(
                path,
                ZoneLocator.FromString(
                    "https://www.googleapis.com/compute/v1/" + path).ToString());
        }
Example #10
0
        public void WhenQualifiedByGoogleapisHost_FromStringReturnsObject()
        {
            var ref1 = ZoneLocator.FromString(
                "https://www.googleapis.com/compute/v1/projects/project-1/zones/us-central1-a");

            Assert.AreEqual("zones", ref1.ResourceType);
            Assert.AreEqual("us-central1-a", ref1.Name);
            Assert.AreEqual("project-1", ref1.ProjectId);
        }
Example #11
0
        public void WhenPathIsValid_FromStringReturnsObject()
        {
            var ref1 = ZoneLocator.FromString(
                "projects/project-1/zones/us-central1-a");

            Assert.AreEqual("zones", ref1.ResourceType);
            Assert.AreEqual("us-central1-a", ref1.Name);
            Assert.AreEqual("project-1", ref1.ProjectId);
        }
Example #12
0
        public void WhenReferencesAreEquivalent_ThenEqualsReturnsTrue()
        {
            var ref1 = new ZoneLocator("proj", "us-central1-a");
            var ref2 = new ZoneLocator("proj", "us-central1-a");

            Assert.IsTrue(ref1.Equals(ref2));
            Assert.IsTrue(ref1.Equals((object)ref2));
            Assert.IsTrue(ref1 == ref2);
            Assert.IsFalse(ref1 != ref2);
        }
Example #13
0
        public void TestEqualsNull()
        {
            var ref1 = new ZoneLocator("proj", "us-central1-a");

            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);
        }
        public async Task <IEnumerable <GuestOsInfo> > ListZoneInventoryAsync(ZoneLocator locator, CancellationToken token)
        {
            var instances = await this.computeEngineAdapter
                            .ListInstancesAsync(locator, token)
                            .ConfigureAwait(false);

            return(await ListInventoryAsync(
                       instances.Select(i => i.GetInstanceLocator()),
                       token)
                   .ConfigureAwait(false));
        }
        private async Task <IReadOnlyCollection <IProjectModelZoneNode> > LoadZones(
            ProjectLocator project,
            CancellationToken token)
        {
            using (ApplicationTraceSources.Default.TraceMethod().WithoutParameters())
                using (var computeEngineAdapter = this.serviceProvider
                                                  .GetService <IComputeEngineAdapter>())
                {
                    var instances = await computeEngineAdapter
                                    .ListInstancesAsync(project.ProjectId, token)
                                    .ConfigureAwait(false);

                    var zoneLocators = instances
                                       .EnsureNotNull()
                                       .Select(i => ZoneLocator.FromString(i.Zone))
                                       .ToHashSet();

                    var zones = new List <ZoneNode>();
                    foreach (var zoneLocator in zoneLocators.OrderBy(z => z.Name))
                    {
                        var instancesInZone = instances
                                              .Where(i => ZoneLocator.FromString(i.Zone) == zoneLocator)
                                              .Where(i => i.Disks != null && i.Disks.Any())
                                              .OrderBy(i => i.Name)
                                              .Select(i => new InstanceNode(
                                                          i.Id.Value,
                                                          new InstanceLocator(
                                                              zoneLocator.ProjectId,
                                                              zoneLocator.Name,
                                                              i.Name),
                                                          i.IsWindowsInstance()
                                ? OperatingSystems.Windows
                                : OperatingSystems.Linux,
                                                          i.Status == "RUNNING"))
                                              .ToList();

                        zones.Add(new ZoneNode(
                                      zoneLocator,
                                      instancesInZone));
                    }

                    return(zones);
                }
        }
Example #16
0
        public async Task <IEnumerable <Instance> > ListInstancesAsync(
            ZoneLocator zoneLocator,
            CancellationToken cancellationToken)
        {
            using (ApplicationTraceSources.Default.TraceMethod().WithParameters(zoneLocator))
            {
                try
                {
                    var result = await new PageStreamer <
                        Instance,
                        InstancesResource.ListRequest,
                        InstanceList,
                        string>(
                        (req, token) => req.PageToken = token,
                        response => response.NextPageToken,
                        response => response.Items)
                                 .FetchAllAsync(
                        this.service.Instances.List(zoneLocator.ProjectId, zoneLocator.Name),
                        cancellationToken)
                                 .ConfigureAwait(false);

                    ApplicationTraceSources.Default.TraceVerbose("Found {0} instances", result.Count());

                    return(result);
                }
                catch (GoogleApiException e) when(e.IsAccessDenied())
                {
                    throw new ResourceAccessDeniedException(
                              "You do not have sufficient permissions to list VM instances in " +
                              $"project {zoneLocator.ProjectId}. " +
                              "You need the 'Compute Viewer' role (or an equivalent custom role) " +
                              "to perform this action.",
                              HelpTopics.ProjectAccessControl,
                              e);
                }
            }
        }
 public static ZoneLocator GetZoneLocator(this Instance instance)
 {
     return(ZoneLocator.FromString(instance.Zone));
 }
Example #18
0
 public void WhenPathLacksProject_FromStringThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => ZoneLocator.FromString(
                                           "/project-1/project-1/zones/us-central1-a"));
 }