Skip to content

Commit

Permalink
add get_bes_conn_using_config_file to besapi, update precommit, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstew committed Nov 18, 2022
1 parent 405b955 commit 5045c89
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
10 changes: 3 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# https://github.com/pre-commit/pre-commit-hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.3.0
hooks:
- id: check-yaml
- id: check-json
Expand All @@ -27,19 +27,15 @@ repos:
# - id: no-commit-to-branch
# args: [--branch, main]
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.26.3
rev: v1.28.0
hooks:
- id: yamllint
args: [-c=.yamllint.yaml]
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
hooks:
- id: isort
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
- repo: https://github.com/psf/black
rev: 22.1.0
rev: 22.10.0
hooks:
- id: black
46 changes: 44 additions & 2 deletions src/besapi/besapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Library for communicating with the BES (BigFix) REST API.
"""

import configparser
import datetime
import json
import logging
Expand All @@ -17,8 +18,6 @@
import site
import string

# import urllib3.poolmanager

try:
from urllib import parse
except ImportError:
Expand Down Expand Up @@ -113,6 +112,7 @@ def parse_bes_modtime(string_datetime):
return datetime.datetime.strptime(string_datetime, "%a, %d %b %Y %H:%M:%S %z")


# import urllib3.poolmanager
# # https://docs.python-requests.org/en/latest/user/advanced/#transport-adapters
# class HTTPAdapterBiggerBlocksize(requests.adapters.HTTPAdapter):
# """custom HTTPAdapter for requests to override blocksize
Expand Down Expand Up @@ -150,6 +150,48 @@ def parse_bes_modtime(string_datetime):
# )


def get_bes_conn_using_config_file(conf_file=None):
"""
read connection values from config file
return besapi connection
"""
config_paths = [
"/etc/besapi.conf",
os.path.expanduser("~/besapi.conf"),
os.path.expanduser("~/.besapi.conf"),
"besapi.conf",
]
# if conf_file specified, then only use that:
if conf_file:
config_paths = [conf_file]

configparser_instance = configparser.ConfigParser()

found_config_files = configparser_instance.read(config_paths)

if found_config_files and configparser_instance:
print("Attempting BESAPI Connection using config file:", found_config_files)
try:
BES_ROOT_SERVER = configparser_instance.get("besapi", "BES_ROOT_SERVER")
except BaseException:
BES_ROOT_SERVER = None

try:
BES_USER_NAME = configparser_instance.get("besapi", "BES_USER_NAME")
except BaseException:
BES_USER_NAME = None

try:
BES_PASSWORD = configparser_instance.get("besapi", "BES_PASSWORD")
except BaseException:
BES_PASSWORD = None

if BES_ROOT_SERVER and BES_USER_NAME and BES_PASSWORD:
return BESConnection(BES_USER_NAME, BES_PASSWORD, BES_ROOT_SERVER)

return None


class BESConnection:
"""BigFix RESTAPI connection abstraction class"""

Expand Down
2 changes: 2 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,5 @@ class RequestResult(object):
'CMD /C python -m besapi ls clear ls conf "query number of bes computers" version error_count exit',
check=True,
)
bes_conn = besapi.besapi.get_bes_conn_using_config_file()
print("login succeeded:", bes_conn.login())

0 comments on commit 5045c89

Please sign in to comment.