Exemplo n.º 1
0
    def get_item(self, key):
        interface = self.e().get(key)
        item = object_to_dict(interface)

        switch = self.e(self.switch_table).get(interface.switch)
        item['switch'] = object_to_dict(switch)

        if interface.port:
            try:
                port = self.e(self.port_table).filter_by(connect_to=interface.port).one()
                item['port'] = object_to_dict(port)
            except:
                item['port'] = None

        ptrn_query = "SELECT {2}.name, {2}.style, {2}.uuid FROM {0}.{1}, {0}.{2} \
            WHERE {1}.if_config_pattern = {2}.uuid and {1}.interface = '{3}'".format(
            self.schema, self.if_to_pat_table, self.pattern_table, interface.uuid
        )
        item['patterns'] = []
        for pattern in self.db.execute(ptrn_query).fetchall():
            row = dict(zip(pattern.keys(), pattern.values()))
            item['patterns'].append(row)
            # item['patterns'].append([row['name'], row['style']])

        if len(item['patterns']) == 0:
            # item['patterns'].append(['Exotic','bad'])
            item['patterns'].append({'name': 'Exotic', 'style': 'bad', 'uuid': None})

        mac1_query = "SELECT * FROM {0}.{1} where sw_if_uuid = '{2}'".format(
            self.schema, self.last_interface_for_mac_advance, interface.uuid)
        item['last_interface_for_mac'] = []
        for mac in self.db.execute(mac1_query).fetchall():
            row = dict(zip(mac.keys(), mac.values()))
            for col in row:
                row[col] = process_value(row[col])
            item['last_interface_for_mac'].append(row)

        mac2_query = "SELECT * FROM {0}.{1} where sw_if_uuid = '{2}'".format(
            self.schema, self.last_macs_on_interface_advance, interface.uuid)
        item['last_macs_on_interface'] = []
        for mac in self.db.execute(mac2_query).fetchall():
            row = dict(zip(mac.keys(), mac.values()))
            for col in row:
                row[col] = process_value(row[col])
            item['last_macs_on_interface'].append(row)

        self.fill_link_status(item)
        item['speed_label'] = self.link_speed_humanize(item['speed'])

        return item
Exemplo n.º 2
0
 def list(self):
     items = []
     for item in self.e().order_by(self.pkey).all():
         item = object_to_dict(item,
                               include=self.include_relations.get('list'))
         items.append(item)
     return items
Exemplo n.º 3
0
    def list_global(self):
        items = []
        query = self.e().filter_by(**{"network": None, "device": None}).all()

        for item in query:
            item = object_to_dict(item, include=self.include_relations.get("list"))
            items.append(item)
        return items
Exemplo n.º 4
0
    def list_global(self):
        items = []
        query = self.e().filter_by(**{'network': None, 'device': None}).all()

        for item in query:
            item = object_to_dict(item, include=self.include_relations.get('list'))
            items.append(item)
        return items
Exemplo n.º 5
0
 def insert(self, item):
     newVal = {}
     for k, v in item.items():
         if k not in self.get_relationships() and v is not None:
             newVal[k] = v
     e = self.e().insert(**newVal)
     self.db.commit()
     return object_to_dict(e)
Exemplo n.º 6
0
 def insert(self, item):
     newVal = {}
     for k,v in item.items():
         if k not in self.get_relationships() and v is not None:
             newVal[k] = v
     e = self.e().insert(**newVal)
     self.db.commit()
     return object_to_dict(e)
Exemplo n.º 7
0
 def update(self, item):
     assert item.get(self.pkey) is not None, 'Primary key is not set'
     entity = self.e().filter_by(**{self.pkey: item.get(self.pkey)}).one()
     for k,v in item.items():
         if k in self.get_relationships() or k == self.pkey:
             continue
         setattr(entity, k, v)
     self.db.commit()
     return object_to_dict(entity)
Exemplo n.º 8
0
 def update(self, item):
     assert item.get(self.pkey) is not None, 'Primary key is not set'
     entity = self.e().filter_by(**{self.pkey: item.get(self.pkey)}).one()
     for k, v in item.items():
         if k in self.get_relationships() or k == self.pkey:
             continue
         setattr(entity, k, v)
     self.db.commit()
     return object_to_dict(entity)
Exemplo n.º 9
0
    def list(self, privileges):
        # Check ACLs
        acl = self.network_acls(privileges)

        items = []
        for item in self.e().order_by(self.pkey).all():
            # Filter out networks
            if "admin" in privileges or str(item.uuid) in acl.keys():
                item = object_to_dict(item, include=self.include_relations.get("list"))
                items.append(item)
        return items
Exemplo n.º 10
0
    def get_item(self, key, privileges):
        item = self.e().filter_by(**{self.pkey: key}).one()
        item = object_to_dict(item, include=self.include_relations.get('item'))

        # Check ACLs
        acl = self.network_acls(privileges)
        if 'admin' not in privileges:
            for interface in item['interfaces']:
                if str(interface['network']) not in acl.keys() or item['type'] not in acl[str(interface['network'])]:
                    raise Exception('RBAC Forbidden')

        return item
Exemplo n.º 11
0
    def list(self, privileges):
        # Check ACLs
        acl = self.network_acls(privileges)

        items = []
        for item in self.e().order_by(self.pkey).all():
            # Filter out networks
            if 'admin' in privileges or str(item.uuid) in acl.keys():
                item = object_to_dict(
                    item, include=self.include_relations.get('list'))
                items.append(item)
        return items
Exemplo n.º 12
0
    def insert(self, data):
        newVal = {}
        for k, v in data.items():
            if k not in self.get_relationships():
                if v == '': v = None
                newVal[k] = v

        e = self.e().insert(**newVal)
        self.db.session.flush()

        self.process_relations(e, e.uuid, data)
        self.db.commit()

        return object_to_dict(e)
Exemplo n.º 13
0
    def insert(self, data):
        newVal = {}
        for k, v in data.items():
            if k not in self.get_relationships():
                if v == "":
                    v = None
                newVal[k] = v

        e = self.e().insert(**newVal)
        self.db.session.flush()

        self.process_relations(e, e.uuid, data)
        self.db.commit()

        return object_to_dict(e)
Exemplo n.º 14
0
    def patch(self, data):
        assert data.get(self.pkey) is not None, 'Primary key is not set'

        id = data[self.pkey]
        item = self.e().filter_by(**{self.pkey: id}).one()

        for k,v in data.items():
            if k in self.get_relationships() or k == self.pkey:
                continue
            if v == '': v = None
            setattr(item, k, v)

        self.db.commit()

        return object_to_dict(item)
Exemplo n.º 15
0
    def patch(self, data):
        assert data.get(self.pkey) is not None, "Primary key is not set"

        uuid = data["uuid"]
        item = self.e().filter_by(**{self.pkey: uuid}).one()

        for k, v in data.items():
            if k in self.get_relationships() or k == self.pkey:
                continue
            if v == "":
                v = None
            setattr(item, k, v)

        self.process_relations(item, uuid, data)
        self.db.commit()

        return object_to_dict(item)
Exemplo n.º 16
0
    def insert(self, data, privileges):
        # Check ACLs
        acl = self.network_acls(privileges)
        if 'admin' not in privileges:
            for interface in data['interfaces']:
                if str(interface['network']) not in acl.keys() or data['type'] not in acl[str(interface['network'])]:
                    raise Exception('RBAC Forbidden')

        newVal = {}
        for k,v in data.items():
            if k not in self.get_relationships() and v is not None:
                newVal[k] = v

        e = self.e().insert(**newVal)
        self.db.session.flush()

        self.process_relations(e, e.uuid, data)
        self.db.commit()

        return object_to_dict(e)
Exemplo n.º 17
0
    def insert(self, data, privileges, uid):
        # Check ACLs
        acl = self.network_acls(privileges)
        if 'admin' not in privileges:
            for interface in data['interfaces']:
                if str(interface['network']) not in acl.keys(
                ) or data['type'] not in acl[str(interface['network'])]:
                    raise Exception('RBAC Forbidden')

        newVal = {}
        for k, v in data.items():
            if k not in self.get_relationships() and v is not None:
                newVal[k] = v

        e = self.e().insert(**newVal)
        self.db.session.flush()

        self.process_relations(e, e.uuid, data)
        self.db.commit()

        return object_to_dict(e)
Exemplo n.º 18
0
    def get_item(self, key, privileges):
        item = self.e().filter_by(**{self.pkey: key}).one()
        item = object_to_dict(item, include=self.include_relations.get('item'))
        for interface in item['interfaces']:
            lease4 = self.db.execute(
                "SELECT (SELECT '0.0.0.0'::inet + address) as address \
                                      FROM lease4 \
                                      WHERE COALESCE(NULLIF(ENCODE(hwaddr, 'hex'), ''), '000000000000')::macaddr = '%s'"
                % interface['macaddr']).fetchone()
            if lease4:
                interface['lease4'] = lease4.address

        # Check ACLs
        acl = self.network_acls(privileges)
        if 'admin' not in privileges:
            for interface in item['interfaces']:
                if str(interface['network']) not in acl.keys(
                ) or item['type'] not in acl[str(interface['network'])]:
                    raise Exception('RBAC Forbidden')

        return item
Exemplo n.º 19
0
    def patch(self, data, privileges):
        assert data.get(self.pkey) is not None, 'Primary key is not set'

        # Check ACLs
        acl = self.network_acls(privileges)
        if 'admin' not in privileges:
            for interface in data['interfaces']:
                if str(interface['network']) not in acl.keys() or data['type'] not in acl[str(interface['network'])]:
                    raise Exception('RBAC Forbidden')

        uuid = data['uuid']
        item = self.e().filter_by(**{self.pkey: uuid}).one()

        for k,v in data.items():
            if k in self.get_relationships() or k == self.pkey:
                continue
            setattr(item, k, v)

        self.process_relations(item, uuid, data)
        self.db.commit()

        return object_to_dict(item)
Exemplo n.º 20
0
    def patch(self, data, privileges, uid):
        assert data.get(self.pkey) is not None, 'Primary key is not set'

        # Check ACLs
        acl = self.network_acls(privileges)
        if 'admin' not in privileges:
            for interface in data['interfaces']:
                if str(interface['network']) not in acl.keys(
                ) or data['type'] not in acl[str(interface['network'])]:
                    raise Exception('RBAC Forbidden')

        uuid = data['uuid']
        item = self.e().filter_by(**{self.pkey: uuid}).one()

        for k, v in data.items():
            if k in self.get_relationships() or k == self.pkey:
                continue
            setattr(item, k, v)

        self.process_relations(item, uuid, data)
        self.db.commit()

        return object_to_dict(item)
Exemplo n.º 21
0
 def list(self):
     items = []
     for item in self.e().order_by(self.pkey).all():
         item = object_to_dict(item, include=self.include_relations.get('list'))
         items.append(item)
     return items
Exemplo n.º 22
0
 def get_item(self, key):
     item = self.e().filter_by(**{self.pkey: key}).one()
     item = object_to_dict(item, include=self.include_relations.get('item'))
     return item
Exemplo n.º 23
0
 def replace(self, item):
     if item.get(self.pkey) is not None:
         self.e().filter_by(**{self.pkey: item[self.pkey]}).delete()
     return object_to_dict(self.insert(item))
Exemplo n.º 24
0
 def get_item(self, key):
     item = self.e().filter_by(**{self.pkey: key}).one()
     item = object_to_dict(item, include=self.include_relations.get('item'))
     return item
Exemplo n.º 25
0
 def replace(self, item):
     if item.get(self.pkey) is not None:
         self.e().filter_by(**{self.pkey: item[self.pkey]}).delete()
     return object_to_dict(self.insert(item))