/// <summary>
        /// Executes an action for all the libraries that belong to the specified project.
        /// </summary>
        /// <param name="projectId">The ID of the project.</param>
        /// <param name="libraryProcessor">The action to execute for each library of the project.</param>
        /// <param name="cancellationToken">The optional cancellation token.</param>
        private async Task ProcessAllProjectLibraries(string projectId, Action <string> libraryProcessor, CancellationToken cancellationToken = default(CancellationToken))
        {
            var getNodeLinksRequest = new GetNodeLinksRequest
            {
                ForestId = string.Format(ProjectDataForestIDPattern, projectId),
                TreeId   = DiscoveryTreeID,
                NodeId   = DiscoveryTreeRootNodeID,
            };

            Console.WriteLine($"Getting the links of the project discovery tree root node (ProjectId={projectId}, ForestId={getNodeLinksRequest.ForestId}, TreeId={getNodeLinksRequest.TreeId}, NodeId={getNodeLinksRequest.NodeId})...");

            var discoveryTreeRootNodeLinks = await this.orgClient.GetNodeLinksAsync(getNodeLinksRequest, cancellationToken).ConfigureAwait(false);

            Console.WriteLine($"Got {discoveryTreeRootNodeLinks.Count} links:");
            discoveryTreeRootNodeLinks.ToList().ForEach(link => Console.WriteLine(link));

            foreach (string link in discoveryTreeRootNodeLinks)
            {
                // Provide a chance to bail out before attempting to process the current link
                cancellationToken.ThrowIfCancellationRequested();

                if (!link.StartsWith(DiscoveryTreeRootNodeLinkPrefix))
                {
                    throw new InvalidDataException("Project discovery tree root node link is in unexpected format.");
                }

                // The ID in the link may contain escaped special characters, so they must be un-escaped.
                string libraryId = Uri.UnescapeDataString(link.Substring(DiscoveryTreeRootNodeLinkPrefix.Length, link.Length - DiscoveryTreeRootNodeLinkPrefix.Length));

                libraryProcessor.Invoke(libraryId);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Discover a library in a project by name.
        /// </summary>
        /// <param name="projectId">The ID of the project in which to discover the library.</param>
        /// <param name="libraryName">The name of the library to discover.</param>
        /// <returns>The ID of the discovered library.</returns>
        private async Task <string> DiscoverLibrary(string projectId, string libraryName)
        {
            var getNodeLinksRequest = new GetNodeLinksRequest
            {
                ForestId = string.Format(ProjectDataForestIDPattern, projectId),
                TreeId   = DiscoveryTreeID,
                NodeId   = DiscoveryTreeRootNodeID,
            };

            Console.WriteLine($"Getting the links of the project discovery tree root node (ProjectId={projectId}, ForestId={getNodeLinksRequest.ForestId}, TreeId={getNodeLinksRequest.TreeId}, NodeId={getNodeLinksRequest.NodeId})...");

            var discoveryTreeRootNodeLinks = await this.orgClient.GetNodeLinksAsync(getNodeLinksRequest).ConfigureAwait(false);

            Console.WriteLine($"Got {discoveryTreeRootNodeLinks.Count} links:");
            discoveryTreeRootNodeLinks.ToList().ForEach(link => Console.WriteLine(link));

            foreach (string link in discoveryTreeRootNodeLinks)
            {
                if (!link.StartsWith(DiscoveryTreeRootNodeLinkPrefix))
                {
                    throw new InvalidDataException("Project discovery tree root node link is in unexpected format.");
                }

                var getLibraryRequest = new GetLibraryRequest
                {
                    LibraryId = link.Substring(DiscoveryTreeRootNodeLinkPrefix.Length, link.Length - DiscoveryTreeRootNodeLinkPrefix.Length),
                };

                Console.WriteLine($"Getting library with LibraryId={getLibraryRequest.LibraryId}...");

                Library library = await this.psetClient.GetLibraryAsync(getLibraryRequest).ConfigureAwait(false);

                Console.Write($"Got library: ");
                this.PrintLibrary(library);
                Console.WriteLine();

                if (library.Name == libraryName)
                {
                    Console.WriteLine("Library discovered!");

                    return(library.Id);
                }
                else
                {
                    Console.WriteLine("Not a match.");
                }
            }

            return(null);
        }