Beispiel #1
0
        /// <summary>
        /// Gets the list of checks for given target servers or databases.
        /// </summary>
        /// <param name="target">
        /// A sequence of target servers or databases.
        /// </param>
        /// <returns>
        /// Returns an <see cref="IEnumerable{SqlObjectLocator}"/>
        /// containing checks available for given <paramref name="target"/>.
        /// </returns>
        internal Task <List <CheckInfo> > GetAssessmentItems(SqlObjectLocator target)
        {
            var result = new List <CheckInfo>();

            var resultsList = Engine.GetChecks(target).ToList();

            Logger.Write(TraceEventType.Verbose, $"SQL Assessment: got {resultsList.Count} items.");

            foreach (var r in resultsList)
            {
                var targetName = target.Type != SqlObjectType.Server
                                     ? $"{target.ServerName}:{target.Name}"
                                     : target.Name;

                var item = new CheckInfo()
                {
                    CheckId        = r.Id,
                    Description    = r.Description,
                    DisplayName    = r.DisplayName,
                    HelpLink       = r.HelpLink,
                    Level          = r.Level.ToString(),
                    TargetName     = targetName,
                    Tags           = r.Tags.ToArray(),
                    TargetType     = target.Type,
                    RulesetName    = Engine.Configuration.DefaultRuleset.Name,
                    RulesetVersion = Engine.Configuration.DefaultRuleset.Version.ToString()
                };

                result.Add(item);
            }

            return(Task.FromResult(result));
        }
Beispiel #2
0
        /// <summary>
        /// Invokes SQL Assessment and formats results.
        /// </summary>
        /// <param name="target">
        /// A sequence of target servers or databases to be assessed.
        /// </param>
        /// <returns>
        /// Returns a <see cref="List{AssessmentResultItem}"/>
        /// containing assessment results.
        /// </returns>
        /// <remarks>
        /// Internal for testing
        /// </remarks>
        internal async Task <List <AssessmentResultItem> > InvokeSqlAssessment(SqlObjectLocator target)
        {
            var resultsList = await Engine.GetAssessmentResultsList(target);

            Logger.Write(TraceEventType.Verbose, $"SQL Assessment: got {resultsList.Count} results.");

            return(resultsList.Select(TranslateAssessmentResult).ToList());
        }
Beispiel #3
0
 /// <summary>
 /// Constructs a <see cref="SqlObjectLocator"/> for specified database.
 /// </summary>
 /// <param name="server">Target server locator.</param>
 /// <param name="databaseName">Target database name.</param>
 /// <returns>Returns a locator for target database.</returns>
 private static SqlObjectLocator GetDatabaseLocator(SqlObjectLocator server, string databaseName)
 {
     return(new SqlObjectLocator
     {
         Connection = server.Connection,
         EngineEdition = server.EngineEdition,
         Name = databaseName,
         Platform = server.Platform,
         ServerName = server.Name,
         Type = SqlObjectType.Database,
         Urn = $"{server.Name}:{databaseName}",
         Version = server.Version
     });
 }
Beispiel #4
0
        /// <summary>
        /// This function obtains a live connection, then calls
        /// an assessment operation specified by <paramref name="assessmentFunc"/>
        /// </summary>
        /// <typeparam name="TResult">
        /// SQL Assessment result item type.
        /// </typeparam>
        /// <param name="requestParams">
        /// Request parameters passed from the host.
        /// </param>
        /// <param name="connectParams">
        /// Connection parameters used to identify and access the target.
        /// </param>
        /// <param name="taskUri">
        /// An URI identifying the request task to enable concurrent execution.
        /// </param>
        /// <param name="assessmentFunc">
        /// A function performing assessment operation for given target.
        /// </param>
        /// <returns>
        /// Returns <see cref="AssessmentResult{TResult}"/> for given target.
        /// </returns>
        internal async Task <AssessmentResult <TResult> > CallAssessmentEngine <TResult>(
            AssessmentParams requestParams,
            ConnectParams connectParams,
            string taskUri,
            Func <SqlObjectLocator, Task <List <TResult> > > assessmentFunc)
            where TResult : AssessmentItemInfo

        {
            var result = new AssessmentResult <TResult>
            {
                ApiVersion = ApiVersion
            };

            await ConnectionService.Connect(connectParams);

            var connection = await ConnectionService.Instance.GetOrOpenConnection(taskUri, ConnectionType.Query);

            try
            {
                var serverInfo = ReliableConnectionHelper.GetServerVersion(connection);
                var hostInfo   = ReliableConnectionHelper.GetServerHostInfo(connection);

                var server = new SqlObjectLocator
                {
                    Connection    = connection,
                    EngineEdition = GetEngineEdition(serverInfo.EngineEditionId),
                    Name          = serverInfo.ServerName,
                    ServerName    = serverInfo.ServerName,
                    Type          = SqlObjectType.Server,
                    Urn           = serverInfo.ServerName,
                    Version       = Version.Parse(serverInfo.ServerVersion),
                    Platform      = hostInfo.Platform
                };

                switch (requestParams.TargetType)
                {
                case SqlObjectType.Server:
                    Logger.Write(
                        TraceEventType.Verbose,
                        $"SQL Assessment: running an operation on a server, platform:{server.Platform}, edition:{server.EngineEdition.ToString()}, version:{server.Version}");

                    result.Items.AddRange(await assessmentFunc(server));

                    Logger.Write(
                        TraceEventType.Verbose,
                        $"SQL Assessment: finished an operation on a server, platform:{server.Platform}, edition:{server.EngineEdition.ToString()}, version:{server.Version}");
                    break;

                case SqlObjectType.Database:
                    var db = GetDatabaseLocator(server, connection.Database);
                    Logger.Write(
                        TraceEventType.Verbose,
                        $"SQL Assessment: running an operation on a database, platform:{server.Platform}, edition:{server.EngineEdition.ToString()}, version:{server.Version}");

                    result.Items.AddRange(await assessmentFunc(db));

                    Logger.Write(
                        TraceEventType.Verbose,
                        $"SQL Assessment: finished an operation on a database, platform:{server.Platform}, edition:{server.EngineEdition.ToString()}, version:{server.Version}");
                    break;
                }

                result.Success = true;
            }
            finally
            {
                ActiveRequests.TryRemove(taskUri, out _);
                ConnectionService.Disconnect(new DisconnectParams {
                    OwnerUri = taskUri, Type = null
                });
            }

            return(result);
        }