public bool Drop()
        {
            if (this.DataContainer.Server.JobServer.ProxyAccounts.Contains(this.proxyAccountName))
            {
                // Try refresh and check again
                this.DataContainer.Server.JobServer.ProxyAccounts.Refresh();
                if (this.DataContainer.Server.JobServer.ProxyAccounts.Contains(this.proxyAccountName))
                {
                    ProxyAccount proxyAccount = AgentProxyAccount.GetProxyAccount(this.proxyAccountName, this.DataContainer.Server.JobServer);
                    proxyAccount.DropIfExists();
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        internal async Task <bool> ConfigureAgentProxy(
            string ownerUri,
            string accountName,
            AgentProxyInfo proxy,
            AgentConfigAction configAction)
        {
            return(await Task <bool> .Run(() =>
            {
                try
                {
                    ConnectionInfo connInfo;
                    ConnectionServiceInstance.TryFindConnection(
                        ownerUri,
                        out connInfo);

                    CDataContainer dataContainer = AdminService.CreateDataContainer(connInfo, databaseExists: true);
                    STParameters param = new STParameters(dataContainer.Document);
                    param.SetParam("proxyaccount", accountName);

                    using (AgentProxyAccount agentProxy = new AgentProxyAccount(dataContainer, proxy))
                    {
                        if (configAction == AgentConfigAction.Create)
                        {
                            return agentProxy.Create();
                        }
                        else if (configAction == AgentConfigAction.Update)
                        {
                            return agentProxy.Update();
                        }
                        else if (configAction == AgentConfigAction.Drop)
                        {
                            return agentProxy.Drop();
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                catch (Exception)
                {
                    // log exception here
                    return false;
                }
            }));
        }
        /// <summary>
        /// It creates a new ProxyAccount or gets an existing
        /// one from JobServer and updates all properties.
        /// </summary>
        private bool CreateOrUpdateProxyAccount(
            AgentProxyInfo proxyInfo,
            bool isUpdate)
        {
            ProxyAccount proxyAccount = null;

            if (!isUpdate)
            {
                proxyAccount = new ProxyAccount(this.DataContainer.Server.JobServer,
                                                proxyInfo.AccountName,
                                                proxyInfo.CredentialName,
                                                proxyInfo.IsEnabled,
                                                proxyInfo.Description);

                UpdateProxyAccount(proxyAccount);
                proxyAccount.Create();
            }
            else if (this.DataContainer.Server.JobServer.ProxyAccounts.Contains(this.proxyAccountName))
            {
                // Try refresh and check again
                this.DataContainer.Server.JobServer.ProxyAccounts.Refresh();
                if (this.DataContainer.Server.JobServer.ProxyAccounts.Contains(this.proxyAccountName))
                {
                    // fail since account exists and asked to create a new one
                    if (!isUpdate)
                    {
                        return(false);
                    }

                    proxyAccount = AgentProxyAccount.GetProxyAccount(this.proxyAccountName, this.DataContainer.Server.JobServer);
                    // Set the other properties
                    proxyAccount.CredentialName = proxyInfo.CredentialName;
                    proxyAccount.Description    = proxyInfo.Description;

                    UpdateProxyAccount(proxyAccount);
                    proxyAccount.Alter();

                    // Rename the proxy if needed
                    // This has to be done after Alter, in order to
                    // work correcly when scripting this action.
                    if (this.proxyAccountName != proxyInfo.AccountName)
                    {
                        proxyAccount.Rename(proxyInfo.AccountName);
                    }
                }
            }
            else
            {
                return(false);
            }

            return(true);

#if false  // @TODO - reenable subsystem code below
            // Update the subsystems
            foreach (AgentSubSystem subsystem in this.addSubSystems)
            {
                proxyAccount.AddSubSystem(subsystem);
            }

            foreach (AgentSubSystem subsystem in this.removeSubSystems)
            {
                proxyAccount.RemoveSubSystem(subsystem);

                // Update jobsteps that use this proxy accunt
                // when some subsystems are removed from it
                string reassignToProxyName = this.reassignToProxyNames[(int)subsystem];

                if (reassignToProxyName != null)
                {
                    // if version is sql 11 and above call SMO API  to reassign proxy account
                    if (Utils.IsSql11OrLater(this.DataContainer.Server.ServerVersion))
                    {
                        proxyAccount.Reassign(reassignToProxyName);
                    }
                    else
                    {
                        // legacy code
                        // Get a list of all job step objects that use this proxy and this subsystem
                        Request req = new Request();
                        req.Urn = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                "Server/JobServer/Job/Step[@ProxyName=\'{0}\' and @SubSystem={1}]",
                                                Urn.EscapeString(proxyAccount.Name),
                                                (int)subsystem);
                        req.Fields = new string[] { "Name" };
                        req.ParentPropertiesRequests = new PropertiesRequest[1] {
                            new PropertiesRequest()
                        };
                        req.ParentPropertiesRequests[0].Fields = new string[] { "Name" };

                        Enumerator en    = new Enumerator();
                        DataTable  table = en.Process(this.DataContainer.ServerConnection, req);
                        foreach (DataRow row in table.Rows)
                        {
                            // Get the actual job step object using urn
                            string  urnString = string.Format(System.Globalization.CultureInfo.InvariantCulture, "Server/JobServer/Job[@Name=\"{0}\"/Step[@Name=\"{1}\"", row["Job_Name"], row["Name"]);
                            Urn     urn       = new Urn(urnString);
                            JobStep jobStep   = (JobStep)this.DataContainer.Server.GetSmoObject(urn);

                            jobStep.ProxyName = reassignToProxyName;
                            jobStep.Alter();
                        }
                    }
                }
            }
#endif
        }