feat: add filter option exclude_tags (#159)

This commit is contained in:
Mathias Petermann 2022-03-03 09:08:27 +01:00 committed by GitHub
parent 7f2dc5a612
commit b5d362417d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 3 deletions

View File

@ -28,6 +28,9 @@ exclude_state: []
# needs to be a list of strings # needs to be a list of strings
exclude_vmid: [] exclude_vmid: []
# can be used to exclude vms by tags (proxmox 6+)
exclude_tags: []
pve: pve:
server: server:
user: user:

View File

@ -94,6 +94,12 @@ class Config():
"file": True, "file": True,
"type": environs.Env().list "type": environs.Env().list
}, },
"exclude_tags": {
"default": [],
"env": "EXCLUDE_TAGS",
"file": True,
"type": environs.Env().list
},
"pve.server": { "pve.server": {
"default": "", "default": "",
"env": "PVE_SERVER", "env": "PVE_SERVER",

View File

@ -157,6 +157,12 @@ class Discovery():
if str(obj["vmid"]) in self.config.config["exclude_vmid"]: if str(obj["vmid"]) in self.config.config["exclude_vmid"]:
continue continue
if (
isinstance(obj["tags"], str)
and not set(obj["tags"].split(",")).isdisjoint(self.config.config["exclude_tags"])
):
continue
filtered.append(item.copy()) filtered.append(item.copy())
return filtered return filtered

View File

@ -34,7 +34,8 @@ def qemus():
"disk": 0, "disk": 0,
"status": "running", "status": "running",
"netout": 12159205236, "netout": 12159205236,
"mem": 496179157 "mem": 496179157,
"tags": "unmonitored,excluded"
}, },
{ {
"diskwrite": 0, "diskwrite": 0,
@ -54,6 +55,25 @@ def qemus():
"netout": 12159205236, "netout": 12159205236,
"mem": 496179157 "mem": 496179157
}, },
{
"diskwrite": 0,
"vmid": "102",
"name": "102.example.com",
"cpu": 0.0202130478509556,
"diskread": 0,
"template": "",
"uptime": 3101505,
"maxdisk": 26843545600,
"maxmem": 1073741824,
"pid": "1765",
"cpus": 1,
"netin": 2856071643,
"disk": 0,
"status": "prelaunch",
"netout": 12159205236,
"mem": 496179157,
"tags": "monitored"
},
] ]

View File

@ -17,13 +17,31 @@ def get_mock(*args):
return False return False
def test_exclude(discovery_fixture, qemus): def test_exclude_vmid(discovery_fixture, qemus):
discovery_fixture.config.config["exclude_vmid"] = ["100", "101"] discovery_fixture.config.config["exclude_vmid"] = ["100", "101", "102"]
expected = [] expected = []
filtered = discovery_fixture._exclude(qemus) filtered = discovery_fixture._exclude(qemus)
assert filtered == expected assert filtered == expected
discovery_fixture.config.config["exclude_vmid"] = []
def test_exclude_state(discovery_fixture, qemus):
discovery_fixture.config.config["exclude_state"] = ["prelaunch"]
filtered = discovery_fixture._exclude(qemus)
assert len(filtered) == 2
discovery_fixture.config.config["exclude_state"] = []
def test_exclude_tags(discovery_fixture, qemus):
discovery_fixture.config.config["exclude_tags"] = ["unmonitored"]
filtered = discovery_fixture._exclude(qemus)
assert len(filtered) == 2
discovery_fixture.config.config["exclude_tags"] = []
def test_validate_ip(discovery_fixture, addresses): def test_validate_ip(discovery_fixture, addresses):