Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions Source/Libraries/Adapters/InfluxDBAdapters/InfluxDBOutputAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ public class InfluxDBOutputAdapter : OutputAdapterBase

// Constants

/// <summary>
/// Default value for <see cref="UserName"/>.
/// </summary>
public const string DefaultUserName = "root";

/// <summary>
/// Default value for <see cref="Password"/>.
/// </summary>
public const string DefaultPassword = "root";

/// <summary>
/// Default value for <see cref="UseParallelPosting"/>.
/// </summary>
Expand All @@ -76,6 +66,7 @@ public class InfluxDBOutputAdapter : OutputAdapterBase
private string m_databaseName; // Database name forInfluxDB connection
private string m_userName; // Username for InfluxDB connection
private string m_password; // Password for InfluxDB connection
private string m_authorizationToken; // Token for InfluxDB authorization
private bool m_useParallelPosting; // Enable parallel posting
private int m_valuesPerPost; // Maximum values to send per post (when using parallel posting)
private string m_connectionResponse; // Response from connection attempt
Expand Down Expand Up @@ -139,7 +130,7 @@ public string DatabaseName
/// <summary>
/// Gets or sets the user name for the InfluxDB connection.
/// </summary>
[ConnectionStringParameter, Description("Defines the the user name for the InfluxDB connection."), DefaultValue(DefaultUserName)]
[ConnectionStringParameter, Description("Defines the the user name for the InfluxDB connection."), DefaultValue(null)]
public string UserName
{
get
Expand All @@ -155,7 +146,7 @@ public string UserName
/// <summary>
/// Gets or sets the password for the InfluxDB connection.
/// </summary>
[ConnectionStringParameter, Description("Defines the password for the InfluxDB connection."), DefaultValue(DefaultPassword)]
[ConnectionStringParameter, Description("Defines the password for the InfluxDB connection."), DefaultValue(null)]
public string Password
{
get
Expand All @@ -168,6 +159,22 @@ public string Password
}
}

/// <summary>
/// Gets or sets the password for the InfluxDB connection.
/// </summary>
[ConnectionStringParameter, Description("Defines the token for the InfluxDB authorization."), DefaultValue(null)]
public string AuthorizationToken
{
get
{
return m_authorizationToken;
}
set
{
m_authorizationToken = value;
}
}

/// <summary>
/// Gets or sets flag that determines if multiple posts to InfluxDB should be made in parallel.
/// </summary>
Expand Down Expand Up @@ -310,13 +317,12 @@ public override void Initialize()

if (settings.TryGetValue("userName", out setting))
m_userName = setting;
else
m_userName = DefaultUserName;

if (settings.TryGetValue("password", out setting))
m_password = setting;
else
m_password = DefaultPassword;

if (settings.TryGetValue("authorizationToken", out setting))
m_authorizationToken = setting;

if (settings.TryGetValue("useParallelPosting", out setting))
m_useParallelPosting = setting.ParseBoolean();
Expand All @@ -333,7 +339,8 @@ public override void Initialize()
// Define request URI
UriBuilder requestUri = new UriBuilder(m_serverUri);
requestUri.Path = string.Format("db/{0}/series", m_databaseName.UriEncode());
requestUri.Query = string.Format("u={0}&p={1}", m_userName.UriEncode(), m_password.UriEncode());
if (!string.IsNullOrEmpty(m_userName) && !string.IsNullOrEmpty(m_password))
requestUri.Query = string.Format("u={0}&p={1}", m_userName.UriEncode(), m_password.UriEncode());
m_requestUri = requestUri.Uri;
}

Expand All @@ -347,7 +354,8 @@ protected override void AttemptConnection()
// Setup an authenticate request
UriBuilder requestUri = new UriBuilder(m_serverUri);
requestUri.Path = string.Format("db/{0}/authenticate", m_databaseName.UriEncode());
requestUri.Query = string.Format("u={0}&p={1}", m_userName.UriEncode(), m_password.UriEncode());
if (!string.IsNullOrEmpty(m_userName) && !string.IsNullOrEmpty(m_password))
requestUri.Query = string.Format("u={0}&p={1}", m_userName.UriEncode(), m_password.UriEncode());

// Create a web request for authentication that will at least make sure the server is available
HttpWebRequest request = WebRequest.Create(requestUri.Uri) as HttpWebRequest;
Expand All @@ -356,6 +364,9 @@ protected override void AttemptConnection()
{
request.ContentType = "application/json";

if (!string.IsNullOrEmpty(m_authorizationToken))
request.Headers.Add(HttpRequestHeader.Authorization, $"Token {m_authorizationToken}");

// Attempt query - if this doesn't throw an exception, query succeeded
using (WebResponse response = request.GetResponse())
{
Expand Down Expand Up @@ -431,6 +442,9 @@ private void PostMeasurementsToArchive(IMeasurement[] measurements)
request.AllowWriteStreamBuffering = true;
request.ContentType = "application/json";

if (!string.IsNullOrEmpty(m_authorizationToken))
request.Headers.Add(HttpRequestHeader.Authorization, $"Token {m_authorizationToken}");

// Build a JSON post expression with measurement values to use as post data
StringBuilder jsonData = new StringBuilder();

Expand Down
Loading