public VirtAccount CreateOrUpdateVirtAccount([NotNull] string username, string password = null, string group = null) { if (username == null) { throw new ArgumentNullException("username"); } try { _server.LockServerSettings(SettingsLockTimeout); _server.LoadServerSettings(); // check for existing user var query = string.Format(@"virtAccount eqi ""{0}""", username); var exists = int.Parse(_server.Query("access.virtAccounts.Count({0})", query)) > 0; var virtAccount = exists ? GetVirtAccounts(query).SingleOrDefault() : new VirtAccount("access.virtAccounts.New", "", ""); if (!exists) { _server.Command(@"access.virtAccounts.NewClear"); } _server.Command(@"{0}.virtAccount ""{1}""", virtAccount.Id, username); if (!string.IsNullOrWhiteSpace(password)) { _server.Command(@"{0}.virtPassword.Set ""{1}""", virtAccount.Id, password); } if (!string.IsNullOrWhiteSpace(group)) { _server.Command(@"{0}.group ""{1}""", virtAccount.Id, group); } if (!exists) { _server.Command("access.virtAccounts.NewCommit"); } _server.SaveServerSettings(); virtAccount = GetVirtAccounts(query).Single(); return(virtAccount); } catch (COMException ex) { throw new BitviseSSHServerException(ex); } finally { _server.UnlockServerSettings(); } }
/// <summary> /// Adds retry/timeout ability to the LockServerSettings method. /// </summary> public static void LockServerSettings([NotNull] this BssCfg641 server, TimeSpan? timeout) { if (server == null) throw new ArgumentNullException("server"); if (!timeout.HasValue) { server.LockServerSettings(); return; } try { Attempt.Repeatedly.Do(server.LockServerSettings) .CatchWhere(x => x.Exception is COMException) .TakeForDuration(timeout.Value) .DelayWhereFailed(TimeSpan.FromMilliseconds(250)) .ThrowIfCantSucceed(); } catch (RepeatedFailureException ex) { throw new BitviseServerSettingsLockingException("Could not obtain server settings lock.", ex); } }