Browse Source

Merge "Lazily cache parsed value of list/json parameters" into stable/train

stable/train
Zuul 3 months ago
committed by Gerrit Code Review
parent
commit
1e84054dcc
2 changed files with 21 additions and 22 deletions
  1. +19
    -20
      heat/engine/parameters.py
  2. +2
    -2
      heat/tests/test_parameters.py

+ 19
- 20
heat/engine/parameters.py View File

@@ -372,32 +372,32 @@ class StringParam(Parameter):
class ParsedParameter(Parameter):
"""A template parameter with cached parsed value."""

__slots__ = ('parsed',)
__slots__ = ('_parsed',)

def __init__(self, name, schema, value=None):
super(ParsedParameter, self).__init__(name, schema, value)
self._update_parsed()
def set_default(self, value):
super(ParsedParameter, self).set_default(value)
self._update_parsed()
def _update_parsed(self):
if self.has_value():
if self.user_value is not None:
self.parsed = self.parse(self.user_value)
self._parsed = None
@property
def parsed(self):
if self._parsed is None:
if self.has_value():
if self.user_value is not None:
self._parsed = self.parse(self.user_value)
else:
self._parsed = self.parse(self.default())
else:
self.parsed = self.parse(self.default())
self._parsed = self.default_parsed()
return self._parsed


class CommaDelimitedListParam(ParsedParameter, collections.Sequence):
"""A template parameter of type "CommaDelimitedList"."""

__slots__ = ('parsed',)
__slots__ = tuple()

def __init__(self, name, schema, value=None):
self.parsed = []
super(CommaDelimitedListParam, self).__init__(name, schema, value)
def default_parsed(self):
return []

def parse(self, value):
# only parse when value is not already a list
@@ -439,11 +439,10 @@ class CommaDelimitedListParam(ParsedParameter, collections.Sequence):
class JsonParam(ParsedParameter):
"""A template parameter who's value is map or list."""

__slots__ = ('parsed',)
__slots__ = tuple()

def __init__(self, name, schema, value=None):
self.parsed = {}
super(JsonParam, self).__init__(name, schema, value)
def default_parsed(self):
return {}

def parse(self, value):
try:


+ 2
- 2
heat/tests/test_parameters.py View File

@@ -380,7 +380,7 @@ class ParameterTestSpecific(common.HeatTestCase):
schema = {'Type': 'Json',
'ConstraintDescription': 'wibble'}
val = {"foo": "bar", "not_json": len}
err = self.assertRaises(ValueError,
err = self.assertRaises(exception.StackValidationFailed,
new_parameter, 'p', schema, val)
self.assertIn('Value must be valid JSON', six.text_type(err))

@@ -398,7 +398,7 @@ class ParameterTestSpecific(common.HeatTestCase):
schema = {'Type': 'Json',
'ConstraintDescription': 'wibble'}
val = "I am not a map"
err = self.assertRaises(ValueError,
err = self.assertRaises(exception.StackValidationFailed,
new_parameter, 'p', schema, val)
self.assertIn('Value must be valid JSON', six.text_type(err))



Loading…
Cancel
Save