你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Python API

CycleCloud Python API 允许你与 CycleCloud REST API 进行交互,而无需手动处理 HTTP 请求。 若要获取 API 源分发,请转到 CycleCloud 安装中的 /about ,然后选择 “下载 Python API ”链接。 下载源分发后,使用 pip install 将其添加到 Python 环境。

客户端对象

可以使用指定的配置或不使用指定配置创建 Client 对象。 如果未指定配置字典,客户端对象会自动尝试从默认 CycleCloud CLI ini 文件(~/.cycle/config.ini)获取配置。

以包含下列键/值对的形式提供配置:

  • url - 必需,即 CycleCloud 安装的 Web 界面的 URL
  • username - 必填
  • password - 必需,用户的纯文本密码
  • timeout - 尝试连接或与系统通信时超时前的秒数(默认情况下为 60)
  • verify_certificates - 一个布尔值,显示是否应启用证书检查(默认情况下为 True)

还可以将这些值作为关键字参数提供给构造函数。

from cyclecloud.client import Client

# configuration read from ~/.cycle/config.ini
cl1 = Client() 

# config provided as dictionary
config = {"url": "http://127.0.0.1:8443",
          "username": "admin",
          "password": "password",
          "timeout": 60,
          "verify_certificates": False}
cl2 = Client(config)

# config provided as keyword arguments
cl3 = Client(url="http://127.0.0.1:8443", username="admin", password="password")

客户端属性

  • session - Session 对象 - 仅用于调用 直接 API

  • clusters - 系统中的群集对象映射表,以群集名称为键

群集对象

群集对象允许你控制 CycleCloud 安装中的特定群集。

群集属性

  • name - 对象引用的群集的名称

  • nodes - 可以循环访问包含群集节点记录的列表

群集函数

  • get_status(nodes=False) - 获取群集的 Cluster Status 对象。 可以选择包含节点列表。

  • scale_by_cores(node_array, total_core_count) - 将系统设置为将指定的节点数组调整为目标总核心数。 如果节点数组已包含多个 total_core_count 核心,则调用不起作用。

  • scale_by_nodes(node_array, total_node_count) - 将系统设置为将指定的节点数组缩放为所需的总节点计数。 如果节点数组已包含多个 total_node_count 节点,则调用不起作用。

直接 API

可以通过使用位于 cyclecloud.apicyclecloud.model 的 API 更直接地访问 REST API,这些 API 是直接从 REST API 生成的。 若要使用此 API,请创建一个 Client 对象,并使用它提供的属性进行调用 session

from cyclecloud.client import Client
from cyclecloud.api import clusters

cl1 = Client()

# prints the current state of the cluster
response_status, cluster_status = clusters.get_cluster_status(cl1.session, "test-cluster-1", nodes=False)
print(cluster_status.state)