def _get_images(image_type): """ Return all list of Tencent Cloud images """ client = get_provider_client("cvm_client") req = cvm_models.DescribeImagesRequest() req.Filters = [{"Name": "image-type", "Values": image_type}] req.Offset = 0 req.Limit = 100 resp = client.DescribeImages(req) ret = {} for image in resp.ImageSet: if image.ImageState != "NORMAL": continue ret[image.ImageId] = { "ImageName": image.ImageName, "ImageType": image.ImageType, "ImageSource": image.ImageSource, "Platform": image.Platform, "Architecture": image.Architecture, "ImageSize": "{}GB".format(image.ImageSize), } return ret
def get_image(self,region): try: cred = self.get_cred() self.hp.endpoint = "cvm.tencentcloudapi.com" self.cp.httpProfile = self.hp client = cvm_client.CvmClient(cred,region,self.cp) req = models.DescribeImagesRequest() params = '{}' req.from_json_string(params) resp = client.DescribeImages(req) return resp.to_json_string() except TencentCloudSDKException as e: return None
def inquery(secu_id, secu_key, pattern, region, zone, instype): try: data = Pattern.objects.get(pattern=pattern) disksize = int((data.ins_type).split('/')[3].replace('G', '')) pay_type = data.pay_type # 根据region获取区域代码 region = ZoneCode.objects.get(zone=region).code # 连接腾讯云 cred = credential.Credential(secu_id, secu_key) httpProfile = HttpProfile() httpProfile.endpoint = "cvm.tencentcloudapi.com" # 设定区域 clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = cvm_client.CvmClient(cred, region, clientProfile) # 获取镜像ID req_imageid = models.DescribeImagesRequest() params = '{}' req_imageid.from_json_string(params) resp_imageid = client.DescribeImages(req_imageid) # 转成字典获取第一个镜像id imageid_set = json.loads(resp_imageid.to_json_string()) imageid = imageid_set['ImageSet'][0]['ImageId'] # 获取单位价格,根据付费模式,参数不同。包年报月/按小时后付费 req = models.InquiryPriceRunInstancesRequest() if pay_type == "PREPAID": params = '{"InstanceChargeType":"%s", "InstanceChargePrepaid":{"Period":3}, "Placement":{"Zone":"%s"},' \ '"InstanceType":"%s","ImageId":"%s","SystemDisk":{"DiskSize":%d}}' \ % (pay_type, zone, instype, imageid, disksize) else: params = '{"InstanceChargeType":"%s", "Placement":{"Zone":"%s"},' \ '"InstanceType":"%s","ImageId":"%s","SystemDisk":{"DiskSize":%d}}' \ % (pay_type, zone, instype, imageid, disksize) req.from_json_string(params) resp = client.InquiryPriceRunInstances(req) # print(resp.to_json_string()) # 转为python字典 price_data = json.loads(resp.to_json_string()) price = price_data['Price']['InstancePrice']['UnitPrice'] return price, region, zone, instype, imageid, pay_type, disksize except TencentCloudSDKException as err: raise err
def show_image(kwargs, call=None): """ Show the details of Tencent Cloud image CLI Examples: .. code-block:: bash salt-cloud -f show_image tencentcloud image=img-31tjrtph """ if call != "function": raise SaltCloudSystemExit( "The show_image function must be called with -f or --function" ) if not isinstance(kwargs, dict): kwargs = {} if "image" not in kwargs: raise SaltCloudSystemExit("No image specified.") image = kwargs["image"] client = get_provider_client("cvm_client") req = cvm_models.DescribeImagesRequest() req.ImageIds = [image] resp = client.DescribeImages(req) if not resp.ImageSet: raise SaltCloudNotFound( "The specified image '{}' could not be found.".format(image) ) ret = {} for image in resp.ImageSet: ret[image.ImageId] = { "ImageName": image.ImageName, "ImageType": image.ImageType, "ImageSource": image.ImageSource, "Platform": image.Platform, "Architecture": image.Architecture, "ImageSize": "{}GB".format(image.ImageSize), "ImageState": image.ImageState, } return ret
def des_imgid(self, imghub_type='PRIVATE_IMAGE', Offset=0, Limit=100): """ 查询镜像id :return: """ des_req = cvm_models.DescribeImagesRequest() des_req.Offset = Offset des_req.Limit = Limit des_req.Filters = [ { # 共有镜像:PUBLIC_IMAGE,私有镜像:PRIVATE_IMAGE "Name": "image-type", "Values": [imghub_type] }, { "Name": "image-state", "Values": ["NORMAL"] } ] response = self.cvm_helper.DescribeImages(des_req) result_content = response.to_json_string() return json.loads(result_content)
def describe_images_request(self, region): client = cvm_client.CvmClient(self.credential, region, self.profile(self.SERVICE_NAME)) req = models.DescribeImagesRequest() req.from_json_string('{}') resp = client.DescribeImages(req) return resp.to_json_string()
try: # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey cred = credential.Credential("AKIDylMjqkOq7Azay9Nq8D5kCSVM1Sfft4Sd", "K8lBONAk7IEzXt30kGXcS5UfbJm0zkG4") httpProfile = HttpProfile() httpProfile.endpoint = "cvm.api3.test.403a.tcecqpoc.fsphere.cn" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile # 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的。 client = cvm_client.CvmClient(cred, "shanghai", clientProfile) # 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。 req = models.DescribeImagesRequest() # 这里还支持以标准json格式的string来赋值请求参数的方式。下面的代码跟上面的参数赋值是等效的。 params = '{"Filters.0.Name":["image-id"],"Filters.0.Values.0":["img-pmqg1cw7"]}' req.from_json_string(params) # 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的。 # 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应。 resp = client.DescribeImages(req) # 输出json格式的字符串回包 print(resp.to_json_string()) # 也可以取出单个值。 # 你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义。 # print(resp.TotalCount)
def describeImages(): req = models.DescribeImagesRequest() req.Limit = 100 resp = client.DescribeImages(req) return resp
def get_available_region_os_list(self, region_code: str) -> List: client = cvm_client.CvmClient(self.isp, region_code) req = tc_models.DescribeImagesRequest() req.from_json_string('{"Limit":100}') resp = client.DescribeImages(req) return resp.ImageSet