tweaking plugin/platform system, added ping platform

This commit is contained in:
Ross Mountjoy 2020-02-07 08:43:51 -05:00
parent 63f9f4b536
commit dba238fa29
6 changed files with 68 additions and 40 deletions

View File

@ -127,9 +127,10 @@ def check_groups(groups, current_user):
def get_data_source(data_source): def get_data_source(data_source):
data_source_args = [] data_source_args = {}
for arg in data_source.args: for arg in data_source.args:
data_source_args.append(row2dict(arg)) arg = row2dict(arg)
data_source_args[arg.get("key")] = arg.get("value")
data_source = row2dict(data_source) data_source = row2dict(data_source)
module = importlib.import_module( module = importlib.import_module(
f"dashmachine.platform.{data_source['platform']}", "." f"dashmachine.platform.{data_source['platform']}", "."

View File

@ -0,0 +1,23 @@
from requests import get
class Platform:
def __init__(self, data_source, data_source_args):
# parse the user's options from the config entries
for key, value in data_source_args.items():
self.__dict__[key] = value
def process(self):
try:
value = get(self.resource)
except Exception:
icon_class = "theme-failure-text"
if 599 >= value.status_code >= 400:
icon_class = "theme-failure-text"
if 399 >= value.status_code >= 300:
icon_class = "theme-warning-text"
if 299 >= value.status_code >= 100:
icon_class = "theme-success-text"
return f"<i class='material-icons right {icon_class}'>fiber_manual_record </i>"

View File

@ -1,41 +1,20 @@
from requests import get import json
from requests import get, post
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
from flask import render_template_string from flask import render_template_string
class Platform: class Platform:
def __init__(self, data_source, data_source_args): def __init__(self, data_source, data_source_args):
self.data_source = data_source
self.name = data_source["name"]
# parse the user's options from the config entries # parse the user's options from the config entries
for source_arg in data_source_args: for key, value in data_source_args.items():
if source_arg.get("key") == "resource": self.__dict__[key] = value
self.resource = source_arg.get("value")
if source_arg.get("key") == "method": # set defaults for omitted options
self.method = source_arg.get("value") if not hasattr(self, "method"):
self.method = "GET"
if source_arg.get("key") == "payload": if not hasattr(self, "authentication"):
self.payload = source_arg.get("value") self.authentication = None
if source_arg.get("key") == "authentication":
self.authentication = source_arg.get("value")
if source_arg.get("key") == "username":
self.username = source_arg.get("value")
if source_arg.get("key") == "password":
self.password = source_arg.get("value")
if source_arg.get("key") == "value_template":
self.value_template = source_arg.get("value")
if source_arg.get("key") == "data_template":
self.data_template = source_arg.get("value")
# set defaults for omitted options
if not hasattr(self, "method"):
self.method = "GET"
def process(self): def process(self):
if self.method.upper() == "GET": if self.method.upper() == "GET":
@ -43,6 +22,17 @@ class Platform:
value = get(self.resource).json() value = get(self.resource).json()
except Exception as e: except Exception as e:
value = f"{e}" value = f"{e}"
elif self.method.upper() == "POST":
if self.authentication:
if self.authentication.lower() == "digest":
auth = HTTPDigestAuth(self.username, self.password)
else:
auth = HTTPBasicAuth(self.username, self.password)
else:
auth = None
payload = json.loads(self.payload.replace("'", '"'))
value = post(self.resource, data=payload, auth=auth)
value_template = render_template_string(self.value_template, value=value) value_template = render_template_string(self.value_template, value=value)
data_template = render_template_string(self.data_template, value=value_template) return value_template
return data_template

View File

@ -11,7 +11,9 @@
--theme-color-font: #2c2f3a; --theme-color-font: #2c2f3a;
--theme-color-font-muted: rgba(44, 47, 58, 0.85); --theme-color-font-muted: rgba(44, 47, 58, 0.85);
--theme-color-font-muted2: rgba(44, 47, 58, 0.65); --theme-color-font-muted2: rgba(44, 47, 58, 0.65);
--theme-warning: #f44336; --theme-failure: #f44336;
--theme-warning: #ffae42;
--theme-success: #4BB543;
--theme-on-primary: #fff; --theme-on-primary: #fff;
} }
[data-theme="dark"] { [data-theme="dark"] {
@ -117,12 +119,24 @@
.theme-text { .theme-text {
color: var(--theme-color-font) !important; color: var(--theme-color-font) !important;
} }
.theme-failure {
background-color: var(--theme-failure) !important;
}
.theme-failure-text {
color: var(--theme-failure) !important;
}
.theme-warning { .theme-warning {
background-color: var(--theme-warning) !important; background-color: var(--theme-warning) !important;
} }
.theme-warning-text { .theme-warning-text {
color: var(--theme-warning) !important; color: var(--theme-warning) !important;
} }
.theme-success {
background-color: var(--theme-success) !important;
}
.theme-success-text {
color: var(--theme-success) !important;
}
.theme-muted-text { .theme-muted-text {
color: var(--theme-color-font-muted) !important; color: var(--theme-color-font-muted) !important;
} }

View File

@ -20,7 +20,7 @@ $( document ).ready(function() {
M.toast({html: 'Config applied successfully'}); M.toast({html: 'Config applied successfully'});
location.reload(true); location.reload(true);
} else { } else {
M.toast({html: data.data.msg, classes: "theme-warning"}); M.toast({html: data.data.msg, classes: "theme-failure"});
} }
} }
}); });
@ -70,7 +70,7 @@ $( document ).ready(function() {
data: $("#edit-user-form").serialize(), data: $("#edit-user-form").serialize(),
success: function(data){ success: function(data){
if (data.data.err !== 'success'){ if (data.data.err !== 'success'){
M.toast({html: data.data.err, classes: 'theme-warning'}); M.toast({html: data.data.err, classes: 'theme-failure'});
} else { } else {
$("#user-form-password").val(''); $("#user-form-password").val('');
$("#user-form-confirm_password").val(''); $("#user-form-confirm_password").val('');

View File

@ -16,8 +16,8 @@
{{ preload_circle() }} {{ preload_circle() }}
</li> </li>
<li class="tcdrop-error-msg tcdrop-li hide"> <li class="tcdrop-error-msg tcdrop-li hide">
<i class="tcdrop-delete-file material-icons theme-warning">error</i> <i class="tcdrop-delete-file material-icons theme-failure">error</i>
<span class="tcdrop-error-msg-txt file-name theme-warning-text"></span> <span class="tcdrop-error-msg-txt file-name theme-failure-text"></span>
</li> </li>
</ul> </ul>
</div> </div>