本文整理汇总了Python中translator.hot.tosca_translator.TOSCATranslator类的典型用法代码示例。如果您正苦于以下问题:Python TOSCATranslator类的具体用法?Python TOSCATranslator怎么用?Python TOSCATranslator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TOSCATranslator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_translate_storage_notation1
def test_translate_storage_notation1(self):
'''TOSCA template with single BlockStorage and Attachment.'''
tosca_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../toscalib/tests/data/storage/"
"tosca_blockstorage_with_attachment_notation1.yaml")
tosca = ToscaTemplate(tosca_tpl)
translate = TOSCATranslator(tosca, self.parsed_params)
output = translate.translate()
expected_resource_1 = {'type': 'OS::Cinder::VolumeAttachment',
'properties':
{'instance_uuid': 'my_web_app_tier_1',
'location': '/default_location',
'volume_id': 'my_storage'}}
expected_resource_2 = {'type': 'OS::Cinder::VolumeAttachment',
'properties':
{'instance_uuid': 'my_web_app_tier_2',
'location': '/some_other_data_location',
'volume_id': 'my_storage'}}
output_dict = translator.toscalib.utils.yamlparser.simple_parse(output)
resources = output_dict.get('resources')
self.assertIn('myattachto_1', resources.keys())
self.assertIn('myattachto_2', resources.keys())
self.assertIn(expected_resource_1, resources.values())
self.assertIn(expected_resource_2, resources.values())
示例2: test_translate_server_existing_network
def test_translate_server_existing_network(self):
'''TOSCA template with 1 server attached to existing network.'''
tosca_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../toscalib/tests/data/network/"
"tosca_server_on_existing_network.yaml")
tosca = ToscaTemplate(tosca_tpl)
translate = TOSCATranslator(tosca, self.parsed_params)
output = translate.translate()
expected_resource_1 = {'type': 'OS::Neutron::Port',
'properties':
{'network': {'get_param': 'network_name'}
}}
expected_resource_2 = [{'port': {'get_resource': 'my_port'}}]
output_dict = translator.toscalib.utils.yamlparser.simple_parse(output)
resources = output_dict.get('resources')
self.assertItemsEqual(resources.keys(), ['my_server', 'my_port'])
self.assertEqual(resources.get('my_port'), expected_resource_1)
self.assertIn('properties', resources.get('my_server'))
self.assertIn('networks', resources.get('my_server').get('properties'))
translated_resource = resources.get('my_server').\
get('properties').get('networks')
self.assertEqual(translated_resource, expected_resource_2)
示例3: compare_tosca_translation_with_hot
def compare_tosca_translation_with_hot(tosca_file, hot_file, params):
"""Verify tosca translation against the given hot specification.
inputs:
tosca_file: relative local path or URL to the tosca input file
hot_file: relative path to expected hot output
params: dictionary of parameter name value pairs
Returns as a dictionary the difference between the HOT translation
of the given tosca_file and the given hot_file.
"""
from toscaparser.tosca_template import ToscaTemplate
from translator.hot.tosca_translator import TOSCATranslator
tosca_tpl = os.path.join(os.path.dirname(os.path.abspath(__file__)), tosca_file)
a_file = os.path.isfile(tosca_tpl)
if not a_file:
tosca_tpl = tosca_file
expected_hot_tpl = os.path.join(os.path.dirname(os.path.abspath(__file__)), hot_file)
tosca = ToscaTemplate(tosca_tpl, params, a_file)
translate = TOSCATranslator(tosca, params)
output = translate.translate()
output_dict = toscaparser.utils.yamlparser.simple_parse(output)
expected_output_dict = YamlUtils.get_dict(expected_hot_tpl)
return CompareUtils.diff_dicts(output_dict, expected_output_dict)
示例4: take_action
def take_action(self, parsed_args):
log.debug(_('Translating the template with input parameters'
'(%s).'), parsed_args)
output = None
if parsed_args.parameter:
parsed_params = parsed_args.parameter
else:
parsed_params = {}
if parsed_args.template_type == "tosca":
path = parsed_args.template_file
a_file = os.path.isfile(path)
a_url = UrlUtils.validate_url(path) if not a_file else False
if a_file or a_url:
validate = parsed_args.validate_only
if validate and validate.lower() == "true":
ToscaTemplate(path, parsed_params, a_file)
else:
tosca = ToscaTemplate(path, parsed_params, a_file)
translator = TOSCATranslator(tosca, parsed_params)
output = translator.translate()
else:
msg = _('Could not find template file.')
log.error(msg)
sys.stdout.write(msg)
raise SystemExit
if output:
if parsed_args.output_file:
with open(parsed_args.output_file, 'w+') as f:
f.write(output)
else:
print(output)
示例5: test_translate_multi_storage
def test_translate_multi_storage(self):
'''TOSCA template with multiple BlockStorage and Attachment.'''
tosca_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../toscalib/tests/data/storage/"
"tosca_multiple_blockstorage_with_attachment.yaml")
tosca = ToscaTemplate(tosca_tpl)
translated_volume_attachment = []
translate = TOSCATranslator(tosca, self.parsed_params)
output = translate.translate()
expected_resource_1 = {'type': 'OS::Cinder::VolumeAttachment',
'properties':
{'instance_uuid': 'my_server',
'location': {'get_param': 'storage_location'},
'volume_id': 'my_storage'}}
expected_resource_2 = {'type': 'OS::Cinder::VolumeAttachment',
'properties':
{'instance_uuid': 'my_server2',
'location': {'get_param': 'storage_location'},
'volume_id': 'my_storage2'}}
output_dict = translator.toscalib.utils.yamlparser.simple_parse(output)
resources = output_dict.get('resources')
translated_volume_attachment.append(resources.get('attachesto_1'))
translated_volume_attachment.append(resources.get('attachesto_2'))
self.assertIn(expected_resource_1, translated_volume_attachment)
self.assertIn(expected_resource_2, translated_volume_attachment)
示例6: _translate
def _translate(self, sourcetype, path, parsed_params, a_file):
output = None
if sourcetype == "tosca":
tosca = ToscaTemplate(path, parsed_params, a_file)
translator = TOSCATranslator(tosca, parsed_params)
output = translator.translate()
return output
示例7: test_translate_single_storage
def test_translate_single_storage(self):
'''TOSCA template with single BlockStorage and Attachment.'''
tosca_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../toscalib/tests/data/storage/"
"tosca_blockstorage_with_attachment.yaml")
tosca = ToscaTemplate(tosca_tpl)
translate = TOSCATranslator(tosca, self.parsed_params)
output = translate.translate()
expected_resouce = {'attachesto_1':
{'type': 'OS::Cinder::VolumeAttachment',
'properties':
{'instance_uuid': 'my_server',
'location': {'get_param': 'storage_location'},
'volume_id': 'my_storage'}}}
output_dict = translator.toscalib.utils.yamlparser.simple_parse(output)
resources = output_dict.get('resources')
translated_value = resources.get('attachesto_1')
expected_value = expected_resouce.get('attachesto_1')
self.assertEqual(translated_value, expected_value)
outputs = output_dict['outputs']
self.assertIn('private_ip', outputs)
self.assertEqual(
'Private IP address of the newly created compute instance.',
outputs['private_ip']['description'])
self.assertEqual({'get_attr': ['my_server', 'networks', 'private', 0]},
outputs['private_ip']['value'])
self.assertIn('volume_id', outputs)
self.assertEqual('The volume id of the block storage instance.',
outputs['volume_id']['description'])
self.assertEqual({'get_resource': 'my_storage'},
outputs['volume_id']['value'])
示例8: test_translate_output
def test_translate_output(self):
tosca_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../../toscalib/tests/data/"
"tosca_nodejs_mongodb_two_instances.yaml")
tosca = ToscaTemplate(tosca_tpl)
translate = TOSCATranslator(tosca, [])
hot_translation = translate.translate()
expected_output = {'nodejs_url':
{'description': 'URL for the nodejs '
'server, http://<IP>:3000',
'value':
{'get_attr':
['app_server', 'networks', 'private', 0]}},
'mongodb_url':
{'description': 'URL for the mongodb server.',
'value':
{'get_attr':
['mongo_server', 'networks', 'private', 0]}}}
hot_translation_dict = \
translator.toscalib.utils.yamlparser.simple_parse(hot_translation)
outputs = hot_translation_dict.get('outputs')
for resource_name in outputs:
translated_value = outputs.get(resource_name)
expected_value = expected_output.get(resource_name)
self.assertEqual(translated_value, expected_value)
示例9: generate_hot_from_tosca
def generate_hot_from_tosca(vnfd_dict):
parsed_params = dev_attrs.pop('param_values', {})
toscautils.updateimports(vnfd_dict)
try:
tosca = ToscaTemplate(parsed_params=parsed_params,
a_file=False,
yaml_dict_tpl=vnfd_dict)
except Exception as e:
LOG.debug("tosca-parser error: %s", str(e))
raise vnfm.ToscaParserFailed(error_msg_details=str(e))
monitoring_dict = toscautils.get_vdu_monitoring(tosca)
mgmt_ports = toscautils.get_mgmt_ports(tosca)
res_tpl = toscautils.get_resources_dict(tosca,
STACK_FLAVOR_EXTRA)
toscautils.post_process_template(tosca)
try:
translator = TOSCATranslator(tosca, parsed_params)
heat_template_yaml = translator.translate()
except Exception as e:
LOG.debug("heat-translator error: %s", str(e))
raise vnfm.HeatTranslatorFailed(error_msg_details=str(e))
heat_template_yaml = toscautils.post_process_heat_template(
heat_template_yaml, mgmt_ports, res_tpl,
unsupported_res_prop)
return heat_template_yaml, monitoring_dict
示例10: take_action
def take_action(self, parsed_args):
self.log.debug('take_action(%s)', parsed_args)
if parsed_args.parameter:
parsed_params = parsed_args.parameter
else:
parsed_params = {}
if parsed_args.template_type == "tosca":
path = parsed_args.template_file
a_file = os.path.isfile(path)
a_url = UrlUtils.validate_url(path) if not a_file else False
if a_file or a_url:
tosca = ToscaTemplate(path, parsed_params, a_file)
translator = TOSCATranslator(tosca, parsed_params)
output = translator.translate()
else:
sys.stdout.write('Could not find template file.')
raise SystemExit
if parsed_args.output_file:
with open(parsed_args.output_file, 'w+') as f:
f.write(output)
else:
print(output)
示例11: _translate
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, self.deploy)
log.debug(_('Translating the tosca template.'))
output = translator.translate()
return output
示例12: test_translate_three_networks_single_server
def test_translate_three_networks_single_server(self):
'''TOSCA template with three Networks and single Compute.'''
tosca_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../toscalib/tests/data/network/"
"tosca_one_server_three_networks.yaml")
tosca = ToscaTemplate(tosca_tpl)
translate = TOSCATranslator(tosca, self.parsed_params)
output = translate.translate()
output_dict = translator.toscalib.utils.yamlparser.simple_parse(output)
resources = output_dict.get('resources')
for net_num in range(1, 4):
net_name = 'my_network%d' % (net_num)
subnet_name = '%s_subnet' % (net_name)
port_name = 'my_port%d' % (net_num)
expected_resource_net = {'type': 'OS::Neutron::Net',
'properties':
{'name': 'net%d' % (net_num)
}}
expected_resource_subnet = {'type': 'OS::Neutron::Subnet',
'properties':
{'cidr': '192.168.%d.0/24' % (net_num),
'ip_version': 4,
'network': {'get_resource': net_name}
}}
expected_resource_port = {'type': 'OS::Neutron::Port',
'depends_on': [net_name],
'properties':
{'network': {'get_resource': net_name}
}}
self.assertIn(net_name, resources.keys())
self.assertIn(subnet_name, resources.keys())
self.assertIn(port_name, resources.keys())
self.assertEqual(resources.get(net_name), expected_resource_net)
self.assertEqual(resources.get(subnet_name),
expected_resource_subnet)
self.assertEqual(resources.get(port_name), expected_resource_port)
self.assertIn('properties', resources.get('my_server'))
self.assertIn('networks', resources.get('my_server').get('properties'))
translated_resource = resources.get('my_server').\
get('properties').get('networks')
expected_srv_networks = [{'port': {'get_resource': 'my_port1'}},
{'port': {'get_resource': 'my_port2'}},
{'port': {'get_resource': 'my_port3'}}]
self.assertEqual(translated_resource, expected_srv_networks)
示例13: test_post_process_heat_template
def test_post_process_heat_template(self):
tosca1 = ToscaTemplate(parsed_params={}, a_file=False, yaml_dict_tpl=self.vnfd_dict)
toscautils.post_process_template(tosca1)
translator = TOSCATranslator(tosca1, {})
heat_template_yaml = translator.translate()
expected_heat_tpl = _get_template("hot_tosca_openwrt.yaml")
mgmt_ports = toscautils.get_mgmt_ports(self.tosca)
heat_tpl = toscautils.post_process_heat_template(heat_template_yaml, mgmt_ports, {}, {})
heatdict = yaml.load(heat_tpl)
expecteddict = yaml.load(expected_heat_tpl)
self.assertEqual(heatdict, expecteddict)
示例14: test_translate_single_network_single_server
def test_translate_single_network_single_server(self):
'''TOSCA template with single Network and single Compute.'''
tosca_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"../toscalib/tests/data/network/"
"tosca_one_server_one_network.yaml")
tosca = ToscaTemplate(tosca_tpl)
translate = TOSCATranslator(tosca, self.parsed_params)
output = translate.translate()
expected_resource_1 = {'type': 'OS::Neutron::Net',
'properties':
{'name': {'get_param': 'network_name'}
}}
expected_resource_2 = {'type': 'OS::Neutron::Subnet',
'properties':
{'cidr': '192.168.0.0/24',
'ip_version': 4,
'allocation_pools': [{'start': '192.168.0.50',
'end': '192.168.0.200'}],
'gateway_ip': '192.168.0.1',
'network': {'get_resource': 'my_network'}
}}
expected_resource_3 = {'type': 'OS::Neutron::Port',
'depends_on': ['my_network'],
'properties':
{'network': {'get_resource': 'my_network'}
}}
expected_resource_4 = [{'port': {'get_resource': 'my_port'}}]
output_dict = translator.toscalib.utils.yamlparser.simple_parse(output)
resources = output_dict.get('resources')
self.assertIn('my_network', resources.keys())
self.assertIn('my_network_subnet', resources.keys())
self.assertIn('my_port', resources.keys())
self.assertIn('my_server', resources.keys())
self.assertEqual(resources.get('my_network'), expected_resource_1)
self.assertEqual(resources.get('my_network_subnet'),
expected_resource_2)
self.assertEqual(resources.get('my_port'), expected_resource_3)
self.assertIn('properties', resources.get('my_server'))
self.assertIn('networks', resources.get('my_server').get('properties'))
translated_resource = resources.get('my_server').\
get('properties').get('networks')
self.assertEqual(translated_resource, expected_resource_4)
示例15: take_action
def take_action(self, parsed_args):
self.log.debug('take_action(%s)', parsed_args)
if not os.path.isfile(parsed_args.template_file):
sys.stdout.write('Could not find template file.')
raise SystemExit
# TODO(stevemar): parsed_params doesn't default nicely
parsed_params = {}
if parsed_args.template_type == "tosca":
tosca = ToscaTemplate(parsed_args.template_file)
translator = TOSCATranslator(tosca, parsed_params)
output = translator.translate()
if parsed_args.output_file:
with open(parsed_args.output_file, 'w+') as f:
f.write(output)
else:
print(output)