示例#1
0
    def find_socks(self, path=None, obj=None, p_obj=None):
        retval = []

        if obj != None:
            # find subscribers for given node

            # eg. path=/dest, args=[label:123]

            # obj = obj[obj.keys()[0]]
            uid = obj.get('uuid', None)
            for s in self.sockets:
                sock_path = util.translate_resourcetypes(s.resource_path)
                matched = True
                if uid == s.uuid:
                    matched = True
                elif sock_path:
                    if sock_path in path:
                        matched = self._check_args(s, obj)
                        if p_obj and not matched:
                            matched = self._check_args(s, p_obj)
                    else:
                        matched = False
                elif not sock_path:   # resource path not defined
                    matched = self._check_args(s, obj)
                    if p_obj and not matched:
                        matched = self._check_args(s, p_obj)
                if matched:
                    retval.append(s)
        return retval
示例#2
0
    def parse_services_dict(self, obj, url, args, verbose):
        res_type_pattern = None
        if url is not None and url != '/' and url != '':
            res_type_pattern = util.translate_resourcetypes(url)

        if 'node' in obj:
            unpacked = etcd_unpack(obj)
            nodes = self._match_nodes(unpacked, res_type_pattern, args,
                                      verbose)
            return nodes

        return []
示例#3
0
    def do_sync(self, ws, socket):
        # HTTP GET on etcd registry at top level
        path = util.translate_resourcetypes(socket.resource_path)
        url = 'http://{}:{}/v2/keys/resource/{}?recursive=true'.format(
            reg['host'], reg['port'], path)

        event = GrainEvent()
        event.source_id = self.gen_source_id()
        event.topic = socket.resource_path
        event.flow_id = socket.uuid

        # TODO: could get expensive with lots of flows...
        try:
            r = requests.request('GET', url, proxies={'http': ''})
            if r.status_code not in [200, 404]:
                err = {
                    "type":
                    "error",
                    "data":
                    "{} getting resources of topic {}".format(
                        r.status_code, path)
                }
                ws.send(json.dumps(err))
                return err

            obj = json.loads(r.text)
            unpacked = etcd_unpack(obj)
            nodes = self._match_nodes(unpacked,
                                      path,
                                      socket.params,
                                      verbose=True)

            for node in nodes:
                event.addGrainFromObj(pre_obj=node, post_obj=node)
            ws.send(json.dumps(event.obj()))

        except Exception as err:
            self.logger.writeError('Exception in do_sync: {}'.format(err))
示例#4
0
 def test_translate_resourcetypes_type_uid(self):
     """ it extracts the type and uid, and lowercases the uid """
     self.assertEqual("nodes/007ff4e5-fe72-4c4b-b858-4c5f37dff946",
                      util.translate_resourcetypes("/nodes/007ff4e5-fe72-4c4b-b858-4c5f37dff946/"))
     self.assertEqual("nodes/007ff4e5-fe72-4c4b-b858-4c5f37dff946",
                      util.translate_resourcetypes("/nodes/007FF4E5-FE72-4C4B-B858-4C5F37DFF946"))
示例#5
0
 def test_translate_resourcetypes_type(self):
     """ it extracts the type only if no uid is given """
     self.assertEqual("nodes", util.translate_resourcetypes("/nodes"))
     self.assertEqual("nodes", util.translate_resourcetypes("/nodes/"))
     self.assertEqual("banana", util.translate_resourcetypes("/banana"))