コード例 #1
0
        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);
        }