From b8c31e1138b6d5fecc8b848e7f48c30a5772831f Mon Sep 17 00:00:00 2001 From: Karl Burtram Date: Mon, 16 Jul 2018 13:18:47 -0700 Subject: [PATCH] Fix Agent Job rename (#657) --- .../Agent/AgentService.cs | 16 ++++++++---- .../Agent/Jobs/JobData.cs | 25 ++++++++++--------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs index 6c116865..4d2fe264 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs @@ -275,6 +275,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent { var result = await ConfigureAgentJob( parameters.OwnerUri, + parameters.Job.Name, parameters.Job, ConfigAction.Create, ManagementUtils.asRunType(parameters.TaskExecutionMode)); @@ -290,6 +291,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent { var result = await ConfigureAgentJob( parameters.OwnerUri, + parameters.OriginalJobName, parameters.Job, ConfigAction.Update, ManagementUtils.asRunType(parameters.TaskExecutionMode)); @@ -305,6 +307,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent { var result = await ConfigureAgentJob( parameters.OwnerUri, + parameters.Job.Name, parameters.Job, ConfigAction.Drop, ManagementUtils.asRunType(parameters.TaskExecutionMode)); @@ -837,6 +840,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent internal async Task> ConfigureAgentJob( string ownerUri, + string originalJobName, AgentJobInfo jobInfo, ConfigAction configAction, RunType runType) @@ -847,7 +851,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent { JobData jobData; CDataContainer dataContainer; - CreateJobData(ownerUri, jobInfo.Name, out dataContainer, out jobData, jobInfo); + CreateJobData(ownerUri, originalJobName, out dataContainer, out jobData, configAction, jobInfo); using (JobActions actions = new JobActions(dataContainer, jobData, configAction)) { @@ -1032,20 +1036,22 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent string jobName, out CDataContainer dataContainer, out JobData jobData, + ConfigAction configAction = ConfigAction.Create, AgentJobInfo jobInfo = null) { ConnectionInfo connInfo; ConnectionServiceInstance.TryFindConnection(ownerUri, out connInfo); dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true); - + XmlDocument jobDoc = CreateJobXmlDocument(dataContainer.Server.Name.ToUpper(), jobName); dataContainer.Init(jobDoc.InnerXml); - + STParameters param = new STParameters(dataContainer.Document); - param.SetParam("job", string.Empty); + string originalName = jobInfo != null && !string.Equals(jobName, jobInfo.Name) ? jobName : string.Empty; + param.SetParam("job", configAction == ConfigAction.Update ? jobName : string.Empty); param.SetParam("jobid", string.Empty); - jobData = new JobData(dataContainer, jobInfo); + jobData = new JobData(dataContainer, jobInfo, configAction); } public static XmlDocument CreateJobXmlDocument(string svrName, string jobName) diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/JobData.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/JobData.cs index 23e896b8..8236b03d 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/JobData.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Jobs/JobData.cs @@ -275,7 +275,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent } } - public String Description + public string Description { get { @@ -601,7 +601,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent #endregion #region construction - public JobData(CDataContainer data, AgentJobInfo jobInfo = null) + public JobData(CDataContainer data, AgentJobInfo jobInfo = null, ConfigAction configAction = ConfigAction.Create) { this.context = data; @@ -632,10 +632,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent this.targetLocalServer = false; } // we are in properties mode. - if (this.originalName.Length > 0 || !string.IsNullOrEmpty(this.jobIdString)) + if (configAction == ConfigAction.Update) { this.mode = ActionMode.Edit; - + CheckAndLoadGeneralData(); } else if (this.script.Length > 0) { @@ -653,13 +653,14 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent // set defaults that do not involve going to the server to retrieve SetDefaults(); } - + // load AgentJobInfo data if (jobInfo != null) { this.currentName = jobInfo.Name; this.owner = jobInfo.Owner; - this.description = jobInfo.Description; + this.description = jobInfo.Description; + this.enabled = jobInfo.Enabled; } } #endregion @@ -758,7 +759,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent Job job = this.Job; - this.currentName = this.originalName; + this.currentName = job.Name; this.description = job.Description; this.enabled = job.IsEnabled; // this.source = job.Source; @@ -1112,8 +1113,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent this.mode = creating ? ActionMode.Create : ActionMode.Edit; - ///Before any job posting if donem make sure that if this is an MSX job that the user has selected at - ///least one Target Server. + // Before any job posting if donem make sure that if this is an MSX job that the user has selected at + // least one Target Server. if (!this.targetLocalServer) { for (int i = 0; i < this.AvailableTargetServers.Length; ++i) @@ -1126,7 +1127,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent } if (!targetServerSelected) { - ///Not target servers selected. Throw error. + // Not target servers selected. Throw error. throw new ApplicationException(SR.TargetServerNotSelected); } } @@ -1267,8 +1268,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent , job.JobID)); } - //Do not attempt to save the job alert if we are in scripting mode, since the job id does not - //yet exists. + // Do not attempt to save the job alert if we are in scripting mode, since the job id does not + // yet exists. if (jobAlerts != null && !scripting) { this.jobAlerts.ApplyChanges(job);