Haiyang DING e39f712e51 Add scalar unit type
Implement scalar-unit.size type:

1. any number of spaces (including zero or none) is allowed between the
scalar value and the unit value.
2. support constraints like greater_or_equal and in_range. (eg:
storage_size: in_range [4 GB, 20 GB ] )
3. default unit is 'B'(byte) according to the TOSCA profile while
allowing user to set the unit of the output number as long as the input
unit is a valid one
4. supported scalar-unit.size (case-insensitive): B, KB, KiB, MB, MiB,
GB, GiB, TB, TiB

implement BP: add-scalar-unit-type

Change-Id: Idf3705312afd33d7aeadc3f8856cf728c41ec190
2015-01-28 01:53:29 +00:00

76 lines
2.3 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from translator.toscalib.dataentity import DataEntity
from translator.toscalib.elements.constraints import Schema
from translator.toscalib.functions import is_function
class Property(object):
'''TOSCA built-in Property type.'''
PROPERTY_KEYS = (
TYPE, REQUIRED, DESCRIPTION, DEFAULT, CONSTRAINTS
) = (
'type', 'required', 'description', 'default', 'constraints'
)
ENTRY_SCHEMA_KEYS = (
ENTRYTYPE, ENTRYPROPERTIES
) = (
'type', 'properties'
)
def __init__(self, property_name, value, schema_dict, custom_def=None):
self.name = property_name
self.value = value
self.custom_def = custom_def
self.schema = Schema(property_name, schema_dict)
@property
def type(self):
return self.schema.type
@property
def required(self):
return self.schema.required
@property
def description(self):
return self.schema.description
@property
def default(self):
return self.schema.default
@property
def constraints(self):
return self.schema.constraints
@property
def entry_schema(self):
return self.schema.entry_schema
def validate(self):
'''Validate if not a reference property.'''
if not is_function(self.value):
if self.type == Schema.STRING:
self.value = str(self.value)
DataEntity.validate_datatype(self.type, self.value,
self.entry_schema, self.custom_def)
self._validate_constraints()
def _validate_constraints(self):
if self.constraints:
for constraint in self.constraints:
constraint.validate(self.value)