def test_plugin_prefix_with_parent_resource(self): controller = self.DummySvcPlugin() parent = dict(member_name="tenant", collection_name="tenants") member = {'custom_member_action': "GET"} collections = {'collection_action': "GET"} res_ext = extensions.ResourceExtension('tweedles', controller, parent, path_prefix="/dummy_svc", member_actions=member, collection_actions=collections) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) index_response = test_app.get("/dummy_svc/tenants/1/tweedles") self.assertEqual(200, index_response.status_int) response = test_app.get("/dummy_svc/tenants/1/" "tweedles/1/custom_member_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['member_action'], "value") response = test_app.get("/dummy_svc/tenants/2/" "tweedles/collection_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['collection'], "value")
def to_hosting_vnf(device_dict, action_cb): return { 'id': device_dict['id'], 'management_ip_addresses': jsonutils.loads( device_dict['mgmt_url']), 'action_cb': action_cb, 'device': device_dict, 'monitoring_policy': jsonutils.loads( device_dict['attributes']['monitoring_policy']) }
def test_resource_extension_for_get_custom_collection_action(self): controller = self.ResourceExtensionController() collections = {'custom_collection_action': "GET"} res_ext = extensions.ResourceExtension('tweedles', controller, collection_actions=collections) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) response = test_app.get("/tweedles/custom_collection_action") self.assertEqual(200, response.status_int) LOG.debug(jsonutils.loads(response.body)) self.assertEqual(jsonutils.loads(response.body)['collection'], "value")
def mgmt_call(self, plugin, context, device, kwargs): if kwargs[mgmt_constants.KEY_ACTION] != mgmt_constants.ACTION_UPDATE_DEVICE: return dev_attrs = device.get("attributes", {}) mgmt_url = jsonutils.loads(device.get("mgmt_url", "{}")) if not mgmt_url: return vdus_config = dev_attrs.get("config", "") config_yaml = yaml.load(vdus_config) if not config_yaml: return vdus_config_dict = config_yaml.get("vdus", {}) for vdu, vdu_dict in vdus_config_dict.items(): config = vdu_dict.get("config", {}) for key, conf_value in config.items(): KNOWN_SERVICES = ("firewall",) if key not in KNOWN_SERVICES: continue mgmt_ip_address = mgmt_url.get(vdu, "") if not mgmt_ip_address: LOG.warning(_("tried to configure unknown mgmt " "address %s"), vdu) continue self._config_service(mgmt_ip_address, key, conf_value)
def mgmt_call(self, plugin, context, device, kwargs): if (kwargs[mgmt_constants.KEY_ACTION] != mgmt_constants.ACTION_UPDATE_DEVICE): return dev_attrs = device.get('attributes', {}) mgmt_url = jsonutils.loads(device.get('mgmt_url', '{}')) if not mgmt_url: return vdus_config = dev_attrs.get('config', '') config_yaml = yaml.load(vdus_config) if not config_yaml: return vdus_config_dict = config_yaml.get('vdus', {}) for vdu, vdu_dict in vdus_config_dict.items(): config = vdu_dict.get('config', {}) for key, conf_value in config.items(): KNOWN_SERVICES = ('firewall', ) if key not in KNOWN_SERVICES: continue mgmt_ip_address = mgmt_url.get(vdu, '') if not mgmt_ip_address: LOG.warning(_('tried to configure unknown mgmt ' 'address %s'), vdu) continue self._config_service(mgmt_ip_address, key, conf_value)
def _goose_handler(req, res): # NOTE: This only handles JSON responses. # You can use content type header to test for XML. data = jsonutils.loads(res.body) data['FOXNSOX:googoose'] = req.GET.get('chewing') res.body = jsonutils.dumps(data) return res
def _bands_handler(req, res): #NOTE: This only handles JSON responses. # You can use content type header to test for XML. data = jsonutils.loads(res.body) data['FOXNSOX:big_bands'] = 'Pig Bands!' res.body = jsonutils.dumps(data) return res
def _bands_handler(req, res): # NOTE: This only handles JSON responses. # You can use content type header to test for XML. data = jsonutils.loads(res.body) data['FOXNSOX:big_bands'] = 'Pig Bands!' res.body = jsonutils.dumps(data) return res
def test_resource_extension_with_custom_member_action_and_attr_map(self): controller = self.ResourceExtensionController() member = {'custom_member_action': "GET"} params = { 'tweedles': { 'id': { 'allow_post': False, 'allow_put': False, 'validate': { 'type:uuid': None }, 'is_visible': True }, 'name': { 'allow_post': True, 'allow_put': True, 'validate': { 'type:string': None }, 'default': '', 'is_visible': True }, } } res_ext = extensions.ResourceExtension('tweedles', controller, member_actions=member, attr_map=params) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) response = test_app.get("/tweedles/some_id/custom_member_action") self.assertEqual(200, response.status_int) self.assertEqual( jsonutils.loads(response.body)['member_action'], "value")
def mgmt_call(self, plugin, context, device, kwargs): if (kwargs[mgmt_constants.KEY_ACTION] != mgmt_constants.ACTION_UPDATE_DEVICE): return dev_attrs = device.get('attributes', {}) service_type = dev_attrs.get('service_type') if not service_type: return mgmt_url = jsonutils.loads(device.get('mgmt_url', '{}')) if not mgmt_url: return vdus_config = dev_attrs.get('config', '') config_yaml = yaml.load(vdus_config) if not config_yaml: return vdus_config_dict = config_yaml.get('vdus', {}) for vdu, vdu_dict in vdus_config_dict.items(): config = vdu_dict.get('config', {}) for key, conf_value in config.items(): KNOWN_SERVICES = ('firewall', ) if key not in KNOWN_SERVICES: continue mgmt_ip_address = mgmt_url.get(vdu, '') if not mgmt_ip_address: LOG.warn(_('tried to configure unknown mgmt address %s'), vdu) continue self._config_service(mgmt_ip_address, key, conf_value)
def _goose_handler(req, res): #NOTE: This only handles JSON responses. # You can use content type header to test for XML. data = jsonutils.loads(res.body) data['FOXNSOX:googoose'] = req.GET.get('chewing') res.body = jsonutils.dumps(data) return res
def test_get_resources(self): app = _setup_extensions_test_app() response = app.get("/dummy_resources/1?chewing=newblue") response_data = jsonutils.loads(response.body) self.assertEqual('newblue', response_data['FOXNSOX:googoose']) self.assertEqual("Pig Bands!", response_data['FOXNSOX:big_bands'])
def to_hosting_device(device_dict, down_cb): return { 'id': device_dict['id'], 'management_ip_addresses': jsonutils.loads( device_dict['mgmt_url']), 'boot_wait': cfg.CONF.monitor.boot_wait, 'down_cb': down_cb, 'device': device_dict, }
def load_json(cls, data, default_rule=None): """ Allow loading of JSON rule data. """ # Suck in the JSON data and parse the rules rules = dict( (k, parse_rule(v)) for k, v in jsonutils.loads(data).items()) return cls(rules, default_rule)
def test_resource_ext_with_custom_member_action_gets_plugin_prefix(self): controller = self.DummySvcPlugin() member = {'custom_member_action': "GET"} collections = {'collection_action': "GET"} res_ext = extensions.ResourceExtension('tweedles', controller, path_prefix="/dummy_svc", member_actions=member, collection_actions=collections) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) response = test_app.get("/dummy_svc/tweedles/1/custom_member_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['member_action'], "value") response = test_app.get("/dummy_svc/tweedles/collection_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['collection'], "value")
def load_json(cls, data, default_rule=None): """ Allow loading of JSON rule data. """ # Suck in the JSON data and parse the rules rules = dict((k, parse_rule(v)) for k, v in jsonutils.loads(data).items()) return cls(rules, default_rule)
def test_resource_extension_with_custom_member_action(self): controller = self.ResourceExtensionController() member = {'custom_member_action': "GET"} res_ext = extensions.ResourceExtension('tweedles', controller, member_actions=member) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) response = test_app.get("/tweedles/some_id/custom_member_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['member_action'], "value")
def test_resource_ext_for_nested_resource_custom_collection_action(self): controller = self.ResourceExtensionController() collections = {'custom_collection_action': "GET"} parent = dict(collection_name='beetles', member_name='beetle') res_ext = extensions.ResourceExtension('tweedles', controller, collection_actions=collections, parent=parent) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) response = test_app.get("/beetles/beetle_id" "/tweedles/custom_collection_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['collection'], "value")
def test_extend_get_resource_response(self): def extend_response_data(req, res): data = jsonutils.loads(res.body) data['FOXNSOX:extended_key'] = req.GET.get('extended_key') res.body = jsonutils.dumps(data) return res app = self._setup_app_with_request_handler(extend_response_data, 'GET') response = app.get("/dummy_resources/1?extended_key=extended_data") self.assertEqual(200, response.status_int) response_data = jsonutils.loads(response.body) self.assertEqual('extended_data', response_data['FOXNSOX:extended_key']) self.assertEqual('knox', response_data['fort'])
def test_resource_extension_with_custom_member_action_and_attr_map(self): controller = self.ResourceExtensionController() member = {'custom_member_action': "GET"} params = { 'tweedles': { 'id': {'allow_post': False, 'allow_put': False, 'validate': {'type:uuid': None}, 'is_visible': True}, 'name': {'allow_post': True, 'allow_put': True, 'validate': {'type:string': None}, 'default': '', 'is_visible': True}, } } res_ext = extensions.ResourceExtension('tweedles', controller, member_actions=member, attr_map=params) test_app = _setup_extensions_test_app(SimpleExtensionManager(res_ext)) response = test_app.get("/tweedles/some_id/custom_member_action") self.assertEqual(200, response.status_int) self.assertEqual(jsonutils.loads(response.body)['member_action'], "value")
def create(self, plugin, context, device, auth_attr): LOG.debug(_('device %s'), device) attributes = device['device_template']['attributes'].copy() vnfd_yaml = attributes.pop('vnfd', None) fields = dict((key, attributes.pop(key)) for key in ('stack_name', 'template_url', 'template') if key in attributes) for key in ('files', 'parameters'): if key in attributes: fields[key] = jsonutils.loads(attributes.pop(key)) # overwrite parameters with given dev_attrs for device creation dev_attrs = device['attributes'].copy() fields.update(dict((key, dev_attrs.pop(key)) for key in ('stack_name', 'template_url', 'template') if key in dev_attrs)) for key in ('files', 'parameters'): if key in dev_attrs: fields.setdefault(key, {}).update( jsonutils.loads(dev_attrs.pop(key))) region_name = device.get('placement_attr', {}).get('region_name', None) heatclient_ = HeatClient(auth_attr, region_name) unsupported_res_prop = self.fetch_unsupported_resource_prop( heatclient_) LOG.debug('vnfd_yaml %s', vnfd_yaml) if vnfd_yaml is not None: vnfd_dict = yamlparser.simple_ordered_parse(vnfd_yaml) LOG.debug('vnfd_dict %s', vnfd_dict) monitoring_dict = {'vdus': {}} if 'tosca_definitions_version' in 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) else: assert 'template' not in fields assert 'template_url' not in fields template_dict = yaml.load(HEAT_TEMPLATE_BASE) outputs_dict = {} template_dict['outputs'] = outputs_dict if 'get_input' in vnfd_yaml: self._process_parameterized_input(dev_attrs, vnfd_dict) KEY_LIST = (('description', 'description'), ) for (key, vnfd_key) in KEY_LIST: if vnfd_key in vnfd_dict: template_dict[key] = vnfd_dict[vnfd_key] for vdu_id, vdu_dict in vnfd_dict.get('vdus', {}).items(): template_dict.setdefault('resources', {})[vdu_id] = { "type": "OS::Nova::Server" } resource_dict = template_dict['resources'][vdu_id] KEY_LIST = (('image', 'vm_image'), ('flavor', 'instance_type')) resource_dict['properties'] = {} properties = resource_dict['properties'] for (key, vdu_key) in KEY_LIST: properties[key] = vdu_dict[vdu_key] if 'network_interfaces' in vdu_dict: self._process_vdu_network_interfaces(vdu_id, vdu_dict, properties, template_dict, unsupported_res_prop) if ('user_data' in vdu_dict and 'user_data_format' in vdu_dict): properties['user_data_format'] = vdu_dict[ 'user_data_format'] properties['user_data'] = vdu_dict['user_data'] elif ('user_data' in vdu_dict or 'user_data_format' in vdu_dict): raise vnfm.UserDataFormatNotFound() if 'placement_policy' in vdu_dict: if 'availability_zone' in vdu_dict['placement_policy']: properties['availability_zone'] = vdu_dict[ 'placement_policy']['availability_zone'] if 'config' in vdu_dict: properties['config_drive'] = True metadata = properties.setdefault('metadata', {}) metadata.update(vdu_dict['config']) for key, value in metadata.items(): metadata[key] = value[:255] monitoring_policy = vdu_dict.get('monitoring_policy', 'noop') failure_policy = vdu_dict.get('failure_policy', 'noop') # Convert the old monitoring specification to the new # network. This should be removed after Mitaka if (monitoring_policy == 'ping' and failure_policy == 'respawn'): vdu_dict['monitoring_policy'] = { 'ping': {'actions': {'failure': 'respawn'}}} vdu_dict.pop('failure_policy') if monitoring_policy != 'noop': monitoring_dict['vdus'][vdu_id] = \ vdu_dict['monitoring_policy'] # to pass necessary parameters to plugin upwards. for key in ('service_type',): if key in vdu_dict: device.setdefault( 'attributes', {})[vdu_id] = jsonutils.dumps( {key: vdu_dict[key]}) heat_template_yaml = yaml.dump(template_dict) fields['template'] = heat_template_yaml if not device['attributes'].get('heat_template'): device['attributes']['heat_template'] = \ heat_template_yaml if monitoring_dict.keys(): device['attributes']['monitoring_policy'] = \ jsonutils.dumps(monitoring_dict) if 'stack_name' not in fields: name = (__name__ + '_' + self.__class__.__name__ + '-' + device['id']) if device['attributes'].get('failure_count'): name += ('-%s') % str(device['attributes']['failure_count']) fields['stack_name'] = name # service context is ignored LOG.debug(_('service_context: %s'), device.get('service_context', [])) LOG.debug(_('fields: %s'), fields) LOG.debug(_('template: %s'), fields['template']) stack = heatclient_.create(fields) return stack['stack']['id']
def create(self, plugin, context, device): LOG.debug(_('device %s'), device) heatclient_ = HeatClient(context) attributes = device['device_template']['attributes'].copy() vnfd_yaml = attributes.pop('vnfd', None) fields = dict((key, attributes.pop(key)) for key in ('stack_name', 'template_url', 'template') if key in attributes) for key in ('files', 'parameters'): if key in attributes: fields[key] = jsonutils.loads(attributes.pop(key)) # overwrite parameters with given dev_attrs for device creation dev_attrs = device['attributes'].copy() fields.update( dict((key, dev_attrs.pop(key)) for key in ('stack_name', 'template_url', 'template') if key in dev_attrs)) for key in ('files', 'parameters'): if key in dev_attrs: fields.setdefault(key, {}).update( jsonutils.loads(dev_attrs.pop(key))) LOG.debug('vnfd_yaml %s', vnfd_yaml) if vnfd_yaml is not None: assert 'template' not in fields assert 'template_url' not in fields template_dict = yaml.load(HEAT_TEMPLATE_BASE) outputs_dict = {} template_dict['outputs'] = outputs_dict vnfd_dict = yamlparser.simple_ordered_parse(vnfd_yaml) LOG.debug('vnfd_dict %s', vnfd_dict) if 'get_input' in vnfd_yaml: self._process_parameterized_input(dev_attrs, vnfd_dict) KEY_LIST = (('description', 'description'), ) for (key, vnfd_key) in KEY_LIST: if vnfd_key in vnfd_dict: template_dict[key] = vnfd_dict[vnfd_key] monitoring_dict = {'vdus': {}} for vdu_id, vdu_dict in vnfd_dict.get('vdus', {}).items(): template_dict.setdefault('resources', {})[vdu_id] = { "type": "OS::Nova::Server" } resource_dict = template_dict['resources'][vdu_id] KEY_LIST = (('image', 'vm_image'), ('flavor', 'instance_type')) resource_dict['properties'] = {} properties = resource_dict['properties'] for (key, vdu_key) in KEY_LIST: properties[key] = vdu_dict[vdu_key] if 'network_interfaces' in vdu_dict: self._process_vdu_network_interfaces( vdu_id, vdu_dict, properties, template_dict) if 'user_data' in vdu_dict and 'user_data_format' in vdu_dict: properties['user_data_format'] = vdu_dict[ 'user_data_format'] properties['user_data'] = vdu_dict['user_data'] elif 'user_data' in vdu_dict or 'user_data_format' in vdu_dict: raise vnfm.UserDataFormatNotFound() if ('placement_policy' in vdu_dict and 'availability_zone' in vdu_dict['placement_policy']): properties['availability_zone'] = vdu_dict[ 'placement_policy']['availability_zone'] if 'config' in vdu_dict: properties['config_drive'] = True metadata = properties.setdefault('metadata', {}) metadata.update(vdu_dict['config']) for key, value in metadata.items(): metadata[key] = value[:255] monitoring_policy = vdu_dict.get('monitoring_policy', 'noop') failure_policy = vdu_dict.get('failure_policy', 'noop') # Convert the old monitoring specification to the new format # This should be removed after Mitaka if monitoring_policy == 'ping' and failure_policy == 'respawn': vdu_dict['monitoring_policy'] = { 'ping': { 'actions': { 'failure': 'respawn' } } } vdu_dict.pop('failure_policy') if monitoring_policy != 'noop': monitoring_dict['vdus'][vdu_id] = \ vdu_dict['monitoring_policy'] # to pass necessary parameters to plugin upwards. for key in ('service_type', ): if key in vdu_dict: device.setdefault('attributes', {})[vdu_id] = jsonutils.dumps( {key: vdu_dict[key]}) if monitoring_dict.keys(): device['attributes']['monitoring_policy'] = jsonutils.dumps( monitoring_dict) heat_template_yaml = yaml.dump(template_dict) fields['template'] = heat_template_yaml if not device['attributes'].get('heat_template'): device['attributes']['heat_template'] = heat_template_yaml if 'stack_name' not in fields: name = (__name__ + '_' + self.__class__.__name__ + '-' + device['id']) if device['attributes'].get('failure_count'): name += ('-%s') % str(device['attributes']['failure_count']) fields['stack_name'] = name # service context is ignored LOG.debug(_('service_context: %s'), device.get('service_context', [])) LOG.debug(_('fields: %s'), fields) LOG.debug(_('template: %s'), fields['template']) stack = heatclient_.create(fields) return stack['stack']['id']
def create(self, plugin, context, device, auth_attr): LOG.debug(_('device %s'), device) attributes = device['device_template']['attributes'].copy() vnfd_yaml = attributes.pop('vnfd', None) fields = dict((key, attributes.pop(key)) for key in ('stack_name', 'template_url', 'template') if key in attributes) for key in ('files', 'parameters'): if key in attributes: fields[key] = jsonutils.loads(attributes.pop(key)) # overwrite parameters with given dev_attrs for device creation dev_attrs = device['attributes'].copy() fields.update( dict((key, dev_attrs.pop(key)) for key in ('stack_name', 'template_url', 'template') if key in dev_attrs)) for key in ('files', 'parameters'): if key in dev_attrs: fields.setdefault(key, {}).update( jsonutils.loads(dev_attrs.pop(key))) region_name = device.get('placement_attr', {}).get('region_name', None) heatclient_ = HeatClient(auth_attr, region_name) unsupported_res_prop = self.fetch_unsupported_resource_prop( heatclient_) LOG.debug('vnfd_yaml %s', vnfd_yaml) if vnfd_yaml is not None: vnfd_dict = yamlparser.simple_ordered_parse(vnfd_yaml) LOG.debug('vnfd_dict %s', vnfd_dict) monitoring_dict = {'vdus': {}} if 'tosca_definitions_version' in 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) else: assert 'template' not in fields assert 'template_url' not in fields template_dict = yaml.load(HEAT_TEMPLATE_BASE) outputs_dict = {} template_dict['outputs'] = outputs_dict if 'get_input' in vnfd_yaml: self._process_parameterized_input(dev_attrs, vnfd_dict) KEY_LIST = (('description', 'description'), ) for (key, vnfd_key) in KEY_LIST: if vnfd_key in vnfd_dict: template_dict[key] = vnfd_dict[vnfd_key] for vdu_id, vdu_dict in vnfd_dict.get('vdus', {}).items(): template_dict.setdefault('resources', {})[vdu_id] = { "type": "OS::Nova::Server" } resource_dict = template_dict['resources'][vdu_id] KEY_LIST = (('image', 'vm_image'), ('flavor', 'instance_type')) resource_dict['properties'] = {} properties = resource_dict['properties'] for (key, vdu_key) in KEY_LIST: properties[key] = vdu_dict[vdu_key] if 'network_interfaces' in vdu_dict: self._process_vdu_network_interfaces( vdu_id, vdu_dict, properties, template_dict, unsupported_res_prop) if ('user_data' in vdu_dict and 'user_data_format' in vdu_dict): properties['user_data_format'] = vdu_dict[ 'user_data_format'] properties['user_data'] = vdu_dict['user_data'] elif ('user_data' in vdu_dict or 'user_data_format' in vdu_dict): raise vnfm.UserDataFormatNotFound() if 'placement_policy' in vdu_dict: if 'availability_zone' in vdu_dict['placement_policy']: properties['availability_zone'] = vdu_dict[ 'placement_policy']['availability_zone'] if 'config' in vdu_dict: properties['config_drive'] = True metadata = properties.setdefault('metadata', {}) metadata.update(vdu_dict['config']) for key, value in metadata.items(): metadata[key] = value[:255] monitoring_policy = vdu_dict.get('monitoring_policy', 'noop') failure_policy = vdu_dict.get('failure_policy', 'noop') # Convert the old monitoring specification to the new # network. This should be removed after Mitaka if (monitoring_policy == 'ping' and failure_policy == 'respawn'): vdu_dict['monitoring_policy'] = { 'ping': { 'actions': { 'failure': 'respawn' } } } vdu_dict.pop('failure_policy') if monitoring_policy != 'noop': monitoring_dict['vdus'][vdu_id] = \ vdu_dict['monitoring_policy'] # to pass necessary parameters to plugin upwards. for key in ('service_type', ): if key in vdu_dict: device.setdefault('attributes', {})[vdu_id] = jsonutils.dumps( {key: vdu_dict[key]}) heat_template_yaml = yaml.dump(template_dict) fields['template'] = heat_template_yaml if not device['attributes'].get('heat_template'): device['attributes']['heat_template'] = \ heat_template_yaml if monitoring_dict.keys(): device['attributes']['monitoring_policy'] = \ jsonutils.dumps(monitoring_dict) if 'stack_name' not in fields: name = (__name__ + '_' + self.__class__.__name__ + '-' + device['id']) if device['attributes'].get('failure_count'): name += ('-%s') % str(device['attributes']['failure_count']) fields['stack_name'] = name # service context is ignored LOG.debug(_('service_context: %s'), device.get('service_context', [])) LOG.debug(_('fields: %s'), fields) LOG.debug(_('template: %s'), fields['template']) stack = heatclient_.create(fields) return stack['stack']['id']
def create(self, plugin, context, device): LOG.debug(_('device %s'), device) heatclient_ = HeatClient(context) attributes = device['device_template']['attributes'].copy() vnfd_yaml = attributes.pop('vnfd', None) fields = dict((key, attributes.pop(key)) for key in ('stack_name', 'template_url', 'template') if key in attributes) for key in ('files', 'parameters'): if key in attributes: fields[key] = jsonutils.loads(attributes.pop(key)) # overwrite parameters with given dev_attrs for device creation dev_attrs = device['attributes'].copy() fields.update(dict((key, dev_attrs.pop(key)) for key in ('stack_name', 'template_url', 'template') if key in dev_attrs)) for key in ('files', 'parameters'): if key in dev_attrs: fields.setdefault(key, {}).update( jsonutils.loads(dev_attrs.pop(key))) LOG.debug('vnfd_yaml %s', vnfd_yaml) if vnfd_yaml is not None: assert 'template' not in fields assert 'template_url' not in fields template_dict = yaml.load(HEAT_TEMPLATE_BASE) outputs_dict = {} template_dict['outputs'] = outputs_dict vnfd_dict = yamlparser.simple_ordered_parse(vnfd_yaml) LOG.debug('vnfd_dict %s', vnfd_dict) if 'get_input' in vnfd_yaml: self._process_parameterized_input(dev_attrs, vnfd_dict) KEY_LIST = (('description', 'description'), ) for (key, vnfd_key) in KEY_LIST: if vnfd_key in vnfd_dict: template_dict[key] = vnfd_dict[vnfd_key] monitoring_dict = {'vdus': {}} for vdu_id, vdu_dict in vnfd_dict.get('vdus', {}).items(): template_dict.setdefault('resources', {})[vdu_id] = { "type": "OS::Nova::Server" } resource_dict = template_dict['resources'][vdu_id] KEY_LIST = (('image', 'vm_image'), ('flavor', 'instance_type')) resource_dict['properties'] = {} properties = resource_dict['properties'] for (key, vdu_key) in KEY_LIST: properties[key] = vdu_dict[vdu_key] if 'network_interfaces' in vdu_dict: self._process_vdu_network_interfaces(vdu_id, vdu_dict, properties, template_dict) if 'user_data' in vdu_dict and 'user_data_format' in vdu_dict: properties['user_data_format'] = vdu_dict[ 'user_data_format'] properties['user_data'] = vdu_dict['user_data'] elif 'user_data' in vdu_dict or 'user_data_format' in vdu_dict: raise vnfm.UserDataFormatNotFound() if ('placement_policy' in vdu_dict and 'availability_zone' in vdu_dict['placement_policy']): properties['availability_zone'] = vdu_dict[ 'placement_policy']['availability_zone'] if 'config' in vdu_dict: properties['config_drive'] = True metadata = properties.setdefault('metadata', {}) metadata.update(vdu_dict['config']) for key, value in metadata.items(): metadata[key] = value[:255] monitoring_policy = vdu_dict.get('monitoring_policy', 'noop') failure_policy = vdu_dict.get('failure_policy', 'noop') # Convert the old monitoring specification to the new format # This should be removed after Mitaka if monitoring_policy == 'ping' and failure_policy == 'respawn': vdu_dict['monitoring_policy'] = {'ping': { 'actions': { 'failure': 'respawn' }}} vdu_dict.pop('failure_policy') if monitoring_policy != 'noop': monitoring_dict['vdus'][vdu_id] = \ vdu_dict['monitoring_policy'] # to pass necessary parameters to plugin upwards. for key in ('service_type',): if key in vdu_dict: device.setdefault( 'attributes', {})[vdu_id] = jsonutils.dumps( {key: vdu_dict[key]}) if monitoring_dict.keys(): device['attributes']['monitoring_policy'] = jsonutils.dumps( monitoring_dict) heat_template_yaml = yaml.dump(template_dict) fields['template'] = heat_template_yaml if not device['attributes'].get('heat_template'): device['attributes']['heat_template'] = heat_template_yaml if 'stack_name' not in fields: name = (__name__ + '_' + self.__class__.__name__ + '-' + device['id']) if device['attributes'].get('failure_count'): name += ('-%s') % str(device['attributes']['failure_count']) fields['stack_name'] = name # service context is ignored LOG.debug(_('service_context: %s'), device.get('service_context', [])) LOG.debug(_('fields: %s'), fields) LOG.debug(_('template: %s'), fields['template']) stack = heatclient_.create(fields) return stack['stack']['id']
def create(self, plugin, context, device): LOG.debug(_('device %s'), device) heatclient_ = HeatClient(context) attributes = device['device_template']['attributes'].copy() vnfd_yaml = attributes.pop('vnfd', None) fields = dict((key, attributes.pop(key)) for key in ('stack_name', 'template_url', 'template') if key in attributes) for key in ('files', 'parameters'): if key in attributes: fields[key] = jsonutils.loads(attributes.pop(key)) # overwrite parameters with given dev_attrs for device creation dev_attrs = device['attributes'].copy() config_yaml = dev_attrs.pop('config', None) fields.update(dict((key, dev_attrs.pop(key)) for key in ('stack_name', 'template_url', 'template') if key in dev_attrs)) for key in ('files', 'parameters'): if key in dev_attrs: fields.setdefault(key, {}).update( jsonutils.loads(dev_attrs.pop(key))) LOG.debug('vnfd_yaml %s', vnfd_yaml) if vnfd_yaml is not None: assert 'template' not in fields assert 'template_url' not in fields template_dict = yaml.load(HEAT_TEMPLATE_BASE) outputs_dict = {} template_dict['outputs'] = outputs_dict vnfd_dict = yaml.load(vnfd_yaml) LOG.debug('vnfd_dict %s', vnfd_dict) if 'get_input' in vnfd_yaml: param_vattrs_yaml = dev_attrs.pop('param_values', None) if param_vattrs_yaml: try: param_vattrs_dict = yaml.load(param_vattrs_yaml) LOG.debug('param_vattrs_yaml', param_vattrs_dict) except Exception as e: LOG.debug("Not Well Formed: %s", str(e)) raise servicevm.ParamYAMLNotWellFormed( error_msg_details=str(e)) else: self._update_params(vnfd_dict, param_vattrs_dict) else: raise servicevm.ParamYAMLInputMissing() KEY_LIST = (('description', 'description'), ) for (key, vnfd_key) in KEY_LIST: if vnfd_key in vnfd_dict: template_dict[key] = vnfd_dict[vnfd_key] for vdu_id, vdu_dict in vnfd_dict.get('vdus', {}).items(): template_dict.setdefault('resources', {})[vdu_id] = { "type": "OS::Nova::Server" } resource_dict = template_dict['resources'][vdu_id] KEY_LIST = (('image', 'vm_image'), ('flavor', 'instance_type')) resource_dict['properties'] = {} properties = resource_dict['properties'] for (key, vdu_key) in KEY_LIST: properties[key] = vdu_dict[vdu_key] if 'network_interfaces' in vdu_dict: # properties['networks'] = ( # vdu_dict['network_interfaces'].values()) networks_list = [] properties['networks'] = networks_list for network_param in vdu_dict[ 'network_interfaces'].values(): if network_param.pop('management', False): mgmt_port = 'mgmt_port-%s' % vdu_id mgmt_port_dict = { 'type': 'OS::Neutron::Port', 'properties': { 'port_security_enabled': False, } } mgmt_port_dict['properties'].update(network_param) template_dict['resources'][ mgmt_port] = mgmt_port_dict network_param = { 'port': {'get_resource': mgmt_port} } mgmt_ip = 'mgmt_ip-%s' % vdu_id outputs_dict[mgmt_ip] = { 'description': 'management ip address', 'value': { 'get_attr': [mgmt_port, 'fixed_ips', 0, 'ip_address'] } } networks_list.append(network_param) if ('placement_policy' in vdu_dict and 'availability_zone' in vdu_dict['placement_policy']): properties['availability_zone'] = vdu_dict[ 'placement_policy']['availability_zone'] if 'config' in vdu_dict: properties['config_drive'] = True metadata = properties.setdefault('metadata', {}) metadata.update(vdu_dict['config']) for key, value in metadata.items(): metadata[key] = value[:255] # monitoring_policy = vdu_dict.get('monitoring_policy', None) # failure_policy = vdu_dict.get('failure_policy', None) # to pass necessary parameters to plugin upwards. for key in ('monitoring_policy', 'failure_policy', 'service_type'): if key in vdu_dict: device.setdefault( 'attributes', {})[key] = vdu_dict[key] if config_yaml is not None: config_dict = yaml.load(config_yaml) resources = template_dict.setdefault('resources', {}) for vdu_id, vdu_dict in config_dict.get('vdus', {}).items(): if vdu_id not in resources: continue config = vdu_dict.get('config', None) if not config: continue properties = resources[vdu_id].setdefault('properties', {}) properties['config_drive'] = True metadata = properties.setdefault('metadata', {}) metadata.update(config) for key, value in metadata.items(): metadata[key] = value[:255] heat_template_yaml = yaml.dump(template_dict) fields['template'] = heat_template_yaml if not device['attributes'].get('heat_template'): device['attributes']['heat_template'] = heat_template_yaml if 'stack_name' not in fields: name = (__name__ + '_' + self.__class__.__name__ + '-' + device['id']) if device['attributes'].get('failure_count'): name += ('-%s') % str(device['attributes']['failure_count']) fields['stack_name'] = name # service context is ignored LOG.debug(_('service_context: %s'), device.get('service_context', [])) LOG.debug(_('fields: %s'), fields) LOG.debug(_('template: %s'), fields['template']) stack = heatclient_.create(fields) return stack['stack']['id']
def _update_handler(req, res): data = jsonutils.loads(res.body) data['uneditable'] = req.params['uneditable'] res.body = jsonutils.dumps(data) return res
def extend_response_data(req, res): data = jsonutils.loads(res.body) data['FOXNSOX:extended_key'] = req.GET.get('extended_key') res.body = jsonutils.dumps(data) return res
def _make_kwargs_dict(self, kwargs_db): return dict((arg.key, jsonutils.loads(arg.value)) for arg in kwargs_db)
def create(self, plugin, context, device): LOG.debug(_("device %s"), device) heatclient_ = HeatClient(context) attributes = device["device_template"]["attributes"].copy() vnfd_yaml = attributes.pop("vnfd", None) fields = dict( (key, attributes.pop(key)) for key in ("stack_name", "template_url", "template") if key in attributes ) for key in ("files", "parameters"): if key in attributes: fields[key] = jsonutils.loads(attributes.pop(key)) # overwrite parameters with given dev_attrs for device creation dev_attrs = device["attributes"].copy() config_yaml = dev_attrs.pop("config", None) fields.update( dict((key, dev_attrs.pop(key)) for key in ("stack_name", "template_url", "template") if key in dev_attrs) ) for key in ("files", "parameters"): if key in dev_attrs: fields.setdefault(key, {}).update(jsonutils.loads(dev_attrs.pop(key))) LOG.debug("vnfd_yaml %s", vnfd_yaml) if vnfd_yaml is not None: assert "template" not in fields assert "template_url" not in fields template_dict = yaml.load(HEAT_TEMPLATE_BASE) outputs_dict = {} template_dict["outputs"] = outputs_dict vnfd_dict = yaml.load(vnfd_yaml) LOG.debug("vnfd_dict %s", vnfd_dict) KEY_LIST = (("description", "description"),) for (key, vnfd_key) in KEY_LIST: if vnfd_key in vnfd_dict: template_dict[key] = vnfd_dict[vnfd_key] for vdu_id, vdu_dict in vnfd_dict.get("vdus", {}).items(): template_dict.setdefault("resources", {})[vdu_id] = {"type": "OS::Nova::Server"} resource_dict = template_dict["resources"][vdu_id] KEY_LIST = (("image", "vm_image"), ("flavor", "instance_type")) resource_dict["properties"] = {} properties = resource_dict["properties"] for (key, vdu_key) in KEY_LIST: properties[key] = vdu_dict[vdu_key] if "network_interfaces" in vdu_dict: # properties['networks'] = ( # vdu_dict['network_interfaces'].values()) networks_list = [] properties["networks"] = networks_list for network_param in vdu_dict["network_interfaces"].values(): if network_param.pop("management", False): mgmt_port = "mgmt_port-%s" % vdu_id mgmt_port_dict = { "type": "OS::Neutron::Port", "properties": {"port_security_enabled": False}, } mgmt_port_dict["properties"].update(network_param) template_dict["resources"][mgmt_port] = mgmt_port_dict network_param = {"port": {"get_resource": mgmt_port}} mgmt_ip = "mgmt_ip-%s" % vdu_id outputs_dict[mgmt_ip] = { "description": "management ip address", "value": {"get_attr": [mgmt_port, "fixed_ips", 0, "ip_address"]}, } networks_list.append(network_param) if "placement_policy" in vdu_dict and "availability_zone" in vdu_dict["placement_policy"]: properties["availability_zone"] = vdu_dict["placement_policy"]["availability_zone"] if "config" in vdu_dict: properties["config_drive"] = True metadata = properties.setdefault("metadata", {}) metadata.update(vdu_dict["config"]) for key, value in metadata.items(): metadata[key] = value[:255] # monitoring_policy = vdu_dict.get('monitoring_policy', None) # failure_policy = vdu_dict.get('failure_policy', None) # to pass necessary parameters to plugin upwards. for key in ("monitoring_policy", "failure_policy", "service_type"): if key in vdu_dict: device.setdefault("attributes", {})[key] = vdu_dict[key] if config_yaml is not None: config_dict = yaml.load(config_yaml) resources = template_dict.setdefault("resources", {}) for vdu_id, vdu_dict in config_dict.get("vdus", {}).items(): if vdu_id not in resources: continue config = vdu_dict.get("config", None) if not config: continue properties = resources[vdu_id].setdefault("properties", {}) properties["config_drive"] = True metadata = properties.setdefault("metadata", {}) metadata.update(config) for key, value in metadata.items(): metadata[key] = value[:255] heat_template_yaml = yaml.dump(template_dict) fields["template"] = heat_template_yaml if not device["attributes"].get("heat_template"): device["attributes"]["heat_template"] = heat_template_yaml if "stack_name" not in fields: name = __name__ + "_" + self.__class__.__name__ + "-" + device["id"] if device["attributes"].get("failure_count"): name += ("-%s") % str(device["attributes"]["failure_count"]) fields["stack_name"] = name # service context is ignored LOG.debug(_("service_context: %s"), device.get("service_context", [])) LOG.debug(_("fields: %s"), fields) LOG.debug(_("template: %s"), fields["template"]) stack = heatclient_.create(fields) return stack["stack"]["id"]
def _from_json(self, datastring): try: return jsonutils.loads(datastring) except ValueError: msg = _("Cannot understand JSON") raise exception.MalformedRequestBody(reason=msg)
def create(self, plugin, context, device): LOG.debug(_("device %s"), device) heatclient_ = HeatClient(context) attributes = device["device_template"]["attributes"].copy() vnfd_yaml = attributes.pop("vnfd", None) fields = dict( (key, attributes.pop(key)) for key in ("stack_name", "template_url", "template") if key in attributes ) for key in ("files", "parameters"): if key in attributes: fields[key] = jsonutils.loads(attributes.pop(key)) # overwrite parameters with given dev_attrs for device creation dev_attrs = device["attributes"].copy() fields.update( dict((key, dev_attrs.pop(key)) for key in ("stack_name", "template_url", "template") if key in dev_attrs) ) for key in ("files", "parameters"): if key in dev_attrs: fields.setdefault(key, {}).update(jsonutils.loads(dev_attrs.pop(key))) LOG.debug("vnfd_yaml %s", vnfd_yaml) if vnfd_yaml is not None: assert "template" not in fields assert "template_url" not in fields template_dict = yaml.load(HEAT_TEMPLATE_BASE) outputs_dict = {} template_dict["outputs"] = outputs_dict vnfd_dict = yamlparser.simple_ordered_parse(vnfd_yaml) LOG.debug("vnfd_dict %s", vnfd_dict) if "get_input" in vnfd_yaml: self._process_parameterized_input(dev_attrs, vnfd_dict) KEY_LIST = (("description", "description"),) for (key, vnfd_key) in KEY_LIST: if vnfd_key in vnfd_dict: template_dict[key] = vnfd_dict[vnfd_key] monitoring_dict = {"vdus": {}} for vdu_id, vdu_dict in vnfd_dict.get("vdus", {}).items(): template_dict.setdefault("resources", {})[vdu_id] = {"type": "OS::Nova::Server"} resource_dict = template_dict["resources"][vdu_id] KEY_LIST = (("image", "vm_image"), ("flavor", "instance_type")) resource_dict["properties"] = {} properties = resource_dict["properties"] for (key, vdu_key) in KEY_LIST: properties[key] = vdu_dict[vdu_key] if "network_interfaces" in vdu_dict: self._process_vdu_network_interfaces(vdu_id, vdu_dict, properties, template_dict) if "user_data" in vdu_dict and "user_data_format" in vdu_dict: properties["user_data_format"] = vdu_dict["user_data_format"] properties["user_data"] = vdu_dict["user_data"] elif "user_data" in vdu_dict or "user_data_format" in vdu_dict: raise vnfm.UserDataFormatNotFound() if "placement_policy" in vdu_dict and "availability_zone" in vdu_dict["placement_policy"]: properties["availability_zone"] = vdu_dict["placement_policy"]["availability_zone"] if "config" in vdu_dict: properties["config_drive"] = True metadata = properties.setdefault("metadata", {}) metadata.update(vdu_dict["config"]) for key, value in metadata.items(): metadata[key] = value[:255] monitoring_policy = vdu_dict.get("monitoring_policy", "noop") failure_policy = vdu_dict.get("failure_policy", "noop") # Convert the old monitoring specification to the new format # This should be removed after Mitaka if monitoring_policy == "ping" and failure_policy == "respawn": vdu_dict["monitoring_policy"] = {"ping": {"actions": {"failure": "respawn"}}} vdu_dict.pop("failure_policy") if monitoring_policy != "noop": monitoring_dict["vdus"][vdu_id] = vdu_dict["monitoring_policy"] # to pass necessary parameters to plugin upwards. for key in ("service_type",): if key in vdu_dict: device.setdefault("attributes", {})[vdu_id] = jsonutils.dumps({key: vdu_dict[key]}) if monitoring_dict.keys(): device["attributes"]["monitoring_policy"] = jsonutils.dumps(monitoring_dict) heat_template_yaml = yaml.dump(template_dict) fields["template"] = heat_template_yaml if not device["attributes"].get("heat_template"): device["attributes"]["heat_template"] = heat_template_yaml if "stack_name" not in fields: name = __name__ + "_" + self.__class__.__name__ + "-" + device["id"] if device["attributes"].get("failure_count"): name += ("-%s") % str(device["attributes"]["failure_count"]) fields["stack_name"] = name # service context is ignored LOG.debug(_("service_context: %s"), device.get("service_context", [])) LOG.debug(_("fields: %s"), fields) LOG.debug(_("template: %s"), fields["template"]) stack = heatclient_.create(fields) return stack["stack"]["id"]