cloudtower-sdk


Namecloudtower-sdk JSON
Version 2.15.0 PyPI version JSON
download
home_pagehttps://github.com/smartxworks/cloudtower-python-sdk
SummaryCloudTower APIs
upload_time2024-05-11 10:14:05
maintainerNone
docs_urlNone
authorCloudtower developers
requires_pythonNone
licenseNone
keywords openapi openapi-generator cloudtower apis
VCS
bugtrack_url
requirements future six python_dateutil setuptools urllib3
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Cloudtower Python SDK

Python 环境下的 Cloudtower SDK,适用于 2.7 与 3.4 以上版本。

- [源码地址](https://github.com/smartxworks/cloudtower-python-sdk)
- [下载地址](https://github.com/smartxworks/cloudtower-python-sdk/releases)
- [通用指南](https://cloudtower-api-doc.vercel.app)

## 安装

- ### whl

  ```shell
  pip install cloudtower_sdk-2.15.0-py2.py3-none-any.whl
  ```

- ### tar.gz

  ```shell
  tar xvzf cloudtower-sdk-2.15.0.tar.gz
  cd cloudtower-sdk-2.15.0
  python setup.py install
  ```

- ### git 源码安装

  ```
  git clone https://github.com/smartxworks/cloudtower-python-sdk.git
  cd cloudtower-python-sdk
  python setup.py install
  ```

- ### git pip 安装

  ```shell
  pip install git+https://github.com/smartxworks/cloudtower-python-sdk.git
  ```

- ### pypi 安装
  ```shell
  pip install cloudtower-sdk
  ```

## 使用

### 创建实例

#### 创建 `ApiClient` 实例

```python
from cloudtower.configuration import Configuration
from cloudtower import ApiClient
# 配置 operation-api endpoint
configuration = Configuration(host="http://192.168.96.133/v2/api")
client = ApiClient(configuration)
```

> 如果需要使用 https,可以安装证书,或者忽略证书验证

```python
configuration = Configuration(host="https://192.168.96.133/v2/api")
configuration.verify_ssl = False
client = ApiClient(configuration)
```

#### 创建对应的 API 实例

> 根据不同用途的操作创建相关的 API 实例,例如虚拟机相关操作需要创建一个 `VmApi`。

```python
from cloudtower.api.vm_api import VmApi
vm_api = VmApi(client)
```

### 鉴权

> 可以通过 utils 中封装的登陆方法来鉴权 `ApiClient`

```python
from cloudtower.utils import wait_tasks, login
conf = Configuration(host="http://api-test.dev-cloudtower.smartx.com/v2/api")
api_client = ApiClient(conf)
login(api_client, "your_username", "your_password") # 默认使用 LOCAL 作为 usersource
```

> 也可以直接将 token 应用置 `configuration` 的 `api_key` 中

```python
from cloudtower.api.user_api import UserApi
from cloudtower.models import UserSource
# 通过 UserApi 中的 login 方法来获得 token。
user_api = UserApi(client)
login_res = user_api.login({
    "username": "your_username",
    "password": "your_password",
    "source": UserSource.LOCAL
})
# 将 token 配置在 configuration.api_key["Authorization"] 中,
# 这样所有使用当前 client 的 api 都会获得鉴权的 token 信息。
configuration.api_key["Authorization"] = login_res.data.token
```

### 发送请求

#### 获取资源

```python
vms = vm_api.get_vms({
  "where": {
    "id": "vm_id"
  },
  "first":1,
})
```

#### 更新资源

> 资源更新会产生相关的异步任务,当异步任务结束时,代表资源操作完成且数据已更新。

```python
start_res = vm_api.start_vm({
  "where": {
    "id": "stopped_vm_id"
  },
})
```

> 可以通过提供的工具方法同步等待异步任务结束

```python
from cloudtower.utils import wait_tasks
try:
 wait_tasks([res.task_id for res in start_res], api_client)
except ApiException as e:
 # 处理错误
else:
 # task完成后的回调
```

##### 方法参数说明

| 参数名        | 类型      | 是否必须 | 说明                                                                                 |
| ------------- | --------- | -------- | ------------------------------------------------------------------------------------ |
| ids           | list[str] | 是       | 需查询的 task 的 id 列表                                                             |
| api_client    | ApiClient | 是       | 查询所使用的 ApiClient 实例                                                          |
| interval      | int       | 否       | 轮询的间隔时间,默认为 5s                                                            |
| timeout       | int       | 否       | 超时时间,默认为 300s                                                                |
| exit_on_error | bool      | 否       | 是否在单个 Task 出错时立即退出,否则则会等待全部 Task 都完成后再退出,默认为 False。 |

##### 错误说明

| 错误码 | 说明             |
| ------ | ---------------- |
| 408    | 超时             |
| 500    | 异步任务内部错误 |

#### 自定义 header

> cloudtower api 支持通过设置 header 中的 content-language 来设置返回信息的语言, 可选值 `en-US`, `zh-CN`。默认为 `en-US`。

##### 通过 `ApiClient` 的 `set_default_header` 方法

> 可以通过 `ApiClient` 的 `set_default_header` 方法设置默认的 header 信息。

```python
api_client.set_default_header("content_language","en-US")
alert_api = AlertApi(api_client)
# 此时得到的 alerts 中的 message, solution, cause, impact 将被转换为英文描述。
alerts = alert_api.get_alerts(
  {
    "where": {
      "cluster": {
        "id": "cluster_id"
      }
    },
    "first": 100
  },
)
```

##### 通过设置请求的关键字参数

> 也可以通过设置请求的关键字参数 `content_language` 来设置返回信息的语言。

```python
from cloudtower.api.user_api import AlertApi

alert_api = AlertApi(api_client)
# 此时得到的 alerts 中的 message, solution, cause, impact 将被转换为中文描述。
alerts = alert_api.get_alerts(
  {
    "where": {
      "cluster": {
        "id": "cluster_id"
      }
    },
    "first": 100
  },
  content_language="zh-CN"
)
```

#### 其他

##### 发送异步请求

> 上述请求的发送都是同步的请求,会堵塞当前进程。如果需要使用异步请求,请在对应请求的关键字参数中加上 `async_req=True`。
> 通过返回结果 `ApplyResult.get()` 来获取对应的结果。

```python
vms = vm_api.get_vms(
  {
    "where": {
      "id": "vm_id"
    }
  },
  async_req=True
)
print(vms.get()[0].name)
```

### 使用完成后销毁 ApiClient 实例

```python
client.close()
```

## 操作示例

### 获取虚拟机

#### 获取所有虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

vms = vm_api.get_vms({})
```

#### 分页获取虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

vms_from_51_to_100 = vm_api.get_vms({
  "first": 50,
  "skip": 50,
})
```

#### 获取所有已开机虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi, VmStatus

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

running_vms = vm_api.get_vms(
    {
        "where": {
            "status": VmStatus.RUNNING
        }
    },
)
```

#### 获取名称或描述中包含特定字符串的虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

vms_name_contains = vm_api.get_vms(
    {
        "where": {
            "name_contains": "string"
        }
    },
)
```

#### 获取所有 vcpu > n 的虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

vms_has_4_more_vcpu = vm_api.get_vms(
    {
        "where": {
            "vcpu_gt": 4
        }
    },
)
```

### 从模版创建虚拟机

#### 仅指定 id

```python
from cloudtower.api import VmApi, ContentLibraryVmTemplateApi, ClusterApi
from cloudtower.utils import login, wait_tasks
from cloudtower.configuration import Configuration
from cloudtower import ApiClient
import os


configuration = Configuration(host=os.getenv("CLOUDTOWER_ENDPOINT"))
client = ApiClient(configuration)

login(client, os.getenv("CLOUDTOWER_USERNAME"), os.getenv("CLOUDTOWER_PASSWORD"))


def create_vm_from_template(template_name, cluster_name, vm_name):
    """
    通过内容库模板创建一台虚拟机,内容通过内容库模板设置
    :param template_name: 指定所需使用的内容库模板名称
    :param cluster_name: 指定虚拟机被部署的集群的集群名称
    :param vm_name: 虚拟机名称
    :return: 被创建的虚拟机
    """
    vm_api = VmApi(client)
    cluster_api = ClusterApi(client)
    template_api = ContentLibraryVmTemplateApi(client)

    cluster = cluster_api.get_clusters({
        "where": {
            "name": cluster_name
        }
    })
    if len(cluster) == 0:
        raise Exception("cluster not found")

    template = template_api.get_content_library_vm_templates({
        "where": {
            "name": template_name
        }
    })
    if len(template) == 0:
        raise Exception("template not found")

    with_task_vms = vm_api.create_vm_from_content_library_template([
        {
            "template_id": template[0].id,
            "cluster_id": cluster[0].id,
            "name": vm_name,
            "is_full_copy": False
        }
    ])
    tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
    vm_ids = [
        with_task_vm.data.id for with_task_vm in with_task_vms]
    wait_tasks(tasks, client)
    return vm_api.get_vms({
        "where": {
            "id_in": vm_ids
        }
    })[0]
```

#### 配置与模板不同的虚拟盘参数

```python
from cloudtower.api import VmApi, ContentLibraryVmTemplateApi, ClusterApi
from cloudtower.utils import login, wait_tasks
from cloudtower.configuration import Configuration
from cloudtower.models import Bus, VmVolumeElfStoragePolicyType
from cloudtower import ApiClient
import os


configuration = Configuration(host=os.getenv("CLOUDTOWER_ENDPOINT"))
client = ApiClient(configuration)

login(client, os.getenv("CLOUDTOWER_USERNAME"), os.getenv("CLOUDTOWER_PASSWORD"))


def create_vm_from_template_modify_disk(template_name, cluster_name, vm_name, disk_operate):
    """
    通过内容库模板创建一台虚拟机,配置虚拟机的磁盘
    :param template_name: 模板名称
    :param cluster_name: 集群名称
    :param vm_name: 虚拟机名称
    :param disk_operate: 磁盘操作,使用详见 create_vm_from_template_modify_disk_example 方法
    :return: 被创建的虚拟机
    """
    vm_api = VmApi(client)
    cluster_api = ClusterApi(client)
    template_api = ContentLibraryVmTemplateApi(client)

    cluster = cluster_api.get_clusters({
        "where": {
            "name": cluster_name
        }
    })
    if len(cluster) == 0:
        raise Exception("cluster not found")

    template = template_api.get_content_library_vm_templates({
        "where": {
            "name": template_name
        }
    })
    if len(template) == 0:
        raise Exception("template not found")

    with_task_vms = vm_api.create_vm_from_content_library_template([
        {
            "template_id": template[0].id,
            "cluster_id": cluster[0].id,
            "name": vm_name,
            "is_full_copy": False,
            "disk_operate": disk_operate
        }
    ])
    tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
    vm_ids = [
        with_task_vm.data.id for with_task_vm in with_task_vms]
    wait_tasks(tasks, client)
    return vm_api.get_vms({
        "where": {
            "id_in": vm_ids
        }
    })[0]


def create_vm_from_template_modify_disk_example():
    """
    通过模板创建虚拟机时,如果希望对原有的磁盘进行任何修改,可以通过 disk_operate 参数进行配置
    disk_operate 参数的类型是 VmDiskOperate,它是一个字典,包含以下字段:
    - remove_disks 由于删除指定index的磁盘
    - modify_disks 修改现有磁盘的配置,目前仅支持修改总线,如果有其他修改可以通过,删除原有盘
    - new_disks 新增磁盘,类型是 VmDiskParams,它是一个字典,包含以下字段:
        - mount_cd_roms 挂载 cd-rom
        - mount_disks 挂载已有磁盘
        - mount_new_create_disks 挂载新磁盘
    """
    disk_operate = {
        "remove_disks": {
            "disk_index": [0]  # 用于删除指定 index 的磁盘,index 从 0 开始计算,这里既是删除第一块磁盘
        },
        "new_disks": {
            "mount_cd_roms": [
                {
                    "boot": 2,  # 启动顺序
                    "content_library_image_id": ""  # 指定挂载内容库镜像的 id
                }
            ],
            "mount_disks": [
                {
                    "boot": 3,  # 启动顺序
                    "bus": Bus.VIRTIO,  # 总线类型
                    "vm_volume_id": "cljm6x2g1405g0958tp3zkhvh"  # 被挂载虚拟卷的 id
                }
            ],
            "mount_new_create_disks": [
                {
                    "boot": 4,
                    "bus": Bus.VIRTIO,
                    "vm_volume": {
                        "name": "test",  # 新建虚拟卷的名称
                        "size": 10 * 1024 * 1024 * 1024,  # 新建虚拟卷的大小,单位是字节
                        "elf_storage_policy": VmVolumeElfStoragePolicyType._2_THIN_PROVISION  # 存储策略
                    }
                }
            ]
        }
    }
    create_vm_from_template_modify_disk("template-name", "cluster-name", "vm-name", disk_operate)
```

#### 配置与模版不同的网卡参数

```python
from cloudtower.api import VmApi, ContentLibraryVmTemplateApi, ClusterApi
from cloudtower.utils import login, wait_tasks
from cloudtower.configuration import Configuration
from cloudtower.models import Bus, VmNicModel
from cloudtower import ApiClient
import os


configuration = Configuration(host=os.getenv("CLOUDTOWER_ENDPOINT"))
client = ApiClient(configuration)

login(client, os.getenv("CLOUDTOWER_USERNAME"), os.getenv("CLOUDTOWER_PASSWORD"))


def create_vm_from_template_modified_nic(template_name, cluster_name, vm_name, nic_params):
    """
    通过内容库模板创建一台虚拟机,配置虚拟机的网卡
    :param template_name: 模板名称
    :param cluster_name: 集群名称
    :param vm_name: 虚拟机名称
    :param nic_params: 磁盘操作,使用详见 create_vm_from_template_modified_nic_example 方法
    :return: 被创建的虚拟机
    """
    vm_api = VmApi(client)
    cluster_api = ClusterApi(client)
    template_api = ContentLibraryVmTemplateApi(client)

    cluster = cluster_api.get_clusters({
        "where": {
            "name": cluster_name
        }
    })
    if len(cluster) == 0:
        raise Exception("cluster not found")

    template = template_api.get_content_library_vm_templates({
        "where": {
            "name": template_name
        }
    })
    if len(template) == 0:
        raise Exception("template not found")

    with_task_vms = vm_api.create_vm_from_content_library_template([
        {
            "template_id": template[0].id,
            "cluster_id": cluster[0].id,
            "name": vm_name,
            "is_full_copy": False,
            "vm_nics": nic_params
        }
    ])
    tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
    vm_ids = [
        with_task_vm.data.id for with_task_vm in with_task_vms]
    wait_tasks(tasks, client)
    return vm_api.get_vms({
        "where": {
            "id_in": vm_ids
        }
    })[0]


def create_vm_from_template_modified_nic_example():
    """
    通过内容库模板创建虚拟机时,如果不传递 vm_nics 参数,会默认使用模板的网卡配置,如果需要修改网卡配置,可以传递 vm_nics 参数,
    vm_nics 参数是一个列表,列表中的每个元素都是一个字典:
    - connect_vlan_id 网卡对应虚拟机网络的 id,并非虚拟机网络的 vlan_id
    - enabled 是否启用网卡
    - model 网卡类型,可以使用 VmNicModel 类的属性,如 VmNicModel.VIRTIO
    创建虚拟机时并不支持修改网卡的 ip,mac,gateway,subnet mask,如果需要配置ip,子网,网关,可以通过 cloudinit 来实现,需要模板支持 cloudinit
    """
    nic_params = [
        {
            "connect_vlan_id": "vlan_id",
            "enabled": True,
            "model": VmNicModel.VIRTIO
        }
    ]
    create_vm_from_template_modified_nic("template_name", "cluster_name", "vm_name", nic_params)
```

### 创建空白虚拟机

#### 简单创建

```python
from cloudtower import (
    ApiClient,
    Configuration,
    VmApi,
    VmStatus,
    VmFirmware,
    Bus
)
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
    {
        "cluster_id": "cluster_id",
        "name": "vm_name",
        "ha": True,
        "cpu_cores": 4,
        "cpu_sockets": 4,
        "memory": 4*1024*1024*1024,
        "vcpu": 16,
        "status": VmStatus.STOPPED,
        "firmware": VmFirmware.BIOS,
        "vm_nics": [
            {
                "connect_vlan_id": "vlan_id",
            }
        ],
        "vm_disks": {
            "mount_cd_roms": [{
                "boot": 0,
                "index": 0
            }],
        }
    }
])[0]

wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

#### 创建时配置虚拟盘

##### CD-ROM 加载 ISO

```python
from cloudtower import (
    ApiClient,
    Configuration,
    VmApi,
    VmStatus,
    VmFirmware,
    Bus
)
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
    {
        "cluster_id": "cluster_id",
        "name": "vm_name",
        "ha": True,
        "cpu_cores": 4,
        "cpu_sockets": 4,
        "memory": 4*1024*1024*1024,
        "vcpu": 16,
        "status": VmStatus.STOPPED,
        "firmware": VmFirmware.BIOS,
        "vm_nics": [
            {
                "connect_vlan_id": "vlan_id",
            }
        ],
        "vm_disks": {
            "mount_cd_roms": [{
                "index": 0,
                "boot": 0,
                "elf_image_id": "elf_image_id"
            }],
        }
    }
])[0]

wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

##### 挂载虚拟卷为虚拟盘

```python
from cloudtower import (
    ApiClient,
    Configuration,
    VmApi,
    VmStatus,
    VmFirmware,
    Bus
)
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
    {
        "cluster_id": "cluster_id",
        "name": "vm_name",
        "ha": True,
        "cpu_cores": 4,
        "cpu_sockets": 4,
        "memory": 4*1024*1024*1024,
        "vcpu": 16,
        "status": VmStatus.STOPPED,
        "firmware": VmFirmware.BIOS,
        "vm_nics": [
            {
                "connect_vlan_id": "vlan_id",
            }
        ],
        "vm_disks": {
            "mount_disks": [{
                "index": 0,
                "boot": 0,
                "bus": Bus.VIRTIO,
                "vm_volume_id": "vm_volume_id",
                "index": 0,
            }],
        }
    }
])[0]

wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

##### 新增并挂载虚拟盘

```python
from cloudtower import (
    ApiClient,
    Configuration,
    VmApi,
    VmStatus,
    VmFirmware,
    Bus,
    VmVolumeElfStoragePolicyType
)
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
    {
        "cluster_id": "cluster_id",
        "name": "vm_name",
        "ha": True,
        "cpu_cores": 4,
        "cpu_sockets": 4,
        "memory": 4 * 1024*1024*1024,
        "vcpu": 16,
        "status": VmStatus.STOPPED,
        "firmware": VmFirmware.BIOS,
        "vm_nics": [
            {
                "connect_vlan_id": "vlan_id",
            }
        ],
        "vm_disks": {
            "mount_new_create_disks": [{
                "boot": 0,
                "bus": Bus.VIRTIO,
                "vm_volume": {
                    "elf_storage_policy": VmVolumeElfStoragePolicyType._2_THIN_PROVISION,
                    "size": 10 * 1024 * 1024 * 1024,
                    "name": "new_volume_name"
                }
            }],
        }
    }
])[0]

wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

#### 创建时配置虚拟网卡

```python
from cloudtower import (
    ApiClient,
    Configuration,
    VmApi,
    VmStatus,
    VmFirmware,
    Bus,
    VmNicModel,
    VmVolumeElfStoragePolicyType
)
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)
with_task_vm = vm_api.create_vm([
    {
        "cluster_id": "cluster_id",
        "name": "vm_name1",
        "ha": True,
        "cpu_cores": 4,
        "cpu_sockets": 4,
        "memory": 4 * 1024*1024*1024,
        "vcpu": 16,
        "status": VmStatus.STOPPED,
        "firmware": VmFirmware.BIOS,
        "vm_nics": [
            {
                "connect_vlan_id": "vlan_id",
                "mirror": True,
                "model": VmNicModel.VIRTIO
            }
        ],
        "vm_disks": {
            "mount_cd_roms": [{
                "index": 0,
                "boot": 0,
            }],
        }
    }
])[0]

wait_tasks([with_task_vm.task_id], api_client)
created_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

### 编辑虚拟机

#### 编辑基本信息

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

with_task_vm = vm_api.update_vm({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "name": "new_name",
        "description": "new_description",
        "ha": False,
        "vcpu": 2 * 2,
        "cpu_cores": 2,
        "cpu_sockets": 2,
        "memory": 1*1024*1024*1024,
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

#### CD-ROM 编辑

##### 添加 CD-ROM

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

with_task_vm = vm_api.add_vm_cd_rom({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "vm_cd_roms": [
            {
                "elf_image_id": "elf_image_id",
                "boot": 0,
                "index": 0
            }
        ]
    }
})[0]
wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

##### 删除 CD-ROM

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

with_task_vm = vm_api.remove_vm_cd_rom({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "cd_rom_ids": ["cd_rom_id_1", "cd_rom_id_2"]
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

#### 虚拟卷操作

##### 添加新虚拟卷

```python
from cloudtower import ApiClient, Configuration, Bus, VmVolumeElfStoragePolicyType, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

with_task_vm = vm_api.add_vm_disk({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "vm_disks": {
            "mount_new_create_disks": [
                {
                    "vm_volume": {
                        "elf_storage_policy": VmVolumeElfStoragePolicyType._2_THIN_PROVISION,
                        "size": 5*1024*1024*1024,
                        "name": "new_volume_name"
                    },
                    "boot": 1,
                    "bus": Bus.VIRTIO,
                }
            ]
        }
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

##### 挂载已存在虚拟卷为虚拟盘

```python
from cloudtower import ApiClient, Configuration, Bus, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

with_task_vm = vm_api.add_vm_disk({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "vm_disks": {
            "mount_disks": [
                {
                    "index": 0,
                    "vm_volume_id": "vm_volume_id",
                    "boot": 1,
                    "bus": Bus.VIRTIO,
                }
            ]
        }
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

##### 卸载虚拟盘

```python
from cloudtower import ApiClient, Configuration, VmVolumeElfStoragePolicyType, Bus, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)
vm_api = VmApi(api_client)

with_task_vm = vm_api.remove_vm_disk({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "disk_ids": ["vm_disk_id_1", "vm_disk_id_2"]
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

updated_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

#### 网卡操作

##### 添加网卡

```python
from cloudtower import ApiClient, Configuration, VmApi, VmNicModel
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)

with_task_vm = vm_api.add_vm_nic({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "vm_nics": [
            {
                "connect_vlan_id": "vlan_id",
                "enabled": False,
                "model": VmNicModel.VIRTIO,
            },
            {
                "connect_vlan_id": "vlan_id_2",
                "enabled": True,
                "mirror": True,
                "model": VmNicModel.VIRTIO,
            }
        ]
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)
updated_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

##### 编辑网卡

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)

with_task_vm = vm_api.update_vm_nic({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "nic_index": 0,
        "enabled": False,
        "mirror": False,
        "connect_vlan_id": "vlan_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)
updated_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

##### 移除网卡

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)

with_task_vm = vm_api.remove_vm_nic({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "nic_index": [0, 1]
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)
updated_vm = vm_api.get_vms({
    "where": {
        "id": with_task_vm.data.id
    }
})
```

#### 虚拟机迁移

##### 迁移至指定主机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)

with_task_vm = vm_api.mig_rate_vm({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "host_id": "host_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)
```

##### 自动调度到合适的主机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)

with_task_vm = vm_api.mig_rate_vm({
    "where": {
        "id": "vm_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)
```

### 虚拟机电源操作

#### 虚拟机开机:

##### 指定虚拟机开机,自动调度到合适的虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)
with_task_vm = vm_api.start_vm({
    "where": {
        "id": "vm_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

opened_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
```

##### 批量虚拟机开机,自动调度到合适的虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)
with_task_vms = vm_api.start_vm({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})

tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, api_client)

opened_vms = vm_api.get_vms({"where": {"id_in": ids}})
```

##### 开机至指定主机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)
with_task_vm = vm_api.start_vm({
    "where": {
        "id": "vm_id"
    },
    "data": {
        "host_id": "host_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

opened_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
```

#### 虚拟机关机

##### 指定虚拟机关机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)
with_task_vm = vm_api.shut_down_vm({
    "where": {
        "id": "vm_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

closed_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
```

##### 批量虚拟机关机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)
with_task_vms = vm_api.shut_down_vm({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})

tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]

wait_tasks(tasks, api_client)

closed_vms = vm_api.get_vms({"where": {"id_in": ids}})
```

##### 强制关机指定虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)
with_task_vm = vm_api.force_shut_down_vm({
    "where": {
        "id": "vm_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

closed_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
```

##### 强制关机批量虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

conf = Configuration(host="http://192.168.96.133/v2/api")
conf.api_key["Authorization"] = "token"
api_client = ApiClient(conf)

vm_api = VmApi(api_client)
with_task_vms = vm_api.force_shut_down_vm({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})

tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]
wait_tasks(tasks, api_client)

closed_vms = vm_api.get_vms({"where": {"id_in": ids}})
```

#### 虚拟机重启

##### 重启指定虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_vm = vm_api.restart_vm({
    "where": {
        "id": "vm_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

restarted_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
```

##### 重启批量虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_vms = vm_api.restart_vm({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})

tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]

wait_tasks(tasks, api_client)

restarted_vms = vm_api.get_vms({"where": {"id_in": ids}})
```

##### 强制重启指定虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_vm = vm_api.force_restart_vm({
    "where": {
        "id": "vm_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

restarted_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
```

##### 强制重启批量虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_vms = vm_api.force_restart_vm({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})

tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]

wait_tasks(tasks, api_client)

restarted_vms = vm_api.get_vms({"where": {"id_in": ids}})
```

#### 虚拟机暂停

##### 暂停指定虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_vm = vm_api.suspend_vm({
    "where": {
        "id": "vm_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

suspended_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
```

##### 暂停批量虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_vms = vm_api.suspend_vm({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})

tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]

wait_tasks(tasks, api_client)

suspended_vms = vm_api.get_vms({"where": {"id_in": ids}})
```

#### 虚拟机恢复

##### 恢复指定虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_vm = vm_api.resume_vm({
    "where": {
        "id": "vm_id"
    }
})[0]

wait_tasks([with_task_vm.task_id], api_client)

resumed_vm = vm_api.get_vms({"where": {"id": with_task_vm.data.id}})[0]
```

##### 恢复批量虚拟机

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_vms = vm_api.resume_vm({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})

tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]

wait_tasks(tasks, api_client)

resumed_vms = vm_api.get_vms({"where": {"id_in": ids}})
```

### 删除虚拟机

#### 回收站

##### 移入回收站

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_delete_vms = vm_api.move_vm_to_recycle_bin({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})

tasks = [with_task_delete_vm.task_id for with_task_delete_vm in with_task_delete_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]

wait_tasks(tasks, api_client)

vm_moved_to_recycle_bin = vm_api.get_vms({"where": {"id_in": ids}})
```

##### 从回收站恢复

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_delete_vms = vm_api.recover_vm_from_recycle_bin({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})

tasks = [with_task_delete_vm.task_id for with_task_delete_vm in with_task_delete_vms]
ids = [with_task_vm.data.id for with_task_vm in with_task_vms]

wait_tasks(tasks, api_client)

recovered_vms = vm_api.get_vms({"where": {"id_in": ids}})
```

#### 永久删除

```python
from cloudtower import ApiClient, Configuration, VmApi
from cloudtower.utils import wait_tasks

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

vm_api = VmApi(api_client)
with_task_delete_vms = vm_api.delete_vm({
    "where": {
        "id_in": ["vm_id_1", "vm_id_2"]
    }
})

tasks = [with_task_delete_vm.task_id for with_task_delete_vm in with_task_delete_vms]

wait_tasks(tasks, api_client)
```

## 场景示例

### 虚拟机备份

```python
from cloudtower import ApiClient
from cloudtower.api.vm_api import VmApi
from cloudtower.api.vm_snapshot_api import VmSnapshotApi
from cloudtower.api.iscsi_lun_snapshot_api import IscsiLunSnapshotApi
from cloudtower.models import (
    ConsistentType,
    VmToolsStatus
)
from cloudtower.utils import wait_tasks


def create_vm_snapshot(
    api_client: ApiClient,
    target_vm_name: str,
    target_snapshot_name: str,
    consistent_type: ConsistentType
):
    vm_api = VmApi(api_client)
    vm_snapshot_api = VmSnapshotApi(api_client)
    iscsi_lun_snapshot_api = IscsiLunSnapshotApi(api_client)
    # 1. 获取所需备份的虚拟机的信息,这里我们需要vm的id来构建创建snapshot的参数
    vm = vm_api.get_vms({
        "where": {
            "name": target_vm_name
        },
        "first": 1
    })
    # vm 已安装并启动 VMTools 时,consistent_type 可以使用 FILE_SYSTEM_CONSISTENT 代表文件系统一致性快照
    if vm.vm_tools_status != VmToolsStatus.RUNNING and consistent_type == ConsistentType.FILE_SYSTEM_CONSISTENT:
        consistent_type = ConsistentType.CRASH_CONSISTENT

    # 2. 创建虚拟机快照
    snapshots_with_task = vm_snapshot_api.create_vm_snapshot({
        "data": [
            {
                "vm_id": vm.id,
                "name": target_snapshot_name,
                "consistent_type": consistent_type
            }
        ]
    })

    # 3. 等待Task完成
    wait_tasks([snapshots_with_task[0].task_id], api_client)

    # 4. 根据返回的id查询生成的虚拟机快照
    snapshot = vm_snapshot_api.get_vm_snapshots({
        "where": {
            "id": snapshots_with_task.data.id
        }
    })[0]
    # 5. 根据返回的snapshot中的vm_disks包含了快照的虚拟盘信息
    # type 为 DISK 表示对应一个卷,其中会包含一个 snapshot_local_id 则表示该虚拟卷对应的lun快照的 local_id
    # type 为 CD-ROM则代表为被挂载的CD-ROM,不会产生lun快照
    lun_snapshot_ids = []
    for disk in snapshot.vm_disks:
        if disk.type == "DISK":
            lun_snapshot_ids.append(disk.snapshot_local_id)

    lun_snapshots = iscsi_lun_snapshot_api.get_iscsi_lun_snapshots({
        "where": {
            "name_in": lun_snapshot_ids
        }
    })

    return {
        "vm_snapshot": snapshot,
        "lun_snapshots": lun_snapshots
    }

```

### Dashboard 构建

#### 定义工具方法

```python
from functools import reduce
from datetime import datetime, timedelta
from cloudtower import ApiClient
from cloudtower.configuration import Configuration
from cloudtower.models import SeverityEnum, ClusterType, Hypervisor, DiskType, DiskUsageStatus, DiskHealthStatus
from cloudtower.api import VmApi, ClusterApi, AlertApi, HostApi, DiskApi, ClusterSettingsApi, GlobalSettingsApi

api_client = ApiClient(Configuration(host="http://192.168.96.133/v2/api"))

byte_units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"]
hz_units = ["Hz", "KHz", "MHz", "GHz", "THz"]


def format_unit(base: int, units, step=1024):
    if not len(units):
        raise Exception("no unit provided")
    if base <= 0:
        return "0" + units[0]
    for unit in units:
        if base < step:
            return "{:.2f}{}".format(base, unit)
        base /= step
    return "{:.2f}{}".format(base, units[-1])
```

#### 构建报警信息

```python
def build_alerts(api_client: ApiClient, cluster_ids):
    alert_api = AlertApi(api_client)
    alerts = alert_api.get_alerts({
        "where": {
            "ended": False,
            "cluster": {
                "id_in": cluster_ids
            },
        }
    })
    critial_alerts = [
        alert for alert in alerts if alert.severity == SeverityEnum.CRITICAL]
    notice_alerts = [
        alert for alert in alerts if alert.severity == SeverityEnum.NOTICE]
    info_alerts = [
        alert for alert in alerts if alert.severity == SeverityEnum.INFO]
    return {
        "critical": critial_alerts,
        "notice": notice_alerts,
        "info": info_alerts
    }
```

#### 构建硬盘信息

> 这里以机械硬盘为例

```python
def build_hdd_info(api_client: ApiClient, cluster_ids):
    disk_api = DiskApi(api_client)
    disks = disk_api.get_disks({
        "where": {
            "host": {
                "cluster": {
                    "id_in": cluster_ids
                }
            }
        }
    })
    hdd = {
        "healthy": 0,
        "warning": 0,
        "error": 0,
        "total": 0,
    }
    for disk in disks:
        if disk.type == DiskType.HDD:
            if disk.health_status in [DiskHealthStatus.UNHEALTHY, DiskHealthStatus.SUBHEALTHY, DiskHealthStatus.SMART_FAILED]:
                hdd['error'] += 1
            elif disk.usage_status in [DiskUsageStatus.UNMOUNTED, DiskUsageStatus.PARTIAL_MOUNTED]:
                hdd['warning'] += 1
            else:
                hdd['healthy'] += 1
            hdd['total'] += 1
    return hdd
```

#### 构建性能指标

> 获取指定集群的 CPU 核数,CPU 频率总数,CPU 使用率,内存总量,内存使用量,存储资源总量,存储资源已使用量,存储资源失效量与存储资源可用量。

```python
def build_metrics(api_client: ApiClient, clusters, cluster_ids):
    result = {}
    host_api = HostApi(api_client)
    hosts = host_api.get_hosts({
        "where": {
            "cluster": {
                "id_in": cluster_ids
            }
        }
    })
    cpu = {
        "total_cpu_cores": 0,
        "total_cpu_hz": 0,
        "used_cpu_hz": 0,
    }
    memory = {
        "total_memory": 0,
        "used_memory": 0,
    }
    storage = {
        "total": 0,
        "used": 0,
        "invalid": 0,
        "available": 0
    }

    for host in hosts:
        cluster = next(
            cluster for cluster in clusters if cluster.id == host.cluster.id)
        if cluster.hypervisor == Hypervisor.ELF:
            memory['total_memory'] += 0 if host.total_memory_bytes is None else host.total_memory_bytes
            memory['used_memory'] += (0 if host.running_pause_vm_memory_bytes is None else host.running_pause_vm_memory_bytes) + \
                (0 if host.os_memory_bytes is None else host.os_memory_bytes)

    for cluster in clusters:
        if cluster.type == ClusterType.SMTX_OS:
            cpu["total_cpu_cores"] += 0 if cluster.total_cpu_cores is None else cluster.total_cpu_cores
            cpu["total_cpu_hz"] += 0 if cluster.total_cpu_hz is None else cluster.total_cpu_hz
            cpu["used_cpu_hz"] += 0 if cluster.used_cpu_hz is None else cluster.used_cpu_hz
            if cluster.hypervisor == Hypervisor.VMWARE:
                memory["total_memory"] += 0 if cluster.total_memory_bytes is None else cluster.total_memory_bytes
                memory["used_memory"] += 0 if cluster.used_memory_bytes is None else cluster.used_memory_bytes
        storage["total"] += 0 if cluster.total_data_capacity is None else cluster.total_data_capacity
        storage["used"] += 0 if cluster.used_data_space is None else cluster.used_data_space
        storage["invalid"] += 0 if cluster.failure_data_space is None else cluster.failure_data_space
    if len([cluster for cluster in clusters if cluster.type != ClusterType.SMTX_ZBS]) > 1:
        cpu["cpu_usage"] = "{:.2f}%".format(
            cpu["used_cpu_hz"] / cpu["total_cpu_hz"])
        cpu["total_cpu_hz"] = format_unit(cpu["total_cpu_hz"], hz_units, 1000)
        cpu["used_cpu_hz"] = format_unit(cpu["used_cpu_hz"], hz_units, 1000)
        result['cpu'] = cpu
        memory["memory_usage"] = "{:.2f}%".format(
            memory["used_memory"] / memory["total_memory"])
        memory["total_memory"] = format_unit(
            memory["total_memory"], byte_units)
        memory["used_memory"] = format_unit(
            memory["used_memory"], byte_units)
        result["memory"] = memory
    storage["available"] = format_unit(
        storage["total"] - storage["used"] - storage["invalid"], byte_units)
    storage["total"] = format_unit(storage["total"], byte_units)
    storage["used"] = format_unit(storage["used"], byte_units)
    storage["invalid"] = format_unit(storage["invalid"], byte_units)
    result["storage"] = storage
    return result
```

#### 构建 Dashboard

```python
def build_dashboard(api_client: ApiClient, datacenter_id: str = None, cluster_id: str = None):
    result = {}
    cluster_api = ClusterApi(api_client)
    clusters = cluster_api.get_clusters({
        "where": {"id": cluster_id} if cluster_id is not None else {"datacenters_some": {"id": datacenter_id}} if datacenter_id is not None else None
    })
    cluster_ids = [cluster.id for cluster in clusters]

    result["alerts"] = build_alerts(api_client, cluster_ids)
    result["hdd"] = build_hdd_info(api_client, cluster_ids)
    metric = build_metrics(api_client, clusters, cluster_ids)
    if "cpu" in metric:
        result["cpu"] = metric["cpu"]
    if "memory" in metric:
        result["memory"] = metric["memory"]
    if "storage" in metric:
        result["storage"] = metric["storage"]
    return result
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/smartxworks/cloudtower-python-sdk",
    "name": "cloudtower-sdk",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "OpenAPI, OpenAPI-Generator, CloudTower APIs",
    "author": "Cloudtower developers",
    "author_email": "info@smartx.com",
    "download_url": "https://files.pythonhosted.org/packages/5d/e6/4b3341a5b03a7c88d115e3e45ae56e8cfd1626a4c712908891fd564d5760/cloudtower-sdk-2.15.0.tar.gz",
    "platform": null,
    "description": "# Cloudtower Python SDK\n\nPython \u73af\u5883\u4e0b\u7684 Cloudtower SDK\uff0c\u9002\u7528\u4e8e 2.7 \u4e0e 3.4 \u4ee5\u4e0a\u7248\u672c\u3002\n\n- [\u6e90\u7801\u5730\u5740](https://github.com/smartxworks/cloudtower-python-sdk)\n- [\u4e0b\u8f7d\u5730\u5740](https://github.com/smartxworks/cloudtower-python-sdk/releases)\n- [\u901a\u7528\u6307\u5357](https://cloudtower-api-doc.vercel.app)\n\n## \u5b89\u88c5\n\n- ### whl\n\n  ```shell\n  pip install cloudtower_sdk-2.15.0-py2.py3-none-any.whl\n  ```\n\n- ### tar.gz\n\n  ```shell\n  tar xvzf cloudtower-sdk-2.15.0.tar.gz\n  cd cloudtower-sdk-2.15.0\n  python setup.py install\n  ```\n\n- ### git \u6e90\u7801\u5b89\u88c5\n\n  ```\n  git clone https://github.com/smartxworks/cloudtower-python-sdk.git\n  cd cloudtower-python-sdk\n  python setup.py install\n  ```\n\n- ### git pip \u5b89\u88c5\n\n  ```shell\n  pip install git+https://github.com/smartxworks/cloudtower-python-sdk.git\n  ```\n\n- ### pypi \u5b89\u88c5\n  ```shell\n  pip install cloudtower-sdk\n  ```\n\n## \u4f7f\u7528\n\n### \u521b\u5efa\u5b9e\u4f8b\n\n#### \u521b\u5efa `ApiClient` \u5b9e\u4f8b\n\n```python\nfrom cloudtower.configuration import Configuration\nfrom cloudtower import ApiClient\n# \u914d\u7f6e operation-api endpoint\nconfiguration = Configuration(host=\"http://192.168.96.133/v2/api\")\nclient = ApiClient(configuration)\n```\n\n> \u5982\u679c\u9700\u8981\u4f7f\u7528 https\uff0c\u53ef\u4ee5\u5b89\u88c5\u8bc1\u4e66\uff0c\u6216\u8005\u5ffd\u7565\u8bc1\u4e66\u9a8c\u8bc1\n\n```python\nconfiguration = Configuration(host=\"https://192.168.96.133/v2/api\")\nconfiguration.verify_ssl = False\nclient = ApiClient(configuration)\n```\n\n#### \u521b\u5efa\u5bf9\u5e94\u7684 API \u5b9e\u4f8b\n\n> \u6839\u636e\u4e0d\u540c\u7528\u9014\u7684\u64cd\u4f5c\u521b\u5efa\u76f8\u5173\u7684 API \u5b9e\u4f8b\uff0c\u4f8b\u5982\u865a\u62df\u673a\u76f8\u5173\u64cd\u4f5c\u9700\u8981\u521b\u5efa\u4e00\u4e2a `VmApi`\u3002\n\n```python\nfrom cloudtower.api.vm_api import VmApi\nvm_api = VmApi(client)\n```\n\n### \u9274\u6743\n\n> \u53ef\u4ee5\u901a\u8fc7 utils \u4e2d\u5c01\u88c5\u7684\u767b\u9646\u65b9\u6cd5\u6765\u9274\u6743 `ApiClient`\n\n```python\nfrom cloudtower.utils import wait_tasks, login\nconf = Configuration(host=\"http://api-test.dev-cloudtower.smartx.com/v2/api\")\napi_client = ApiClient(conf)\nlogin(api_client, \"your_username\", \"your_password\") # \u9ed8\u8ba4\u4f7f\u7528 LOCAL \u4f5c\u4e3a usersource\n```\n\n> \u4e5f\u53ef\u4ee5\u76f4\u63a5\u5c06 token \u5e94\u7528\u7f6e `configuration` \u7684 `api_key` \u4e2d\n\n```python\nfrom cloudtower.api.user_api import UserApi\nfrom cloudtower.models import UserSource\n# \u901a\u8fc7 UserApi \u4e2d\u7684 login \u65b9\u6cd5\u6765\u83b7\u5f97 token\u3002\nuser_api = UserApi(client)\nlogin_res = user_api.login({\n    \"username\": \"your_username\",\n    \"password\": \"your_password\",\n    \"source\": UserSource.LOCAL\n})\n# \u5c06 token \u914d\u7f6e\u5728 configuration.api_key[\"Authorization\"] \u4e2d\uff0c\n# \u8fd9\u6837\u6240\u6709\u4f7f\u7528\u5f53\u524d client \u7684 api \u90fd\u4f1a\u83b7\u5f97\u9274\u6743\u7684 token \u4fe1\u606f\u3002\nconfiguration.api_key[\"Authorization\"] = login_res.data.token\n```\n\n### \u53d1\u9001\u8bf7\u6c42\n\n#### \u83b7\u53d6\u8d44\u6e90\n\n```python\nvms = vm_api.get_vms({\n  \"where\": {\n    \"id\": \"vm_id\"\n  },\n  \"first\":1,\n})\n```\n\n#### \u66f4\u65b0\u8d44\u6e90\n\n> \u8d44\u6e90\u66f4\u65b0\u4f1a\u4ea7\u751f\u76f8\u5173\u7684\u5f02\u6b65\u4efb\u52a1\uff0c\u5f53\u5f02\u6b65\u4efb\u52a1\u7ed3\u675f\u65f6\uff0c\u4ee3\u8868\u8d44\u6e90\u64cd\u4f5c\u5b8c\u6210\u4e14\u6570\u636e\u5df2\u66f4\u65b0\u3002\n\n```python\nstart_res = vm_api.start_vm({\n  \"where\": {\n    \"id\": \"stopped_vm_id\"\n  },\n})\n```\n\n> \u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u7684\u5de5\u5177\u65b9\u6cd5\u540c\u6b65\u7b49\u5f85\u5f02\u6b65\u4efb\u52a1\u7ed3\u675f\n\n```python\nfrom cloudtower.utils import wait_tasks\ntry:\n wait_tasks([res.task_id for res in start_res], api_client)\nexcept ApiException as e:\n # \u5904\u7406\u9519\u8bef\nelse:\n # task\u5b8c\u6210\u540e\u7684\u56de\u8c03\n```\n\n##### \u65b9\u6cd5\u53c2\u6570\u8bf4\u660e\n\n| \u53c2\u6570\u540d        | \u7c7b\u578b      | \u662f\u5426\u5fc5\u987b | \u8bf4\u660e                                                                                 |\n| ------------- | --------- | -------- | ------------------------------------------------------------------------------------ |\n| ids           | list[str] | \u662f       | \u9700\u67e5\u8be2\u7684 task \u7684 id \u5217\u8868                                                             |\n| api_client    | ApiClient | \u662f       | \u67e5\u8be2\u6240\u4f7f\u7528\u7684 ApiClient \u5b9e\u4f8b                                                          |\n| interval      | int       | \u5426       | \u8f6e\u8be2\u7684\u95f4\u9694\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3a 5s                                                            |\n| timeout       | int       | \u5426       | \u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3a 300s                                                                |\n| exit_on_error | bool      | \u5426       | \u662f\u5426\u5728\u5355\u4e2a Task \u51fa\u9519\u65f6\u7acb\u5373\u9000\u51fa\uff0c\u5426\u5219\u5219\u4f1a\u7b49\u5f85\u5168\u90e8 Task \u90fd\u5b8c\u6210\u540e\u518d\u9000\u51fa\uff0c\u9ed8\u8ba4\u4e3a False\u3002 |\n\n##### \u9519\u8bef\u8bf4\u660e\n\n| \u9519\u8bef\u7801 | \u8bf4\u660e             |\n| ------ | ---------------- |\n| 408    | \u8d85\u65f6             |\n| 500    | \u5f02\u6b65\u4efb\u52a1\u5185\u90e8\u9519\u8bef |\n\n#### \u81ea\u5b9a\u4e49 header\n\n> cloudtower api \u652f\u6301\u901a\u8fc7\u8bbe\u7f6e header \u4e2d\u7684 content-language \u6765\u8bbe\u7f6e\u8fd4\u56de\u4fe1\u606f\u7684\u8bed\u8a00, \u53ef\u9009\u503c `en-US`, `zh-CN`\u3002\u9ed8\u8ba4\u4e3a `en-US`\u3002\n\n##### \u901a\u8fc7 `ApiClient` \u7684 `set_default_header` \u65b9\u6cd5\n\n> \u53ef\u4ee5\u901a\u8fc7 `ApiClient` \u7684 `set_default_header` \u65b9\u6cd5\u8bbe\u7f6e\u9ed8\u8ba4\u7684 header \u4fe1\u606f\u3002\n\n```python\napi_client.set_default_header(\"content_language\",\"en-US\")\nalert_api = AlertApi(api_client)\n# \u6b64\u65f6\u5f97\u5230\u7684 alerts \u4e2d\u7684 message, solution, cause, impact \u5c06\u88ab\u8f6c\u6362\u4e3a\u82f1\u6587\u63cf\u8ff0\u3002\nalerts = alert_api.get_alerts(\n  {\n    \"where\": {\n      \"cluster\": {\n        \"id\": \"cluster_id\"\n      }\n    },\n    \"first\": 100\n  },\n)\n```\n\n##### \u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u7684\u5173\u952e\u5b57\u53c2\u6570\n\n> \u4e5f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u7684\u5173\u952e\u5b57\u53c2\u6570 `content_language` \u6765\u8bbe\u7f6e\u8fd4\u56de\u4fe1\u606f\u7684\u8bed\u8a00\u3002\n\n```python\nfrom cloudtower.api.user_api import AlertApi\n\nalert_api = AlertApi(api_client)\n# \u6b64\u65f6\u5f97\u5230\u7684 alerts \u4e2d\u7684 message, solution, cause, impact \u5c06\u88ab\u8f6c\u6362\u4e3a\u4e2d\u6587\u63cf\u8ff0\u3002\nalerts = alert_api.get_alerts(\n  {\n    \"where\": {\n      \"cluster\": {\n        \"id\": \"cluster_id\"\n      }\n    },\n    \"first\": 100\n  },\n  content_language=\"zh-CN\"\n)\n```\n\n#### \u5176\u4ed6\n\n##### \u53d1\u9001\u5f02\u6b65\u8bf7\u6c42\n\n> \u4e0a\u8ff0\u8bf7\u6c42\u7684\u53d1\u9001\u90fd\u662f\u540c\u6b65\u7684\u8bf7\u6c42\uff0c\u4f1a\u5835\u585e\u5f53\u524d\u8fdb\u7a0b\u3002\u5982\u679c\u9700\u8981\u4f7f\u7528\u5f02\u6b65\u8bf7\u6c42\uff0c\u8bf7\u5728\u5bf9\u5e94\u8bf7\u6c42\u7684\u5173\u952e\u5b57\u53c2\u6570\u4e2d\u52a0\u4e0a `async_req=True`\u3002\n> \u901a\u8fc7\u8fd4\u56de\u7ed3\u679c `ApplyResult.get()` \u6765\u83b7\u53d6\u5bf9\u5e94\u7684\u7ed3\u679c\u3002\n\n```python\nvms = vm_api.get_vms(\n  {\n    \"where\": {\n      \"id\": \"vm_id\"\n    }\n  },\n  async_req=True\n)\nprint(vms.get()[0].name)\n```\n\n### \u4f7f\u7528\u5b8c\u6210\u540e\u9500\u6bc1 ApiClient \u5b9e\u4f8b\n\n```python\nclient.close()\n```\n\n## \u64cd\u4f5c\u793a\u4f8b\n\n### \u83b7\u53d6\u865a\u62df\u673a\n\n#### \u83b7\u53d6\u6240\u6709\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nvms = vm_api.get_vms({})\n```\n\n#### \u5206\u9875\u83b7\u53d6\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nvms_from_51_to_100 = vm_api.get_vms({\n  \"first\": 50,\n  \"skip\": 50,\n})\n```\n\n#### \u83b7\u53d6\u6240\u6709\u5df2\u5f00\u673a\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi, VmStatus\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nrunning_vms = vm_api.get_vms(\n    {\n        \"where\": {\n            \"status\": VmStatus.RUNNING\n        }\n    },\n)\n```\n\n#### \u83b7\u53d6\u540d\u79f0\u6216\u63cf\u8ff0\u4e2d\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u4e32\u7684\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nvms_name_contains = vm_api.get_vms(\n    {\n        \"where\": {\n            \"name_contains\": \"string\"\n        }\n    },\n)\n```\n\n#### \u83b7\u53d6\u6240\u6709 vcpu > n \u7684\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nvms_has_4_more_vcpu = vm_api.get_vms(\n    {\n        \"where\": {\n            \"vcpu_gt\": 4\n        }\n    },\n)\n```\n\n### \u4ece\u6a21\u7248\u521b\u5efa\u865a\u62df\u673a\n\n#### \u4ec5\u6307\u5b9a id\n\n```python\nfrom cloudtower.api import VmApi, ContentLibraryVmTemplateApi, ClusterApi\nfrom cloudtower.utils import login, wait_tasks\nfrom cloudtower.configuration import Configuration\nfrom cloudtower import ApiClient\nimport os\n\n\nconfiguration = Configuration(host=os.getenv(\"CLOUDTOWER_ENDPOINT\"))\nclient = ApiClient(configuration)\n\nlogin(client, os.getenv(\"CLOUDTOWER_USERNAME\"), os.getenv(\"CLOUDTOWER_PASSWORD\"))\n\n\ndef create_vm_from_template(template_name, cluster_name, vm_name):\n    \"\"\"\n    \u901a\u8fc7\u5185\u5bb9\u5e93\u6a21\u677f\u521b\u5efa\u4e00\u53f0\u865a\u62df\u673a\uff0c\u5185\u5bb9\u901a\u8fc7\u5185\u5bb9\u5e93\u6a21\u677f\u8bbe\u7f6e\n    :param template_name: \u6307\u5b9a\u6240\u9700\u4f7f\u7528\u7684\u5185\u5bb9\u5e93\u6a21\u677f\u540d\u79f0\n    :param cluster_name: \u6307\u5b9a\u865a\u62df\u673a\u88ab\u90e8\u7f72\u7684\u96c6\u7fa4\u7684\u96c6\u7fa4\u540d\u79f0\n    :param vm_name: \u865a\u62df\u673a\u540d\u79f0\n    :return: \u88ab\u521b\u5efa\u7684\u865a\u62df\u673a\n    \"\"\"\n    vm_api = VmApi(client)\n    cluster_api = ClusterApi(client)\n    template_api = ContentLibraryVmTemplateApi(client)\n\n    cluster = cluster_api.get_clusters({\n        \"where\": {\n            \"name\": cluster_name\n        }\n    })\n    if len(cluster) == 0:\n        raise Exception(\"cluster not found\")\n\n    template = template_api.get_content_library_vm_templates({\n        \"where\": {\n            \"name\": template_name\n        }\n    })\n    if len(template) == 0:\n        raise Exception(\"template not found\")\n\n    with_task_vms = vm_api.create_vm_from_content_library_template([\n        {\n            \"template_id\": template[0].id,\n            \"cluster_id\": cluster[0].id,\n            \"name\": vm_name,\n            \"is_full_copy\": False\n        }\n    ])\n    tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]\n    vm_ids = [\n        with_task_vm.data.id for with_task_vm in with_task_vms]\n    wait_tasks(tasks, client)\n    return vm_api.get_vms({\n        \"where\": {\n            \"id_in\": vm_ids\n        }\n    })[0]\n```\n\n#### \u914d\u7f6e\u4e0e\u6a21\u677f\u4e0d\u540c\u7684\u865a\u62df\u76d8\u53c2\u6570\n\n```python\nfrom cloudtower.api import VmApi, ContentLibraryVmTemplateApi, ClusterApi\nfrom cloudtower.utils import login, wait_tasks\nfrom cloudtower.configuration import Configuration\nfrom cloudtower.models import Bus, VmVolumeElfStoragePolicyType\nfrom cloudtower import ApiClient\nimport os\n\n\nconfiguration = Configuration(host=os.getenv(\"CLOUDTOWER_ENDPOINT\"))\nclient = ApiClient(configuration)\n\nlogin(client, os.getenv(\"CLOUDTOWER_USERNAME\"), os.getenv(\"CLOUDTOWER_PASSWORD\"))\n\n\ndef create_vm_from_template_modify_disk(template_name, cluster_name, vm_name, disk_operate):\n    \"\"\"\n    \u901a\u8fc7\u5185\u5bb9\u5e93\u6a21\u677f\u521b\u5efa\u4e00\u53f0\u865a\u62df\u673a\uff0c\u914d\u7f6e\u865a\u62df\u673a\u7684\u78c1\u76d8\n    :param template_name: \u6a21\u677f\u540d\u79f0\n    :param cluster_name: \u96c6\u7fa4\u540d\u79f0\n    :param vm_name: \u865a\u62df\u673a\u540d\u79f0\n    :param disk_operate: \u78c1\u76d8\u64cd\u4f5c\uff0c\u4f7f\u7528\u8be6\u89c1 create_vm_from_template_modify_disk_example \u65b9\u6cd5\n    :return: \u88ab\u521b\u5efa\u7684\u865a\u62df\u673a\n    \"\"\"\n    vm_api = VmApi(client)\n    cluster_api = ClusterApi(client)\n    template_api = ContentLibraryVmTemplateApi(client)\n\n    cluster = cluster_api.get_clusters({\n        \"where\": {\n            \"name\": cluster_name\n        }\n    })\n    if len(cluster) == 0:\n        raise Exception(\"cluster not found\")\n\n    template = template_api.get_content_library_vm_templates({\n        \"where\": {\n            \"name\": template_name\n        }\n    })\n    if len(template) == 0:\n        raise Exception(\"template not found\")\n\n    with_task_vms = vm_api.create_vm_from_content_library_template([\n        {\n            \"template_id\": template[0].id,\n            \"cluster_id\": cluster[0].id,\n            \"name\": vm_name,\n            \"is_full_copy\": False,\n            \"disk_operate\": disk_operate\n        }\n    ])\n    tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]\n    vm_ids = [\n        with_task_vm.data.id for with_task_vm in with_task_vms]\n    wait_tasks(tasks, client)\n    return vm_api.get_vms({\n        \"where\": {\n            \"id_in\": vm_ids\n        }\n    })[0]\n\n\ndef create_vm_from_template_modify_disk_example():\n    \"\"\"\n    \u901a\u8fc7\u6a21\u677f\u521b\u5efa\u865a\u62df\u673a\u65f6\uff0c\u5982\u679c\u5e0c\u671b\u5bf9\u539f\u6709\u7684\u78c1\u76d8\u8fdb\u884c\u4efb\u4f55\u4fee\u6539\uff0c\u53ef\u4ee5\u901a\u8fc7 disk_operate \u53c2\u6570\u8fdb\u884c\u914d\u7f6e\n    disk_operate \u53c2\u6570\u7684\u7c7b\u578b\u662f VmDiskOperate\uff0c\u5b83\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a\n    - remove_disks \u7531\u4e8e\u5220\u9664\u6307\u5b9aindex\u7684\u78c1\u76d8\n    - modify_disks \u4fee\u6539\u73b0\u6709\u78c1\u76d8\u7684\u914d\u7f6e\uff0c\u76ee\u524d\u4ec5\u652f\u6301\u4fee\u6539\u603b\u7ebf\uff0c\u5982\u679c\u6709\u5176\u4ed6\u4fee\u6539\u53ef\u4ee5\u901a\u8fc7\uff0c\u5220\u9664\u539f\u6709\u76d8\n    - new_disks \u65b0\u589e\u78c1\u76d8\uff0c\u7c7b\u578b\u662f VmDiskParams\uff0c\u5b83\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a\n        - mount_cd_roms \u6302\u8f7d cd-rom\n        - mount_disks \u6302\u8f7d\u5df2\u6709\u78c1\u76d8\n        - mount_new_create_disks \u6302\u8f7d\u65b0\u78c1\u76d8\n    \"\"\"\n    disk_operate = {\n        \"remove_disks\": {\n            \"disk_index\": [0]  # \u7528\u4e8e\u5220\u9664\u6307\u5b9a index \u7684\u78c1\u76d8\uff0cindex \u4ece 0 \u5f00\u59cb\u8ba1\u7b97\uff0c\u8fd9\u91cc\u65e2\u662f\u5220\u9664\u7b2c\u4e00\u5757\u78c1\u76d8\n        },\n        \"new_disks\": {\n            \"mount_cd_roms\": [\n                {\n                    \"boot\": 2,  # \u542f\u52a8\u987a\u5e8f\n                    \"content_library_image_id\": \"\"  # \u6307\u5b9a\u6302\u8f7d\u5185\u5bb9\u5e93\u955c\u50cf\u7684 id\n                }\n            ],\n            \"mount_disks\": [\n                {\n                    \"boot\": 3,  # \u542f\u52a8\u987a\u5e8f\n                    \"bus\": Bus.VIRTIO,  # \u603b\u7ebf\u7c7b\u578b\n                    \"vm_volume_id\": \"cljm6x2g1405g0958tp3zkhvh\"  # \u88ab\u6302\u8f7d\u865a\u62df\u5377\u7684 id\n                }\n            ],\n            \"mount_new_create_disks\": [\n                {\n                    \"boot\": 4,\n                    \"bus\": Bus.VIRTIO,\n                    \"vm_volume\": {\n                        \"name\": \"test\",  # \u65b0\u5efa\u865a\u62df\u5377\u7684\u540d\u79f0\n                        \"size\": 10 * 1024 * 1024 * 1024,  # \u65b0\u5efa\u865a\u62df\u5377\u7684\u5927\u5c0f\uff0c\u5355\u4f4d\u662f\u5b57\u8282\n                        \"elf_storage_policy\": VmVolumeElfStoragePolicyType._2_THIN_PROVISION  # \u5b58\u50a8\u7b56\u7565\n                    }\n                }\n            ]\n        }\n    }\n    create_vm_from_template_modify_disk(\"template-name\", \"cluster-name\", \"vm-name\", disk_operate)\n```\n\n#### \u914d\u7f6e\u4e0e\u6a21\u7248\u4e0d\u540c\u7684\u7f51\u5361\u53c2\u6570\n\n```python\nfrom cloudtower.api import VmApi, ContentLibraryVmTemplateApi, ClusterApi\nfrom cloudtower.utils import login, wait_tasks\nfrom cloudtower.configuration import Configuration\nfrom cloudtower.models import Bus, VmNicModel\nfrom cloudtower import ApiClient\nimport os\n\n\nconfiguration = Configuration(host=os.getenv(\"CLOUDTOWER_ENDPOINT\"))\nclient = ApiClient(configuration)\n\nlogin(client, os.getenv(\"CLOUDTOWER_USERNAME\"), os.getenv(\"CLOUDTOWER_PASSWORD\"))\n\n\ndef create_vm_from_template_modified_nic(template_name, cluster_name, vm_name, nic_params):\n    \"\"\"\n    \u901a\u8fc7\u5185\u5bb9\u5e93\u6a21\u677f\u521b\u5efa\u4e00\u53f0\u865a\u62df\u673a\uff0c\u914d\u7f6e\u865a\u62df\u673a\u7684\u7f51\u5361\n    :param template_name: \u6a21\u677f\u540d\u79f0\n    :param cluster_name: \u96c6\u7fa4\u540d\u79f0\n    :param vm_name: \u865a\u62df\u673a\u540d\u79f0\n    :param nic_params: \u78c1\u76d8\u64cd\u4f5c\uff0c\u4f7f\u7528\u8be6\u89c1 create_vm_from_template_modified_nic_example \u65b9\u6cd5\n    :return: \u88ab\u521b\u5efa\u7684\u865a\u62df\u673a\n    \"\"\"\n    vm_api = VmApi(client)\n    cluster_api = ClusterApi(client)\n    template_api = ContentLibraryVmTemplateApi(client)\n\n    cluster = cluster_api.get_clusters({\n        \"where\": {\n            \"name\": cluster_name\n        }\n    })\n    if len(cluster) == 0:\n        raise Exception(\"cluster not found\")\n\n    template = template_api.get_content_library_vm_templates({\n        \"where\": {\n            \"name\": template_name\n        }\n    })\n    if len(template) == 0:\n        raise Exception(\"template not found\")\n\n    with_task_vms = vm_api.create_vm_from_content_library_template([\n        {\n            \"template_id\": template[0].id,\n            \"cluster_id\": cluster[0].id,\n            \"name\": vm_name,\n            \"is_full_copy\": False,\n            \"vm_nics\": nic_params\n        }\n    ])\n    tasks = [with_task_vm.task_id for with_task_vm in with_task_vms]\n    vm_ids = [\n        with_task_vm.data.id for with_task_vm in with_task_vms]\n    wait_tasks(tasks, client)\n    return vm_api.get_vms({\n        \"where\": {\n            \"id_in\": vm_ids\n        }\n    })[0]\n\n\ndef create_vm_from_template_modified_nic_example():\n    \"\"\"\n    \u901a\u8fc7\u5185\u5bb9\u5e93\u6a21\u677f\u521b\u5efa\u865a\u62df\u673a\u65f6\uff0c\u5982\u679c\u4e0d\u4f20\u9012 vm_nics \u53c2\u6570\uff0c\u4f1a\u9ed8\u8ba4\u4f7f\u7528\u6a21\u677f\u7684\u7f51\u5361\u914d\u7f6e\uff0c\u5982\u679c\u9700\u8981\u4fee\u6539\u7f51\u5361\u914d\u7f6e\uff0c\u53ef\u4ee5\u4f20\u9012 vm_nics \u53c2\u6570\uff0c\n    vm_nics \u53c2\u6570\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u5b57\u5178\uff1a\n    - connect_vlan_id \u7f51\u5361\u5bf9\u5e94\u865a\u62df\u673a\u7f51\u7edc\u7684 id\uff0c\u5e76\u975e\u865a\u62df\u673a\u7f51\u7edc\u7684 vlan_id\n    - enabled \u662f\u5426\u542f\u7528\u7f51\u5361\n    - model \u7f51\u5361\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528 VmNicModel \u7c7b\u7684\u5c5e\u6027\uff0c\u5982 VmNicModel.VIRTIO\n    \u521b\u5efa\u865a\u62df\u673a\u65f6\u5e76\u4e0d\u652f\u6301\u4fee\u6539\u7f51\u5361\u7684 ip\uff0cmac\uff0cgateway\uff0csubnet mask\uff0c\u5982\u679c\u9700\u8981\u914d\u7f6eip\uff0c\u5b50\u7f51\uff0c\u7f51\u5173\uff0c\u53ef\u4ee5\u901a\u8fc7 cloudinit \u6765\u5b9e\u73b0\uff0c\u9700\u8981\u6a21\u677f\u652f\u6301 cloudinit\n    \"\"\"\n    nic_params = [\n        {\n            \"connect_vlan_id\": \"vlan_id\",\n            \"enabled\": True,\n            \"model\": VmNicModel.VIRTIO\n        }\n    ]\n    create_vm_from_template_modified_nic(\"template_name\", \"cluster_name\", \"vm_name\", nic_params)\n```\n\n### \u521b\u5efa\u7a7a\u767d\u865a\u62df\u673a\n\n#### \u7b80\u5355\u521b\u5efa\n\n```python\nfrom cloudtower import (\n    ApiClient,\n    Configuration,\n    VmApi,\n    VmStatus,\n    VmFirmware,\n    Bus\n)\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.create_vm([\n    {\n        \"cluster_id\": \"cluster_id\",\n        \"name\": \"vm_name\",\n        \"ha\": True,\n        \"cpu_cores\": 4,\n        \"cpu_sockets\": 4,\n        \"memory\": 4*1024*1024*1024,\n        \"vcpu\": 16,\n        \"status\": VmStatus.STOPPED,\n        \"firmware\": VmFirmware.BIOS,\n        \"vm_nics\": [\n            {\n                \"connect_vlan_id\": \"vlan_id\",\n            }\n        ],\n        \"vm_disks\": {\n            \"mount_cd_roms\": [{\n                \"boot\": 0,\n                \"index\": 0\n            }],\n        }\n    }\n])[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\ncreated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n#### \u521b\u5efa\u65f6\u914d\u7f6e\u865a\u62df\u76d8\n\n##### CD-ROM \u52a0\u8f7d ISO\n\n```python\nfrom cloudtower import (\n    ApiClient,\n    Configuration,\n    VmApi,\n    VmStatus,\n    VmFirmware,\n    Bus\n)\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.create_vm([\n    {\n        \"cluster_id\": \"cluster_id\",\n        \"name\": \"vm_name\",\n        \"ha\": True,\n        \"cpu_cores\": 4,\n        \"cpu_sockets\": 4,\n        \"memory\": 4*1024*1024*1024,\n        \"vcpu\": 16,\n        \"status\": VmStatus.STOPPED,\n        \"firmware\": VmFirmware.BIOS,\n        \"vm_nics\": [\n            {\n                \"connect_vlan_id\": \"vlan_id\",\n            }\n        ],\n        \"vm_disks\": {\n            \"mount_cd_roms\": [{\n                \"index\": 0,\n                \"boot\": 0,\n                \"elf_image_id\": \"elf_image_id\"\n            }],\n        }\n    }\n])[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\ncreated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n##### \u6302\u8f7d\u865a\u62df\u5377\u4e3a\u865a\u62df\u76d8\n\n```python\nfrom cloudtower import (\n    ApiClient,\n    Configuration,\n    VmApi,\n    VmStatus,\n    VmFirmware,\n    Bus\n)\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.create_vm([\n    {\n        \"cluster_id\": \"cluster_id\",\n        \"name\": \"vm_name\",\n        \"ha\": True,\n        \"cpu_cores\": 4,\n        \"cpu_sockets\": 4,\n        \"memory\": 4*1024*1024*1024,\n        \"vcpu\": 16,\n        \"status\": VmStatus.STOPPED,\n        \"firmware\": VmFirmware.BIOS,\n        \"vm_nics\": [\n            {\n                \"connect_vlan_id\": \"vlan_id\",\n            }\n        ],\n        \"vm_disks\": {\n            \"mount_disks\": [{\n                \"index\": 0,\n                \"boot\": 0,\n                \"bus\": Bus.VIRTIO,\n                \"vm_volume_id\": \"vm_volume_id\",\n                \"index\": 0,\n            }],\n        }\n    }\n])[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\ncreated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n##### \u65b0\u589e\u5e76\u6302\u8f7d\u865a\u62df\u76d8\n\n```python\nfrom cloudtower import (\n    ApiClient,\n    Configuration,\n    VmApi,\n    VmStatus,\n    VmFirmware,\n    Bus,\n    VmVolumeElfStoragePolicyType\n)\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.create_vm([\n    {\n        \"cluster_id\": \"cluster_id\",\n        \"name\": \"vm_name\",\n        \"ha\": True,\n        \"cpu_cores\": 4,\n        \"cpu_sockets\": 4,\n        \"memory\": 4 * 1024*1024*1024,\n        \"vcpu\": 16,\n        \"status\": VmStatus.STOPPED,\n        \"firmware\": VmFirmware.BIOS,\n        \"vm_nics\": [\n            {\n                \"connect_vlan_id\": \"vlan_id\",\n            }\n        ],\n        \"vm_disks\": {\n            \"mount_new_create_disks\": [{\n                \"boot\": 0,\n                \"bus\": Bus.VIRTIO,\n                \"vm_volume\": {\n                    \"elf_storage_policy\": VmVolumeElfStoragePolicyType._2_THIN_PROVISION,\n                    \"size\": 10 * 1024 * 1024 * 1024,\n                    \"name\": \"new_volume_name\"\n                }\n            }],\n        }\n    }\n])[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\ncreated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n#### \u521b\u5efa\u65f6\u914d\u7f6e\u865a\u62df\u7f51\u5361\n\n```python\nfrom cloudtower import (\n    ApiClient,\n    Configuration,\n    VmApi,\n    VmStatus,\n    VmFirmware,\n    Bus,\n    VmNicModel,\n    VmVolumeElfStoragePolicyType\n)\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.create_vm([\n    {\n        \"cluster_id\": \"cluster_id\",\n        \"name\": \"vm_name1\",\n        \"ha\": True,\n        \"cpu_cores\": 4,\n        \"cpu_sockets\": 4,\n        \"memory\": 4 * 1024*1024*1024,\n        \"vcpu\": 16,\n        \"status\": VmStatus.STOPPED,\n        \"firmware\": VmFirmware.BIOS,\n        \"vm_nics\": [\n            {\n                \"connect_vlan_id\": \"vlan_id\",\n                \"mirror\": True,\n                \"model\": VmNicModel.VIRTIO\n            }\n        ],\n        \"vm_disks\": {\n            \"mount_cd_roms\": [{\n                \"index\": 0,\n                \"boot\": 0,\n            }],\n        }\n    }\n])[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\ncreated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n### \u7f16\u8f91\u865a\u62df\u673a\n\n#### \u7f16\u8f91\u57fa\u672c\u4fe1\u606f\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.update_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"name\": \"new_name\",\n        \"description\": \"new_description\",\n        \"ha\": False,\n        \"vcpu\": 2 * 2,\n        \"cpu_cores\": 2,\n        \"cpu_sockets\": 2,\n        \"memory\": 1*1024*1024*1024,\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nupdated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n#### CD-ROM \u7f16\u8f91\n\n##### \u6dfb\u52a0 CD-ROM\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.add_vm_cd_rom({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"vm_cd_roms\": [\n            {\n                \"elf_image_id\": \"elf_image_id\",\n                \"boot\": 0,\n                \"index\": 0\n            }\n        ]\n    }\n})[0]\nwait_tasks([with_task_vm.task_id], api_client)\n\nupdated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n##### \u5220\u9664 CD-ROM\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.remove_vm_cd_rom({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"cd_rom_ids\": [\"cd_rom_id_1\", \"cd_rom_id_2\"]\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nupdated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n#### \u865a\u62df\u5377\u64cd\u4f5c\n\n##### \u6dfb\u52a0\u65b0\u865a\u62df\u5377\n\n```python\nfrom cloudtower import ApiClient, Configuration, Bus, VmVolumeElfStoragePolicyType, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.add_vm_disk({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"vm_disks\": {\n            \"mount_new_create_disks\": [\n                {\n                    \"vm_volume\": {\n                        \"elf_storage_policy\": VmVolumeElfStoragePolicyType._2_THIN_PROVISION,\n                        \"size\": 5*1024*1024*1024,\n                        \"name\": \"new_volume_name\"\n                    },\n                    \"boot\": 1,\n                    \"bus\": Bus.VIRTIO,\n                }\n            ]\n        }\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nupdated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n##### \u6302\u8f7d\u5df2\u5b58\u5728\u865a\u62df\u5377\u4e3a\u865a\u62df\u76d8\n\n```python\nfrom cloudtower import ApiClient, Configuration, Bus, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.add_vm_disk({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"vm_disks\": {\n            \"mount_disks\": [\n                {\n                    \"index\": 0,\n                    \"vm_volume_id\": \"vm_volume_id\",\n                    \"boot\": 1,\n                    \"bus\": Bus.VIRTIO,\n                }\n            ]\n        }\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nupdated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n##### \u5378\u8f7d\u865a\u62df\u76d8\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmVolumeElfStoragePolicyType, Bus, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.remove_vm_disk({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"disk_ids\": [\"vm_disk_id_1\", \"vm_disk_id_2\"]\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nupdated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n#### \u7f51\u5361\u64cd\u4f5c\n\n##### \u6dfb\u52a0\u7f51\u5361\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi, VmNicModel\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.add_vm_nic({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"vm_nics\": [\n            {\n                \"connect_vlan_id\": \"vlan_id\",\n                \"enabled\": False,\n                \"model\": VmNicModel.VIRTIO,\n            },\n            {\n                \"connect_vlan_id\": \"vlan_id_2\",\n                \"enabled\": True,\n                \"mirror\": True,\n                \"model\": VmNicModel.VIRTIO,\n            }\n        ]\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\nupdated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n##### \u7f16\u8f91\u7f51\u5361\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.update_vm_nic({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"nic_index\": 0,\n        \"enabled\": False,\n        \"mirror\": False,\n        \"connect_vlan_id\": \"vlan_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\nupdated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n##### \u79fb\u9664\u7f51\u5361\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.remove_vm_nic({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"nic_index\": [0, 1]\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\nupdated_vm = vm_api.get_vms({\n    \"where\": {\n        \"id\": with_task_vm.data.id\n    }\n})\n```\n\n#### \u865a\u62df\u673a\u8fc1\u79fb\n\n##### \u8fc1\u79fb\u81f3\u6307\u5b9a\u4e3b\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.mig_rate_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"host_id\": \"host_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n```\n\n##### \u81ea\u52a8\u8c03\u5ea6\u5230\u5408\u9002\u7684\u4e3b\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\n\nwith_task_vm = vm_api.mig_rate_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n```\n\n### \u865a\u62df\u673a\u7535\u6e90\u64cd\u4f5c\n\n#### \u865a\u62df\u673a\u5f00\u673a:\n\n##### \u6307\u5b9a\u865a\u62df\u673a\u5f00\u673a\uff0c\u81ea\u52a8\u8c03\u5ea6\u5230\u5408\u9002\u7684\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.start_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nopened_vm = vm_api.get_vms({\"where\": {\"id\": with_task_vm.data.id}})[0]\n```\n\n##### \u6279\u91cf\u865a\u62df\u673a\u5f00\u673a\uff0c\u81ea\u52a8\u8c03\u5ea6\u5230\u5408\u9002\u7684\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\nwith_task_vms = vm_api.start_vm({\n    \"where\": {\n        \"id_in\": [\"vm_id_1\", \"vm_id_2\"]\n    }\n})\n\ntasks = [with_task_vm.task_id for with_task_vm in with_task_vms]\nids = [with_task_vm.data.id for with_task_vm in with_task_vms]\nwait_tasks(tasks, api_client)\n\nopened_vms = vm_api.get_vms({\"where\": {\"id_in\": ids}})\n```\n\n##### \u5f00\u673a\u81f3\u6307\u5b9a\u4e3b\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.start_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    },\n    \"data\": {\n        \"host_id\": \"host_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nopened_vm = vm_api.get_vms({\"where\": {\"id\": with_task_vm.data.id}})[0]\n```\n\n#### \u865a\u62df\u673a\u5173\u673a\n\n##### \u6307\u5b9a\u865a\u62df\u673a\u5173\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.shut_down_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nclosed_vm = vm_api.get_vms({\"where\": {\"id\": with_task_vm.data.id}})[0]\n```\n\n##### \u6279\u91cf\u865a\u62df\u673a\u5173\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\nwith_task_vms = vm_api.shut_down_vm({\n    \"where\": {\n        \"id_in\": [\"vm_id_1\", \"vm_id_2\"]\n    }\n})\n\ntasks = [with_task_vm.task_id for with_task_vm in with_task_vms]\nids = [with_task_vm.data.id for with_task_vm in with_task_vms]\n\nwait_tasks(tasks, api_client)\n\nclosed_vms = vm_api.get_vms({\"where\": {\"id_in\": ids}})\n```\n\n##### \u5f3a\u5236\u5173\u673a\u6307\u5b9a\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.force_shut_down_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nclosed_vm = vm_api.get_vms({\"where\": {\"id\": with_task_vm.data.id}})[0]\n```\n\n##### \u5f3a\u5236\u5173\u673a\u6279\u91cf\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\nconf = Configuration(host=\"http://192.168.96.133/v2/api\")\nconf.api_key[\"Authorization\"] = \"token\"\napi_client = ApiClient(conf)\n\nvm_api = VmApi(api_client)\nwith_task_vms = vm_api.force_shut_down_vm({\n    \"where\": {\n        \"id_in\": [\"vm_id_1\", \"vm_id_2\"]\n    }\n})\n\ntasks = [with_task_vm.task_id for with_task_vm in with_task_vms]\nids = [with_task_vm.data.id for with_task_vm in with_task_vms]\nwait_tasks(tasks, api_client)\n\nclosed_vms = vm_api.get_vms({\"where\": {\"id_in\": ids}})\n```\n\n#### \u865a\u62df\u673a\u91cd\u542f\n\n##### \u91cd\u542f\u6307\u5b9a\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.restart_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nrestarted_vm = vm_api.get_vms({\"where\": {\"id\": with_task_vm.data.id}})[0]\n```\n\n##### \u91cd\u542f\u6279\u91cf\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_vms = vm_api.restart_vm({\n    \"where\": {\n        \"id_in\": [\"vm_id_1\", \"vm_id_2\"]\n    }\n})\n\ntasks = [with_task_vm.task_id for with_task_vm in with_task_vms]\nids = [with_task_vm.data.id for with_task_vm in with_task_vms]\n\nwait_tasks(tasks, api_client)\n\nrestarted_vms = vm_api.get_vms({\"where\": {\"id_in\": ids}})\n```\n\n##### \u5f3a\u5236\u91cd\u542f\u6307\u5b9a\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.force_restart_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nrestarted_vm = vm_api.get_vms({\"where\": {\"id\": with_task_vm.data.id}})[0]\n```\n\n##### \u5f3a\u5236\u91cd\u542f\u6279\u91cf\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_vms = vm_api.force_restart_vm({\n    \"where\": {\n        \"id_in\": [\"vm_id_1\", \"vm_id_2\"]\n    }\n})\n\ntasks = [with_task_vm.task_id for with_task_vm in with_task_vms]\nids = [with_task_vm.data.id for with_task_vm in with_task_vms]\n\nwait_tasks(tasks, api_client)\n\nrestarted_vms = vm_api.get_vms({\"where\": {\"id_in\": ids}})\n```\n\n#### \u865a\u62df\u673a\u6682\u505c\n\n##### \u6682\u505c\u6307\u5b9a\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.suspend_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nsuspended_vm = vm_api.get_vms({\"where\": {\"id\": with_task_vm.data.id}})[0]\n```\n\n##### \u6682\u505c\u6279\u91cf\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_vms = vm_api.suspend_vm({\n    \"where\": {\n        \"id_in\": [\"vm_id_1\", \"vm_id_2\"]\n    }\n})\n\ntasks = [with_task_vm.task_id for with_task_vm in with_task_vms]\nids = [with_task_vm.data.id for with_task_vm in with_task_vms]\n\nwait_tasks(tasks, api_client)\n\nsuspended_vms = vm_api.get_vms({\"where\": {\"id_in\": ids}})\n```\n\n#### \u865a\u62df\u673a\u6062\u590d\n\n##### \u6062\u590d\u6307\u5b9a\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_vm = vm_api.resume_vm({\n    \"where\": {\n        \"id\": \"vm_id\"\n    }\n})[0]\n\nwait_tasks([with_task_vm.task_id], api_client)\n\nresumed_vm = vm_api.get_vms({\"where\": {\"id\": with_task_vm.data.id}})[0]\n```\n\n##### \u6062\u590d\u6279\u91cf\u865a\u62df\u673a\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_vms = vm_api.resume_vm({\n    \"where\": {\n        \"id_in\": [\"vm_id_1\", \"vm_id_2\"]\n    }\n})\n\ntasks = [with_task_vm.task_id for with_task_vm in with_task_vms]\nids = [with_task_vm.data.id for with_task_vm in with_task_vms]\n\nwait_tasks(tasks, api_client)\n\nresumed_vms = vm_api.get_vms({\"where\": {\"id_in\": ids}})\n```\n\n### \u5220\u9664\u865a\u62df\u673a\n\n#### \u56de\u6536\u7ad9\n\n##### \u79fb\u5165\u56de\u6536\u7ad9\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_delete_vms = vm_api.move_vm_to_recycle_bin({\n    \"where\": {\n        \"id_in\": [\"vm_id_1\", \"vm_id_2\"]\n    }\n})\n\ntasks = [with_task_delete_vm.task_id for with_task_delete_vm in with_task_delete_vms]\nids = [with_task_vm.data.id for with_task_vm in with_task_vms]\n\nwait_tasks(tasks, api_client)\n\nvm_moved_to_recycle_bin = vm_api.get_vms({\"where\": {\"id_in\": ids}})\n```\n\n##### \u4ece\u56de\u6536\u7ad9\u6062\u590d\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_delete_vms = vm_api.recover_vm_from_recycle_bin({\n    \"where\": {\n        \"id_in\": [\"vm_id_1\", \"vm_id_2\"]\n    }\n})\n\ntasks = [with_task_delete_vm.task_id for with_task_delete_vm in with_task_delete_vms]\nids = [with_task_vm.data.id for with_task_vm in with_task_vms]\n\nwait_tasks(tasks, api_client)\n\nrecovered_vms = vm_api.get_vms({\"where\": {\"id_in\": ids}})\n```\n\n#### \u6c38\u4e45\u5220\u9664\n\n```python\nfrom cloudtower import ApiClient, Configuration, VmApi\nfrom cloudtower.utils import wait_tasks\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nvm_api = VmApi(api_client)\nwith_task_delete_vms = vm_api.delete_vm({\n    \"where\": {\n        \"id_in\": [\"vm_id_1\", \"vm_id_2\"]\n    }\n})\n\ntasks = [with_task_delete_vm.task_id for with_task_delete_vm in with_task_delete_vms]\n\nwait_tasks(tasks, api_client)\n```\n\n## \u573a\u666f\u793a\u4f8b\n\n### \u865a\u62df\u673a\u5907\u4efd\n\n```python\nfrom cloudtower import ApiClient\nfrom cloudtower.api.vm_api import VmApi\nfrom cloudtower.api.vm_snapshot_api import VmSnapshotApi\nfrom cloudtower.api.iscsi_lun_snapshot_api import IscsiLunSnapshotApi\nfrom cloudtower.models import (\n    ConsistentType,\n    VmToolsStatus\n)\nfrom cloudtower.utils import wait_tasks\n\n\ndef create_vm_snapshot(\n    api_client: ApiClient,\n    target_vm_name: str,\n    target_snapshot_name: str,\n    consistent_type: ConsistentType\n):\n    vm_api = VmApi(api_client)\n    vm_snapshot_api = VmSnapshotApi(api_client)\n    iscsi_lun_snapshot_api = IscsiLunSnapshotApi(api_client)\n    # 1. \u83b7\u53d6\u6240\u9700\u5907\u4efd\u7684\u865a\u62df\u673a\u7684\u4fe1\u606f\uff0c\u8fd9\u91cc\u6211\u4eec\u9700\u8981vm\u7684id\u6765\u6784\u5efa\u521b\u5efasnapshot\u7684\u53c2\u6570\n    vm = vm_api.get_vms({\n        \"where\": {\n            \"name\": target_vm_name\n        },\n        \"first\": 1\n    })\n    # vm \u5df2\u5b89\u88c5\u5e76\u542f\u52a8 VMTools \u65f6\uff0cconsistent_type \u53ef\u4ee5\u4f7f\u7528 FILE_SYSTEM_CONSISTENT \u4ee3\u8868\u6587\u4ef6\u7cfb\u7edf\u4e00\u81f4\u6027\u5feb\u7167\n    if vm.vm_tools_status != VmToolsStatus.RUNNING and consistent_type == ConsistentType.FILE_SYSTEM_CONSISTENT:\n        consistent_type = ConsistentType.CRASH_CONSISTENT\n\n    # 2. \u521b\u5efa\u865a\u62df\u673a\u5feb\u7167\n    snapshots_with_task = vm_snapshot_api.create_vm_snapshot({\n        \"data\": [\n            {\n                \"vm_id\": vm.id,\n                \"name\": target_snapshot_name,\n                \"consistent_type\": consistent_type\n            }\n        ]\n    })\n\n    # 3. \u7b49\u5f85Task\u5b8c\u6210\n    wait_tasks([snapshots_with_task[0].task_id], api_client)\n\n    # 4. \u6839\u636e\u8fd4\u56de\u7684id\u67e5\u8be2\u751f\u6210\u7684\u865a\u62df\u673a\u5feb\u7167\n    snapshot = vm_snapshot_api.get_vm_snapshots({\n        \"where\": {\n            \"id\": snapshots_with_task.data.id\n        }\n    })[0]\n    # 5. \u6839\u636e\u8fd4\u56de\u7684snapshot\u4e2d\u7684vm_disks\u5305\u542b\u4e86\u5feb\u7167\u7684\u865a\u62df\u76d8\u4fe1\u606f\n    # type \u4e3a DISK \u8868\u793a\u5bf9\u5e94\u4e00\u4e2a\u5377\uff0c\u5176\u4e2d\u4f1a\u5305\u542b\u4e00\u4e2a snapshot_local_id \u5219\u8868\u793a\u8be5\u865a\u62df\u5377\u5bf9\u5e94\u7684lun\u5feb\u7167\u7684 local_id\n    # type \u4e3a CD-ROM\u5219\u4ee3\u8868\u4e3a\u88ab\u6302\u8f7d\u7684CD-ROM\uff0c\u4e0d\u4f1a\u4ea7\u751flun\u5feb\u7167\n    lun_snapshot_ids = []\n    for disk in snapshot.vm_disks:\n        if disk.type == \"DISK\":\n            lun_snapshot_ids.append(disk.snapshot_local_id)\n\n    lun_snapshots = iscsi_lun_snapshot_api.get_iscsi_lun_snapshots({\n        \"where\": {\n            \"name_in\": lun_snapshot_ids\n        }\n    })\n\n    return {\n        \"vm_snapshot\": snapshot,\n        \"lun_snapshots\": lun_snapshots\n    }\n\n```\n\n### Dashboard \u6784\u5efa\n\n#### \u5b9a\u4e49\u5de5\u5177\u65b9\u6cd5\n\n```python\nfrom functools import reduce\nfrom datetime import datetime, timedelta\nfrom cloudtower import ApiClient\nfrom cloudtower.configuration import Configuration\nfrom cloudtower.models import SeverityEnum, ClusterType, Hypervisor, DiskType, DiskUsageStatus, DiskHealthStatus\nfrom cloudtower.api import VmApi, ClusterApi, AlertApi, HostApi, DiskApi, ClusterSettingsApi, GlobalSettingsApi\n\napi_client = ApiClient(Configuration(host=\"http://192.168.96.133/v2/api\"))\n\nbyte_units = [\"B\", \"KiB\", \"MiB\", \"GiB\", \"TiB\", \"PiB\"]\nhz_units = [\"Hz\", \"KHz\", \"MHz\", \"GHz\", \"THz\"]\n\n\ndef format_unit(base: int, units, step=1024):\n    if not len(units):\n        raise Exception(\"no unit provided\")\n    if base <= 0:\n        return \"0\" + units[0]\n    for unit in units:\n        if base < step:\n            return \"{:.2f}{}\".format(base, unit)\n        base /= step\n    return \"{:.2f}{}\".format(base, units[-1])\n```\n\n#### \u6784\u5efa\u62a5\u8b66\u4fe1\u606f\n\n```python\ndef build_alerts(api_client: ApiClient, cluster_ids):\n    alert_api = AlertApi(api_client)\n    alerts = alert_api.get_alerts({\n        \"where\": {\n            \"ended\": False,\n            \"cluster\": {\n                \"id_in\": cluster_ids\n            },\n        }\n    })\n    critial_alerts = [\n        alert for alert in alerts if alert.severity == SeverityEnum.CRITICAL]\n    notice_alerts = [\n        alert for alert in alerts if alert.severity == SeverityEnum.NOTICE]\n    info_alerts = [\n        alert for alert in alerts if alert.severity == SeverityEnum.INFO]\n    return {\n        \"critical\": critial_alerts,\n        \"notice\": notice_alerts,\n        \"info\": info_alerts\n    }\n```\n\n#### \u6784\u5efa\u786c\u76d8\u4fe1\u606f\n\n> \u8fd9\u91cc\u4ee5\u673a\u68b0\u786c\u76d8\u4e3a\u4f8b\n\n```python\ndef build_hdd_info(api_client: ApiClient, cluster_ids):\n    disk_api = DiskApi(api_client)\n    disks = disk_api.get_disks({\n        \"where\": {\n            \"host\": {\n                \"cluster\": {\n                    \"id_in\": cluster_ids\n                }\n            }\n        }\n    })\n    hdd = {\n        \"healthy\": 0,\n        \"warning\": 0,\n        \"error\": 0,\n        \"total\": 0,\n    }\n    for disk in disks:\n        if disk.type == DiskType.HDD:\n            if disk.health_status in [DiskHealthStatus.UNHEALTHY, DiskHealthStatus.SUBHEALTHY, DiskHealthStatus.SMART_FAILED]:\n                hdd['error'] += 1\n            elif disk.usage_status in [DiskUsageStatus.UNMOUNTED, DiskUsageStatus.PARTIAL_MOUNTED]:\n                hdd['warning'] += 1\n            else:\n                hdd['healthy'] += 1\n            hdd['total'] += 1\n    return hdd\n```\n\n#### \u6784\u5efa\u6027\u80fd\u6307\u6807\n\n> \u83b7\u53d6\u6307\u5b9a\u96c6\u7fa4\u7684 CPU \u6838\u6570\uff0cCPU \u9891\u7387\u603b\u6570\uff0cCPU \u4f7f\u7528\u7387\uff0c\u5185\u5b58\u603b\u91cf\uff0c\u5185\u5b58\u4f7f\u7528\u91cf\uff0c\u5b58\u50a8\u8d44\u6e90\u603b\u91cf\uff0c\u5b58\u50a8\u8d44\u6e90\u5df2\u4f7f\u7528\u91cf\uff0c\u5b58\u50a8\u8d44\u6e90\u5931\u6548\u91cf\u4e0e\u5b58\u50a8\u8d44\u6e90\u53ef\u7528\u91cf\u3002\n\n```python\ndef build_metrics(api_client: ApiClient, clusters, cluster_ids):\n    result = {}\n    host_api = HostApi(api_client)\n    hosts = host_api.get_hosts({\n        \"where\": {\n            \"cluster\": {\n                \"id_in\": cluster_ids\n            }\n        }\n    })\n    cpu = {\n        \"total_cpu_cores\": 0,\n        \"total_cpu_hz\": 0,\n        \"used_cpu_hz\": 0,\n    }\n    memory = {\n        \"total_memory\": 0,\n        \"used_memory\": 0,\n    }\n    storage = {\n        \"total\": 0,\n        \"used\": 0,\n        \"invalid\": 0,\n        \"available\": 0\n    }\n\n    for host in hosts:\n        cluster = next(\n            cluster for cluster in clusters if cluster.id == host.cluster.id)\n        if cluster.hypervisor == Hypervisor.ELF:\n            memory['total_memory'] += 0 if host.total_memory_bytes is None else host.total_memory_bytes\n            memory['used_memory'] += (0 if host.running_pause_vm_memory_bytes is None else host.running_pause_vm_memory_bytes) + \\\n                (0 if host.os_memory_bytes is None else host.os_memory_bytes)\n\n    for cluster in clusters:\n        if cluster.type == ClusterType.SMTX_OS:\n            cpu[\"total_cpu_cores\"] += 0 if cluster.total_cpu_cores is None else cluster.total_cpu_cores\n            cpu[\"total_cpu_hz\"] += 0 if cluster.total_cpu_hz is None else cluster.total_cpu_hz\n            cpu[\"used_cpu_hz\"] += 0 if cluster.used_cpu_hz is None else cluster.used_cpu_hz\n            if cluster.hypervisor == Hypervisor.VMWARE:\n                memory[\"total_memory\"] += 0 if cluster.total_memory_bytes is None else cluster.total_memory_bytes\n                memory[\"used_memory\"] += 0 if cluster.used_memory_bytes is None else cluster.used_memory_bytes\n        storage[\"total\"] += 0 if cluster.total_data_capacity is None else cluster.total_data_capacity\n        storage[\"used\"] += 0 if cluster.used_data_space is None else cluster.used_data_space\n        storage[\"invalid\"] += 0 if cluster.failure_data_space is None else cluster.failure_data_space\n    if len([cluster for cluster in clusters if cluster.type != ClusterType.SMTX_ZBS]) > 1:\n        cpu[\"cpu_usage\"] = \"{:.2f}%\".format(\n            cpu[\"used_cpu_hz\"] / cpu[\"total_cpu_hz\"])\n        cpu[\"total_cpu_hz\"] = format_unit(cpu[\"total_cpu_hz\"], hz_units, 1000)\n        cpu[\"used_cpu_hz\"] = format_unit(cpu[\"used_cpu_hz\"], hz_units, 1000)\n        result['cpu'] = cpu\n        memory[\"memory_usage\"] = \"{:.2f}%\".format(\n            memory[\"used_memory\"] / memory[\"total_memory\"])\n        memory[\"total_memory\"] = format_unit(\n            memory[\"total_memory\"], byte_units)\n        memory[\"used_memory\"] = format_unit(\n            memory[\"used_memory\"], byte_units)\n        result[\"memory\"] = memory\n    storage[\"available\"] = format_unit(\n        storage[\"total\"] - storage[\"used\"] - storage[\"invalid\"], byte_units)\n    storage[\"total\"] = format_unit(storage[\"total\"], byte_units)\n    storage[\"used\"] = format_unit(storage[\"used\"], byte_units)\n    storage[\"invalid\"] = format_unit(storage[\"invalid\"], byte_units)\n    result[\"storage\"] = storage\n    return result\n```\n\n#### \u6784\u5efa Dashboard\n\n```python\ndef build_dashboard(api_client: ApiClient, datacenter_id: str = None, cluster_id: str = None):\n    result = {}\n    cluster_api = ClusterApi(api_client)\n    clusters = cluster_api.get_clusters({\n        \"where\": {\"id\": cluster_id} if cluster_id is not None else {\"datacenters_some\": {\"id\": datacenter_id}} if datacenter_id is not None else None\n    })\n    cluster_ids = [cluster.id for cluster in clusters]\n\n    result[\"alerts\"] = build_alerts(api_client, cluster_ids)\n    result[\"hdd\"] = build_hdd_info(api_client, cluster_ids)\n    metric = build_metrics(api_client, clusters, cluster_ids)\n    if \"cpu\" in metric:\n        result[\"cpu\"] = metric[\"cpu\"]\n    if \"memory\" in metric:\n        result[\"memory\"] = metric[\"memory\"]\n    if \"storage\" in metric:\n        result[\"storage\"] = metric[\"storage\"]\n    return result\n```\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "CloudTower APIs",
    "version": "2.15.0",
    "project_urls": {
        "Homepage": "https://github.com/smartxworks/cloudtower-python-sdk"
    },
    "split_keywords": [
        "openapi",
        " openapi-generator",
        " cloudtower apis"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "30556fb0b1e186111b44694f83b516c79253996ed8ff02997a21b16dfedc9eba",
                "md5": "2933234352709d5b3b9e14001ac3cab6",
                "sha256": "0665fa8f7beaaf70bac4286cb9dcf65b5cc520751e371ce27690a58c640d79ec"
            },
            "downloads": -1,
            "filename": "cloudtower_sdk-2.15.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2933234352709d5b3b9e14001ac3cab6",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 3518063,
            "upload_time": "2024-05-11T10:14:01",
            "upload_time_iso_8601": "2024-05-11T10:14:01.103665Z",
            "url": "https://files.pythonhosted.org/packages/30/55/6fb0b1e186111b44694f83b516c79253996ed8ff02997a21b16dfedc9eba/cloudtower_sdk-2.15.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5de64b3341a5b03a7c88d115e3e45ae56e8cfd1626a4c712908891fd564d5760",
                "md5": "e33cb5a1e109cc88b112bde15c2d044d",
                "sha256": "2a3ef7776c025ede055ffa0bb807e8710b14f9f0884a76dd25a8a0b167330c75"
            },
            "downloads": -1,
            "filename": "cloudtower-sdk-2.15.0.tar.gz",
            "has_sig": false,
            "md5_digest": "e33cb5a1e109cc88b112bde15c2d044d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 1618061,
            "upload_time": "2024-05-11T10:14:05",
            "upload_time_iso_8601": "2024-05-11T10:14:05.280478Z",
            "url": "https://files.pythonhosted.org/packages/5d/e6/4b3341a5b03a7c88d115e3e45ae56e8cfd1626a4c712908891fd564d5760/cloudtower-sdk-2.15.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-11 10:14:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "smartxworks",
    "github_project": "cloudtower-python-sdk",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "future",
            "specs": []
        },
        {
            "name": "six",
            "specs": [
                [
                    ">=",
                    "1.10"
                ]
            ]
        },
        {
            "name": "python_dateutil",
            "specs": [
                [
                    ">=",
                    "2.5.3"
                ]
            ]
        },
        {
            "name": "setuptools",
            "specs": [
                [
                    ">=",
                    "21.0.0"
                ]
            ]
        },
        {
            "name": "urllib3",
            "specs": [
                [
                    ">=",
                    "1.25.3"
                ]
            ]
        }
    ],
    "tox": true,
    "lcname": "cloudtower-sdk"
}
        
Elapsed time: 0.24410s