接上篇博文html
嘗試添加一張主機表,用於管理主機信息java
無非表的內容是host,那麼確定是在schema中創建,直接寫描述便可python
schema 填入id,id默認是1,name對應的是hostweb
這裏虛擬表所對應的東西是1,描述的字段還暫時沒有正則表達式
以下所示:數據庫
MariaDB [cmdb]> insert into `schema`(name) values('hosts'); MariaDB [cmdb]> select * from `schema`; +----+-------+------+ | id | name | desc | +----+-------+------+ | 1 | hosts | NULL | +----+-------+------+ 1 row in set (0.00 sec)
描述字段json
經過filed表中添加兩個字段 hostname,schema_id 這倆字段與host與關,其實就是與id與關聯ide
這樣自增的話,會產生第一個索引,那若是再進行添加ip 的話,則id是2函數
MariaDB [cmdb]> insert into `field` (name,schema_id) value ('hostname',1); MariaDB [cmdb]> insert into `field` (name,schema_id) value ('ip',1); MariaDB [cmdb]> select * from field; +----+----------+------+-----------+ | id | name | meta | schema_id | +----+----------+------+-----------+ | 1 | hostname | NULL | 1 | | 5 | ip | NULL | 1 | +----+----------+------+-----------+ 2 rows in set (0.00 sec)
如上,這裏value對應的schema_id 都是1,也就是找到schema表中與id=1對應相關的表,也就是屬hosts所管轄測試
MariaDB [cmdb]> select * from `schema`; +----+-------+------+ | id | name | desc | +----+-------+------+ | 1 | hosts | NULL | +----+-------+------+
這樣建立假設自增的話,產生第一個主鍵
查詢
查出對應的虛擬表全部的字段,只要指定schemaid就能夠查出當前信息
MariaDB [cmdb]> select * from `schema`,`field` where 1 = 1 and `field`.schema_id = `schema`.id and `schema`.id = 1; +----+-------+------+----+----------+------+-----------+ | id | name | desc | id | name | meta | schema_id | +----+-------+------+----+----------+------+-----------+ | 1 | hosts | NULL | 1 | hostname | NULL | 1 | | 1 | hosts | NULL | 5 | ip | NULL | 1 | +----+-------+------+----+----------+------+-----------+ 2 rows in set (0.00 sec)
查出它全部字段,只要能知名schema id 至關於虛擬表已經被查出
記錄一個主機信息,entity
MariaDB [cmdb]> insert into entity (id,`key`,schema_id) values(1,'0123123',1);
對value表填入數據
當前entity 的值是1,那麼對應entity_id = 1就能夠了,字段id是
MariaDB [cmdb]> insert into `value`(entity_id,filed_id,`value`) values(1,1,'webserver'); MariaDB [cmdb]> insert into `value`(entity_id,filed_id,`value`) values(1,2,'182.168.1.1'); MariaDB [cmdb]> select * from value; +----+-------------+----------+-----------+ | id | value | field_id | entity_id | +----+-------------+----------+-----------+ | 1 | webser | 1 | 1 | | 2 | 182.168.1.1 | 2 | 1 | +----+-------------+----------+-----------+ 2 rows in set (0.01 sec)
能夠看到其field對應的表字段
MariaDB [cmdb]> select * from field; +----+----------+------+-----------+ | id | name | meta | schema_id | +----+----------+------+-----------+ | 1 | hostname | NULL | 1 | | 2 | ip | NULL | 1 | +----+----------+------+-----------+ 2 rows in set (0.00 sec)
插入第二個主機信息:
經過惟一key來區別主機,以及還須要對entity 進行添加行進行id對應
MariaDB [cmdb]> insert into entity(`key`,schema_id) values ('0456456',1); Query OK, 1 row affected, 1 warning (0.00 sec) MariaDB [cmdb]> select * from entity; +----+---------+-----------+ | id | key | schema_id | +----+---------+-----------+ | 0 | 0456456 | 1 | | 1 | 0123123 | 1 | +----+---------+-----------+ 2 rows in set (0.00 sec)
插入數據 ,對應entity 第二個id
insert into `value`(entity_id,filed_id,`value`) VALUES (1,1,'DBser'); insert into `value`(entity_id,filed_id,`value`) VALUES (1,2,'127.0.0.1');
以上存入了兩個主機信息
查看
select entity.id as entity_id,entity.`key`, entity.schema_id, `schema`.`name`, field.id,field.`name` as fname, `value`.`value` FROM entity INNER JOIN `value` on `value`.entity_id = entity_id INNER JOIN `schema` on `value`.schema_id = `schema`.id INNER JOIN field on `value`.field_id = field_id
利與弊
好處:以前每一個類都會生成一個表,如今無非是在scaehma添加一行記錄而已
壞處:關係複雜、表結構複雜,多長關係表組成的關係鏈,複雜的同時帶來了靈活性,ORM不識別這樣的表,只能本身封裝進行實現
能否在value 的value段用約束進行?
好比記錄ip,那麼ip不容許重複如何去寫?
這樣至關於全部表都互相干擾了,因此不容許加惟一鍵約束
能否創建一個ip 池的表,若是存放的主機信息的,對於資產管理,將全部的服務配置,那麼就確定涉及到ip
若是互不干擾的話,那麼能否經過sechma id進行判斷,須要考慮重複的時候判斷問題,那麼無非是插入的時候判斷
單獨寫類型很是有限,既然類型不合適,那麼ip地址確定不合適,總須要方法來解決這些
那麼能否經過正則表達式,可是比較難掌握
經過meta進行限制,meta是text類型,是否可使用json?
將json中的字符串轉爲python代碼,經過反射動態加載運行,這樣實現的話須要約定好調用的接口
考慮的問題點:
1. 如何存放?如何描述?描述什麼?
2. 若是用到反射的話,那能否存放一個類名,一個模塊名 直接加載它 直接調用這個方法
開發
設計一個類 ,經過反射來判斷這個值,進行字段類型來驗證
存儲以前經過類轉爲特殊類型轉爲字符串,來進行校驗,若是校驗成功則存入數據庫
創建約束類型,目錄結構以下:
創建基類,用於校驗,功能方法冗餘性所有在基類中實現,子類用於加強
校驗字符合法性
class BaseType: def stringify(self,value): raise NotImplementedError() def destringify(self,value): raise NotImplementedError() class Int(BaseType): def stringify(self,value): str(int(value)) def destringify(self,value): pass class Int(BaseType): def stringify(self,value): return str(int(value)) def destringify(self,value): pass
ip地址校驗
經過ipaddress 模塊進行校驗ip地址合法性 import ipaddress class BaseType: def stringify(self,value): raise NotImplementedError() def destringify(self,value): raise NotImplementedError() class Int(BaseType): def stringify(self,value): return int(str(value)) def destringify(self,value): return value class IP(BaseType): def stringify(self,value): return str(ipaddress.ip_address(value)) def destringify(self,value): return value
反射
既然拿到了類型和值了,接下來如何操做?
經過getattr反射進行找到對應的方法
經過name返回object屬性值,當屬性不存在,將使用default返回,若是沒有默認,則拋出AttributeError
經過getattr進行反射
當前模塊還未導入,並且字符串還沒被分段
import json jsonstr = """ { "type":"cmdb.types.Int", "value":300 } """ obj = json.loads(jsonstr) print(obj) m,c = obj['type'].rsplit('.',maxsplit=1) print(m,c) # 返回以下 {'type': 'cmdb.types.Int', 'value': 300} types.Int cmdb
導入模塊importlib
經過獲取的值進行反射,這裏m對應的是cmdb.types, 正是init.py,將其導入
咱們看到看init是類方法,那麼須要扔一個值進去
進行實例化,傳入一個參數,那麼這個值是value
# 導入cmdb.types mod = importlib.import_module(m) cls = getattr(mod,c) cls().stringify(obj['value']) # 返回以下 {'value': 300, 'type': 'cmdb.types.Int'} cmdb.types Int
校驗IP
通常要求分4段式
這裏用於測試 value 不該該寫在這裏,切記
抽象函數
通常抽象的時候都將其返回一個對象爲止,因此須要改進以下:
import json import importlib jsonstr = """ { "type":"cmdb.types.IP", "value":"10.10.10.1" } """ obj = json.loads(jsonstr) def get_instance(type:str): # 這裏要約定好,否則沒法判斷 m,c = type.rsplit('.',maxsplit=1) mod = importlib.import_module(m) cls = getattr(mod,c) return cls() print(get_instance(obj['type']).stringify(obj['value']))
給予一個類模塊取出,將類加載起來以後返回類的實例
調用它的方法,將函數get_instance導入到init中 import ipaddress import importlib def get_instance(type:str): m,c = type.rsplit('.',maxsplit=1) mod = importlib.import_module(m) cls = getattr(mod,c) return cls() class BaseType: def stringify(self,value): raise NotImplementedError() def destringify(self,value): raise NotImplementedError() class Int(BaseType): def stringify(self,value): return int(str(value)) def destringify(self,value): return value class IP(BaseType): def stringify(self,value): return str(ipaddress.ip_address(value)) def destringify(self,value): return value
這樣就能夠完美解決數據類型的問題,經過傳遞進來的json串來找到對應方法並解析,從而進行判斷類型並return