-
Notifications
You must be signed in to change notification settings - Fork 19
/
hosts.py
323 lines (293 loc) · 9.42 KB
/
hosts.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# Copyright (c) 2017 Red Hat, Inc.
#
# This file is part of ARA: Ansible Run Analysis.
#
# ARA is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ARA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ARA. If not, see <http://www.gnu.org/licenses/>.
from ara.api.v1 import utils as api_utils
from ara.db.models import db
from ara.db.models import Host
from ara.db.models import NoResultFound
from ara.db.models import Playbook
from flask import Blueprint
from flask_restful import Api
from flask_restful import abort
from flask_restful import fields
from flask_restful import marshal
from flask_restful import reqparse
from flask_restful import Resource
blueprint = Blueprint('hosts', __name__)
api = Api(blueprint)
HOST_FIELDS = {
'id': fields.Integer,
'facts': fields.Raw,
'name': api_utils.Encoded,
'changed': fields.Integer,
'failed': fields.Integer,
'ok': fields.Integer,
'skipped': fields.Integer,
'unreachable': fields.Integer,
'playbook': fields.Nested({
'id': fields.Integer,
'href': fields.Url('playbooks.playbookrestapi')
}),
}
class HostRestApi(Resource):
"""
REST API for Hosts: api.v1.hosts
"""
def post(self):
"""
Creates a host with the provided arguments
"""
parser = self._post_parser()
args = parser.parse_args()
# Validate and retrieve the playbook reference
playbook = Playbook.query.get(args.playbook_id)
if not playbook:
abort(404,
message="Playbook {} doesn't exist".format(args.playbook_id),
help=api_utils.help(parser.args, HOST_FIELDS))
# Hosts are stored uniquely by name for a playbook, get it or create it
try:
host = Host.query.filter_by(
playbook_id=args.playbook_id,
name=args.name
).one()
return self.get(id=host.id)
except NoResultFound:
pass
host = Host(
playbook=playbook,
name=args.name,
facts=args.facts,
changed=args.changed,
failed=args.failed,
ok=args.ok,
skipped=args.skipped,
unreachable=args.unreachable,
)
db.session.add(host)
db.session.commit()
return self.get(id=host.id)
def patch(self):
"""
Updates provided parameters for a host
"""
parser = self._patch_parser()
args = parser.parse_args()
host = Host.query.get(args.id)
if not host:
abort(404, message="Host {} doesn't exist".format(args.id),
help=api_utils.help(parser.args, HOST_FIELDS))
keys = ['name', 'facts', 'changed', 'failed', 'ok',
'skipped', 'unreachable']
updates = 0
for key in keys:
if getattr(args, key) is not None:
updates += 1
setattr(host, key, getattr(args, key))
if not updates:
abort(400, message="No parameters to update provided",
help=api_utils.help(parser.args, HOST_FIELDS))
db.session.add(host)
db.session.commit()
return self.get(id=host.id)
def get(self, id=None):
"""
Retrieves one or many hosts based on the request and the query
"""
parser = self._get_parser()
if id is not None:
host = _find_hosts(id=id)
if host is None:
abort(404, message="Host {} doesn't exist".format(id),
help=api_utils.help(parser.args, HOST_FIELDS))
return marshal(host, HOST_FIELDS)
args = parser.parse_args()
hosts = _find_hosts(**args)
if not hosts:
abort(404, message='No hosts found for this query',
help=api_utils.help(parser.args, HOST_FIELDS))
return marshal(hosts, HOST_FIELDS)
@staticmethod
def _post_parser():
parser = reqparse.RequestParser()
parser.add_argument(
'playbook_id', dest='playbook_id',
type=int,
location='json',
required=True,
help='The playbook_id of the host'
)
parser.add_argument(
'name', dest='name',
type=api_utils.encoded_input,
location='json',
required=True,
help='The name of the host'
)
parser.add_argument(
'facts', dest='facts',
type=dict,
location='json',
required=False,
default={},
help='The facts for the host'
)
parser.add_argument(
'changed', dest='changed',
type=int,
location='json',
required=False,
default=0,
help='Changed tasks for the host'
)
parser.add_argument(
'failed', dest='failed',
type=int,
location='json',
required=False,
default=0,
help='Failed tasks for the host'
)
parser.add_argument(
'ok', dest='ok',
type=int,
location='json',
required=False,
default=0,
help='Failed tasks for the host'
)
parser.add_argument(
'skipped', dest='skipped',
type=int,
location='json',
required=False,
default=0,
help='Skipped tasks for the host'
)
parser.add_argument(
'unreachable', dest='unreachable',
type=int,
location='json',
required=False,
default=0,
help='Unreachable tasks for the host'
)
return parser
@staticmethod
def _patch_parser():
parser = reqparse.RequestParser()
parser.add_argument(
'id', dest='id',
type=int,
location='json',
required=True,
help='The id of the host'
)
parser.add_argument(
'name', dest='name',
type=api_utils.encoded_input,
location='json',
required=False,
help='The name of the host'
)
parser.add_argument(
'facts', dest='facts',
type=dict,
location='json',
required=False,
help='The facts for the host'
)
parser.add_argument(
'changed', dest='changed',
type=int,
location='json',
required=False,
help='Changed tasks for the host'
)
parser.add_argument(
'failed', dest='failed',
type=int,
location='json',
required=False,
help='Failed tasks for the host'
)
parser.add_argument(
'ok', dest='ok',
type=int,
location='json',
required=False,
help='Failed tasks for the host'
)
parser.add_argument(
'skipped', dest='skipped',
type=int,
location='json',
required=False,
help='Skipped tasks for the host'
)
parser.add_argument(
'unreachable', dest='unreachable',
type=int,
location='json',
required=False,
help='Unreachable tasks for the host'
)
return parser
@staticmethod
def _get_parser():
parser = reqparse.RequestParser()
parser.add_argument(
'id', dest='id',
type=int,
location='values',
required=False,
help='Search with the id of the host'
)
parser.add_argument(
'playbook_id', dest='playbook_id',
type=int,
location='values',
required=False,
help='Search hosts for a playbook id'
)
parser.add_argument(
'name', dest='name',
type=api_utils.encoded_input,
location='values',
required=False,
help='Search with the name (full or part) of a host'
)
return parser
def _find_hosts(**kwargs):
if 'id' in kwargs and kwargs['id'] is not None:
return Host.query.get(kwargs['id'])
query = Host.query
if 'playbook_id' in kwargs and kwargs['playbook_id'] is not None:
query = query.filter_by(playbook_id=kwargs['playbook_id'])
if 'name' in kwargs and kwargs['name'] is not None:
query = query.filter(
Host.name.like(
u"%{0}%".format(kwargs['name'])
)
)
return query.order_by(Host.id.desc()).all()
# Note (dmsimard)
# We are (unfortunately) routing /api/v1/<resource>/ instead of
# /api/v1/<resource> so that flask-frozen creates a <resource> directory
# instead of a <resource> file.
# In practice, the endpoint <resource> returns a 301 redirection to <resource>/
# when used on a live HTTP server.
api.add_resource(HostRestApi, '/', '', '/<int:id>')