Ejemplo n.º 1
0
        internal async Task <Tuple <bool, string> > ConfigureAgentOperator(
            string ownerUri,
            AgentOperatorInfo operatorInfo,
            ConfigAction configAction,
            RunType runType)
        {
            return(await Task <Tuple <bool, string> > .Run(() =>
            {
                try
                {
                    ConnectionInfo connInfo;
                    ConnectionServiceInstance.TryFindConnection(ownerUri, out connInfo);
                    CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true);
                    STParameters param = new STParameters(dataContainer.Document);
                    param.SetParam("operator", operatorInfo.Name);

                    using (AgentOperatorActions agentOperator = new AgentOperatorActions(dataContainer, operatorInfo, configAction))
                    {
                        var executionHandler = new ExecutonHandler(agentOperator);
                        executionHandler.RunNow(runType, this);
                    }

                    return new Tuple <bool, string>(true, string.Empty);
                }
                catch (Exception ex)
                {
                    return new Tuple <bool, string>(false, ex.ToString());
                }
            }));
        }
        public async Task TestHandleUpdateAgentOperatorRequest()
        {
            using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
            {
                var createContext = new Mock <RequestContext <CreateAgentOperatorResult> >();

                var service      = new AgentService();
                var operatorInfo = new AgentOperatorInfo()
                {
                    Id           = 10,
                    Name         = "Joe DBA",
                    EmailAddress = "*****@*****.**"
                };

                var connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);

                await service.HandleCreateAgentOperatorRequest(new CreateAgentOperatorParams
                {
                    OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
                    Operator = operatorInfo
                }, createContext.Object);

                createContext.VerifyAll();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Constructor
        /// </summary>
        public AgentOperator(CDataContainer dataContainer, AgentOperatorInfo operatorInfo)
        {
            try
            {
                if (dataContainer == null)
                {
                    throw new ArgumentNullException("dataContainer");
                }

                if (operatorInfo == null)
                {
                    throw new ArgumentNullException("operatorInfo");
                }

                this.operatorInfo  = operatorInfo;
                this.DataContainer = dataContainer;

                STParameters parameters = new STParameters();
                parameters.SetDocument(dataContainer.Document);

                string agentOperatorName = null;
                if (parameters.GetParam("operator", ref agentOperatorName))
                {
                    this.operatorsData = new AgentOperatorsData(dataContainer, agentOperatorName);
                }
                else
                {
                    throw new ArgumentNullException("agentOperatorName");
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException("AgentOperatorsSR.FailedToCreateInitializeAgentOperatorDialog", e);
            }
        }
Ejemplo n.º 4
0
        internal static async Task DeleteAgentOperator(
            AgentService service,
            TestConnectionResult connectionResult,
            AgentOperatorInfo operatorInfo)
        {
            var context = new Mock <RequestContext <ResultStatus> >();
            await service.HandleDeleteAgentOperatorRequest(new DeleteAgentOperatorParams
            {
                OwnerUri = connectionResult.ConnectionInfo.OwnerUri,
                Operator = operatorInfo
            }, context.Object);

            context.VerifyAll();
        }
Ejemplo n.º 5
0
        internal async Task HandleAgentOperatorsRequest(AgentOperatorsParams parameters, RequestContext <AgentOperatorsResult> requestContext)
        {
            await Task.Run(async() =>
            {
                var result = new AgentOperatorsResult();
                try
                {
                    ConnectionInfo connInfo;
                    ConnectionServiceInstance.TryFindConnection(parameters.OwnerUri, out connInfo);
                    CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true);

                    int operatorCount = dataContainer.Server.JobServer.Operators.Count;
                    var operators     = new AgentOperatorInfo[operatorCount];
                    for (int i = 0; i < operatorCount; ++i)
                    {
                        var item     = dataContainer.Server.JobServer.Operators[i];
                        operators[i] = new AgentOperatorInfo
                        {
                            Name                   = item.Name,
                            Id                     = item.ID,
                            EmailAddress           = item.EmailAddress,
                            Enabled                = item.Enabled,
                            LastEmailDate          = item.LastEmailDate.ToString(),
                            LastNetSendDate        = item.LastNetSendDate.ToString(),
                            LastPagerDate          = item.LastPagerDate.ToString(),
                            PagerAddress           = item.PagerAddress,
                            CategoryName           = item.CategoryName,
                            PagerDays              = (Contracts.WeekDays)item.PagerDays,
                            SaturdayPagerEndTime   = item.SaturdayPagerEndTime.ToString(),
                            SaturdayPagerStartTime = item.SaturdayPagerEndTime.ToString(),
                            SundayPagerEndTime     = item.SundayPagerEndTime.ToString(),
                            SundayPagerStartTime   = item.SundayPagerStartTime.ToString(),
                            NetSendAddress         = item.NetSendAddress,
                            WeekdayPagerStartTime  = item.WeekdayPagerStartTime.ToString(),
                            WeekdayPagerEndTime    = item.WeekdayPagerEndTime.ToString()
                        };
                    }

                    result.Operators = operators;
                    result.Success   = true;
                }
                catch (Exception ex)
                {
                    result.Success      = false;
                    result.ErrorMessage = ex.ToString();
                }
                await requestContext.SendResult(result);
            });
        }
Ejemplo n.º 6
0
        internal async Task HandleCreateAgentOperatorRequest(CreateAgentOperatorParams parameters, RequestContext <CreateAgentOperatorResult> requestContext)
        {
            await Task.Run(async() =>
            {
                var result = new CreateAgentOperatorResult();
                ConnectionInfo connInfo;
                ConnectionServiceInstance.TryFindConnection(
                    parameters.OwnerUri,
                    out connInfo);

                AgentOperatorInfo operatorInfo = parameters.Operator;
                CDataContainer dataContainer   = AdminService.CreateDataContainer(connInfo, databaseExists: true);
                STParameters param             = new STParameters(dataContainer.Document);
                param.SetParam("operator", operatorInfo.Name);

                using (AgentOperator agentOperator = new AgentOperator(dataContainer, operatorInfo))
                {
                    agentOperator.CreateOrUpdate();
                }

                await requestContext.SendResult(result);
            });
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public AgentOperatorActions(
            CDataContainer dataContainer,
            AgentOperatorInfo operatorInfo,
            ConfigAction configAction)
        {
            if (dataContainer == null)
            {
                throw new ArgumentNullException("dataContainer");
            }

            if (operatorInfo == null)
            {
                throw new ArgumentNullException("operatorInfo");
            }

            this.operatorInfo  = operatorInfo;
            this.DataContainer = dataContainer;
            this.configAction  = configAction;

            STParameters parameters = new STParameters();

            parameters.SetDocument(dataContainer.Document);

            string agentOperatorName = null;

            if (parameters.GetParam("operator", ref agentOperatorName))
            {
                this.operatorsData = new AgentOperatorsData(
                    dataContainer,
                    agentOperatorName,
                    createMode: configAction == ConfigAction.Create);
            }
            else
            {
                throw new ArgumentNullException("agentOperatorName");
            }
        }
        /// <summary>
        /// apply any changes to the operator. If the operator does not exist create it.
        /// </summary>
        public void ApplyChanges(AgentOperatorInfo operatorInfo)
        {
            // do nothing if we are read only
            if (this.readOnly)
            {
                return;
            }

            JobServer jobServer = GetJobServer();

            // get the operator. This will create a new one if it does not already exist
            Microsoft.SqlServer.Management.Smo.Agent.Operator currentOperator = GetCurrentOperator();

            // general tab
            currentOperator.Enabled = operatorInfo.Enabled;

            if (!string.IsNullOrWhiteSpace(operatorInfo.EmailAddress))
            {
                currentOperator.EmailAddress = operatorInfo.EmailAddress;
            }

            if (!string.IsNullOrWhiteSpace(operatorInfo.PagerAddress))
            {
                currentOperator.PagerAddress = operatorInfo.PagerAddress;
            }

            currentOperator.PagerDays = this.pagerDays;

            if ((operatorInfo.PagerDays & Contracts.WeekDays.WeekDays) > 0)
            {
                TimeSpan weekdayPagerStartTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.WeekdayPagerStartTime))
                {
                    weekdayPagerStartTime = TimeSpan.Parse(operatorInfo.WeekdayPagerStartTime);
                }

                TimeSpan weekdayPagerEndTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.WeekdayPagerEndTime))
                {
                    weekdayPagerEndTime = TimeSpan.Parse(operatorInfo.WeekdayPagerEndTime);
                }

                currentOperator.WeekdayPagerStartTime = weekdayPagerStartTime;
                currentOperator.WeekdayPagerEndTime   = weekdayPagerEndTime;
            }

            if ((operatorInfo.PagerDays & Contracts.WeekDays.Saturday) > 0)
            {
                TimeSpan saturdayPagerStartTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.SaturdayPagerStartTime))
                {
                    saturdayPagerStartTime = TimeSpan.Parse(operatorInfo.SaturdayPagerStartTime);
                }

                TimeSpan saturdayPagerEndTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.SaturdayPagerEndTime))
                {
                    saturdayPagerEndTime = TimeSpan.Parse(operatorInfo.SaturdayPagerEndTime);
                }

                currentOperator.SaturdayPagerStartTime = saturdayPagerStartTime;
                currentOperator.SaturdayPagerEndTime   = saturdayPagerEndTime;
            }

            if ((operatorInfo.PagerDays & Contracts.WeekDays.Sunday) > 0)
            {
                TimeSpan sundayPagerStartTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.SundayPagerStartTime))
                {
                    sundayPagerStartTime = TimeSpan.Parse(operatorInfo.SundayPagerStartTime);
                }

                TimeSpan sundayPagerEndTime = default(TimeSpan);
                if (!string.IsNullOrWhiteSpace(operatorInfo.SundayPagerEndTime))
                {
                    sundayPagerEndTime = TimeSpan.Parse(operatorInfo.SundayPagerEndTime);
                }

                currentOperator.SundayPagerStartTime = sundayPagerStartTime;
                currentOperator.SundayPagerEndTime   = sundayPagerEndTime;
            }

            if (this.createMode)
            {
                // create the object
                currentOperator.Create();
                this.originalOperatorName = this.name;
            }
            else
            {
                // alter the object
                currentOperator.Alter();
            }

            // only set this up if the notifications has been set
            if (this.alertNotifications != null)
            {
                SqlServer.Management.Smo.Agent.NotifyMethods notifyMethods;
                for (int i = 0; i < alertNotifications.Count; ++i)
                {
                    notifyMethods = 0;

                    if (alertNotifications[i].NotifyEmail)
                    {
                        notifyMethods |= SqlServer.Management.Smo.Agent.NotifyMethods.NotifyEmail;
                    }
                    if (alertNotifications[i].NotifyPager)
                    {
                        notifyMethods |= SqlServer.Management.Smo.Agent.NotifyMethods.Pager;
                    }

                    bool alertAlreadyNotifiesOperator = false;

                    // if we're not creating see if the current alert already notifies this operator
                    if (!createMode)
                    {
                        DataTable notifications = alertNotifications[i].Alert.EnumNotifications(this.originalOperatorName);
                        if (notifications.Rows.Count > 0)
                        {
                            alertAlreadyNotifiesOperator = true;
                        }
                    }

                    // either update or clear existing notifications
                    if (alertAlreadyNotifiesOperator)
                    {
                        if (notifyMethods != SqlServer.Management.Smo.Agent.NotifyMethods.None)
                        {
                            alertNotifications[i].Alert.UpdateNotification(this.originalOperatorName, notifyMethods);
                        }
                        else
                        {
                            alertNotifications[i].Alert.RemoveNotification(this.originalOperatorName);
                        }
                    }
                    else if (notifyMethods != SqlServer.Management.Smo.Agent.NotifyMethods.None)
                    {
                        // add a new notification
                        alertNotifications[i].Alert.AddNotification(this.originalOperatorName, notifyMethods);
                    }
                }
            }

            // see if we need to rename. This has to be done last otherwise any scripts generated will be incorrect.
            if (!this.createMode && currentOperator.Name != this.originalOperatorName)
            {
                currentOperator.Rename(this.name);
                if (this.dataContainer.Server.ConnectionContext.SqlExecutionModes != SqlExecutionModes.CaptureSql)
                {
                    this.originalOperatorName = this.name;
                }
            }
            // update state if we aren't scripting
            if (this.createMode && this.dataContainer.Server.ConnectionContext.SqlExecutionModes != SqlExecutionModes.CaptureSql)
            {
                this.createMode = false;
            }
        }