Improve Promenade validatedesign message
Improves validatedesign return message by including count and list of validation errors. Change-Id: I7771eafedaa56c748f43fe1212926154933f247f
This commit is contained in:
parent
3faccce445
commit
0604b0404f
|
@ -11,8 +11,9 @@ GET /v1.0/health
|
||||||
|
|
||||||
Returns the health status.
|
Returns the health status.
|
||||||
|
|
||||||
Responses
|
Responses:
|
||||||
- 204 No Content
|
|
||||||
|
+ 204 No Content
|
||||||
|
|
||||||
|
|
||||||
/v1.0/join-scripts
|
/v1.0/join-scripts
|
||||||
|
@ -37,9 +38,10 @@ dynamic.labels
|
||||||
static.labels
|
static.labels
|
||||||
Used to set configuration options in the generated script
|
Used to set configuration options in the generated script
|
||||||
|
|
||||||
Responses
|
Responses:
|
||||||
- 200 OK: Script returned as response body
|
|
||||||
- 400 Bad Request: One or more query parameters is missing or misspelled
|
+ 200 OK: Script returned as response body
|
||||||
|
+ 400 Bad Request: One or more query parameters is missing or misspelled
|
||||||
|
|
||||||
|
|
||||||
/v1.0/validatedesign
|
/v1.0/validatedesign
|
||||||
|
@ -55,10 +57,8 @@ Message Body
|
||||||
|
|
||||||
href
|
href
|
||||||
Location of the document to be validated
|
Location of the document to be validated
|
||||||
type
|
|
||||||
Type of document to be validated
|
|
||||||
|
|
||||||
Responses:
|
Responses:
|
||||||
- 200 OK: Documents were successfully validated
|
|
||||||
- 400 Bad Request: Documents were not successfully validated
|
+ 200 OK: Documents were successfully validated
|
||||||
- 404 Not Found: The document (of that type) was not found at the specified location
|
+ 400 Bad Request: Documents were not successfully validated
|
||||||
|
|
|
@ -26,13 +26,15 @@ LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ValidateDesignResource(base.BaseResource):
|
class ValidateDesignResource(base.BaseResource):
|
||||||
def _return_msg(self, resp, status_code, status="Valid", message=""):
|
def _return_msg(self, resp, result):
|
||||||
if status_code is falcon.HTTP_200:
|
if result['err_count'] == 0:
|
||||||
count = 0
|
message = "Promenade validations succeeded."
|
||||||
msg_list = []
|
status_code = falcon.HTTP_200
|
||||||
|
status = "Valid"
|
||||||
else:
|
else:
|
||||||
count = 1
|
message = "Promenade validations failed."
|
||||||
msg_list = [message]
|
status_code = falcon.HTTP_400
|
||||||
|
status = "Invalid"
|
||||||
resp.body = json.dumps({
|
resp.body = json.dumps({
|
||||||
"kind": "Status",
|
"kind": "Status",
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
|
@ -41,8 +43,8 @@ class ValidateDesignResource(base.BaseResource):
|
||||||
"message": message,
|
"message": message,
|
||||||
"reason": "Validation",
|
"reason": "Validation",
|
||||||
"details": {
|
"details": {
|
||||||
"errorCount": count,
|
"errorCount": result['err_count'],
|
||||||
"messageList": msg_list,
|
"messageList": result['msg'],
|
||||||
},
|
},
|
||||||
"code": status_code,
|
"code": status_code,
|
||||||
})
|
})
|
||||||
|
@ -55,12 +57,10 @@ class ValidateDesignResource(base.BaseResource):
|
||||||
href = json_data.get('href', None)
|
href = json_data.get('href', None)
|
||||||
config = Configuration.from_design_ref(
|
config = Configuration.from_design_ref(
|
||||||
href, allow_missing_substitutions=False)
|
href, allow_missing_substitutions=False)
|
||||||
validation.check_design(config)
|
result = validation.check_design(config)
|
||||||
msg = "Promenade validations succeeded"
|
|
||||||
return self._return_msg(resp, falcon.HTTP_200, message=msg)
|
|
||||||
except exceptions.InvalidFormatError as e:
|
except exceptions.InvalidFormatError as e:
|
||||||
msg = "Invalid JSON Format: %s" % str(e)
|
msg = "Invalid JSON Format: %s" % str(e)
|
||||||
except exceptions.ValidationException as e:
|
result = {'msg': [msg], 'err_count': 1}
|
||||||
msg = "Promenade validations failed: %s" % str(e)
|
except exceptions.DeckhandException as e:
|
||||||
return self._return_msg(
|
result = {'msg': [str(e)], 'err_count': 1}
|
||||||
resp, falcon.HTTP_400, status="Invalid", message=msg)
|
return self._return_msg(resp, result)
|
||||||
|
|
|
@ -14,30 +14,42 @@
|
||||||
|
|
||||||
from promenade import exceptions
|
from promenade import exceptions
|
||||||
from promenade import logging
|
from promenade import logging
|
||||||
|
import copy
|
||||||
import jsonschema
|
import jsonschema
|
||||||
import os
|
import os
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
__all__ = ['check_schema', 'check_schemas']
|
__all__ = ['check_schema', 'check_schemas']
|
||||||
|
result_template = {'msg': [], 'err_count': 0}
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def check_design(config):
|
def check_design(config):
|
||||||
kinds = ['Docker', 'HostSystem', 'Kubelet', 'KubernetesNetwork']
|
kinds = ['Docker', 'HostSystem', 'Kubelet', 'KubernetesNetwork']
|
||||||
|
result = copy.deepcopy(result_template)
|
||||||
for kind in kinds:
|
for kind in kinds:
|
||||||
count = 0
|
count = 0
|
||||||
for doc in config.documents:
|
for doc in config.documents:
|
||||||
schema = doc.get('schema', None)
|
schema = doc.get('schema', None)
|
||||||
if not schema:
|
if not schema:
|
||||||
raise exceptions.ValidationException(
|
result['msg'].append(
|
||||||
'"schema" is a required document key.')
|
str(
|
||||||
|
exceptions.ValidationException(
|
||||||
|
'"schema" is a required document key.')))
|
||||||
|
result['err_count'] += 1
|
||||||
|
return result
|
||||||
name = schema.split('/')[1]
|
name = schema.split('/')[1]
|
||||||
if name == kind:
|
if name == kind:
|
||||||
count += 1
|
count += 1
|
||||||
if count != 1:
|
if count != 1:
|
||||||
raise exceptions.ValidationException()
|
msg = ('There are {0} {1} documents. However, there should be one.'
|
||||||
|
).format(count, kind)
|
||||||
|
result['msg'].append(
|
||||||
|
str(exceptions.ValidationException(description=msg)))
|
||||||
|
result['err_count'] += 1
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def check_schemas(documents, schemas=None):
|
def check_schemas(documents, schemas=None):
|
||||||
|
@ -49,7 +61,8 @@ def check_schemas(documents, schemas=None):
|
||||||
|
|
||||||
def check_schema(document, schemas=None):
|
def check_schema(document, schemas=None):
|
||||||
if not isinstance(document, dict):
|
if not isinstance(document, dict):
|
||||||
LOG.error('Non-dictionary document passed to schema validation.')
|
msg = 'Non-dictionary document passed to schema validation.'
|
||||||
|
LOG.error(msg)
|
||||||
return
|
return
|
||||||
|
|
||||||
schema_name = document.get('schema', '<missing>')
|
schema_name = document.get('schema', '<missing>')
|
||||||
|
|
Loading…
Reference in New Issue