阿里云Python SDK

时间:Nov. 6, 2019 分类:

目录:

安装

阿里云有好多种python的API版本,测试这个是可以用的

$ pip3.6 install alibaba-cloud-python-sdk-v2

主要目的是

  1. 获取云主机的Ecs信息,将数据同步到CMDB
  2. 获取CMDB的Tag同步到阿里云上

ESC信息获取

# -*- coding: utf-8 -*-
from alibabacloud import get_client

ecs_client = get_client(
    'ecs',  # 产品code
    access_key_id=your_access_key_id,  # 账号的AccessKey ID
    access_key_secret=your_access_key_secret,  # 账号的AccessKey Secret
    region_id=your_region_id  # 地域ID
)

page_number = 1
page_size = 100
total = 101
esc_response = []
while (page_number - 1) * page_size  < total:
    esc_response_tmp = ecs_client.describe_instances(page_size=page_size, page_number=page_number)
    esc_response.extend(esc_response_tmp['Instances']['Instance'])
    total = esc_response_tmp['TotalCount']
    page_number += 1

因为查出来的数据是分页的,按照每页100条进行获取,获取主机字典的列表

ESC对应磁盘挂载信息

for instance in esc_response:
    print(instance)
    disk_response = ecs_client.describe_disks(instance_id=instance['InstanceId'], page_size=20)
    disk_list = []
    for disk in disk_response['Disks']['Disk']:
        disk_list.append(
            {
                "media": 1,
                "volume": disk['Size'],
                "model": disk["Category"],
                "serial": disk["DiskId"],
                "rotation_rate": 1,
                "form_factor": 1,
                "power_on_hours": 1
            }
        )

通过主机id获取磁盘相关信息

ESC差异数据同步

def different_cmdb_ali(your_region_id):
    """
        同步差异数据到cmdb
    :param region:
    :return:
    """
    all_host_form_ali = describe_instances_fromali(your_region_id, idList=True)
    all_host_form_cmdb = describe_instances_fromcmdb(your_region_id)

    # 获取新增节点ID
    add_host_id_list = set(all_host_form_ali).difference(set(all_host_form_cmdb))
    if len(add_host_id_list) == 0:
        pass
    else:
        for id in add_host_id_list:
            one_instances_tocmdb(your_region_id, id, "add")
    # 获取删除节点ID
    del_host_id_list = set(all_host_form_cmdb).difference(set(all_host_form_ali))
    if len(del_host_id_list) == 0:
        pass
    else:
        for id in del_host_id_list:
            one_instances_tocmdb(your_region_id, id, "del")

获取阿里云和CMDB数据,然后直接通过集合对比方式获取增加的节点和删除的节点,然后再进行操作

ECS的Tag增删

            ali_add_tags = []
            ali_del_tags = []
            if len(add_tags) != 0:
                for tag in add_tags:
                    obj, created = Tags.objects.get_or_create(tag=tag)
                    machine.tags.add(obj)
                    ali_add_tags.append({'Value': '', 'Key': tag})
                tags_response = ecs_client.tag_resources(list_of_tag=ali_add_tags,
                                                         list_of_resource_id=[serial, ],
                                                         resource_type='instance')

            if len(del_tags) != 0:
                for tag in del_tags:
                    obj = Tags.objects.get(tag=tag)
                    machine.tags.remove(obj)
                    ali_del_tags.append(tag)
                tags_response = ecs_client.untag_resources(list_of_tag_key=ali_del_tags,
                                                           list_of_resource_id=[serial, ],
                                                           resource_type='instance')

增加是key: value的形式,删除直接通过key删除

未完待续~~~~