コード例 #1
0
        private void HandleSaveDBSettingPostBack(NameValueCollection parts)
        {
            StringBuilder results = new StringBuilder();

            // Validate

            if (!System.Uri.TryCreate(parts[DBUriKey], UriKind.Absolute, out Uri dbUri))
            {
                results.AppendLine("Url is not Valid.<br>");
            }

            string database = parts[DBKey];

            if (string.IsNullOrWhiteSpace(database))
            {
                results.AppendLine("Database is not Valid.<br>");
            }

            string username  = parts[UserKey];
            string password  = parts[PasswordKey];
            string retention = parts[RetentionKey];

            try
            {
                using (var influxDbClient = new InfluxDBClient(dbUri.ToString(), username, password))
                {
                    var databases = influxDbClient.GetInfluxDBNamesAsync().ResultForSync();

                    var selectedDb = databases.Where((db) => { return(db == database); }).FirstOrDefault();
                    if (selectedDb == null)
                    {
                        results.AppendLine("Database not found on server.<br>");
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(retention))
                        {
                            var retentionPolcies = influxDbClient.GetRetentionPoliciesAsync(selectedDb).ResultForSync();
                            if (!retentionPolcies.Any(r => r.Name == retention))
                            {
                                results.AppendLine("Retention policy not found for database.<br>");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                results.AppendLine(Invariant($"Failed to connect to InfluxDB with {ex.GetFullMessage()}"));
            }

            if (results.Length > 0)
            {
                this.divToUpdate.Add(ErrorDivId, results.ToString());
            }
            else
            {
                this.divToUpdate.Add(ErrorDivId, string.Empty);
                var dbConfig = new InfluxDBLoginInformation(dbUri,
                                                            PluginConfig.CheckEmptyOrWhitespace(username),
                                                            PluginConfig.CheckEmptyOrWhitespace(password),
                                                            PluginConfig.CheckEmptyOrWhitespace(parts[DBKey]),
                                                            PluginConfig.CheckEmptyOrWhitespace(retention));
                this.pluginConfig.DBLoginInformation = dbConfig;
                this.pluginConfig.DebugLogging       = parts[DebugLoggingId] == "checked";
                this.pluginConfig.FireConfigChanged();
            }
        }