private void DeployManagedAccount(object modelHost, SPFarm farm, ManagedAccountDefinition managedAccountDefinition)
        {
            var currentAccount = GetManagedAccount(farm, managedAccountDefinition);

            var loginName = managedAccountDefinition.LoginName;
            var accounts = new SPFarmManagedAccountCollection(farm);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = currentAccount,
                ObjectType = typeof(SPManagedAccount),
                ObjectDefinition = managedAccountDefinition,
                ModelHost = modelHost
            });


            if (currentAccount == null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new managed account");

                currentAccount = accounts.FindOrCreateAccount(loginName);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = currentAccount,
                    ObjectType = typeof(SPManagedAccount),
                    ObjectDefinition = managedAccountDefinition,
                    ModelHost = modelHost
                });

                currentAccount.Update();
            }
            else
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing managed account");

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = currentAccount,
                    ObjectType = typeof(SPManagedAccount),
                    ObjectDefinition = managedAccountDefinition,
                    ModelHost = modelHost
                });

                currentAccount.Update();
            }
        }
        protected SPManagedAccount GetManagedAccount(SPFarm farm, ManagedAccountDefinition managedAccountDefinition)
        {
            var loginName = managedAccountDefinition.LoginName;
            var accounts = new SPFarmManagedAccountCollection(farm);

            SPManagedAccount currentAccount = null;

            try
            {
                currentAccount = accounts[loginName];
            }
            catch (Exception)
            { }

            return currentAccount;
        }
 public static ModelNode AddManagedAccount(this ModelNode model, ManagedAccountDefinition fielDefinition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(fielDefinition, action);
 }
 public static ModelNode AddManagedAccount(this ModelNode siteModel, ManagedAccountDefinition definition)
 {
     return AddManagedAccount(siteModel, definition, null);
 }