Esempio n. 1
0
    def request_to_disk(self):
        """
        :return:  磁盘信息
        """
        page_size = 50
        total_num_count = self.get_total_disk()

        page_num = total_num_count / page_size
        page_num = int(page_num) + 1
        disk_list = []
        for num in range(1, page_num + 1):
            request = DescribeDisksRequest.DescribeDisksRequest()
            request.set_PageNumber(num)
            request.set_PageSize(page_size)
            request.set_accept_format('json')
            response = self.client.do_action(request)
            response = json.loads(response)
            disk_info = response["Disks"]["Disk"]
            for disk in disk_info:
                disks = {
                    "InstanceId": disk["InstanceId"],
                    "device": disk["Device"],
                    "DiskSize": disk["Size"],
                    "disk": disk["DiskId"]
                }
                disk_list.append(disks)
        return disk_list
 def get_disks(self):
     req = DescribeDisksRequest.DescribeDisksRequest()
     req.set_InstanceId(self.instance_id)
     req.set_PageSize(20)
     req.set_PageNumber(1)
     resp = do_action(req)
     return find_by_path(resp, 'Disks', 'Disk')
Esempio n. 3
0
    def GetInfoENdisk(self, TotalPageDisk):

        data = []

        for PageNum in range(1, TotalPageDisk + int(1)):
            request = DescribeDisksRequest.DescribeDisksRequest()
            request.set_PageNumber(PageNum)

            response = json.loads(self.client.do_action_with_exception(request))

            for i in response["Disks"]["Disk"]:

                info = (
                    self.Account,
                    i.get("RegionId"),
                    i.get("ZoneId"),
                    i.get("InstanceId"),
                    i.get("DiskId"),
                    i.get("Device"),
                    i.get("Size"),
                    i.get("Status"),
                    i.get("CreationTime"),
                    i.get("ExpiredTime")
                )
                data.append(info)
        return data
Esempio n. 4
0
 def describedisks(self, instanceid=None):
     '''查询磁盘'''
     page = 0
     disks_info = []
     while True:
         page += 1
         values = {
             "RegionId": "cn-hangzhou",
             "PageNumber": page,
             "PageSize": 100
         }
         if instanceid:
             values = {"RegionId": "cn-hangzhou", "InstanceId": instanceid}
         request = DescribeDisksRequest.DescribeDisksRequest()
         #info = self.request_api(request, values)['Disks']['Disk']
         info = self.request_api(request, values)
         for disk_info in info['Disks']['Disk']:
             if disk_info['Category'] == 'cloud_efficiency':
                 disk_info['Category'] = 'efficiency'
             if disk_info['Category'] == 'cloud_ssd':
                 disk_info['Category'] = 'ssd'
             disks_info.append({
                 disk_info['InstanceId']:
                 [disk_info['Size'], disk_info['Category']]
             })
         if len(info['Disks']['Disk']) != 100:
             break
     return disks_info
Esempio n. 5
0
    def get_disksinfo(self):
        self.connect()

        retdisks = []
        for num in range(1, 4):
            request = DescribeDisksRequest.DescribeDisksRequest()
            request.set_accept_format("json")
            request.set_PageSize(100)
            request.set_PageNumber(num)
            response_string = self.clt.do_action_with_exception(request)

            response = json.loads(response_string)
            diskinfos = response['Disks']['Disk']
            if len(diskinfos) == 0:
                break
            retdisks.extend(diskinfos)


        ret = {}
        for x in retdisks:
            instance_id = x['InstanceId']
            device_size = x['Size']
            device_name = x['Device']
            tmp = {'device_size': device_size, 'device_name': device_name, 'device_type': ''}
            if instance_id in ret:
                ret[instance_id].append(tmp)
                ret[instance_id] = ret[instance_id]
            else:
                ret[instance_id] = [tmp]
        return ret
Esempio n. 6
0
def get_yundisk_info(keys):
    '''
    根据InstanceId获取磁盘大小
    :param keys:
    :return:
    '''
    key_id = keys['key_id']
    key_secret = keys['key_secret']
    region_id = keys['region_id']
    diskIds = [keys['instanceId']]
    result = {}

    try:
        client = AcsClient(key_id, key_secret, region_id)
        request = DescribeDisksRequest.DescribeDisksRequest()
        request.set_DiskIds(diskIds)
        response = client.do_action_with_exception(request)
        response_json = json.loads(response)
        for item in response_json['Disks']['Disk']:
            result['diskId'] = item.get('DiskId')
            result['instanceId'] = item.get('InstanceId')
            result['size'] = item.get('Size')
            result['status'] = item.get('Status')
            result['creationTime'] = item.get('CreationTime')
            result['expiredTime'] = item.get('ExpiredTime')

    except Exception as e:
        print(e)

    return result
Esempio n. 7
0
    def get_disks(self, instance_id):

        request = DescribeDisksRequest.DescribeDisksRequest()
        request.set_action_name('DescribeDisks')
        request.set_InstanceId(instance_id)
        response = self.asc_client.do_action_with_exception(request)
        disks = json.loads(response.decode())
        return disks
Esempio n. 8
0
 def get_total_disk(self):
     """
     :return: 磁盘总数量
     """
     request = DescribeDisksRequest.DescribeDisksRequest()
     request.set_accept_format('json')
     response = self.client.do_action(request)
     disk_info = json.loads(response.decode("utf-8"))
     total_num_count = disk_info["TotalCount"]
     return total_num_count
Esempio n. 9
0
 def describe_disks(self, diskids=None):
     request = DescribeDisksRequest.DescribeDisksRequest()
     key_list = ["RegionId", "ZoneId", "DiskName", "Category", "PageSize"]
     self.vm_params.setdefault("Category", "cloud_ssd")
     self.vm_params.setdefault("PageSize", "100")
     if diskids:
         key_list.append("DiskIds")
         self.vm_params["DiskIds"] = diskids
     request = self._add_params(request, key_list, self.vm_params)
     return self._send_request(request)
Esempio n. 10
0
def wait_for_dick_status(config, status):
    client = config.create_api_client()
    DiskId = config.get('DiskId')
    while True:
        time.sleep(20)
        req = DescribeDisksRequest.DescribeDisksRequest()
        result = do_action(client, req)
        items = result['Disks']['Disk']
        lookups = {item['DiskId']: item for item in items}
        if lookups[DiskId]['Status'] == status:
            return
Esempio n. 11
0
 def get_DescribeSnapshots(self, DiskId, SnapshotId):
     try:
         request = DescribeDisksRequest.DescribeDisksRequest()
         request.set_accept_format('json')
         request.set_action_name('DescribeSnapshots')
         request.set_DiskIds(DiskId)
         request.set_SnapshotId(SnapshotId)
         results = json.loads(self.client.do_action_with_exception(request))
         return results['Snapshots']['Snapshot']
     except Exception as e:
         logging.info(e)
         return {}
Esempio n. 12
0
 def get_disk_ids(self, instance_id):
     request = DescribeDisksRequest.DescribeDisksRequest()
     request.set_InstanceId(instance_id)
     request.set_DiskType('all')
     request.set_Status('In_use')
     request.set_PageSize(20)
     response = self.client.do_action_with_exception(request)
     response_dic = json.loads(response)
     disks = response_dic.get('Disks').get('Disk')
     disk_ids = []
     for disk in disks:
         disk_ids.append(disk.get('DiskId'))
     return disk_ids
Esempio n. 13
0
 def get_disks(self, instance_id):
     request = DescribeDisksRequest.DescribeDisksRequest()
     request.set_InstanceId(instance_id)
     request.set_DiskType('all')
     request.set_Status('In_use')
     request.set_PageSize(20)
     response = self.client.do_action_with_exception(request)
     response_dic = json.loads(response)
     disks_info = response_dic.get('Disks').get('Disk')
     disk_lst = []
     for disk in disks_info:
         disk_lst.append({'DiskId': disk.get('DiskId'), 'Device': disk.get('Device'), 'Type': disk.get('Type'), 'Size': disk.get('Size')})
     return disk_lst
Esempio n. 14
0
    def describe_disks(self, diskids=None):
        """Describe cloud disks.

        diskids should be a string like '"id1","id2","id3"'.
        """
        request = DescribeDisksRequest.DescribeDisksRequest()
        key_list = ["ZoneId", "DiskName", "Category", "PageSize"]
        self.vm_params.setdefault("Category", self.vm_params['DiskCategory'])
        self.vm_params.setdefault("PageSize", "100")
        if diskids:
            key_list.append("DiskIds")
            self.vm_params["DiskIds"] = diskids
        request = self._add_params(request, key_list, self.vm_params)
        return self._send_request(request)
Esempio n. 15
0
 def get_image_list(self, pageNumber = 1, pageSize = 100):
     result = []
     request = DescribeDisksRequest.DescribeDisksRequest()
     request.set_accept_format('json')
     request.set_query_params(dict(PageNumber = pageNumber, PageSize = pageSize))
     for clt in self.clt_conn_list:
         clt_result = json.loads(clt.do_action(request))
         result += clt_result['Disks']['Disk']
         totalCount = clt_result['TotalCount']
         while totalCount > pageNumber * pageSize:
             pageNumber += 1 
             request.set_query_params(dict(PageNumber = pageNumber, PageSize = pageSize))
             clt_result = json.loads(clt.do_action(request))
             result += clt_result['Disks']['Disk']
     return result
Esempio n. 16
0
 def get_diskinfo(self, instance_id):
     request = DescribeDisksRequest.DescribeDisksRequest()
     request.set_accept_format("json")
     request.set_InstanceId(InstanceId=instance_id)
     response_string = self.clt.do_action_with_exception(request)
     response = json.loads(response_string)
     disks = []
     for disk in response['Disks']['Disk']:
         temp = {
             'disk_id': disk['DiskId'],
             'device': disk['Device'],
             'size': disk['Size'],
             'type': disk['Type'],
             'creation_time': disk['CreationTime'].replace('T', ' ').rstrip('Z'),
             'expired_time': disk['ExpiredTime'].replace('T', ' ').rstrip('Z')
         }
         disks.append(temp)
     return disks
Esempio n. 17
0
    def __init__(self, Account, AccessKey, AccessKeySecret, Region, host, user, passwd, db):
        
        self.Account = Account
        # Auth
        self.client = AcsClient(AccessKey, AccessKeySecret, Region)

        # instance
        self.request = DescribeInstancesRequest.DescribeInstancesRequest()
        self.request.set_PageSize(1)
        self.response = json.loads(self.client.do_action_with_exception(self.request))

        # Disk
        self.disk_request = DescribeDisksRequest.DescribeDisksRequest()
        self.disk_request.set_PageSize(1)
        self.disk_response = json.loads(self.client.do_action_with_exception(self.disk_request))

        # MySQL
        self.mysql_conn = MySQLdb.connect(host = host, user = user, passwd = passwd, db = db, charset="utf8")
        self.db_cursor = self.mysql_conn.cursor()
Esempio n. 18
0
    def checkSnapshotStatus(self):
        pageNumber = 1

        # 创建request,并设置参数
        request = DescribeDisksRequest.DescribeDisksRequest()
        request.set_accept_format('json')
        request.set_PageSize(100)
        request.set_PageNumber(pageNumber)
        # request.set_
        response = self.client.do_action_with_exception(request)

        # 发起API请求并显示返回值
        response_dict = json.loads(response)

        # 生成生成器
        while response_dict['Disks']['Disk']:
            yield response_dict['Disks']['Disk']
            pageNumber += 1
            request.set_PageNumber(pageNumber)
            response = self.client.do_action_with_exception(request)
            response_dict = json.loads(response)
Esempio n. 19
0
def get_yundisk_size(keys):
    '''
    根据InstanceId获取磁盘大小
    :param keys:
    :return:
    '''
    key_id = keys['key_id']
    key_secret = keys['key_secret']
    region_id = keys['region_id']
    instanceId = keys['instanceId']
    Size = []

    try:
        client = AcsClient(key_id, key_secret, region_id)
        request = DescribeDisksRequest.DescribeDisksRequest()
        request.set_InstanceId(instanceId)
        response = client.do_action_with_exception(request)
        response_json = json.loads(response)
        for i in response_json['Disks']['Disk']:
            Size.append(i['Size'])
    except:
        Size = []

    return Size
Esempio n. 20
0
for i in range(pagenum):
    print('页码:', i+1)
    request.set_PageNumber(i+1)    # 第几页
    response = client.do_action_with_exception(request)
    jsonData = json.loads(response, encoding='utf-8')

    instances = dict()

    for item in jsonData['Instances']['Instance']:

        instance = dict()
        disk = dict()

        # 取回挂载的硬盘信息
        requestDisk = DescribeDisksRequest.DescribeDisksRequest()
        requestDisk.set_InstanceId(item['InstanceId'])   # 关联相关InstanceId
        responseDisk = client.do_action_with_exception(requestDisk)
        jsonDataDisk = json.loads(responseDisk, encoding='utf-8')

        for j in range(jsonDataDisk['TotalCount']):
            disk[jsonDataDisk['Disks']['Disk'][j]['Device']] = jsonDataDisk['Disks']['Disk'][j]['Size']

        # 写入字典
        instance['InstanceId'] = item['InstanceId']
        instance['HostName'] = item['HostName']
        instance['IpAddress'] = item['VpcAttributes']['PrivateIpAddress']['IpAddress'][0]
        instance['OSName'] = item['OSName']
        instance['Cpu'] = item['Cpu']
        instance['Memory'] = item['Memory']
        instance['Status'] = item['Status']