Skip to content

Commit

Permalink
validation on config version, remove repeated code
Browse files Browse the repository at this point in the history
  • Loading branch information
mblackstock committed Oct 24, 2020
1 parent 9434bab commit c372aca
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 198 deletions.
126 changes: 87 additions & 39 deletions influxdb.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,38 @@
</script>

<script type="text/javascript">
function getVersion() {
return $("#node-config-input-influxdbVersion option:selected").val();
}
RED.nodes.registerType('influxdb', {
category: 'config',
color: "rgb(218, 196, 180)",
defaults: {
hostname: {value: "127.0.0.1", required: true},
port: {value: 8086, required: true},
protocol: {value: "http", required: true},
database: {value: "database", required: true},
database: {value: "database",
validate:function(db) {
let version = getVersion();
if (!version || version === '1.x') {
return db.length > 0;
}
return true;
}
},
name: {value: ""},
usetls: {value: false},
tls: {type:"tls-config",required: false},
influxdbVersion: {value: "1.x", required: false},
url: {value: "http://localhost:8086", required: true},
url: {value: "http://localhost:8086",
validate:function(url) {
let version = getVersion();
if (version === '1.8-flux' || version === '2.0') {
return url.length > 0;
}
return true;
}
},
rejectUnauthorized: {value: true}
},
credentials: {
Expand All @@ -73,13 +92,11 @@
token: {type: "password"}
},
label: function () {
if (!this.influxdbVersion || this.influxdbVersion === '1.x') {
if (this.influxdbVersion === '1.8-flux' || this.influxdbVersion === '2.0') {
return this.name || "[v"+this.influxdbVersion+"] " + this.url;
} else {
return this.name || "[v1.x] " + this.hostname + ":" + this.port + "/" + this.database;
} else if (this.influxdbVersion === '1.8-flux') {
return this.name || "[v1.8-flux] " + this.url;
} else if (this.influxdbVersion === '2.0') {
return this.name || "[v2.0] " + this.url;
}
}
},
oneditprepare: function () {
function updateTLSOptions() {
Expand Down Expand Up @@ -139,7 +156,6 @@
$("#node-config-input-usetls").on("click", function () {
updateTLSOptions();
});
break;
}
});
// if version not set assume 1.x to support older flows
Expand Down Expand Up @@ -237,6 +253,17 @@
</script>

<script type="text/javascript">
function selectedVersion() {
// use prefix to update UI when version of config node changes
var optionSelected = $("#node-input-influxdb option:selected").text();
var influxDBVersionMatches = optionSelected.match(/\[(.*?)\]/);
var influxDBVersion = '';
if (influxDBVersionMatches) {
influxDBVersion = influxDBVersionMatches[1];
}
return influxDBVersion;
}

RED.nodes.registerType('influxdb out', {
category: 'storage-output',
color: "rgb(218, 196, 180)",
Expand All @@ -246,11 +273,33 @@
measurement: {value: ""},
precision: {value: ""},
retentionPolicy: {value: ""},
database: {value: "database", required: true},
database: {value: "database",
validate:function(db) {
if (selectedVersion() === 'v1.8-flux') {
return db.length > 0;
}
return true;
}
},
precisionV18FluxV20: {value: "ms"},
retentionPolicyV18Flux: {value: ""},
org: {value: "organisation", required: true},
bucket: {value: "bucket", required: true}
org: {value: "organisation",
validate:function(org) {
if (selectedVersion() === 'v2.0') {
return org.length > 0;
}
return true;
}
},
bucket: {value: "bucket",
validate:function(bucket) {
let version = selectedVersion()
if (version === 'v2.0') {
return bucket.length > 0;
}
return true;
}
}
},
inputs: 1,
outputs: 0,
Expand Down Expand Up @@ -312,20 +361,15 @@
}

$("#node-input-influxdb").change(function () {
// use prefix to update UI when version of config node changes
var optionSelected = $("#node-input-influxdb option:selected").text();
var influxDBVersionMatches = optionSelected.match(/\[(.*?)\]/);
var influxDBVersion = '';
if (influxDBVersionMatches) {
influxDBVersion = influxDBVersionMatches[1];
}

if (influxDBVersion === 'v1.x') {
update18Options();
} else if (influxDBVersion === 'v1.8-flux') {
update18FluxOptions();
} else if (influxDBVersion === 'v2.0') {
update20Options();
switch (selectedVersion()) {
case 'v1.8-flux':
update18FluxOptions();
break;
case 'v2.0':
update20Options();
break;
default:
update18Options();
}
});

Expand Down Expand Up @@ -522,7 +566,14 @@
rawOutput: {value: false},
precision: {value: ""},
retentionPolicy: {value: ""},
org: {value: "organisation", required: true}
org: { value: "organisation",
validate:function(org) {
if (selectedVersion() === 'v2.0') {
return org.length > 0;
}
return true;
}
}
},
inputs: 1,
outputs: 1,
Expand Down Expand Up @@ -579,18 +630,15 @@
}

$("#node-input-influxdb").change(function () {
var optionSelected = $("#node-input-influxdb option:selected").text();
var influxDBVersionMatches = optionSelected.match(/\[(.*?)\]/);
var influxDBVersion = '';
if (influxDBVersionMatches) {
var influxDBVersion = influxDBVersionMatches[1];
}
if (influxDBVersion === 'v1.x' || influxDBVersion == '') {
update18Options();
} else if (influxDBVersion === 'v1.8-flux') {
update18FluxOptions();
} else if (influxDBVersion === 'v2.0') {
update20Options();
switch (selectedVersion()) {
case 'v1.8-flux':
update18FluxOptions();
break;
case 'v2.0':
update20Options();
break;
default:
update18Options();
}
});
},
Expand Down
Loading

0 comments on commit c372aca

Please sign in to comment.