事件订阅(Event Subscription)

订阅事件 (SubscribeEvent)

API请求

URLs
POST zstack/v1/zwatch/events/subscriptions
Headers
Authorization: OAuth the-session-uuid
Body
{
  "params": {
    "namespace": "ZStack/VM",
    "eventName": "VMUuid",
    "actions": [
      {
        "actionUuid": "2fbeae18082445d09c890fa076ff9fd9",
        "actionType": "sns"
      }
    ],
    "labels": [
      {
        "key": "VMUuid",
        "value": "0c60949d81964f23a0d860b30472c51b",
        "op": "Equal"
      }
    ]
  },
  "systemTags": [],
  "userTags": []
}
Note: 上述示例中systemTags、userTags字段可以省略。列出是为了表示body中可以包含这两个字段。
Curl示例
curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X POST -d '{"params":{"namespace":"ZStack/VM","eventName":"VMUuid","actions":[{"actionUuid":"c7b1b7e054494aeab843a9c7371cdfc4","actionType":"sns"}],"labels":[{"key":"VMUuid","value":"47056270f2ea45409a419f1165f83945","op":"Equal"}]}}' \
http://localhost:8080/zstack/v1/zwatch/events/subscriptions
参数列表
名字 类型 位置 描述 可选值 起始版本
namespace String body(包含在params结构中) 名字空间 2.3
eventName String body(包含在params结构中) 事件名 2.3
actions (可选) List body(包含在params结构中) 事件动作 2.3
labels (可选) List body(包含在params结构中) 事件标签 2.3
resourceUuid (可选) String body(包含在params结构中) 2.3
systemTags (可选) List body 系统标签 2.3
userTags (可选) List body 用户标签 2.3
name (可选) String body(包含在params结构中) 资源名称 3.0.0
emergencyLevel (可选) String body(包含在params结构中) 报警等级
  • Emergent
  • Important
  • Normal
3.8.0
tagUuids (可选) List body(包含在params结构中) 标签UUID列表 3.8.0

API返回

该API成功时返回一个空的JSON结构{},出错时返回的JSON结构包含一个error字段,例如:
{
"error": {
    "code": "SYS.1001",
    "description": "A message or a operation timeout",
    "details": "Create VM on KVM timeout after 300s"
}
}

SDK示例

Java SDK
SubscribeEventAction action = new SubscribeEventAction();
action.namespace = "ZStack/VM";
action.eventName = "VMUuid";
action.actions = asList([actionUuid:08a502ad6e944ceb8c70f523b69385e3, actionType:sns]);
action.labels = asList([key:VMUuid, value:896391b873dc41288c0841b5fe8cb71b, op:Equal]);
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
SubscribeEventAction.Result res = action.call();
Python SDK
SubscribeEventAction action = SubscribeEventAction()
action.namespace = "ZStack/VM"
action.eventName = "VMUuid"
action.actions = [[actionUuid:393df28f2bab49aba01fc229dae849c8, actionType:sns]]
action.labels = [[key:VMUuid, value:abd2e73d08d248b6be73e31eb6a97e82, op:Equal]]
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c"
SubscribeEventAction.Result res = action.call()

退订事件(UnsubscribeEvent)

API请求

URLs
DELETE zstack/v1/zwatch/events/subscriptions/{uuid}
Headers
Authorization: OAuth the-session-uuid
Curl示例
curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X DELETE http://localhost:8080/zstack/v1/zwatch/events/subscriptions/298e2e2506163851a268907ed8ea7995
参数列表
名字 类型 位置 描述 可选值 起始版本
uuid String url 资源的UUID,唯一标示该资源 2.3
deleteMode (可选) String body 2.3
systemTags (可选) List body 系统标签 2.3
userTags (可选) List body 用户标签 2.3

API返回

该API成功时返回一个空的JSON结构{},出错时返回的JSON结构包含一个error字段,例如:
{
"error": {
    "code": "SYS.1001",
    "description": "A message or a operation timeout",
    "details": "Create VM on KVM timeout after 300s"
}
}

SDK示例

Java SDK
UnsubscribeEventAction action = new UnsubscribeEventAction();
action.uuid = "298e2e2506163851a268907ed8ea7995";
action.deleteMode = "Permissive";
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
UnsubscribeEventAction.Result res = action.call();
Python SDK
UnsubscribeEventAction action = UnsubscribeEventAction()
action.uuid = "298e2e2506163851a268907ed8ea7995"
action.deleteMode = "Permissive"
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c"
UnsubscribeEventAction.Result res = action.call()

查询事件订阅 (QueryEventSubscription)

API请求

URLs
GET zstack/v1/zwatch/events/subscriptions
GET zstack/v1/zwatch/events/subscriptions/{uuid}
Headers
Authorization: OAuth the-session-uuid
Curl示例
curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X GET http://localhost:8080/zstack/v1/zwatch/events/subscriptions?q=name=test
curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X GET http://localhost:8080/zstack/v1/zwatch/events/subscriptions/57618e9763ce3b7e990870c3a7ae8c53

可查询字段

运行CLI命令行工具,输入QueryEventSubscription并按Tab键查看所有可查询字段以及可跨表查询的资源名。

API返回

返回示例
{
  "inventories": [
    {
      "uuid": "9404fe697a6b4d7dbac98065af20dc57",
      "namespace": "ZStack/VM",
      "eventName": "org.zstack.zwatch.datatype.EventFamily@80723ca",
      "state": "Enabled",
      "actions": [
        {
          "subscriptionUuid": "650cf6a269284671a42f93631c87dfec",
          "actionType": "sns",
          "actionUuid": "fe296dcbc31c4afa9f76494b1930c8cc"
        }
      ],
      "labels": [
        {
          "uuid": "d29fe54ca47540a9b20b308429f21434",
          "key": "DestinationHostUuid",
          "operator": "Equal",
          "value": "733509c8c5684c678ffe99f59d4aba21"
        }
      ],
      "lastOpDate": "Jul 6, 2018 1:51:00 PM",
      "createDate": "Jul 6, 2018 1:51:00 PM"
    }
  ]
}
名字 类型 描述 起始版本
error ErrorCode 错误码,若不为null,则表示操作失败, 操作成功时该字段为null。 详情参考error 2.3.1
inventories List 详情参考inventories 2.3.1
#error
名字 类型 描述 起始版本
code String 错误码号,错误的全局唯一标识,例如SYS.1000, HOST.1001 2.3.1
description String 错误的概要描述 2.3.1
details String 错误的详细信息 2.3.1
elaboration String 保留字段,默认为null 2.3.1
opaque LinkedHashMap 保留字段,默认为null 2.3.1
cause ErrorCode 根错误,引发当前错误的源错误,若无原错误,该字段为null 2.3.1
#inventories
名字 类型 描述 起始版本
uuid String 资源的UUID,唯一标示该资源 2.3.1
name String 资源名称 3.0.0
namespace String 名字空间 2.3.1
eventName String 事件名 2.3.1
lastOpDate Timestamp 最后一次修改时间 2.3.1
createDate Timestamp 创建时间 2.3.1
state EventSubscriptionState 详情参考state 2.3.1
actions List 详情参考actions 2.3.1
labels List 详情参考labels 2.3.1
#state
名字 类型 描述 起始版本
name String 资源名称 2.3.1
ordinal int 2.3.1
#actions
名字 类型 描述 起始版本
subscriptionUuid String 事件订阅UUID 2.3.1
actionType String 动作类型 2.3.1
actionUuid String 动作UUID 2.3.1
#labels
名字 类型 描述 起始版本
uuid String 资源的UUID,唯一标示该资源 2.3.1
key String 标签名 2.3.1
value String 标签值 2.3.1
operator Operator 详情参考operator 2.3.1
#operator
名字 类型 描述 起始版本
op String 2.3.1
name String 资源名称 2.3.1
ordinal int 2.3.1

SDK示例

Java SDK
QueryEventSubscriptionAction action = new QueryEventSubscriptionAction();
action.conditions = asList("name=test");
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
QueryEventSubscriptionAction.Result res = action.call();
Python SDK
QueryEventSubscriptionAction action = QueryEventSubscriptionAction()
action.conditions = ["name=test"]
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c"
QueryEventSubscriptionAction.Result res = action.call()

更新事件订阅的标签(UpdateEventSubscriptionLabel)

API请求

URLs
PUT zstack/v1/zwatch/events/subscriptions/labels/{uuid}/actions
Headers
Authorization: OAuth the-session-uuid
Body
{
  "updateEventSubscriptionLabel": {
    "key": "VMUuid",
    "value": "42c233c17c6e4907b7be1f11a41f3a17",
    "operator": "Equal"
  },
  "systemTags": [],
  "userTags": []
}
Note: 上述示例中systemTags、userTags字段可以省略。列出是为了表示body中可以包含这两个字段。
Curl示例
curl -H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: OAuth b86c9016b4f24953a9edefb53ca0678c" \
-X PUT -d '{"updateEventSubscriptionLabel":{"key":"VMUuid","value":"228fc2c5041248df9b0d317f6f7a8585","operator":"Equal"}}' http://localhost:8080/zstack/v1/zwatch/events/subscriptions/labels/cfb859d412933a1da57ae99e2aa442ce/actions
参数列表
名字 类型 位置 描述 可选值 起始版本
uuid String url 原事件订阅标签的UUID,唯一标示该资源 3.9.0
key String body(包含在updateEventSubscriptionLabel结构中) 标签的新名 3.9.0
value String body(包含在updateEventSubscriptionLabel结构中) 标签的新值 3.9.0
operator String body(包含在updateEventSubscriptionLabel结构中) 标签的新操作符
  • Regex
  • Equal
3.9.0
systemTags (可选) List body 3.9.0
userTags (可选) List body 3.9.0

API返回

返回示例
{
  "inventory": {
    "uuid": "d937a1c6d9de3f2285b0cb23316624a8",
    "key": "VMUuid",
    "operator": "Equal",
    "value": "19cf33283b484ff6b73a6d98e1d762f5"
  }
}
名字 类型 描述 起始版本
error ErrorCode 错误码,若不为null,则表示操作失败, 操作成功时该字段为null。 详情参考error 3.9.0
inventory EventSubscriptionLabelInventory 详情参考inventory 3.9.0
#error
名字 类型 描述 起始版本
code String 错误码号,错误的全局唯一标识,例如SYS.1000, HOST.1001 3.9.0
description String 错误的概要描述 3.9.0
details String 错误的详细信息 3.9.0
elaboration String 保留字段,默认为null 3.9.0
opaque LinkedHashMap 保留字段,默认为null 3.9.0
cause ErrorCode 根错误,引发当前错误的源错误,若无原错误,该字段为null 3.9.0
#inventory
名字 类型 描述 起始版本
uuid String 资源的UUID,唯一标示该资源 3.9.0
key String 标签名 3.9.0
value String 标签值 3.9.0
operator Operator 详情参考operator 3.9.0
#operator
名字 类型 描述 起始版本
op String 3.9.0
name String 资源名称 3.9.0
ordinal int 3.9.0

SDK示例

Java SDK
UpdateEventSubscriptionLabelAction action = new UpdateEventSubscriptionLabelAction();
action.uuid = "cfb859d412933a1da57ae99e2aa442ce";
action.key = "VMUuid";
action.value = "9d7099dd361049aaaf801ca6b17b2858";
action.operator = "Equal";
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c";
UpdateEventSubscriptionLabelAction.Result res = action.call();
Python SDK
UpdateEventSubscriptionLabelAction action = UpdateEventSubscriptionLabelAction()
action.uuid = "cfb859d412933a1da57ae99e2aa442ce"
action.key = "VMUuid"
action.value = "27e59f9fe7ed46118a2f6b8a383ce494"
action.operator = "Equal"
action.sessionId = "b86c9016b4f24953a9edefb53ca0678c"
UpdateEventSubscriptionLabelAction.Result res = action.call()
开发手册 | ZStack ZSphere · ZVF | ZStack 资源中心