Merge "Validate, retrieve and set relationships instead of foreign keys" into feature/1.0

This commit is contained in:
Jenkins 2017-08-20 04:13:18 +00:00 committed by Gerrit Code Review
commit 5c3c791b76
12 changed files with 314 additions and 106 deletions

View File

@ -21,6 +21,7 @@ from ara.db.models import db
from ara.db.models import File
from ara.db.models import FileContent
from ara.db.models import NoResultFound
from ara.db.models import Playbook
from flask import Blueprint
from flask_restful import Api
@ -55,6 +56,13 @@ class FileRestApi(Resource):
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, FILE_FIELDS))
# Files are stored uniquely by sha1, get it or create it
sha1 = content_sha1(args.content)
try:
@ -63,7 +71,7 @@ class FileRestApi(Resource):
content = FileContent(content=args.content)
file_ = File(
playbook_id=args.playbook_id,
playbook=playbook,
path=args.path,
is_playbook=args.is_playbook,
content=content
@ -86,7 +94,7 @@ class FileRestApi(Resource):
abort(404, message="File {} doesn't exist".format(args.id),
help=api_utils.help(parser.args, FILE_FIELDS))
keys = ['playbook_id', 'path', 'is_playbook', 'content']
keys = ['path', 'is_playbook', 'content']
updates = 0
for key in keys:
if getattr(args, key) is not None:
@ -176,13 +184,6 @@ class FileRestApi(Resource):
required=True,
help='The id of the file'
)
parser.add_argument(
'playbook_id', dest='playbook_id',
type=int,
location='json',
required=False,
help='The playbook_id of the file'
)
parser.add_argument(
'path', dest='path',
type=str,

View File

@ -18,6 +18,7 @@
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 Playbook
from flask import Blueprint
from flask_restful import Api
@ -54,8 +55,15 @@ class HostRestApi(Resource):
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))
host = Host(
playbook_id=args.playbook_id,
playbook=playbook,
name=args.name,
facts=args.facts,
changed=args.changed,
@ -81,8 +89,8 @@ class HostRestApi(Resource):
abort(404, message="Host {} doesn't exist".format(args.id),
help=api_utils.help(parser.args, HOST_FIELDS))
keys = ['playbook_id', 'name', 'facts', 'changed', 'failed',
'ok', 'skipped', 'unreachable']
keys = ['name', 'facts', 'changed', 'failed', 'ok',
'skipped', 'unreachable']
updates = 0
for key in keys:
if getattr(args, key) is not None:
@ -196,13 +204,6 @@ class HostRestApi(Resource):
required=True,
help='The id of the host'
)
parser.add_argument(
'playbook_id', dest='playbook_id',
type=int,
location='json',
required=False,
help='The playbook_id of the host'
)
parser.add_argument(
'name', dest='name',
type=api_utils.encoded_input,

View File

@ -18,6 +18,7 @@
from ara.api.v1 import utils as api_utils
from ara.db.models import db
from ara.db.models import Play
from ara.db.models import Playbook
from flask import Blueprint
from flask_restful import Api
@ -59,8 +60,15 @@ class PlayRestApi(Resource):
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, PLAY_FIELDS))
play = Play(
playbook_id=args.playbook_id,
playbook=playbook,
name=args.name,
started=args.started,
ended=args.ended
@ -82,7 +90,7 @@ class PlayRestApi(Resource):
abort(404, message="Play {} doesn't exist".format(args.id),
help=api_utils.help(parser.args, PLAY_FIELDS))
keys = ['playbook_id', 'name', 'started', 'ended']
keys = ['name', 'started', 'ended']
updates = 0
for key in keys:
if getattr(args, key) is not None:
@ -162,13 +170,6 @@ class PlayRestApi(Resource):
required=True,
help='The id of the play'
)
parser.add_argument(
'playbook_id', dest='playbook_id',
type=int,
location='json',
required=False,
help='The playbook_id of the play'
)
parser.add_argument(
'name', dest='name',
type=api_utils.encoded_input,

View File

@ -18,6 +18,7 @@
from ara.api.v1 import utils as api_utils
from ara.db.models import db
from ara.db.models import Record
from ara.db.models import Playbook
from flask import Blueprint
from flask_restful import Api
@ -50,8 +51,15 @@ class RecordRestApi(Resource):
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, RECORD_FIELDS))
record = Record(
playbook_id=args.playbook_id,
playbook=playbook,
key=args.key,
value=args.value,
type=args.type
@ -73,7 +81,7 @@ class RecordRestApi(Resource):
abort(404, message="Record {} doesn't exist".format(args.id),
help=api_utils.help(parser.args, RECORD_FIELDS))
keys = ['playbook_id', 'key', 'value', 'type']
keys = ['key', 'value', 'type']
updates = 0
for key in keys:
if getattr(args, key) is not None:
@ -155,13 +163,6 @@ class RecordRestApi(Resource):
required=True,
help='The id of the record'
)
parser.add_argument(
'playbook_id', dest='playbook_id',
type=int,
location='json',
required=False,
help='The playbook_id for the record'
)
parser.add_argument(
'key', dest='key',
type=str,

View File

@ -17,7 +17,9 @@
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 Result
from ara.db.models import Task
from flask import Blueprint
from flask_restful import Api
@ -60,11 +62,25 @@ class ResultRestApi(Resource):
parser = self._post_parser()
args = parser.parse_args()
# Validate and retrieve the task reference
task = Task.query.get(args.task_id)
if not task:
abort(404,
message="Task {} doesn't exist".format(args.task_id),
help=api_utils.help(parser.args, RESULT_FIELDS))
# Validate and retrieve the host reference
host = Host.query.get(args.host_id)
if not host:
abort(404,
message="Host {} doesn't exist".format(args.host_id),
help=api_utils.help(parser.args, RESULT_FIELDS))
result = Result(
playbook_id=args.playbook_id,
host_id=args.host_id,
play_id=args.play_id,
task_id=args.task_id,
playbook=task.playbook,
host=host,
play=task.play,
task=task,
status=args.status,
changed=args.changed,
failed=args.failed,
@ -92,9 +108,8 @@ class ResultRestApi(Resource):
abort(404, message="Result {} doesn't exist".format(args.id),
help=api_utils.help(parser.args, RESULT_FIELDS))
keys = ['playbook_id', 'host_id', 'play_id', 'task_id', 'status',
'changed', 'failed', 'skipped', 'unreachable',
'ignore_errors', 'result', 'started', 'ended']
keys = ['host_id', 'task_id', 'status', 'changed', 'failed', 'skipped',
'unreachable', 'ignore_errors', 'result', 'started', 'ended']
updates = 0
for key in keys:
if getattr(args, key) is not None:
@ -134,20 +149,6 @@ class ResultRestApi(Resource):
@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 result'
)
parser.add_argument(
'play_id', dest='play_id',
type=int,
location='json',
required=True,
help='The play_id of the result'
)
parser.add_argument(
'task_id', dest='task_id',
type=int,
@ -243,20 +244,6 @@ class ResultRestApi(Resource):
required=True,
help='The id of the result'
)
parser.add_argument(
'playbook_id', dest='playbook_id',
type=int,
location='json',
required=False,
help='The playbook_id of the result'
)
parser.add_argument(
'play_id', dest='play_id',
type=int,
location='json',
required=False,
help='The play_id of the result'
)
parser.add_argument(
'task_id', dest='task_id',
type=int,

View File

@ -17,6 +17,8 @@
from ara.api.v1 import utils as api_utils
from ara.db.models import db
from ara.db.models import File
from ara.db.models import Play
from ara.db.models import Task
from flask import Blueprint
@ -60,10 +62,25 @@ class TaskRestApi(Resource):
"""
parser = self._post_parser()
args = parser.parse_args()
# Validate and retrieve the play reference
play = Play.query.get(args.play_id)
if not play:
abort(404,
message="Play {} doesn't exist".format(args.play_id),
help=api_utils.help(parser.args, TASK_FIELDS))
# Validate and retrieve the file reference
file_ = File.query.get(args.file_id)
if not file_:
abort(404,
message="File {} doesn't exist".format(args.file_id),
help=api_utils.help(parser.args, TASK_FIELDS))
task = Task(
playbook_id=args.playbook_id,
play_id=args.play_id,
file_id=args.file_id,
playbook=play.playbook,
play=play,
file=file_,
name=args.name,
action=args.action,
lineno=args.lineno,
@ -89,8 +106,8 @@ class TaskRestApi(Resource):
abort(404, message="Task {} doesn't exist".format(args.id),
help=api_utils.help(parser.args, TASK_FIELDS))
keys = ['playbook_id', 'name', 'action', 'lineno', 'tags',
'handler', 'started', 'ended']
keys = ['name', 'action', 'lineno', 'tags', 'handler', 'started',
'ended']
updates = 0
for key in keys:
if getattr(args, key) is not None:
@ -130,13 +147,6 @@ class TaskRestApi(Resource):
@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 task'
)
parser.add_argument(
'file_id', dest='file_id',
type=int,
@ -213,27 +223,6 @@ class TaskRestApi(Resource):
required=True,
help='The id of the task'
)
parser.add_argument(
'playbook_id', dest='playbook_id',
type=int,
location='json',
required=False,
help='The playbook_id of the task'
)
parser.add_argument(
'file_id', dest='file_id',
type=int,
location='json',
required=False,
help='The file_id of the task'
)
parser.add_argument(
'play_id', dest='play_id',
type=int,
location='json',
required=False,
help='The play_id of the task'
)
parser.add_argument(
'name', dest='name',
type=api_utils.encoded_input,

View File

@ -151,6 +151,38 @@ class TestApiFiles(TestAra):
res = FileApi().post(data)
self.assertEqual(res.status_code, 400)
def test_post_http_with_nonexistant_playbook(self):
ansible_run()
data = {
"playbook_id": 9001,
"path": "/root/playbook.yml",
"content": """
---
- name: A task from ünit tests
debug:
msg: "hello world"
"""
}
res = self.client.post('/api/v1/files/',
data=jsonutils.dumps(data),
content_type='application/json')
self.assertEqual(res.status_code, 404)
def test_post_internal_with_nonexistant_playbook(self):
ansible_run()
data = {
"playbook_id": 9001,
"path": "/root/playbook.yml",
"content": """
---
- name: A task from ünit tests
debug:
msg: "hello world"
"""
}
res = FileApi().post(data)
self.assertEqual(res.status_code, 404)
###########
# PATCH
###########

View File

@ -154,6 +154,24 @@ class TestApiHosts(TestAra):
res = HostApi().post(data)
self.assertEqual(res.status_code, 400)
def test_post_http_with_nonexistant_playbook(self):
data = {
"playbook_id": 9001,
"name": "hostname",
}
res = self.client.post('/api/v1/hosts/',
data=jsonutils.dumps(data),
content_type='application/json')
self.assertEqual(res.status_code, 404)
def test_post_internal_with_nonexistant_playbook(self):
data = {
"playbook_id": 9001,
"name": "hostname",
}
res = HostApi().post(data)
self.assertEqual(res.status_code, 404)
###########
# PATCH
###########

View File

@ -140,6 +140,28 @@ class TestApiPlays(TestAra):
res = PlayApi().post(data)
self.assertEqual(res.status_code, 400)
def test_post_http_with_nonexistant_playbook(self):
ansible_run()
data = {
"playbook_id": 9001,
"name": "Play from unit tests",
"started": "1970-08-14T00:52:49.570031"
}
res = self.client.post('/api/v1/plays/',
data=jsonutils.dumps(data),
content_type='application/json')
self.assertEqual(res.status_code, 404)
def test_post_internal_with_nonexistant_playbook(self):
ansible_run()
data = {
"playbook_id": 9001,
"name": "Play from unit tests",
"started": "1970-08-14T00:52:49.570031"
}
res = PlayApi().post(data)
self.assertEqual(res.status_code, 404)
###########
# PATCH
###########

View File

@ -146,6 +146,30 @@ class TestApiRecords(TestAra):
res = RecordApi().post(data)
self.assertEqual(res.status_code, 400)
def test_post_http_with_nonexistant_playbook(self):
ansible_run()
data = {
"playbook_id": 9001,
"key": "foo",
"value": "bar",
"type": "text"
}
res = self.client.post('/api/v1/records/',
data=jsonutils.dumps(data),
content_type='application/json')
self.assertEqual(res.status_code, 404)
def test_post_internal_with_nonexistant_playbook(self):
ansible_run()
data = {
"playbook_id": 9001,
"key": "foo",
"value": "bar",
"type": "text"
}
res = RecordApi().post(data)
self.assertEqual(res.status_code, 404)
###########
# PATCH
###########

View File

@ -184,12 +184,84 @@ class TestApiResults(TestAra):
"skipped": False,
"unreachable": False,
"ignore_errors": False,
"result": '{"msg": "some result"}',
"result": {"msg": "some result"},
"started": "1970-08-14T00:52:49.570031"
}
res = ResultApi().post(data)
self.assertEqual(res.status_code, 400)
def test_post_http_with_nonexistant_task(self):
ansible_run()
data = {
"task_id": 9001,
"host_id": 1,
"status": "ok",
"changed": True,
"failed": False,
"skipped": False,
"unreachable": False,
"ignore_errors": False,
"result": {"msg": "some result"},
"started": "1970-08-14T00:52:49.570031"
}
res = self.client.post('/api/v1/results/',
data=jsonutils.dumps(data),
content_type='application/json')
self.assertEqual(res.status_code, 404)
def test_post_internal_with_nonexistant_task(self):
ansible_run()
data = {
"task_id": 9001,
"host_id": 1,
"status": "ok",
"changed": True,
"failed": False,
"skipped": False,
"unreachable": False,
"ignore_errors": False,
"result": {"msg": "some result"},
"started": "1970-08-14T00:52:49.570031"
}
res = ResultApi().post(data)
self.assertEqual(res.status_code, 404)
def test_post_http_with_nonexistant_host(self):
ansible_run()
data = {
"task_id": 1,
"host_id": 9001,
"status": "ok",
"changed": True,
"failed": False,
"skipped": False,
"unreachable": False,
"ignore_errors": False,
"result": {"msg": "some result"},
"started": "1970-08-14T00:52:49.570031"
}
res = self.client.post('/api/v1/results/',
data=jsonutils.dumps(data),
content_type='application/json')
self.assertEqual(res.status_code, 404)
def test_post_internal_with_nonexistant_host(self):
ansible_run()
data = {
"task_id": 1,
"host_id": 9001,
"status": "ok",
"changed": True,
"failed": False,
"skipped": False,
"unreachable": False,
"ignore_errors": False,
"result": {"msg": "some result"},
"started": "1970-08-14T00:52:49.570031"
}
res = ResultApi().post(data)
self.assertEqual(res.status_code, 404)
###########
# PATCH
###########

View File

@ -166,6 +166,66 @@ class TestApiTasks(TestAra):
res = TaskApi().post(data)
self.assertEqual(res.status_code, 400)
def test_post_http_with_nonexistant_play(self):
ansible_run()
data = {
"play_id": 9001,
"file_id": 1,
"name": "Task from unit tests",
"action": "debug",
"lineno": 1,
"tags": ['one', 'two'],
"started": "1970-08-14T00:52:49.570031"
}
res = self.client.post('/api/v1/tasks/',
data=jsonutils.dumps(data),
content_type='application/json')
self.assertEqual(res.status_code, 404)
def test_post_internal_with_nonexistant_play(self):
ansible_run()
data = {
"play_id": 9001,
"file_id": 1,
"name": "Task from unit tests",
"action": "debug",
"lineno": 1,
"tags": ['one', 'two'],
"started": "1970-08-14T00:52:49.570031"
}
res = TaskApi().post(data)
self.assertEqual(res.status_code, 404)
def test_post_http_with_nonexistant_file(self):
ansible_run()
data = {
"play_id": 1,
"file_id": 9001,
"name": "Task from unit tests",
"action": "debug",
"lineno": 1,
"tags": ['one', 'two'],
"started": "1970-08-14T00:52:49.570031"
}
res = self.client.post('/api/v1/tasks/',
data=jsonutils.dumps(data),
content_type='application/json')
self.assertEqual(res.status_code, 404)
def test_post_internal_with_nonexistant_file(self):
ansible_run()
data = {
"play_id": 1,
"file_id": 9001,
"name": "Task from unit tests",
"action": "debug",
"lineno": 1,
"tags": ['one', 'two'],
"started": "1970-08-14T00:52:49.570031"
}
res = TaskApi().post(data)
self.assertEqual(res.status_code, 404)
###########
# PATCH
###########