Exemple #1
0
        private static async Task <AssessmentResult <TResult> > CallAssessment <TResult>(
            string methodName,
            SqlObjectType sqlObjectType,
            LiveConnectionHelper.TestConnectionResult liveConnection)
            where TResult : AssessmentItemInfo
        {
            var connInfo = liveConnection.ConnectionInfo;

            AssessmentResult <TResult> response;

            using (var service = new SqlAssessmentService(
                       TestServiceProvider.Instance.ConnectionService,
                       TestServiceProvider.Instance.WorkspaceService))
            {
                AddTestRules(service);

                string           randomUri     = Guid.NewGuid().ToString();
                AssessmentParams requestParams =
                    new AssessmentParams {
                    OwnerUri = randomUri, TargetType = sqlObjectType
                };
                ConnectParams connectParams = new ConnectParams
                {
                    OwnerUri   = requestParams.OwnerUri,
                    Connection = connInfo.ConnectionDetails,
                    Type       = ConnectionType.Default
                };

                var methodInfo = typeof(SqlAssessmentService).GetMethod(
                    methodName,
                    BindingFlags.Instance | BindingFlags.NonPublic);

                Assert.NotNull(methodInfo);

                var func = (AssessmentMethod <TResult>)Delegate.CreateDelegate(
                    typeof(AssessmentMethod <TResult>),
                    service,
                    methodInfo);

                response = await service.CallAssessmentEngine <TResult>(
                    requestParams,
                    connectParams,
                    randomUri,
                    t => func(t));
            }

            Assert.NotNull(response);
            if (response.Success)
            {
                Assert.All(
                    response.Items,
                    i =>
                {
                    Assert.Equal(sqlObjectType, i.TargetType);
                    Assert.Contains(i.Level, AllowedSeverityLevels);
                });
            }

            return(response);
        }
Exemple #2
0
        private async Task HandleAssessmentRequest <TResult>(
            RequestContext <AssessmentResult <TResult> > requestContext,
            AssessmentParams requestParams,
            Func <SqlObjectLocator, Task <List <TResult> > > assessmentFunction)
            where TResult : AssessmentItemInfo
        {
            try
            {
                string randomUri = Guid.NewGuid().ToString();

                // get connection
                if (!ConnectionService.TryFindConnection(requestParams.OwnerUri, out var connInfo))
                {
                    await requestContext.SendError(SR.SqlAssessmentQueryInvalidOwnerUri);

                    return;
                }

                ConnectParams connectParams = new ConnectParams
                {
                    OwnerUri   = randomUri,
                    Connection = connInfo.ConnectionDetails,
                    Type       = ConnectionType.Default
                };

                if (!connInfo.TryGetConnection(ConnectionType.Default, out var connection))
                {
                    await requestContext.SendError(SR.SqlAssessmentConnectingError);
                }

                var workTask = CallAssessmentEngine <TResult>(
                    requestParams,
                    connectParams,
                    randomUri,
                    assessmentFunction)
                               .ContinueWith(async tsk =>
                {
                    await requestContext.SendResult(tsk.Result);
                });

                ActiveRequests.TryAdd(randomUri, workTask);
            }
            catch (Exception ex)
            {
                if (ex is StackOverflowException || ex is OutOfMemoryException)
                {
                    throw;
                }

                await requestContext.SendError(ex.ToString());
            }
        }
Exemple #3
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);
        }