def data_is_valid_without_id(self): '''when there's no asset id in reporting data ,goes through this function fisrt''' data = self.request.POST.get("asset_data") if data: try: # push asset id into reporting data before doing the mandatory check if self.mandatory_check(data, only_check_filed=True): self.asset_obj = CMDB_MODELS.Asset.objects.create( sn=data['sn']) else: return False if self.asset_obj.asset_num: return False else: new_asset_num = asset_num.asset_num_builder() self.asset_obj.asset_num = new_asset_num self.asset_obj.save() self.clean_data = data if not self.response['error']: return True except ValueError as e: print(ValueError, e) self.response_msg('error', 'AssetDataInvalid', str(e)) else: self.response_msg( 'error', 'AssetDataInvalid', "The reported asset data is not valid or provided")
def data_is_valid_without_id(self): '''when there's no asset id in reporting data ,goes through this function fisrt''' data = self.request.POST.get("asset_data") if data: try: # push asset id into reporting data before doing the mandatory check if self.mandatory_check(data, only_check_filed=True): self.asset_obj = CMDB_MODELS.Asset.objects.create(sn=data['sn']) else: return False if self.asset_obj.asset_num: return False else: new_asset_num = asset_num.asset_num_builder() self.asset_obj.asset_num = new_asset_num self.asset_obj.save() self.clean_data = data if not self.response['error']: return True except ValueError as e: print(ValueError, e) self.response_msg('error', 'AssetDataInvalid', str(e)) else: self.response_msg('error', 'AssetDataInvalid', "The reported asset data is not valid or provided")
def asset_data_create(request): response = BaseResponse() try: asset_data = QueryDict(request.body, encoding='utf-8') new_asset_num = asset_num.asset_num_builder() asset_sn = asset_data.get('sn') try: Memory = int(asset_data.get('Memory')) except: Memory = None try: DeviceSize = int(asset_data.get('DeviceSize')) except: DeviceSize = None try: cpu_count = int(asset_data.get('cpu_count')) except: cpu_count = None if not asset_sn: asset_sn = new_asset_num # 创建asset obj asset_obj = models.Asset( device_type_id = asset_data.get('device_type_id'), asset_num = new_asset_num, sn = asset_sn, idc_id = asset_data.get('idc_id'), business_unit_id = asset_data.get('business_unit_id'), ) asset_obj.save() # 创建server obj server_obj = models.Server( asset_id = asset_obj.id, hostname = asset_data.get('hostname'), ipaddress = asset_data.get('ipaddress'), manage_ip = asset_data.get('manage_ip'), Memory = Memory, DeviceSize = DeviceSize, cpu_count = cpu_count, ) server_obj.save() response.message = '获取成功' except Exception as e: print(Exception, e) response.status = False response.message = str(e) return response
def asset_data_create(request): response = BaseResponse() try: asset_data = QueryDict(request.body, encoding='utf-8') new_asset_num = asset_num.asset_num_builder() asset_sn = asset_data.get('sn') try: Memory = int(asset_data.get('Memory')) except: Memory = None try: DeviceSize = int(asset_data.get('DeviceSize')) except: DeviceSize = None try: cpu_count = int(asset_data.get('cpu_count')) except: cpu_count = None if not asset_sn: asset_sn = new_asset_num # 创建asset obj asset_obj = models.Asset( device_type_id=asset_data.get('device_type_id'), asset_num=new_asset_num, sn=asset_sn, idc_id=asset_data.get('idc_id'), business_unit_id=asset_data.get('business_unit_id'), ) asset_obj.save() # 创建server obj server_obj = models.Server( asset_id=asset_obj.id, hostname=asset_data.get('hostname'), ipaddress=asset_data.get('ipaddress'), manage_ip=asset_data.get('manage_ip'), Memory=Memory, DeviceSize=DeviceSize, cpu_count=cpu_count, ) server_obj.save() response.message = '获取成功' except Exception as e: print(Exception, e) response.status = False response.message = str(e) return response
def asset_data_create(request): response = BaseResponse() try: asset_data = QueryDict(request.body, encoding='utf-8') new_asset_num = asset_num.asset_num_builder() asset_sn = asset_data.get('sn') Memory = int(asset_data.get('Memory')) DeviceSize = int(asset_data.get('DeviceSize')) cpu_count = int(asset_data.get('cpu_count')) if not asset_sn: asset_sn = new_asset_num if not models.Server.objects.filter( ipaddress=asset_data.get('ipaddress')): # 创建asset obj asset_obj = models.Asset( device_type_id=asset_data.get('device_type_id'), asset_num=new_asset_num, sn=asset_sn, idc_id=asset_data.get('idc_id'), business_unit_id=asset_data.get('business_unit_id'), manage_ip=asset_data.get('manage_ip'), creator_id=request.user.id, memo=asset_data.get('memo')) asset_obj.save() asset_obj.tag.add(asset_data.get('tag_id')) # 创建server obj server_obj = models.Server( asset_id=asset_obj.id, hostname=asset_data.get('hostname'), ipaddress=asset_data.get('ipaddress'), Memory=Memory, DeviceSize=DeviceSize, cpu_count=cpu_count, configuration='( %sC /%sG /%sG )' % (cpu_count, Memory, DeviceSize)) server_obj.save() else: response.status = False response.message = 'Ipaddress is already in system, Please check.' except Exception as e: print(Exception, e) response.status = False response.message = str(e) return response
def update_apply_order(request, order_id): response = BaseResponse() update_data = QueryDict(request.body, encoding='utf-8') order_id = update_data.get('order_id') try: apply_order_data = models.ServerApplyOrder.objects.filter( id=order_id) if apply_order_data: apply_order_obj = apply_order_data[0] for asset_obj in apply_order_obj.project.all(): try: cmdb_asset_obj = models.Asset( device_type_id=3, asset_num=asset_num.asset_num_builder(), sn=asset_num.asset_num_builder(), idc=models.IDC.objects.get(name=asset_obj.idc), business_unit=models.BusinessUnit.objects.get( id=apply_order_obj.business), memo=asset_obj.memo, creator=user_center_models.UserProfile.objects.get( username=request.user.username)) cmdb_asset_obj.save() cmdb_asset_obj.tag.add( models.Tag.objects.get(name=asset_obj.function)) # 创建资产日志 log_handler(asset_id=cmdb_asset_obj.id, event_type=1, detail='Server Created, apply user is %s' % apply_order_obj.user_apply, user=request.user) except Exception as e: print(e) cmdb_asset_obj.delete() asset_obj.approved = 4 asset_obj.save() continue try: cmdb_server_obj = models.Server( asset=cmdb_asset_obj, ipaddress=asset_obj.ipaddress, configuration='( %sC /%sG /%sG )' % (asset_obj.cpu, asset_obj.mem, asset_obj.disk), cpu_count=asset_obj.cpu, Memory=asset_obj.mem, DeviceSize=asset_obj.disk, os_type=asset_obj.sys_type, ) cmdb_server_obj.save() except Exception as e: print(e) cmdb_asset_obj.delete() asset_obj.approved = 4 asset_obj.save() continue asset_obj.approved = 3 asset_obj.save() # 更新任务单状态 这个地方注意一下更新的顺序,放在这里有点问题,上面失败,下面也会被更新 apply_order_obj.approved = True apply_order_obj.user_approve = request.user.username apply_order_obj.save() asset_detail_json = list(apply_order_obj.project.values()) # 任务创建完成,通知组内成员 send_to = '' business_obj = models.BusinessUnit.objects.filter( id=apply_order_obj.business) for user_groups in business_obj[0].manager.all(): for user in user_groups.users.all(): send_to += user.username + '@m1om.me,' template_var = { 'title': apply_order_obj.name, 'json_data': asset_detail_json, 'business_unit': business_node_top(business_obj[0]), 'user_apply': apply_order_obj.user_apply, 'user_approve': apply_order_obj.user_approve, 'server_count': len(asset_detail_json) } email_smtp.mail_send(send_to, 'asset_apply_create_inform_group', template_var) except Exception as e: print(Exception, e) response.status = False response.message = str(e) return response