def __call__(self, target, creds): """ Check http: rules by calling to a remote server. This example implementation simply verifies that the response is exactly 'True'. """ url = ('http:' + self.match) % target data = {'target': jsonutils.dumps(target), 'credentials': jsonutils.dumps(creds)} post_data = urllib_parse.urlencode(data) f = urlrequest.urlopen(url, post_data) return f.read() == "True"
def format(self, record): message = {'message': record.getMessage(), 'asctime': self.formatTime(record, self.datefmt), 'name': record.name, 'msg': record.msg, 'args': record.args, 'levelname': record.levelname, 'levelno': record.levelno, 'pathname': record.pathname, 'filename': record.filename, 'module': record.module, 'lineno': record.lineno, 'funcname': record.funcName, 'created': record.created, 'msecs': record.msecs, 'relative_created': record.relativeCreated, 'thread': record.thread, 'thread_name': record.threadName, 'process_name': record.processName, 'process': record.process, 'traceback': None} if hasattr(record, 'extra'): message['extra'] = record.extra if record.exc_info: message['traceback'] = self.formatException(record.exc_info) return jsonutils.dumps(message)
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 format(self, record): message = { 'message': record.getMessage(), 'asctime': self.formatTime(record, self.datefmt), 'name': record.name, 'msg': record.msg, 'args': record.args, 'levelname': record.levelname, 'levelno': record.levelno, 'pathname': record.pathname, 'filename': record.filename, 'module': record.module, 'lineno': record.lineno, 'funcname': record.funcName, 'created': record.created, 'msecs': record.msecs, 'relative_created': record.relativeCreated, 'thread': record.thread, 'thread_name': record.threadName, 'process_name': record.processName, 'process': record.process, 'traceback': None } if hasattr(record, 'extra'): message['extra'] = record.extra if record.exc_info: message['traceback'] = self.formatException(record.exc_info) return jsonutils.dumps(message)
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 __call__(self, target, creds): """ Check http: rules by calling to a remote server. This example implementation simply verifies that the response is exactly 'True'. """ url = ('http:' + self.match) % target data = { 'target': jsonutils.dumps(target), 'credentials': jsonutils.dumps(creds) } post_data = urllib_parse.urlencode(data) f = urlrequest.urlopen(url, post_data) return f.read() == "True"
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_extended_action_for_adding_extra_data(self): action_name = 'FOXNSOX:add_tweedle' action_params = dict(name='Beetle') req_body = jsonutils.dumps({action_name: action_params}) response = self.extension_app.post('/dummy_resources/1/action', req_body, content_type='application/json') self.assertEqual("Tweedle Beetle Added.", response.body)
def test_extended_action_for_deleting_extra_data(self): action_name = 'FOXNSOX:delete_tweedle' action_params = dict(name='Bailey') req_body = jsonutils.dumps({action_name: action_params}) response = self.extension_app.post("/dummy_resources/1/action", req_body, content_type='application/json') self.assertEqual("Tweedle Bailey Deleted.", response.body)
def test_extended_action_for_deleting_extra_data(self): action_name = 'FOXNSOX:delete_tweedle' action_params = dict(name='Bailey') req_body = jsonutils.dumps({action_name: action_params}) response = self.extension_app.post("/dummy_resources/1/action", req_body, content_type='application/json') self.assertEqual("Tweedle Bailey Deleted.", response.body)
def test_extended_action_for_adding_extra_data(self): action_name = 'FOXNSOX:add_tweedle' action_params = dict(name='Beetle') req_body = jsonutils.dumps({action_name: action_params}) response = self.extension_app.post('/dummy_resources/1/action', req_body, content_type='application/json') self.assertEqual("Tweedle Beetle Added.", response.body)
def test_returns_404_for_non_existent_resource(self): action_name = 'add_tweedle' action_params = dict(name='Beetle') req_body = jsonutils.dumps({action_name: action_params}) response = self.extension_app.post("/asdf/1/action", req_body, content_type='application/json', status='*') self.assertEqual(404, response.status_int)
def test_returns_404_for_non_existent_resource(self): action_name = 'add_tweedle' action_params = dict(name='Beetle') req_body = jsonutils.dumps({action_name: action_params}) response = self.extension_app.post("/asdf/1/action", req_body, content_type='application/json', status='*') self.assertEqual(404, response.status_int)
def test_returns_404_for_non_existent_action(self): non_existent_action = 'blah_action' action_params = dict(name="test") req_body = jsonutils.dumps({non_existent_action: action_params}) response = self.extension_app.post("/dummy_resources/1/action", req_body, content_type='application/json', status='*') self.assertEqual(404, response.status_int)
def test_returns_404_for_non_existent_action(self): non_existent_action = 'blah_action' action_params = dict(name="test") req_body = jsonutils.dumps({non_existent_action: action_params}) response = self.extension_app.post("/dummy_resources/1/action", req_body, content_type='application/json', status='*') self.assertEqual(404, response.status_int)
def create_wait(self, plugin, context, device_dict, device_id, auth_attr): region_name = device_dict.get('placement_attr', {}).get('region_name', None) heatclient_ = HeatClient(auth_attr, region_name) stack = heatclient_.get(device_id) status = stack.stack_status stack_retries = STACK_RETRIES error_reason = None while status == 'CREATE_IN_PROGRESS' and stack_retries > 0: time.sleep(STACK_RETRY_WAIT) try: stack = heatclient_.get(device_id) except Exception: LOG.exception( _("Device Instance cleanup may not have " "happened because Heat API request failed " "while waiting for the stack %(stack)s to be " "deleted"), {'stack': device_id}) break status = stack.stack_status LOG.debug(_('status: %s'), status) stack_retries = stack_retries - 1 LOG.debug(_('stack status: %(stack)s %(status)s'), { 'stack': str(stack), 'status': status }) if stack_retries == 0 and status != 'CREATE_COMPLETE': error_reason = _("Resource creation is not completed within" " {wait} seconds as creation of stack {stack}" " is not completed").format( wait=(STACK_RETRIES * STACK_RETRY_WAIT), stack=device_id) LOG.warning(_("VNF Creation failed: %(reason)s"), {'reason': error_reason}) raise vnfm.DeviceCreateWaitFailed(device_id=device_id, reason=error_reason) elif stack_retries != 0 and status != 'CREATE_COMPLETE': error_reason = stack.stack_status_reason raise vnfm.DeviceCreateWaitFailed(device_id=device_id, reason=error_reason) outputs = stack.outputs LOG.debug(_('outputs %s'), outputs) PREFIX = 'mgmt_ip-' mgmt_ips = dict( (output['output_key'][len(PREFIX):], output['output_value']) for output in outputs if output.get('output_key', '').startswith(PREFIX)) if mgmt_ips: device_dict['mgmt_url'] = jsonutils.dumps(mgmt_ips)
def _test_set_rules_with_deprecated_policy(self, input_rules, expected_rules): policy._set_rules(json.dumps(input_rules)) # verify deprecated policy has been removed for pol in input_rules.keys(): self.assertNotIn(pol, common_policy._rules) # verify deprecated policy was correctly translated. Iterate # over items for compatibility with unittest2 in python 2.6 for rule in expected_rules: self.assertIn(rule, common_policy._rules) self.assertEqual(str(common_policy._rules[rule]), expected_rules[rule])
def _test_set_rules_with_deprecated_policy(self, input_rules, expected_rules): policy._set_rules(json.dumps(input_rules)) # verify deprecated policy has been removed for pol in input_rules.keys(): self.assertNotIn(pol, common_policy._rules) # verify deprecated policy was correctly translated. Iterate # over items for compatibility with unittest2 in python 2.6 for rule in expected_rules: self.assertIn(rule, common_policy._rules) self.assertEqual(str(common_policy._rules[rule]), expected_rules[rule])
def __str__(self): """Dumps a string representation of the rules.""" # Start by building the canonical strings for the rules out_rules = {} for key, value in self.items(): # Use empty string for singleton TrueCheck instances if isinstance(value, TrueCheck): out_rules[key] = '' else: out_rules[key] = str(value) # Dump a pretty-printed JSON representation return jsonutils.dumps(out_rules, indent=4)
def create_wait(self, plugin, context, device_dict, device_id, auth_attr): region_name = device_dict.get('placement_attr', {}).get( 'region_name', None) heatclient_ = HeatClient(auth_attr, region_name) stack = heatclient_.get(device_id) status = stack.stack_status stack_retries = STACK_RETRIES error_reason = None while status == 'CREATE_IN_PROGRESS' and stack_retries > 0: time.sleep(STACK_RETRY_WAIT) try: stack = heatclient_.get(device_id) except Exception: LOG.exception(_("Device Instance cleanup may not have " "happened because Heat API request failed " "while waiting for the stack %(stack)s to be " "deleted"), {'stack': device_id}) break status = stack.stack_status LOG.debug(_('status: %s'), status) stack_retries = stack_retries - 1 LOG.debug(_('stack status: %(stack)s %(status)s'), {'stack': str(stack), 'status': status}) if stack_retries == 0 and status != 'CREATE_COMPLETE': error_reason = _("Resource creation is not completed within" " {wait} seconds as creation of stack {stack}" " is not completed").format( wait=(STACK_RETRIES * STACK_RETRY_WAIT), stack=device_id) LOG.warning(_("VNF Creation failed: %(reason)s"), {'reason': error_reason}) raise vnfm.DeviceCreateWaitFailed(device_id=device_id, reason=error_reason) elif stack_retries != 0 and status != 'CREATE_COMPLETE': error_reason = stack.stack_status_reason raise vnfm.DeviceCreateWaitFailed(device_id=device_id, reason=error_reason) outputs = stack.outputs LOG.debug(_('outputs %s'), outputs) PREFIX = 'mgmt_ip-' mgmt_ips = dict((output['output_key'][len(PREFIX):], output['output_value']) for output in outputs if output.get('output_key', '').startswith(PREFIX)) if mgmt_ips: device_dict['mgmt_url'] = jsonutils.dumps(mgmt_ips)
def __str__(self): """Dumps a string representation of the rules.""" # Start by building the canonical strings for the rules out_rules = {} for key, value in self.items(): # Use empty string for singleton TrueCheck instances if isinstance(value, TrueCheck): out_rules[key] = '' else: out_rules[key] = str(value) # Dump a pretty-printed JSON representation return jsonutils.dumps(out_rules, indent=4)
def mgmt_url(self, plugin, context, device): mgmt_entries = [sc_entry for sc_entry in device.service_context if (sc_entry.role == constants.ROLE_MGMT and sc_entry.port_id)] if not mgmt_entries: return port = plugin._core_plugin.get_port(context, mgmt_entries[0].port_id) if not port: return mgmt_url = port['fixed_ips'][0] # subnet_id and ip_address mgmt_url['network_id'] = port['network_id'] mgmt_url['port_id'] = port['id'] mgmt_url['mac_address'] = port['mac_address'] return jsonutils.dumps(mgmt_url)
def create_wait(self, plugin, context, device_dict, device_id): heatclient_ = HeatClient(context) stack = heatclient_.get(device_id) status = stack.stack_status stack_retries = STACK_RETRIES while status == "CREATE_IN_PROGRESS" and stack_retries > 0: time.sleep(STACK_RETRY_WAIT) try: stack = heatclient_.get(device_id) except Exception: LOG.exception( _( "Device Instance cleanup may not have " "happened because Heat API request failed " "while waiting for the stack %(stack)s to be " "deleted" ), {"stack": device_id}, ) break status = stack.stack_status LOG.debug(_("status: %s"), status) stack_retries = stack_retries - 1 LOG.debug(_("stack status: %(stack)s %(status)s"), {"stack": stack, "status": status}) if stack_retries == 0: LOG.warn( _( "Resource creation is" " not completed within %(wait)s seconds as " "creation of Stack %(stack)s is not completed" ), {"wait": (STACK_RETRIES * STACK_RETRY_WAIT), "stack": device_id}, ) if status != "CREATE_COMPLETE": raise RuntimeError(_("creation of server %s faild") % device_id) outputs = stack.outputs LOG.debug(_("outputs %s"), outputs) PREFIX = "mgmt_ip-" mgmt_ips = dict( (output["output_key"][len(PREFIX) :], output["output_value"]) for output in outputs if output.get("output_key", "").startswith(PREFIX) ) if mgmt_ips: device_dict["mgmt_url"] = jsonutils.dumps(mgmt_ips)
def create_wait(self, plugin, context, device_dict, device_id): heatclient_ = HeatClient(context) stack = heatclient_.get(device_id) status = stack.stack_status stack_retries = STACK_RETRIES while status == 'CREATE_IN_PROGRESS' and stack_retries > 0: time.sleep(STACK_RETRY_WAIT) try: stack = heatclient_.get(device_id) except Exception: LOG.exception( _("Device Instance cleanup may not have " "happened because Heat API request failed " "while waiting for the stack %(stack)s to be " "deleted"), {'stack': device_id}) break status = stack.stack_status LOG.debug(_('status: %s'), status) stack_retries = stack_retries - 1 LOG.debug(_('stack status: %(stack)s %(status)s'), { 'stack': str(stack), 'status': status }) if stack_retries == 0: LOG.warning( _("Resource creation is" " not completed within %(wait)s seconds as " "creation of Stack %(stack)s is not completed"), { 'wait': (STACK_RETRIES * STACK_RETRY_WAIT), 'stack': device_id }) if status != 'CREATE_COMPLETE': raise vnfm.DeviceCreateWaitFailed(device_id=device_id) outputs = stack.outputs LOG.debug(_('outputs %s'), outputs) PREFIX = 'mgmt_ip-' mgmt_ips = dict( (output['output_key'][len(PREFIX):], output['output_value']) for output in outputs if output.get('output_key', '').startswith(PREFIX)) if mgmt_ips: device_dict['mgmt_url'] = jsonutils.dumps(mgmt_ips)
def create_wait(self, plugin, context, device_dict, device_id): heatclient_ = HeatClient(context) stack = heatclient_.get(device_id) status = stack.stack_status stack_retries = STACK_RETRIES while status == 'CREATE_IN_PROGRESS' and stack_retries > 0: time.sleep(STACK_RETRY_WAIT) try: stack = heatclient_.get(device_id) except Exception: LOG.exception(_("Device Instance cleanup may not have " "happened because Heat API request failed " "while waiting for the stack %(stack)s to be " "deleted"), {'stack': device_id}) break status = stack.stack_status LOG.debug(_('status: %s'), status) stack_retries = stack_retries - 1 LOG.debug(_('stack status: %(stack)s %(status)s'), {'stack': str(stack), 'status': status}) if stack_retries == 0: LOG.warning(_("Resource creation is" " not completed within %(wait)s seconds as " "creation of Stack %(stack)s is not completed"), {'wait': (STACK_RETRIES * STACK_RETRY_WAIT), 'stack': device_id}) if status != 'CREATE_COMPLETE': raise vnfm.DeviceCreateWaitFailed(device_id=device_id) outputs = stack.outputs LOG.debug(_('outputs %s'), outputs) PREFIX = 'mgmt_ip-' mgmt_ips = dict((output['output_key'][len(PREFIX):], output['output_value']) for output in outputs if output.get('output_key', '').startswith(PREFIX)) if mgmt_ips: device_dict['mgmt_url'] = jsonutils.dumps(mgmt_ips)
def _create_device_pre(self, context, device): device = device['device'] LOG.debug(_('device %s'), device) tenant_id = self._get_tenant_id_for_create(context, device) template_id = device['template_id'] device_id = str(uuid.uuid4()) kwargs = device.get('kwargs', {}) service_context = device.get('service_context', []) with context.session.begin(subtransactions=True): device_db = Device(id=device_id, tenant_id=tenant_id, instance_id=None, template_id=template_id, status=constants.PENDING_CREATE) context.session.add(device_db) for key, value in kwargs.items(): arg = DeviceArg(id=str(uuid.uuid4()), device_id=device_id, key=key, value=jsonutils.dumps(value)) context.session.add(arg) LOG.debug(_('service_context %s'), service_context) for sc_entry in service_context: LOG.debug(_('sc_entry %s'), sc_entry) network_id = sc_entry.get('network_id') subnet_id = sc_entry.get('subnet_id') port_id = sc_entry.get('port_id') router_id = sc_entry.get('router_id') role = sc_entry.get('role') index = sc_entry.get('index') network_binding = DeviceServiceContext( id=str(uuid.uuid4()), device_id=device_id, network_id=network_id, subnet_id=subnet_id, port_id=port_id, router_id=router_id, role=role, index=index) context.session.add(network_binding) return self._make_device_dict(device_db)
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 default(self, data): def sanitizer(obj): return six.text_type(obj) return jsonutils.dumps(data, default=sanitizer)
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 default(self, data): def sanitizer(obj): return unicode(obj) return jsonutils.dumps(data, default=sanitizer)
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 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 _update_handler(req, res): data = jsonutils.loads(res.body) data['uneditable'] = req.params['uneditable'] res.body = jsonutils.dumps(data) return res
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 _update_handler(req, res): data = jsonutils.loads(res.body) data['uneditable'] = req.params['uneditable'] res.body = jsonutils.dumps(data) return res
def default(self, data): def sanitizer(obj): return six.text_type(obj) return jsonutils.dumps(data, default=sanitizer)
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 default(self, data): def sanitizer(obj): return unicode(obj) return jsonutils.dumps(data, default=sanitizer)
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"]