Skip to content

Commit

Permalink
feat(rootfs/api): add validation and changes.
Browse files Browse the repository at this point in the history
implement validation for serializer and add termination grace period to
the list changes
  • Loading branch information
notmaxx committed Jun 16, 2018
1 parent 581bead commit 63c3f60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
10 changes: 10 additions & 0 deletions rootfs/api/models/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ def save(self, *args, **kwargs): # noqa
changes = 'changed limits for '+', '.join(changes)
self.summary += "{} {}".format(self.config.owner, changes)

<<<<<<< HEAD
# if the lifecycle_post_start hooks changed, log the dict diff
changes = []
old_lifecycle_post_start = old_config.lifecycle_post_start if old_config else {}
Expand Down Expand Up @@ -459,6 +460,15 @@ def save(self, *args, **kwargs): # noqa
if changes:
if self.summary:
self.summary += ' and '

# if the timeouts changed, log the dict diff
changes = []
old_timeout = old_config.termination_grace_period if old_config else {}
diff = dict_diff(self.config.termination_grace_period, old_timeout)
if diff.get('added') or diff.get('changed') or diff.get('deleted'):
changes.append('termination_grace_period')
if changes:
changes = 'changed timeouts for '+', '.join(changes)
self.summary += "{} {}".format(self.config.owner, changes)

# if the tags changed, log the dict diff
Expand Down
31 changes: 15 additions & 16 deletions rootfs/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,21 @@ def validate_cpu(self, data):

return data

def validate_termination_grace_period(self, data):
for key, value in data.items():
if value is None: # use NoneType to unset an item
continue

if not re.match(PROCTYPE_MATCH, key):
raise serializers.ValidationError(PROCTYPE_MISMATCH_MSG)

timeout = re.match(TERMINATION_GRACE_PERIOD_MATCH, value)
if not timeout:
raise serializers.ValidationError(
"Termination Grace Period format: <value>, where value must be a numeric")

return data

def validate_tags(self, data):
for key, value in data.items():
if value is None: # use NoneType to unset an item
Expand Down Expand Up @@ -374,22 +389,6 @@ def validate_healthcheck(self, data):

return data

def validate_termination_grace_period(self, data):
for key, value in data.items():
if value is None: # use NoneType to unset an item
continue

if not re.match(PROCTYPE_MATCH, key):
raise serializers.ValidationError(PROCTYPE_MISMATCH_MSG)

timeout = re.match(TERMINATION_GRACE_PERIOD_MATCH, value)
if not timeout:
raise serializers.ValidationError(
"Termination Grace Period format: <value>, where value must be a numeric")

return data


class ReleaseSerializer(serializers.ModelSerializer):
"""Serialize a :class:`~api.models.Release` model."""

Expand Down

0 comments on commit 63c3f60

Please sign in to comment.