2121class CVMApi ():
2222 def __init__ (self , access_id , access_key , region , default_admin_user ):
2323 self .offset = '0' # 偏移量,这里拼接的时候必须是字符串
24- self .limit = '100' # 官方默认是20,我们直接拿最大,小于100可以获取所有, 大于100需要设置偏移量再次请求:offset=100,offset={机器总数}
24+ self .limit = '100' # 官方默认是20,大于100需要设置偏移量再次请求:offset=100,offset={机器总数}
2525 self .access_id = access_id
2626 self .access_key = access_key
2727 self .region = region
@@ -60,7 +60,7 @@ def get_result_data(self):
6060 response = requests .get (result_url )
6161 result_data = json .loads (response .text )
6262 if result_data ['Response' ].get ('Error' ):
63- ins_log .read_log ('error' ,'{}' .format (result_data ['Response' ]))
63+ ins_log .read_log ('error' , '{}' .format (result_data ['Response' ]))
6464 return False
6565 else :
6666 ret = result_data ['Response' ]
@@ -109,7 +109,7 @@ def get_server_info(self):
109109 try :
110110 public_ip = i ['PublicIpAddresses' ][0 ]
111111 except (KeyError , TypeError ):
112- public_ip = private_ip #不存在公网就给私网IP
112+ public_ip = private_ip # 不存在公网就给私网IP
113113 os_type = i .get ('OsName' )
114114 region = i ['Placement' ].get ('Zone' )
115115 asset_data ['region' ] = region
@@ -125,7 +125,7 @@ def get_server_info(self):
125125 asset_data ['os_type' ] = os_type
126126 # print(asset_data)
127127 server_list .append (asset_data )
128-
128+ ins_log . read_log ( 'info' , asset_data )
129129 return server_list
130130
131131 def sync_cmdb (self ):
@@ -144,7 +144,7 @@ def sync_cmdb(self):
144144 ip = server .get ('public_ip' )
145145 instance_id = server .get ('instance_id' , 'Null' )
146146 hostname = server .get ('hostname' , instance_id )
147- if hostname == '' or not hostname :
147+ if not hostname . strip () :
148148 hostname = instance_id
149149 region = server .get ('region' , 'Null' )
150150 instance_type = server .get ('instance_type' , 'Null' )
@@ -180,19 +180,6 @@ def sync_cmdb(self):
180180
181181 session .commit ()
182182
183- # def test(self):
184- # count = int(450)
185- # # count = self.get_server_count()
186- # print('Tocal:{}'.format(count))
187- # for c in range(0, count, 100):
188- # self.offset = str(c)
189- # if (c + 100) > count:
190- # self.limit = str(count)
191- # else:
192- # self.limit = str(c + 100)
193- #
194- # print('开始同步第{}--第{}台机器'.format(self.offset, self.limit))
195- # print(f"offset:{self.offset},limit:{self.limit}")
196183 def test_auth (self ):
197184 """
198185 测试下用户给的信息是否正确
@@ -218,7 +205,7 @@ def index(self):
218205 self .limit = str (count )
219206 else :
220207 self .limit = str (c + 100 )
221- ins_log .read_log ('info' , '开始同步第 {}--{}台机器' .format (self .offset , self .limit ))
208+ ins_log .read_log ('info' , '开始同步腾讯云的第 {}--{}台机器' .format (self .offset , self .limit ))
222209 self .sync_cmdb ()
223210
224211
@@ -261,6 +248,10 @@ def main():
261248 obj .index ()
262249
263250
251+ # def test():
252+ # obj = CVMApi('','','ap-shanghai','')
253+ # obj.index()
254+
264255
265256if __name__ == '__main__' :
266257 fire .Fire (main )
0 commit comments