Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 1207b5d

Browse files
pszkamruk-splunkgithub-actions[bot]
authored andcommitted
feat: added job which checks if code is formatted correctly in Circle… (#20)
* feat: added job which checks if code is formatted correctly in CircleCi, code formatted using black * Creating file for storing CLA Signatures * @weliasz has signed the CLA from Pull Request #24 * @pszkamruk-splunk has signed the CLA from Pull Request #20 Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 1b21c8c commit 1207b5d

File tree

7 files changed

+81
-55
lines changed

7 files changed

+81
-55
lines changed

.circleci/config.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ jobs:
4040
path: build-container
4141
destination: build-container
4242

43+
format-code:
44+
docker:
45+
- image: circleci/python:3.8.6
46+
steps:
47+
- checkout
48+
- run:
49+
name: "Check Formatting"
50+
command: |
51+
pip install black
52+
black --check ./ --exclude mibs
53+
4354
release:
4455
docker:
4556
- image: circleci/node:12
@@ -68,11 +79,11 @@ jobs:
6879
- run:
6980
name: Release Docker Hub
7081
command: |
71-
export VERSION=${CIRCLE_TAG:=CIRCLE_SHA1}
82+
export VERSION=${CIRCLE_TAG:=CIRCLE_SHA1}
7283
VERSION_DOCKER_MMP=$(echo $VERSION | sed -n 's/v\([0-9]*\.[0-9]*\.[0-9]*\).*/\1/p')
7384
VERSION_DOCKER_MM=$(echo $VERSION | sed -n 's/v\([0-9]*\.[0-9]*\).*/\1/p')
7485
VERSION_DOCKER_M=$(echo $VERSION | sed -n 's/v\([0-9]*\).*/\1/p')
75-
86+
7687
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 -
7788
source $HOME/.poetry/env
7889
poetry build
@@ -112,7 +123,7 @@ jobs:
112123
command: |
113124
go get -v -u github.com/tcnksm/ghr
114125
PATH=$PATH:/usr/local/go/bin
115-
export VERSION=${CIRCLE_TAG:=CIRCLE_SHA1}
126+
export VERSION=${CIRCLE_TAG:=CIRCLE_SHA1}
116127
[[ $VERSION =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] && ISPRE="-prerelease"
117128
$HOME/go/bin/ghr -t ${GH_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} ${ISPRE} ${VERSION} /tmp/workspace/
118129
- store_artifacts:
@@ -122,6 +133,8 @@ workflows:
122133
version: 2
123134
build_test:
124135
jobs:
136+
- format-code
137+
125138
- build:
126139
context:
127140
- gdi-github

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,6 @@ dmypy.json
156156
.pyre/
157157
mibs/.DS_Store
158158
.DS_Store
159+
160+
# Intellij
161+
.idea
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import logging.config
22

3-
logging.basicConfig(level=logging.INFO, format='%(asctime)s | %(name)s | %(levelname)s | %(message)s')
3+
logging.basicConfig(
4+
level=logging.INFO, format="%(asctime)s | %(name)s | %(levelname)s | %(message)s"
5+
)
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
from flask import Flask, request, abort, jsonify, send_from_directory
22
from flask_autoindex import AutoIndex
33
import os
4-
import yaml
4+
import yaml
55
from splunk_connect_for_snmp_mib_server.mongo import MibsRepository, OidsRepository
66
from splunk_connect_for_snmp_mib_server.translator import Translator
77
import logging
88

99
logger = logging.getLogger(__name__)
1010

11+
1112
class MibServer:
1213
def __init__(self, args, server_config):
1314
self._args = args
1415
self._server_config = server_config
1516
self._translator = Translator(server_config)
1617
self._flask_app = self.build_flask_app()
17-
18-
def build_flask_app(self):
18+
19+
def build_flask_app(self):
1920
app = Flask(__name__)
2021
# ppath = os.environ['MIBS_SERVER_URL']
2122
ppath = self._server_config["snmp"]["mibs"]["mibs_path"]
2223
files_index = AutoIndex(app, ppath, add_url_rules=False)
2324

24-
@app.route('/')
25+
@app.route("/")
2526
def hello():
2627
return "Hello, This is SNMP MIB server!"
2728

2829
# Custom indexing
29-
@app.route('/files/')
30-
@app.route('/files/<path:path>')
31-
def autoindex(path='.'):
30+
@app.route("/files/")
31+
@app.route("/files/<path:path>")
32+
def autoindex(path="."):
3233
logger.debug(f"path: {path}")
3334
return files_index.render_autoindex(path)
3435

3536
# Translate oid
36-
@app.route('/translation', methods=['POST'])
37+
@app.route("/translation", methods=["POST"])
3738
def translator():
3839
logger.debug(request.json)
39-
var_binds = request.json.get('var_binds')
40+
var_binds = request.json.get("var_binds")
4041
metric = request.args.get("metric")
4142
logger.debug(f"type of var_binds: {type(var_binds)}")
4243
logger.debug(f"var_binds: {var_binds}")
4344
logger.debug(f"type of metric: {str(metric)} -- metric: {metric}")
4445
if metric == "True":
4546
# TODO
46-
# If metric is true, var_binds just has one
47+
# If metric is true, var_binds just has one
4748
var_bind = var_binds[0]
48-
result = self._translator.format_metric_data(var_bind)
49+
result = self._translator.format_metric_data(var_bind)
4950
else:
5051
result = self._translator.format_trap_event(var_binds)
5152
return result
52-
53+
5354
return app
54-
55+
5556
def run_mib_server(self):
5657
# TODO poetry run fails when debug=True
57-
self._flask_app.run(host="0.0.0.0",port=self._args.port)
58-
58+
self._flask_app.run(host="0.0.0.0", port=self._args.port)

splunk_connect_for_snmp_mib_server/snmp_mib_server.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import argparse
22
import os
3-
import yaml
3+
import yaml
44
from splunk_connect_for_snmp_mib_server.mongo import MibsRepository, OidsRepository
55
from splunk_connect_for_snmp_mib_server.mib_server import MibServer
66
import logging
@@ -13,12 +13,13 @@ def upload_mibs(server_config):
1313
mib_files_dir = server_config["snmp"]["mibs"]["dir"]
1414
mibs_collection = MibsRepository(mibs_mongo_config)
1515
# TODO do we need to clean up before loading
16-
# Clean up
16+
# Clean up
1717
mibs_collection.clear()
1818
# Upload all mib files in specific dir into mongo
1919
mibs_collection.upload_files(mib_files_dir)
2020
logger.debug("Uploaded all mib files into mongo!")
2121

22+
2223
def main():
2324
logger.info(f"Startup Config")
2425
parser = argparse.ArgumentParser()
@@ -36,7 +37,6 @@ def main():
3637
)
3738
parser.add_argument("-c", "--config", default="config.yaml", help="Config File")
3839

39-
4040
args = parser.parse_args()
4141

4242
log_level = args.loglevel.upper()
@@ -51,15 +51,13 @@ def main():
5151
with open(config_file, "r") as yamlfile:
5252
server_config = yaml.load(yamlfile, Loader=yaml.FullLoader)
5353

54-
55-
5654
# Upload mib fiels into mongo
5755
upload_mibs(server_config)
58-
56+
5957
# Init MibServer
6058
mib_server = MibServer(args, server_config)
6159
mib_server.run_mib_server()
6260

6361

6462
if __name__ == "__main__":
65-
main()
63+
main()

tests/test_mongo.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
import sys
2-
sys.path.append('../')
2+
3+
sys.path.append("../")
34
from splunk_connect_for_snmp_mib_server.mongo import MibsRepository
45
import os
56

67

78
def main():
8-
mongo_config = {'host': 'localhost', 'port': 27017, 'database': 'test', 'collection': 'mibs'}
9+
mongo_config = {
10+
"host": "localhost",
11+
"port": 27017,
12+
"database": "test",
13+
"collection": "mibs",
14+
}
915
mongo = MibsRepository(mongo_config)
1016

1117
mongo.clear()
1218

1319
# Upload mib files in /mibs/test to mongo
14-
mib_files_dir = os.path.join(os.getcwd(), "..", "mibs","test")
20+
mib_files_dir = os.path.join(os.getcwd(), "..", "mibs", "test")
1521
mongo.upload_files(mib_files_dir)
1622

1723
# Search mib files based on oid
1824
oid = "1, 3, 6, 1, 4, 1, 43, 2, 29, 1, 1"
1925
filename = mongo.search_oid(oid)
2026
print(filename)
21-
27+
2228
# Delete one specifc file
2329
if filename:
2430
mongo.delete_mib(filename)
2531
print(mongo.search_oid(oid))
2632

2733
# Delete multiple files
28-
filename_regex="A3COM"
34+
filename_regex = "A3COM"
2935
mongo.delete_mib(filename_regex)
3036

3137

32-
if __name__ == '__main__':
33-
main()
38+
if __name__ == "__main__":
39+
main()

tests/test_translator.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import sys
2-
sys.path.append('../')
2+
3+
sys.path.append("../")
34
import yaml
5+
46
# from splunk_connect_for_snmp_mib_server.mongo import OidsRepository
57
from splunk_connect_for_snmp_mib_server.translator import Translator
68
import os
79
import logging
10+
811
logger = logging.getLogger(__name__)
912

13+
1014
def main():
1115
logger.debug("Start:....")
1216
# Read config file
@@ -18,27 +22,27 @@ def main():
1822
my_translator = Translator(server_config)
1923

2024
var_binds_list = [
21-
{
22-
"oid": '1.3.6.1.2.1.1.3.0',
23-
"oid_type": "ObjectName",
24-
"val": "123",
25-
"val_type": "TimeTicks"
26-
},
27-
{
28-
"oid": '1.3.6.1.6.3.1.1.4.1.0',
29-
"oid_type": "ObjectName",
30-
"val": "1.3.6.1.4.1.1978.2.14.1.1.1",
31-
"val_type": "ObjectIdentifier"
32-
},
33-
{
34-
"oid": '1.3.6.1.6.3.1.1.4.1.0',
35-
"oid_type": "ObjectName",
36-
"val": "1.3.6.1.4.1.90000.1",
37-
"val_type": "ObjectIdentifier"
38-
}
39-
]
25+
{
26+
"oid": "1.3.6.1.2.1.1.3.0",
27+
"oid_type": "ObjectName",
28+
"val": "123",
29+
"val_type": "TimeTicks",
30+
},
31+
{
32+
"oid": "1.3.6.1.6.3.1.1.4.1.0",
33+
"oid_type": "ObjectName",
34+
"val": "1.3.6.1.4.1.1978.2.14.1.1.1",
35+
"val_type": "ObjectIdentifier",
36+
},
37+
{
38+
"oid": "1.3.6.1.6.3.1.1.4.1.0",
39+
"oid_type": "ObjectName",
40+
"val": "1.3.6.1.4.1.90000.1",
41+
"val_type": "ObjectIdentifier",
42+
},
43+
]
4044
my_translator.format_trap_event(var_binds_list)
4145

42-
if __name__ == '__main__':
43-
main()
4446

47+
if __name__ == "__main__":
48+
main()

0 commit comments

Comments
 (0)