Skip to content

Commit

Permalink
Release for empty config fix (#61)
Browse files Browse the repository at this point in the history
* added validations to check for configfile input against null and none

* added fix to loop through route hosts to create independent records

* refactored code to improve the efficiency

* more refactoring

* fetch name inside the hosts loop

Co-authored-by: Nithin Shekar Kuruba <nithinshekar.kuruba@gov.bc.ca>
  • Loading branch information
ikethecoder and NithinKuruba authored Dec 29, 2021
1 parent 0f3bf52 commit ad2cc4f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
32 changes: 32 additions & 0 deletions docs/samples/service-plugins/service-request-transformer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
services:
- name: MY_REST_API
tags: [ _NS_ ]
plugins:
- config:
add:
body: {}
headers: {}
querystring: {}
append:
body: {}
headers: {}
querystring: {}
remove:
body: {}
headers:
- Cookie
querystring: {}
rename:
body: {}
headers: {}
querystring: {}
replace:
body: {}
headers: {}
querystring: {}
enabled: true
name: request-transformer
protocols:
- https
- http
tags: [ _NS_ ]
21 changes: 19 additions & 2 deletions microservices/gatewayApi/v1/routes/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ def write_config(namespace: str) -> object:

dfile = None

if 'configFile' in request.files:
if 'configFile' in request.files and not request.files['configFile'].filename == '':
log.debug("[%s] %s", namespace, request.files['configFile'])
dfile = request.files['configFile']
dry_run = request.values['dryRun']
elif request.content_type.startswith("application/json"):
elif request.content_type.startswith("application/json") and not request.json['configFile'] in [None, '']:
dfile = request.json['configFile']
dry_run = request.json['dryRun']
else:
Expand All @@ -202,6 +202,10 @@ def write_config(namespace: str) -> object:
for doc in yaml_documents_iter:
yaml_documents.append(doc)

if len(yaml_documents) == 0:
log.error("%s - %s" % (namespace, "Empty Configuration Passed"))
abort_early(event_id, 'publish', namespace, jsonify(error="Empty Configuration Passed"))

selectTag = "ns.%s" % namespace
ns_qualifier = None

Expand Down Expand Up @@ -290,6 +294,19 @@ def write_config(namespace: str) -> object:
cmd = "diff"

log.info("[%s] %s action using %s" % (namespace, cmd, selectTag))

args = [
"deck", "validate", "--config", "/tmp/deck.yaml", "--state", tempFolder
]
log.debug("[%s] Running %s" % (namespace, args))
deck_validate = Popen(args, stdout=PIPE, stderr=STDOUT)
out, err = deck_validate.communicate()

if deck_validate.returncode != 0:
log.warn("[%s] - %s" % (namespace, out.decode('utf-8')))
abort_early(event_id, 'validate', namespace, jsonify(
error="Validation Failed.", results=mask(out.decode('utf-8'))))

args = [
"deck", cmd, "--config", "/tmp/deck.yaml", "--skip-consumers", "--select-tag", selectTag, "--state", tempFolder
]
Expand Down
21 changes: 19 additions & 2 deletions microservices/gatewayApi/v2/routes/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ def write_config(namespace: str) -> object:

dfile = None

if 'configFile' in request.files:
if 'configFile' in request.files and not request.files['configFile'].filename == '':
log.debug("[%s] %s", namespace, request.files['configFile'])
dfile = request.files['configFile']
dry_run = request.values['dryRun']
elif request.content_type.startswith("application/json"):
elif request.content_type.startswith("application/json") and not request.json['configFile'] in [None, '']:
dfile = request.json['configFile']
dry_run = request.json['dryRun']
else:
Expand All @@ -205,6 +205,10 @@ def write_config(namespace: str) -> object:
for doc in yaml_documents_iter:
yaml_documents.append(doc)

if len(yaml_documents) == 0:
log.error("%s - %s" % (namespace, "Empty Configuration Passed"))
abort_early(event_id, 'publish', namespace, jsonify(error="Empty Configuration Passed"))

selectTag = "ns.%s" % namespace
ns_qualifier = None

Expand Down Expand Up @@ -285,6 +289,19 @@ def write_config(namespace: str) -> object:
cmd = "diff"

log.info("[%s] %s action using %s" % (namespace, cmd, selectTag))

args = [
"deck", "validate", "--config", "/tmp/deck.yaml", "--state", tempFolder
]
log.debug("[%s] Running %s" % (namespace, args))
deck_validate = Popen(args, stdout=PIPE, stderr=STDOUT)
out, err = deck_validate.communicate()

if deck_validate.returncode != 0:
log.warn("[%s] - %s" % (namespace, out.decode('utf-8')))
abort_early(event_id, 'validate', namespace, jsonify(
error="Validation Failed.", results=mask(out.decode('utf-8'))))

args = [
"deck", cmd, "--config", "/tmp/deck.yaml", "--skip-consumers", "--select-tag", selectTag, "--state", tempFolder
]
Expand Down
9 changes: 4 additions & 5 deletions microservices/gatewayJobScheduler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,18 @@ def transform_data_by_ns(data):
ns_attr_dict = {}
for route_obj in data:
select_tag = get_select_tag(route_obj['tags'])
host = route_obj['hosts'][0]
namespace = select_tag.split(".")[1]
name = 'wild-%s-%s' % (select_tag.replace(".", "-"), route_obj['hosts'][0])

if namespace not in ns_dict:
ns_dict[namespace] = []
ns_attr_dict[namespace] = ns_svc.get_namespace_attributes(namespace)

# check if namespace has data plane attribute
if ns_attr_dict[namespace].get('perm-data-plane', [''])[0] == os.getenv('DATA_PLANE'):
ns_dict[namespace].append({"name": name, "selectTag": select_tag, "host": host,
"dataPlane": ns_attr_dict[namespace].get('perm-data-plane')[0]})

for host in route_obj['hosts']:
name = 'wild-%s-%s' % (select_tag.replace(".", "-"), host)
ns_dict[namespace].append({"name": name, "selectTag": select_tag, "host": host,
"dataPlane": os.getenv('DATA_PLANE')})
return ns_dict
except Exception as err:
traceback.print_exc()
Expand Down

0 comments on commit ad2cc4f

Please sign in to comment.