-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
139 lines (118 loc) · 4.79 KB
/
app.py
File metadata and controls
139 lines (118 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Robert Lynch (C) 2019
#
#
#
#
#
nutserver_IP="127.0.0.1"
requireAPIkey=False
APIkey=""
# Import statements
from flask import Flask, request
from flask_restplus import Api, Resource, fields
from functools import wraps
from importlib import reload
import nut2
# JSON for API key
authorizations = {
'apikey': {
'type': 'apiKey',
'in': 'header',
'name': 'X-API-KEY'
}
}
app=Flask(__name__)
# if requireAPIkey is set to true, Swagger documentation will require add authorization
if requireAPIkey==True:
api=Api(app, authorizations=authorizations, doc='/docs',version=1.0, title='NUT Server API', description='API tool to get information from a NUT server')
else:
api=Api(app, doc='/docs',version=1.0, title='NUT Server API', description='API tool to get information from a NUT server')
# API Authentication function
def API_authentication(f):
@wraps(f)
def decorated(*args, **kwargs):
if requireAPIkey:
token = None
# checks to see if the token header is present in the header
if 'X-API-KEY' in request.headers:
token=request.headers["X-API-KEY"]
if token!=APIkey:
return {"Error":"Not Authorized"}, 401
# if the token is not present the user receives an error.
if not token:
return {"Error":"Token Missing"}, 401
return f(*args, **kwargs)
return decorated #Returns the arguments after confirming if the token is correct
# API routes below:
#
#
#
# Get UPSes ...(side note, is it UPSes or UPSs....????) that are defined in the nut/ups.conf file
@api.route('/getups')
class GetUPS(Resource):
@api.doc(security='apikey', responses={200:'Success', '401': 'Authentication Issue'})
@api.param('host', 'IP address of host')
@API_authentication #passes to the API_authentication function
def get(self):
reload(nut2)
# If user passes a host, it will use the host that is passed, else it uses the defined host in nutserver_IP
if request.args.get('host')!=None:
client = nut2.PyNUTClient(request.args.get('host'))
else:
client = nut2.PyNUTClient(nutserver_IP)
return client.list_ups(), 200
# Gets all values from UPS
@api.route('/values')
class GetUPSValues(Resource):
@api.doc(security='apikey', responses={200:'Success', '401': 'Authentication Issue'})
@api.param('host', 'IP address of host')
@api.param('ups', 'UPS name')
@API_authentication #passes to the API_authentication function
def get(self):
reload(nut2)
# If user passes a host, it will use the host that is passed, else it uses the defined host in nutserver_IP
if request.args.get('host')!=None:
client = nut2.PyNUTClient(request.args.get('host'))
else:
client = nut2.PyNUTClient(nutserver_IP)
if request.args.get('ups')!=None:
return client.list_vars(request.args.get('ups')), 200
else:
return {'Error':'UPS not requested'}, 400
# Gets Specific variables from UPS
@api.route('/variables/<var>')
class GetUPSSpecificVariables(Resource):
@api.doc(security='apikey', responses={200:'Success', '401': 'Authentication Issue'}, params={'var':'Variable name'})
@api.param('host', 'IP address of host')
@api.param('ups', 'UPS name')
# @API_authentication #passes to the API_authentication function
def get(self, var):
reload(nut2)
# If user passes a host, it will use the host that is passed, else it uses the defined host in nutserver_IP
if request.args.get('host')!=None:
client = nut2.PyNUTClient(request.args.get('host'))
else:
client = nut2.PyNUTClient(nutserver_IP)
if request.args.get('ups')!=None:
return client.get(request.args.get('ups'), var), 200
else:
return {'Error':'UPS not requested'}, 400
@api.route('/variablesdescription/<var>')
class GetUPSVariablesDescription(Resource):
@api.doc(security='apikey', responses={200:'Success', '401': 'Authentication Issue'}, params={'var':'Variable name'})
@api.param('host', 'IP address of host')
@api.param('ups', 'UPS name')
@API_authentication #passes to the API_authentication function
def get(self, var):
reload(nut2)
# If user passes a host, it will use the host that is passed, else it uses the defined host in nutserver_IP
if request.args.get('host')!=None:
client = nut2.PyNUTClient(request.args.get('host'))
else:
client = nut2.PyNUTClient(nutserver_IP)
if request.args.get('ups')!=None:
return client.var_description(request.args.get('ups'), var), 200
else:
return {'Error':'UPS not requested'}, 400
if __name__=="__main__":
app.run(debug=True)