def test_load_neighbordb(self, _, m_load): contents = ''' variables: foo: bar patterns: - any: any ''' m_load.return_value = yaml.load(contents) result = load_neighbordb(random_string()) self.assertIsNotNone(result)
def test_load_neighbordb(self, _, m_load): contents = ''' variables: foo: bar patterns: - any: any ''' m_load.return_value = yaml.load(contents) result = load_neighbordb(random_string()) self.assertIsNotNone(result)
def test_load_neighbordb_no_variables(self, m_load): # github issue #114 contents = """ patterns: - name: dummy pattern interfaces: - any: any """ m_load.return_value = yaml.load(contents) result = load_neighbordb(random_string()) self.assertIsInstance(result, Neighbordb)
def test_load_neighbordb_no_variables(self, m_load): # github issue #114 contents = """ patterns: - name: dummy pattern interfaces: - any: any """ m_load.return_value = yaml.load(contents) result = load_neighbordb(random_string()) self.assertIsInstance(result, Neighbordb)
def post_node(self, response, *args, **kwargs): """ Checks topology validation matches and writes node specific files This method will attempt to match the current node against the defined topology. If a match is found, then the pattern matched and definition (defined in the pattern) are written to the nodes folder in the repository and the response status is set to HTTP 201 Created. Args: response (dict): the response object being constructed kwargs (dict): arbitrary keyword arguments Returns: a tuple of response object and next state. The next state is 'dump_node' Raises: If a match is not found, then a log message is created and an IndexError is raised. If the node does not already exist in the repository, then a log message is created and a FileObjectNotFound exception is raised """ node = kwargs['node'] node_id = kwargs['node_id'] neighbordb = load_neighbordb(node_id) if not neighbordb: return (self.http_bad_request(), None) # pylint: disable=E1103 matches = neighbordb.match_node(node) if not matches: log.info('%s: node matched no patterns in neighbordb' % node_id) return (self.http_bad_request(), None) log.debug('%s: %d pattern(s) in neihgbordb are a good match' % (node_id, len(matches))) match = matches[0] log.info('%s: node matched \'%s\' pattern in neighbordb' % (node_id, match.name)) # Load definition try: definition_url = self.expand(match.definition, folder='definitions') fobj = self.repository.get_file(definition_url) log.info('%s: node definition copied from: %s' % (node_id, definition_url)) except FileObjectNotFound: log.error('%s: failed to find definition (%s)' % (node_id, definition_url)) raise try: definition = fobj.read(content_type=CONTENT_TYPE_YAML) except FileObjectError: log.error('%s: failed to load definition' % (node_id)) raise definition_fn = self.expand(node_id, DEFINITION_FN) # Load config-handler if match.config_handler: try: config_handler_url = self.expand(match.config_handler, folder='config-handlers') fobj = self.repository.get_file(config_handler_url) log.info('%s: node config-handler copied from: %s' % (node_id, config_handler_url)) except FileObjectNotFound: log.error('%s: failed to find config-handler (%s)' % (node_id, config_handler_url)) raise try: config_handler = fobj.read(content_type=CONTENT_TYPE_OTHER) except FileObjectError: log.error('%s: failed to load config-handler' % (node_id)) raise config_handler_fn = self.expand(node_id, CONFIG_HANDLER_FN) # Create node folder self.repository.add_folder(self.expand(node_id)) log.info('%s: new dynamically-provisioned node created: /nodes/%s' % (node_id, node_id)) # Add definition fobj = self.repository.add_file(definition_fn) fobj.write(definition, CONTENT_TYPE_YAML) # Add pattern pattern_fn = self.expand(node_id, PATTERN_FN) fobj = self.repository.add_file(pattern_fn) pattern = match.serialize() # No need to write the definition name in the pattern file del pattern['definition'] for attr in ['node', 'variables']: if not pattern[attr]: del pattern[attr] # Add default pattern, if 'interfaces' is not specified if 'interfaces' not in pattern or not pattern['interfaces']: pattern['interfaces'] = [{'any': {'any': 'any'}}] fobj.write(pattern, CONTENT_TYPE_YAML) # Add config-handler if match.config_handler: fobj = self.repository.add_file(config_handler_fn) fobj.write(config_handler, CONTENT_TYPE_OTHER) response['status'] = HTTP_STATUS_CREATED return (response, 'dump_node')
def _load_neighbordb(self): return load_neighbordb(ID, contents=self.ndb)
def post_node(self, response, *args, **kwargs): """ Checks topology validation matches and writes node specific files This method will attempt to match the current node against the defined topology. If a match is found, then the pattern matched and definition (defined in the pattern) are written to the nodes folder in the repository and the response status is set to HTTP 201 Created. Args: response (dict): the response object being constructed kwargs (dict): arbitrary keyword arguments Returns: a tuple of response object and next state. The next state is 'dump_node' Raises: If a match is not found, then a log message is created and an IndexError is raised. If the node does not already exist in the repository, then a log message is created and a FileObjectNotFound exception is raised """ node = kwargs['node'] node_id = kwargs['node_id'] neighbordb = load_neighbordb(node_id) # pylint: disable=E1103 matches = neighbordb.match_node(node) if not matches: log.info('%s: node matched no patterns in neighbordb' % node_id) return (self.http_bad_request(), None) log.debug('%s: %d pattern(s) in neihgbordb are a good match' % (node_id, len(matches))) match = matches[0] log.info('%s: node matched \'%s\' pattern in neighbordb' % (node_id, match.name)) try: definition_url = self.expand(match.definition, folder='definitions') fobj = self.repository.get_file(definition_url) except FileObjectNotFound: log.error('%s: failed to find definition (%s)' % (node_id, definition_url)) raise try: definition = fobj.read(content_type=CONTENT_TYPE_YAML) except FileObjectError: log.error('%s: failed to load definition' % (node_id)) raise definition_fn = self.expand(node_id, DEFINITION_FN) self.repository.add_folder(self.expand(node_id)) log.info('%s: new /nodes/%s folder created' % (node_id, node_id)) fobj = self.repository.add_file(definition_fn) fobj.write(definition, CONTENT_TYPE_YAML) pattern_fn = self.expand(node_id, PATTERN_FN) fobj = self.repository.add_file(pattern_fn) fobj.write(match.serialize(), CONTENT_TYPE_YAML) response['status'] = HTTP_STATUS_CREATED return (response, 'dump_node')
def post_node(self, response, *args, **kwargs): """ Checks topology validation matches and writes node specific files This method will attempt to match the current node against the defined topology. If a match is found, then the pattern matched and definition (defined in the pattern) are written to the nodes folder in the repository and the response status is set to HTTP 201 Created. Args: response (dict): the response object being constructed kwargs (dict): arbitrary keyword arguments Returns: a tuple of response object and next state. The next state is 'dump_node' Raises: If a match is not found, then a log message is created and an IndexError is raised. If the node does not already exist in the repository, then a log message is created and a FileObjectNotFound exception is raised """ node = kwargs['node'] node_id = kwargs['node_id'] neighbordb = load_neighbordb(node_id) if not neighbordb: return (self.http_bad_request(), None) # pylint: disable=E1103 matches = neighbordb.match_node(node) if not matches: log.info('%s: node matched no patterns in neighbordb' % node_id) return (self.http_bad_request(), None) log.debug('%s: %d pattern(s) in neihgbordb are a good match' % (node_id, len(matches))) match = matches[0] log.info('%s: node matched \'%s\' pattern in neighbordb' % (node_id, match.name)) # Load definition try: definition_url = self.expand(match.definition, folder='definitions') fobj = self.repository.get_file(definition_url) log.info('%s: node definition copied from: %s' % (node_id, definition_url)) except FileObjectNotFound: log.error('%s: failed to find definition (%s)' % (node_id, definition_url)) raise try: definition = fobj.read(content_type=CONTENT_TYPE_YAML) except FileObjectError: log.error('%s: failed to load definition' % (node_id)) raise definition_fn = self.expand(node_id, DEFINITION_FN) # Load config-handler if match.config_handler: try: config_handler_url = self.expand(match.config_handler, folder='config-handlers') fobj = self.repository.get_file(config_handler_url) log.info('%s: node config-handler copied from: %s' % (node_id, config_handler_url)) except FileObjectNotFound: log.error('%s: failed to find config-handler (%s)' % (node_id, config_handler_url)) raise try: config_handler = fobj.read(content_type=CONTENT_TYPE_OTHER) except FileObjectError: log.error('%s: failed to load config-handler' % (node_id)) raise config_handler_fn = self.expand(node_id, CONFIG_HANDLER_FN) # Create node folder self.repository.add_folder(self.expand(node_id)) log.info('%s: new dynamically-provisioned node created: /nodes/%s' % (node_id, node_id)) # Add definition fobj = self.repository.add_file(definition_fn) fobj.write(definition, CONTENT_TYPE_YAML) # Add pattern pattern_fn = self.expand(node_id, PATTERN_FN) fobj = self.repository.add_file(pattern_fn) pattern = match.serialize() # No need to write the definition name in the pattern file del pattern['definition'] for attr in ['node', 'variables']: if not pattern[attr]: del pattern[attr] # Add default pattern, if 'interfaces' is not specified if 'interfaces' not in pattern or not pattern['interfaces']: pattern['interfaces'] = [{'any': {'any': 'any'}}] fobj.write(pattern, CONTENT_TYPE_YAML) # Add config-handler if match.config_handler: fobj = self.repository.add_file(config_handler_fn) fobj.write(config_handler, CONTENT_TYPE_OTHER) response['status'] = HTTP_STATUS_CREATED return (response, 'dump_node')
def post_node(self, response, *args, **kwargs): """ Checks topology validation matches and writes node specific files This method will attempt to match the current node against the defined topology. If a match is found, then the pattern matched and definition (defined in the pattern) are written to the nodes folder in the repository and the response status is set to HTTP 201 Created. Args: response (dict): the response object being constructed kwargs (dict): arbitrary keyword arguments Returns: a tuple of response object and next state. The next state is 'dump_node' Raises: If a match is not found, then a log message is created and an IndexError is raised. If the node does not already exist in the repository, then a log message is created and a FileObjectNotFound exception is raised """ node = kwargs['node'] node_id = kwargs['node_id'] neighbordb = load_neighbordb(node_id) # pylint: disable=E1103 matches = neighbordb.match_node(node) if not matches: log.info('%s: node matched no patterns in neighbordb' % node_id) return (self.http_bad_request(), None) log.debug('%s: %d pattern(s) in neihgbordb are a good match' % (node_id, len(matches))) match = matches[0] log.info('%s: node matched \'%s\' pattern in neighbordb' % (node_id, match.name)) try: definition_url = self.expand(match.definition, folder='definitions') fobj = self.repository.get_file(definition_url) except FileObjectNotFound: log.error('%s: failed to find definition (%s)' % (node_id, definition_url)) raise try: definition = fobj.read(content_type=CONTENT_TYPE_YAML) except FileObjectError: log.error('%s: failed to load definition' % (node_id)) raise definition_fn = self.expand(node_id, DEFINITION_FN) self.repository.add_folder(self.expand(node_id)) log.info('%s: new /nodes/%s folder created' % (node_id, node_id)) fobj = self.repository.add_file(definition_fn) fobj.write(definition, CONTENT_TYPE_YAML) pattern_fn = self.expand(node_id, PATTERN_FN) fobj = self.repository.add_file(pattern_fn) fobj.write(match.serialize(), CONTENT_TYPE_YAML) response['status'] = HTTP_STATUS_CREATED return (response, 'dump_node')