예제 #1
0
        public override async Task RefreshDataSetAsync(ConnectedSystemDataSet dataSet, CancellationToken cancellationToken)
        {
            Logger.LogDebug($"Refreshing DataSet {dataSet.Name}");

            var inputText        = dataSet.QueryConfig.Query ?? throw new ConfigurationException($"Missing Query in QueryConfig for dataSet '{dataSet.Name}'");
            var query            = new SubstitutionString(inputText);
            var substitutedQuery = query.ToString();
            // Send the query off to ServiceNow
            var connectedSystemItems = await _serviceNowClient
                                       .GetAllByQueryAsync(
                dataSet.QueryConfig.Type,
                substitutedQuery,
                extraQueryString : dataSet.QueryConfig.Options,
                cancellationToken : cancellationToken)
                                       .ConfigureAwait(false);

            Logger.LogDebug($"Got {connectedSystemItems.Count} results for {dataSet.Name}.");

            await ProcessConnectedSystemItemsAsync(
                dataSet,
                connectedSystemItems,
                ConnectedSystem,
                cancellationToken
                ).ConfigureAwait(false);
        }
예제 #2
0
        public async Task ExecuteAsync(DiagnosticTest test)
        {
            _logger.LogInformation($"Starting {nameof(PagingDiagnostic)}");

            if (string.IsNullOrWhiteSpace(test.Table))
            {
                throw new ConfigurationException($"{nameof(test.Table)} must be set.");
            }

            try
            {
                using (var client = new ServiceNowClient(
                           _configuration.Credentials.ServiceNowAccount,
                           _configuration.Credentials.ServiceNowUsername,
                           _configuration.Credentials.ServiceNowPassword,
                           new Options {
                    ValidateCountItemsReturned = true, ValidateCountItemsReturnedTolerance = 0, PageSize = test.PageSize.Value, Logger = _logger
                }))
                {
                    var results = await client.GetAllByQueryAsync(test.Table, test.Query, fieldList : test.Fields).ConfigureAwait(false);

                    _logger.LogInformation($"Got {results.Count} results");

                    // Check for dupes
                    var dupes  = results.GroupBy(ci => ci["sys_id"]).Where(g => g.Count() > 1).Select(g => new { Id = g.First()["sys_id"], Count = g.Count() }).ToList();
                    var unique = results.GroupBy(ci => ci["sys_id"]).Select(ci => ci.First()).ToList();

                    _logger.LogInformation($"Found {dupes.Count} dupes - total retrieved = {results.Count} - unique = {unique.Count}");
                }
            }
            catch (System.Exception e)
            {
                _logger.LogError(e, e.Message);
            }
        }
예제 #3
0
        public async static Task Main(string[] args)
        {
            var account  = args[0];
            var username = args[1];
            var password = args[2];

            Console.WriteLine("Lists Windows Servers");

            using var serviceNowClient = new ServiceNowClient(account, username, password, new Options());

            // MANDATORY: The table name can be obtained from this list:
            // https://docs.servicenow.com/bundle/london-platform-administration/page/administer/reference-pages/reference/r_TablesAndClasses.html
            const string tableName = "cmdb_ci_win_server";

            // OPTIONAL: The main sysparm_query goes here.  See documention here:
            // https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/integrate/inbound_rest/reference/r_TableAPI-GET.html
            // If you omit this, an unfiltered result will be returned
            const string query = "name";

            // OPTIONAL: The fields to bring back.
            // This should be set to constrain the response to ONLY the fields that you are going to process.
            // Doing so will hugely speed up your query.
            var fields = new List <string> {
                "sys_id", "name"
            };

            var jObjectResults = await serviceNowClient.GetAllByQueryAsync(
                tableName,
                query,
                fields
                ).ConfigureAwait(false);

            var modelResults = jObjectResults.ConvertAll(o => o.ToObject <WinServerModel>());

            Console.WriteLine("Windows Servers:");
            foreach (var modelResult in modelResults)
            {
                Console.WriteLine($"  - {modelResult.Id}: {modelResult.Name}");
            }
        }