Skip to content

Commit

Permalink
Merge pull request NETWAYS#16 from NETWAYS/feature/envflags
Browse files Browse the repository at this point in the history
Add option to set flags via env variables
  • Loading branch information
RincewindsHat authored Dec 6, 2023
2 parents 02634bd + 8b38157 commit 00cddd5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ optional arguments:
-h, --help show this help message and exit
--api API, -A API VMware NSX-T URL without any sub-path (e.g. https://vmware-nsx.local)
--username USERNAME, -u USERNAME
Username for Basic Auth
Username for Basic Auth (CHECK_VMWARE_NSXT_API_USER)
--password PASSWORD, -p PASSWORD
Password for Basic Auth
Password for Basic Auth (CHECK_VMWARE_NSXT_API_PASSWORD)
--mode MODE, -m MODE Check mode
--exclude [EXCLUDE ...]
Exclude alarms or usage from the check results.
Expand All @@ -45,6 +45,8 @@ The `--exclude` parameter will match against alarms and capacity-usage. It uses
* alarms: `severity` `node_display_name` `feature_display_name` `event_type_display_name`
* capacity-usage: `severity` `display_name`

Various flags can be set with environment variables, refer to the help to see which flags.

## Examples

Mode: cluster-status
Expand Down
18 changes: 14 additions & 4 deletions check_vmware_nsxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,24 @@ def worst_state(*states):


def commandline(args):
"""
Parse commandline arguments.
"""
def environ_or_required(key):
return ({'default': os.environ.get(key)} if os.environ.get(key) else {'required': True})

parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument('--api', '-A', required=True,
help='VMware NSX-T URL without any sub-path (e.g. https://vmware-nsx.local)')
parser.add_argument('--api', '-A',
**environ_or_required('CHECK_VMWARE_NSXT_API_URL'),
help='VMware NSX-T URL without any sub-path (e.g. https://vmware-nsx.local)')
parser.add_argument('--username', '-u',
help='Username for Basic Auth', required=True)
**environ_or_required('CHECK_VMWARE_NSXT_API_USER'),
help='Username for Basic Auth')
parser.add_argument('--password', '-p',
help='Password for Basic Auth', required=True)
**environ_or_required('CHECK_VMWARE_NSXT_API_PASSWORD'),
help='Password for Basic Auth')

parser.add_argument('--mode', '-m', choices=['cluster-status', 'alarms', 'capacity-usage'],
help='Check mode to exectue. Hint: alarms will only include open alarms.', required=True)
parser.add_argument('--exclude', nargs='*', action='extend', type=str,
Expand Down
4 changes: 2 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pylint==2.15.0
coverage==7.0.5
pylint==3.0.2
coverage==7.3.2
12 changes: 11 additions & 1 deletion test_check_vmware_nsxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import unittest
import unittest.mock as mock
import sys

import os
import datetime
import json
Expand Down Expand Up @@ -42,6 +41,17 @@ def test_commandline(self):
self.assertFalse(actual.insecure)
self.assertEqual(actual.max_age, 5)

def test_commandline_fromenv(self):
os.environ['CHECK_VMWARE_NSXT_API_USER'] = 'GEH'
os.environ['CHECK_VMWARE_NSXT_API_PASSWORD'] = 'HEIM'

actual = commandline(['-A', 'api', '-m', 'alarms'])
self.assertEqual(actual.username, 'GEH')
self.assertEqual(actual.password, 'HEIM')

os.unsetenv('CHECK_VMWARE_NSXT_API_USER')
os.unsetenv('CHECK_VMWARE_NSXT_API_PASSWORD')

class UtilTesting(unittest.TestCase):

def test_worst_state(self):
Expand Down

0 comments on commit 00cddd5

Please sign in to comment.