public async override Task<bool> InvokeAsync(string paramList) { var configs = _configBusiness.OpenDatabaseConfig().ToArray(); if (configs.Length > 1) { OutputWarning("There are {0} databases configured. When using multiple targets you will have to update the config files manually.", configs.Length); return false; } var currentConfig = configs.First(); var config = new InfluxDatabaseConfig(currentConfig.Url, string.Empty, string.Empty, string.Empty); var index = 0; var logonInfo = await GetUsernameAsync(paramList, index++, config, "config_database"); if (logonInfo == null) return false; var result = await ServiceCommands.GetServiceStatusAsync(); if (result != null) { await ServiceCommands.RestartServiceAsync(); } return true; }
protected async Task<IDatabaseConfig> GetUsernameAsync(string paramList, int index, IDatabaseConfig config, string action) { var dataChanged = false; var url = config.Url; IInfluxDbAgent client = null; InfluxDbApiResponse response = null; try { if (!string.IsNullOrEmpty(config.Name) && !string.IsNullOrEmpty(config.Username) && !string.IsNullOrEmpty(config.Password)) { client = _influxDbAgentLoader.GetAgent(config); response = await MetaDataBusiness.TestWriteAccess(client, action); } } catch (Exception exception) { OutputError(exception.Message); } if (response == null || !response.Success) OutputInformation("Enter the database, username and password for the InfluxDB."); while (response == null || !response.Success) { var database = string.Empty; try { database = QueryParam<string>("DatabaseName", GetParam(paramList, index++)); var user = QueryParam<string>("Username", GetParam(paramList, index++)); var password = QueryParam<string>("Password", GetParam(paramList, index++)); config = new InfluxDatabaseConfig(url, user, password, database); client = _influxDbAgentLoader.GetAgent(config); response = await MetaDataBusiness.TestWriteAccess(client, action); dataChanged = true; } catch (CommandEscapeException) { return null; } catch (InfluxDbApiException exception) { if (exception.StatusCode == HttpStatusCode.NotFound) { var create = QueryParam("Database does not exist, create?", GetParam(paramList, index++), new Dictionary<bool, string>() { { true, "Yes" }, { false, "No" } }); if (create) { client.CreateDatabaseAsync(database); response = MetaDataBusiness.TestWriteAccess(client, action).Result; dataChanged = true; } } } catch (Exception exception) { OutputError("{0}", exception.Message); } } OutputInformation("Access to database {0} confirmed.", config.Name); if (dataChanged) _configBusiness.SaveDatabaseConfig(config.Name, config.Username, config.Password); return config; }
private static IDatabaseConfig GetInfluxDatabaseConfig(XmlNode database) { string url = null; string username = null; string password = null; string name = null; foreach (XmlElement item in database.ChildNodes) { switch (item.Name) { case "Url": url = item.InnerText; break; case "Username": username = item.InnerText; break; case "Password": password = Decrypt(item.InnerText); break; case "Name": name = item.InnerText; break; case "": break; } } var db = new InfluxDatabaseConfig(url, username, password, name); return db; }
private void SaveDatabaseConfigEx(InfluxDatabaseConfig newDbConfig) { var path = GetAppDataFolder(); var databaseConfigFilePath = path + "\\database.xml"; var xml = new XmlDocument(); var xme = xml.CreateElement(Constants.ServiceName); xml.AppendChild(xme); var dme = xml.CreateElement("Database"); xme.AppendChild(dme); var xmeUrl = xml.CreateElement("Url"); xmeUrl.InnerText = newDbConfig.Url; dme.AppendChild(xmeUrl); var xmeUsername = xml.CreateElement("Username"); xmeUsername.InnerText = newDbConfig.Username; dme.AppendChild(xmeUsername); var xmePassword = xml.CreateElement("Password"); xmePassword.InnerText = Encrypt(newDbConfig.Password); dme.AppendChild(xmePassword); var xmeName = xml.CreateElement("Name"); xmeName.InnerText = newDbConfig.Name; dme.AppendChild(xmeName); var xmlData = xml.ToFormattedString(); _fileLoaderAgent.WriteAllText(databaseConfigFilePath, xmlData); }
public void SaveDatabaseConfig(string databaseName, string username, string password) { var config = OpenDatabaseConfig().First(); var newDbConfig = new InfluxDatabaseConfig(config.Url, username, password, databaseName); SaveDatabaseConfigEx(newDbConfig); }
public void SaveDatabaseUrl(string url) { var config = OpenDatabaseConfig().First(); var newDbConfig = new InfluxDatabaseConfig(url, config.Username, config.Password, config.Name); SaveDatabaseConfigEx(newDbConfig); }
private static IDatabaseConfig GetInfluxDatabaseConfig(XmlNode database) { var enabled = IsDatabaseEnabled(database); string url = null; string username = null; string password = null; string name = null; TimeSpan? requestTimeout = null; foreach (XmlElement item in database.ChildNodes) { switch (item.Name) { case "Url": url = item.InnerText; break; case "Username": username = item.InnerText; break; case "Password": password = Decrypt(item.InnerText); break; case "Name": name = item.InnerText; break; case "RequestTimeoutMs": requestTimeout = new TimeSpan(0, 0, 0, 0, int.Parse(item.InnerText)); break; case "": break; } } var db = new InfluxDatabaseConfig(enabled, url, username, password, name, requestTimeout); return db; }