Added log messages

Added log messages for heat-translator

Change-Id: I2a6f3626ab565b19a5433d1ca6ae5d6b6254c298
Implements: blueprint heat-translator-logging
This commit is contained in:
Mounika 2015-12-11 15:56:08 +05:30 committed by Aparna Kalyani
parent ab4979a816
commit 19cf8f1d5b
12 changed files with 94 additions and 34 deletions

View File

@ -23,7 +23,6 @@ from toscaparser.utils.gettextutils import _
import toscaparser.utils.yamlparser
YAML_ORDER_PARSER = toscaparser.utils.yamlparser.simple_ordered_parse
log = logging.getLogger('tosca')
log = logging.getLogger('heat-translator')
@ -49,7 +48,7 @@ class MemoryUnit(object):
else:
unit = MemoryUnit.UNIT_SIZE_DEFAULT
log.info(_('A memory unit is not provided for size; using the '
'default unit %(default)s') % {'default': 'B'})
'default unit %(default)s.') % {'default': 'B'})
regex = re.compile('(\d*)\s*(\w*)')
result = regex.match(str(size)).groups()
if result[1]:
@ -59,7 +58,7 @@ class MemoryUnit(object):
* math.pow(MemoryUnit.UNIT_SIZE_DICT
[unit], -1))
log.info(_('Given size %(size)s is converted to %(num)s '
'%(unit)s') % {'size': size,
'%(unit)s.') % {'size': size,
'num': converted, 'unit': unit})
else:
converted = (str_to_num(result[0]))
@ -76,7 +75,7 @@ class MemoryUnit(object):
msg = _('Provided unit "{0}" is not valid. The valid units are'
' {1}').format(unit, MemoryUnit.UNIT_SIZE_DICT.keys())
log.warning(msg)
log.error(msg)
raise ValueError(msg)

View File

@ -12,10 +12,14 @@
# under the License.
from collections import OrderedDict
import logging
from toscaparser.utils.gettextutils import _
KEYS = (TYPE, DESCRIPTION, DEFAULT, CONSTRAINTS, HIDDEN, LABEL) = \
('type', 'description', 'default', 'constraints', 'hidden', 'label')
log = logging.getLogger('heat-translator')
class HotParameter(object):
'''Attributes for HOT parameter section.'''
@ -29,6 +33,7 @@ class HotParameter(object):
self.default = default
self.hidden = hidden
self.constraints = constraints
log.info(_('Initialized the input parameters.'))
def get_dict_output(self):
param_sections = OrderedDict()

View File

@ -12,6 +12,7 @@
# under the License.
from collections import OrderedDict
import logging
import six
from toscaparser.functions import GetInput
@ -23,6 +24,7 @@ SECTIONS = (TYPE, PROPERTIES, MEDADATA, DEPENDS_ON, UPDATE_POLICY,
DELETION_POLICY) = \
('type', 'properties', 'metadata',
'depends_on', 'update_policy', 'deletion_policy')
log = logging.getLogger('heat-translator')
class HotResource(object):
@ -31,6 +33,7 @@ class HotResource(object):
def __init__(self, nodetemplate, name=None, type=None, properties=None,
metadata=None, depends_on=None,
update_policy=None, deletion_policy=None):
log.debug(_('Translating TOSCA node type to HOT resource type.'))
self.nodetemplate = nodetemplate
if name:
self.name = name

View File

@ -12,9 +12,13 @@
# under the License.
from collections import OrderedDict
import logging
import textwrap
from toscaparser.utils.gettextutils import _
import yaml
log = logging.getLogger('heat-translator')
class HotTemplate(object):
'''Container for full Heat Orchestration template.'''
@ -41,6 +45,7 @@ class HotTemplate(object):
return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', nodes)
def output_to_yaml(self):
log.debug(_('Converting translated output to yaml format.'))
dict_output = OrderedDict()
# Version
version_string = self.VERSION + ": " + self.LATEST + "\n\n"

View File

@ -18,7 +18,7 @@ from toscaparser.functions import GetInput
from toscaparser.utils.gettextutils import _
from translator.hot.syntax.hot_resource import HotResource
log = logging.getLogger("tosca")
log = logging.getLogger('heat-translator')
# Name used to dynamically load appropriate map class.
TARGET_CLASS_NAME = 'ToscaBlockStorage'
@ -45,8 +45,10 @@ class ToscaBlockStorage(HotResource):
get_num_from_scalar_unit('GiB'))
if size_value == 0:
# OpenStack Heat expects size in GB
msg = _('Cinder Volume Size unit should be in GB.')
log.error(msg)
raise InvalidPropertyValueError(
what=_('Cinder Volume Size unit should be in GBs'))
what=msg)
elif int(size_value) < size_value:
size_value = int(size_value) + 1
log.warning(_("Cinder unit value should be in "

View File

@ -16,10 +16,12 @@ import logging
import os
import requests
from toscaparser.utils.gettextutils import _
from toscaparser.utils.validateutils import TOSCAVersionProperty
import translator.common.utils
from translator.hot.syntax.hot_resource import HotResource
log = logging.getLogger('tosca')
log = logging.getLogger('heat-translator')
# Name used to dynamically load appropriate map class.
TARGET_CLASS_NAME = 'ToscaCompute'
@ -171,6 +173,7 @@ class ToscaCompute(HotResource):
return flavor_dict
def _best_flavor(self, properties):
log.info(_('Choosing the best flavor for given attributes.'))
# Check whether user exported all required environment variables.
flavors = FLAVORS
if self._check_for_env_variables():
@ -239,6 +242,7 @@ class ToscaCompute(HotResource):
if isinstance(size, int):
if this_dict[flavor][attr] >= size:
matching_flavors.append(flavor)
log.debug(_('Returning list of flavors matching the attribute size.'))
return matching_flavors
def _least_flavor(self, this_list, this_dict, attr):
@ -267,6 +271,8 @@ class ToscaCompute(HotResource):
# Note: We treat private and public IP addresses equally, but
# this will change in the future when TOSCA starts to support
# multiple private/public IP addresses.
log.debug(_('Converting TOSCA attribute for a nodetemplate to a HOT \
attriute.'))
if attribute == 'private_address' or \
attribute == 'public_address':
attr['get_attr'] = [self.name, 'networks', 'private', 0]

View File

@ -12,23 +12,25 @@
# under the License.
import logging
from toscaparser.utils.gettextutils import _
from translator.hot.syntax.hot_template import HotTemplate
from translator.hot.translate_inputs import TranslateInputs
from translator.hot.translate_node_templates import TranslateNodeTemplates
from translator.hot.translate_outputs import TranslateOutputs
log = logging.getLogger('heat-translator')
class TOSCATranslator(object):
'''Invokes translation methods.'''
log = logging.getLogger('heat-translator')
def __init__(self, tosca, parsed_params):
super(TOSCATranslator, self).__init__()
self.tosca = tosca
self.hot_template = HotTemplate()
self.parsed_params = parsed_params
self.node_translator = None
log.info(_('Initialized parmaters for translation.'))
def translate(self):
self._resolve_input()
@ -58,7 +60,7 @@ class TOSCATranslator(object):
self.parsed_params[node_prop.value['get_input']]
except Exception:
msg = (_('Must specify all input values in \
TOSCA template, missing %s') %
TOSCA template, missing %s.') %
node_prop.value['get_input'])
self.log.warning(msg)
log.error(msg)
raise ValueError(msg)

View File

@ -58,6 +58,7 @@ log = logging.getLogger('heat-translator')
class TranslateInputs(object):
'''Translate TOSCA Inputs to Heat Parameters.'''
def __init__(self, inputs, parsed_params):
@ -70,6 +71,7 @@ class TranslateInputs(object):
def _translate_inputs(self):
hot_inputs = []
hot_default = None
log.info(_('Translating TOSCA input type to HOT input type.'))
for input in self.inputs:
hot_input_type = TOSCA_TO_HOT_INPUT_TYPES[input.type]
@ -80,10 +82,10 @@ class TranslateInputs(object):
hot_default = DataEntity.validate_datatype(input.type,
input.default)
else:
log.warning(_("Need to specify a value "
"for input {0}").format(input.name))
raise Exception(_("Need to specify a value "
"for input {0}").format(input.name))
msg = _("Need to specify a value "
"for input {0}.").format(input.name)
log.error(msg)
raise Exception(msg)
if input.type == "scalar-unit.size":
# Assumption here is to use this scalar-unit.size for size of
# cinder volume in heat templates and will be in GB.
@ -92,9 +94,9 @@ class TranslateInputs(object):
hot_default = (ScalarUnit_Size(hot_default).
get_num_from_scalar_unit('GiB'))
if hot_default == 0:
log.warning(_('Unit value should be > 0.'))
raise Exception(_(
'Unit value should be > 0.'))
msg = _('Unit value should be > 0.')
log.error(msg)
raise Exception(msg)
elif int(hot_default) < hot_default:
hot_default = int(hot_default) + 1
log.warning(_("Cinder unit value should be in multiples"

View File

@ -20,6 +20,7 @@ from toscaparser.functions import GetAttribute
from toscaparser.functions import GetInput
from toscaparser.functions import GetProperty
from toscaparser.relationship_template import RelationshipTemplate
from toscaparser.utils.gettextutils import _
from translator.common.exception import ToscaClassAttributeError
from translator.common.exception import ToscaClassImportError
from translator.common.exception import ToscaModImportError
@ -141,6 +142,7 @@ class TranslateNodeTemplates(object):
# list of all HOT resources generated
self.hot_resources = []
# mapping between TOSCA nodetemplate and HOT resource
log.debug(_('Mapping between TOSCA nodetemplate and HOT resource.'))
self.hot_lookup = {}
def translate(self):
@ -159,6 +161,7 @@ class TranslateNodeTemplates(object):
def _translate_nodetemplates(self):
log.debug(_('Translating the node templates.'))
suffix = 0
# Copy the TOSCA graph: nodetemplate
for node in self.nodetemplates:
@ -381,7 +384,7 @@ class TranslateNodeTemplates(object):
msg = _("Template error: "
"no configuration found for ConnectsTo "
"in {1}").format(self.nodetemplate.name)
log.warning(msg)
log.error(msg)
raise Exception(msg)
config_name = source_node.name + '_' + target_name + '_connect_config'
implement = connect_config.get('implementation')

View File

@ -11,13 +11,19 @@
# License for the specific language governing permissions and limitations
# under the License.
import logging
from toscaparser.utils.gettextutils import _
from translator.hot.syntax.hot_output import HotOutput
log = logging.getLogger('heat-translator')
class TranslateOutputs(object):
'''Translate TOSCA Outputs to Heat Outputs.'''
def __init__(self, outputs, node_translator):
log.debug(_('Translating TOSCA outputs to HOT outputs.'))
self.outputs = outputs
self.nodes = node_translator

View File

@ -12,6 +12,7 @@
"""Translate action implementations"""
import logging
import logging.config
import os
import sys
@ -19,15 +20,20 @@ import sys
from cliff import command
from toscaparser.tosca_template import ToscaTemplate
from toscaparser.utils.gettextutils import _
from translator.common.utils import UrlUtils
from translator.hot.tosca_translator import TOSCATranslator
from translator.osc import utils
logging.config.fileConfig('heat_translator_logging.conf')
log = logging.getLogger('heat-translator')
class TranslateTemplate(command.Command):
"""Translate a template"""
log = logging.getLogger('heat-translator' + '.TranslateTemplate')
auth_required = False
def get_parser(self, prog_name):
@ -62,7 +68,8 @@ class TranslateTemplate(command.Command):
return parser
def take_action(self, parsed_args):
self.log.debug('take_action(%s)', parsed_args)
log.debug(_('Translating the template with input parameters'
'(%s).'), parsed_args)
output = None
if parsed_args.parameter:
@ -83,7 +90,9 @@ class TranslateTemplate(command.Command):
translator = TOSCATranslator(tosca, parsed_params)
output = translator.translate()
else:
sys.stdout.write('Could not find template file.')
msg = _('Could not find template file.')
log.error(msg)
sys.stdout.write(msg)
raise SystemExit
if output:

View File

@ -11,6 +11,7 @@
# under the License.
import logging
import logging.config
import os
import sys
@ -37,6 +38,7 @@ other required arguments.
"""
logging.config.fileConfig('heat_translator_logging.conf')
log = logging.getLogger("heat-translator")
@ -48,14 +50,17 @@ class TranslatorShell(object):
if len(args) < 2:
msg = _("The program requires minimum two arguments. "
"Please refer to the usage documentation.")
log.error(msg)
raise ValueError(msg)
if "--template-file=" not in args[0]:
msg = _("The program expects --template-file as first argument. "
"Please refer to the usage documentation.")
log.error(msg)
raise ValueError(msg)
if "--template-type=" not in args[1]:
msg = _("The program expects --template-type as second argument. "
"Please refer to the usage documentation.")
log.error(msg)
raise ValueError(msg)
def main(self, args):
@ -65,11 +70,14 @@ class TranslatorShell(object):
template_type = args[1].split('--template-type=')[1]
# e.g. --template_type=tosca
if not template_type:
raise ValueError(_("Template type is needed. "
"For example, 'tosca'"))
msg = _("Template type is needed. For example, 'tosca'")
log.error(msg)
raise ValueError(msg)
elif template_type not in self.SUPPORTED_TYPES:
raise ValueError(_("%(value)s is not a valid template type.")
% {'value': template_type})
msg = _("%(value)s is not a valid template type.") % {
'value': template_type}
log.error(msg)
raise ValueError(msg)
parsed_params = {}
validate_only = None
output_file = None
@ -96,13 +104,17 @@ class TranslatorShell(object):
if run_only_validation:
ToscaTemplate(path, parsed_params, a_file)
else:
log.info(
_('Checked whether template path is a file or url path.'))
heat_tpl = self._translate(template_type, path, parsed_params,
a_file)
if heat_tpl:
self._write_output(heat_tpl, output_file)
else:
raise ValueError(_("The path %(path)s is not a valid file"
" or URL.") % {'path': path})
msg = _("The path %(path)s is not a valid file or URL.") % {
'path': path}
log.error(msg)
raise ValueError(msg)
def _parse_parameters(self, parameter_list):
parsed_inputs = {}
@ -114,27 +126,33 @@ class TranslatorShell(object):
for param in inputs:
keyvalue = param.split('=')
# Validate the parameter has both a name and value
msg = _("'%(param)s' is not a well-formed parameter.") % {
'param': param}
if keyvalue.__len__() is 2:
# Assure parameter name is not zero-length or whitespace
stripped_name = keyvalue[0].strip()
if not stripped_name:
raise ValueError(_("'%(param)s' is not a well-formed "
"parameter.") % {'param': param})
log.error(msg)
raise ValueError(msg)
# Add the valid parameter to the dictionary
parsed_inputs[keyvalue[0]] = keyvalue[1]
else:
raise ValueError(_("'%(param)s' is not a well-formed "
"parameter.") % {'param': param})
log.error(msg)
raise ValueError(msg)
else:
raise ValueError(_("'%(list)s' is not a valid parameter list.")
% {'list': parameter_list})
msg = _("'%(list)s' is not a valid parameter list.") % {
'list': parameter_list}
log.error(msg)
raise ValueError(msg)
return parsed_inputs
def _translate(self, sourcetype, path, parsed_params, a_file):
output = None
if sourcetype == "tosca":
log.debug(_('Loading the tosca template.'))
tosca = ToscaTemplate(path, parsed_params, a_file)
translator = TOSCATranslator(tosca, parsed_params)
log.debug(_('Translating the tosca template.'))
output = translator.translate()
return output