def bandwith_info(self): # 默认带宽为1024kb band_width_kbps = 1024 bandwidth = self.session.execute( "select * from neutron.qos_bandwidth_limit_rules where qos_policy_id = \'{0}\'" .format(self.qos_policy_id)).first() if bandwidth: band_width_kbps = bandwidth[2] # 得到资源ID floating_id = self.session.execute( "select * from neutron.floatingips where floating_port_id = \'{0}\'" .format(self.port_id)).first() self.base_data.resource_id = floating_id[1] self.base_data.resources = {"max_kbps": band_width_kbps} self.base_data.timestamp = get_now_time() self.base_data.order_type = "2" self.base_data.tenant_id = self.user_data.get("project").get("id") self.base_data._context_project_name = self.user_data.get( "project").get("name") self.base_data._context_user_name = self.user_data.get("user").get( "name") self.base_data.user_id = self.user_data.get("user").get("id") self.base_data.resource = self.resource_type order_data = PostData(self.base_data) return order_data.post_data
def get_vm_data(self): result = self.session.execute("select uuid, vcpus, memory_mb, root_gb, created_at, deleted_at, vm_state,deleted," \ "user_id, project_id, availability_zone from nova.instances WHERE uuid = \'{0}\'". format(self.base_data.resource_id)).first() if not result: return None # 云主机是否被删除 self.base_data.timestamp = str(result[5]) if result[7] else str( result[4]) if result[10]: resources = { result[10] + "/" + "vcpus": "" if self.method == "DELETE" else result[1], result[10] + "/" + "memory_mb": "" if self.method == "DELETE" else result[2], result[10] + "/" + "disk_gb": "" if self.method == "DELETE" else result[3] } else: resources = { "vcpus": "" if self.method == "DELETE" else result[1], "memory_mb": "" if self.method == "DELETE" else result[2], "disk_gb": "" if self.method == "DELETE" else result[3], } self.base_data.resources = resources self.base_data.resource_id = result[0] self.base_data.tenant_id = self.user_data.get("project").get("id") self.base_data._context_project_name = self.user_data.get( "project").get("name") self.base_data._context_user_name = self.user_data.get("user").get( "name") self.base_data.resource = self.resource_type self.base_data.user_id = self.user_data.get("user").get("id") order_data = PostData(self.base_data) return order_data.post_data
def post_data(self): # return post data vm_data = self.vm_data if not vm_data: return {} vm_data_obj = InitCvmData(vm_data) # 获取价格 url = options.api_gateway_url + "/cloud/tencent/instanceprice" req_data = { "region": self.region, "zone": vm_data_obj.zone, "temptype": vm_data_obj.temptype, "imageid": vm_data_obj.imageid, "PublicIpAssigned": True if vm_data_obj.PublicIpAssigned else False, "PublicIpBandwidth": 1, "period": self.period } data = json.dumps(req_data) result = post_http(url=url, data=data, headers=self.headers).json() if result.get("code") != 200: em = "get price error from tencent. msg: {0}".format( result.get("message")) LOG.exception(em) return False # 得到虚拟机价格 total_price = 0 price_data = result.get('data') for types in price_data: total_price += price_data.get(types).get("DiscountPrice") # 构建post数据 self.base_data.resources = { "instance": 1, # "vcpus": vm_data_obj.vcpu, # "disk_gb": vm_data_obj.disk, # "floating_ip": vm_data_obj.PublicIpAssigned[0] if vm_data_obj.PublicIpAssigned else "" } self.base_data.resource_id = self.cvm_id self.base_data.timestamp = vm_data_obj.start_time self.base_data.end_time = vm_data_obj.end_time self.base_data.money = total_price self.base_data.resource_from_provider = "tencent" self.base_data.resource = "instance" self.base_data.resource_from = self.region self.base_data.order_type = "2" self.base_data.tenant_id = self.user_data.get("project").get("id") self.base_data._context_project_name = self.user_data.get( "project").get("name") self.base_data._context_user_name = self.user_data.get("user").get( "name") self.base_data.user_id = self.user_data.get("user").get("id") order_data = PostData(self.base_data) return order_data.post_data
def post_data(self): """return post data""" # 构建post数据 self.base_data.resources = {self.zone + "/" + "volume_size": self.used} # 云磁盘价格目前是以分为单位 self.base_data.order_type = "2" self.base_data.tenant_id = self.user_data.get("project").get("id") self.base_data._context_project_name = self.user_data.get( "project").get("name") self.base_data._context_user_name = self.user_data.get("user").get( "name") self.base_data.user_id = self.user_data.get("user").get("id") order_data = PostData(self.base_data) return order_data.post_data
def floating_ip_data(self): # 获取带宽 band_width_kbps = 1024 # 获取端口ID floating_ip = self.session.execute( "select * from neutron.floatingips where id = \'{0}\'".format( self.base_data.resource_id)).first() if not floating_ip: em = "can not found floating ip. <{0}>".format( self.base_data.resource_id) LOG.exception(em) return {} port_id = floating_ip[4] # 获取带宽与端口绑定的策略 policy_bindings = self.session.execute( "select * from neutron.qos_port_policy_bindings where port_id = \'{0}\'" .format(port_id)).first() if policy_bindings: policy_id = policy_bindings[1] bandwidth = self.session.execute( "select * from neutron.qos_bandwidth_limit_rules where qos_policy_id = \'{0}\'" .format(policy_id)).first() if bandwidth: band_width_kbps = bandwidth[2] # 返回data self.base_data.resources = { "floating_ip": 1, "max_kbps": band_width_kbps } self.base_data.timestamp = get_now_time() self.base_data.tenant_id = self.user_data.get("project").get("id") self.base_data._context_project_name = self.user_data.get( "project").get("name") self.base_data._context_user_name = self.user_data.get("user").get( "name") self.base_data.user_id = self.user_data.get("user").get("id") self.base_data.resource = self.ip_address self.base_data.order_type = "2" order_data = PostData(self.base_data) return order_data.post_data
def post_data(self): self.base_data.resources = { "tencent_cdn": self.cdn_flow_price.get("total_flow_day") } self.base_data.timestamp = self.today + " 00:00:00" self.base_data.end_time = self.today + " 23:59:59" # 云磁盘价格目前是以分为单位 self.base_data.money = self.cdn_flow_price.get("money") self.base_data.resource_from_provider = "tencent" self.base_data.resource = "tencent_cdn" self.base_data.order_type = "2" self.base_data.resource = self.resource_type # user about self.base_data.tenant_id = self.keystone_user_data.get("project_id") self.base_data._context_project_name = self.keystone_user_data.get( "project_name") self.base_data._context_user_name = self.keystone_user_data.get( "user_name") self.base_data.user_id = self.user_id order_data = PostData(self.base_data) return order_data.post_data
def router_info(self): # 根据路由ID找到port id router = self.session.execute( "select * from neutron.routers where id = \'{0}\'".format( self.base_data.resource_id)).first() if not router: em = "can not get router info. router id: <{0}>".format( self.base_data.resource_id) LOG.exception(em) return False port_id = router[5] # 默认带宽为1024kb,如果没有找到则用默认 band_width = 1024 # 根据`port_id`查找策略ID policy = self.session.execute( "select * from neutron.qos_port_policy_bindings where port_id = \'{0}\'" .format(port_id)).first() if policy: policy_id = policy[0] # 根据策略ID最终找到带宽 bandwidth = self.session.execute( "select * from neutron.qos_bandwidth_limit_rules where qos_policy_id = \'{0}\'" .format(policy_id)).first() if bandwidth: band_width = bandwidth[2] self.base_data.resources = {"floating_ip": 1, "max_kbps": band_width} self.base_data.timestamp = get_now_time() self.base_data.order_type = "2" self.base_data.tenant_id = self.user_data.get("project").get("id") self.base_data._context_project_name = self.user_data.get( "project").get("name") self.base_data._context_user_name = self.user_data.get("user").get( "name") self.base_data.user_id = self.user_data.get("user").get("id") self.base_data.resource = self.ip_address order_data = PostData(self.base_data) return order_data.post_data
def post_data(self): # return post data volumes_data = self.volumes_data if not volumes_data: return {} volumes_data_obj = InitCloudDiskData(volumes_data) # 获取价格 url = options.api_gateway_url + "/cloud/tencent/volumeprice" req_data = {"region": self.region, "zone": volumes_data_obj.zone, "type": volumes_data_obj.storge_type, "size": volumes_data_obj.disk_size, "period": self.period } req_data = json.dumps(req_data) result = post_http(url=url, data=req_data, headers=self.headers).json() if result.get("code") != 200: em = "get price error from tencent. msg: {0}".format(result.get("message")) LOG.exception(em) return False # 构建post数据 self.base_data.resource_id = self.disk_id self.base_data.resources = {"cloud_disk": volumes_data_obj.disk_size } self.base_data.timestamp = volumes_data_obj.start_time self.base_data.end_time = volumes_data_obj.end_time # 云磁盘价格目前是以分为单位 self.base_data.money = result.get("data") self.base_data.resource_from_provider = "tencent" self.base_data.resource = "cloud_disk" self.base_data.resource_from = self.region self.base_data.order_type = "2" self.base_data.tenant_id = self.user_data.get("project").get("id") self.base_data._context_project_name = self.user_data.get("project").get("name") self.base_data._context_user_name = self.user_data.get("user").get("name") self.base_data.user_id = self.user_data.get("user").get("id") order_data = PostData(self.base_data) return order_data.post_data