Skip to content

Commit

Permalink
Add pylint configuration and fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed May 13, 2023
1 parent 73227c4 commit b4e829c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
uses: actions/checkout@v3
- name: Install dependencies
run: |
python -m pip install -r requirements.txt
python -m pip install -r requirements-dev.txt
- name: Lint
run: |
Expand Down
15 changes: 15 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# pylint config
# [FORMAT]
# good-names=m,p,l,ok
[MESSAGES CONTROL]
disable=fixme,
line-too-long,
too-many-locals,
too-many-arguments,
too-many-statements,
too-many-branches,
bare-except,
missing-module-docstring,
missing-function-docstring,
missing-class-docstring,
consider-using-f-string
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.PHONY: lint test coverage

lint:
python -m pylint check_vmware_nsxt.py
test:
python -m unittest -v -b test_check_vmware_nsxt.py
coverage:
python -m coverage run -m unittest test_check_vmware_nsxt.py
python -m coverage report -m --include check_vmware_nsxt.py
33 changes: 13 additions & 20 deletions check_vmware_nsxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ def request(self, url, method='GET'):
self.logger.debug("starting API %s request from: %s", method, url)

try:
response = requests.request(method, request_url, auth=HTTPBasicAuth(self.username, self.password), verify=self.verify)
response = requests.request(method, request_url, auth=HTTPBasicAuth(self.username, self.password), verify=self.verify, timeout=10)
except requests.exceptions.RequestException as req_exc:
raise CriticalException(req_exc)
raise CriticalException(req_exc) # pylint: disable=raise-missing-from

if response.status_code != 200:
raise CriticalException('Request to %s was not successful: %s' % (request_url, response.status_code))

try:
return response.json()
except Exception as e:
raise CriticalException('Could not decode API JSON: ' + str(e))
except Exception as json_exc:
raise CriticalException('Could not decode API JSON: ' + str(json_exc)) # pylint: disable=raise-missing-from

def get_cluster_status(self):
"""
Expand Down Expand Up @@ -261,8 +261,8 @@ def build_output(self):
self.summary.append("%d alarms" % count)
self.perfdata.append("alarms=%d;;;0" % count)

for state in states:
self.summary.append("%d %s" % (states[state], state.lower()))
for state, value in states.items():
self.summary.append("%d %s" % (value, state.lower()))

def build_status(self):
states = []
Expand Down Expand Up @@ -320,8 +320,8 @@ def build_output(self):
# Maybe we need count at some point...
# self.perfdata.append("%s_count=%d;;;0;%d" % (label, usage['current_usage_count'], usage['max_supported_count']))

for state in states:
self.summary.append("%d %s" % (states[state], state.lower()))
for state, value in states.items():
self.summary.append("%d %s" % (value, state.lower()))

if len(states) == 0:
self.summary.append("no usages")
Expand Down Expand Up @@ -420,9 +420,9 @@ def main():

if args.mode == 'cluster-status':
return client.get_cluster_status().print_and_return()
elif args.mode == 'alarms':
if args.mode == 'alarms':
return client.get_alarms().print_and_return()
elif args.mode == 'capacity-usage':
if args.mode == 'capacity-usage':
return client.get_capacity_usage().print_and_return()

print("[UNKNOWN] unknown mode %s" % args.mode)
Expand All @@ -432,17 +432,10 @@ def main():
if __package__ == '__main__' or __package__ is None:
try:
sys.exit(main())
except CriticalException as e:
print("[CRITICAL] " + str(e))
except CriticalException as main_exc:
print("[CRITICAL] " + str(main_exc))
sys.exit(CRITICAL)
except Exception:
except Exception: # pylint: disable=broad-except
exception = sys.exc_info()
print("[UNKNOWN] Unexpected Python error: %s %s" % (exception[0], exception[1]))

try:
import traceback
traceback.print_tb(exception[2])
except:
pass

sys.exit(UNKNOWN)
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pylint==2.15.0
coverage==7.0.5
7 changes: 7 additions & 0 deletions test_check_vmware_nsxt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3

import unittest
import unittest.mock as mock
import sys

sys.path.append('..')

0 comments on commit b4e829c

Please sign in to comment.