用法
該動作用來檢查雲產品資源是否符合預期狀態,如是預期狀態,則任務執行成功。否則,任務執行失敗。通常用來做前置檢查。如要停止某ECS執行個體,則可以先檢查該ECS執行個體的狀態是運行中狀態(Running),如果不是Running,則不執行停止操作。
文法
此文法格式除了Type
為ACS::CheckFor
以外的所有欄位和ACS::WaitFor
一致,請參考ACS::WaitFor文法和說明。
Tasks:
- Name: checkForTaskExample
Action: ACS::CheckFor
Properties:
# 以下屬性和ACS::ExecuteAPI一致,請參考ACS::ExecuteAPI文法說明。
Service: ECS
API: DescribeInstances
Parameters:
InstanceId: i-12345678abcedfg
# 以下屬性為ACS::CheckFor的特殊屬性
PropertySelector: "jq selector" # 必填,需要等待的具體的API傳回值,參考ACS::ExecuteAPI中的ValueSelector說明
DesiredValues: # DesiredValue,NotDesiredValues 必填其一,指上面截取的值要匹配到以下值之一,匹配則表示成功,否則會逾時然後失敗。
- value1
- value2
NotDesiredValues: # DesiredValue,NotDesiredValues 必填其一,指上面截取的值要不匹配到以下的所有值,全都不匹配則表示成功,否則會逾時然後失敗。
- value3
- value4
Outputs: # CheckFor動作所在的任務支援輸出,當任務滿足NotDesiredValues/DesiredValues的條件時,任務執行成功後會返回輸出參數的值。
OutputParameter1:
ValueSelector: 'jq selector' # jq的選取器文法,以OpenAPI的返回作為JSON輸入,jq的文法請參考 https://stedolan.github.io/jq/
Type: String/Boolean/List(Array)/Number/Object
{
"Tasks": [
{
"Name": "checkForTaskExample",
"Action": "ACS::CheckFor",
"Properties": {
"Service": "ECS",
"API": "DescribeInstances",
"Parameters": {
"InstanceId": "i-12345678abcedfg"
},
"PropertySelector": "jq selector",
"DesiredValues": [
"value1",
"value2"
],
"NotDesiredValues": [
"value3",
"value4"
]
},
"Outputs": {
"OutputParameter1": {
"ValueSelector": "jq selector",
"Type": "String/Boolean/List(Array)/Number/Object"
}
}
}
]
}
樣本
如下模板:為指定磁碟建立快照。用CheckFor檢查特定執行個體是否處於Running或Stopped狀態。
FormatVersion: OOS-2019-06-01
Parameters:
DiskId:
Description: the disk id of the snap shot to copy
Type: String
InstanceId:
Description: the instances id of the snap shot to copy
Type: String
SnapshotName:
Description: snap shot name of the new snap
Type: String
Tasks:
- Name: checkInstanceReady
Action: ACS::CheckFor
Properties:
Service: ECS
API: DescribeInstances
DesiredValues:
- Running
- Stopped
Parameters:
InstanceIds:
- '{{ InstanceId }} '
PropertySelector: Instances.Instance[].Status
- Name: createSnapshot
Action: ACS::ExecuteAPI
Properties:
Service: ECS
API: CreateSnapshot
Parameters:
DiskId: '{{ DiskId }}'
SnapshotName: '{{ SnapshotName }}'
Outputs:
SnapshotId:
Type: String
ValueSelector: SnapshotId
- Name: waitSnapshotsReady
Action: ACS::WaitFor
Properties:
Service: ECS
API: DescribeSnapshots
DesiredValues:
- accomplished
Parameters:
SnapshotIds:
- '{{ createSnapshot.SnapshotId }}'
PropertySelector: Snapshots.Snapshot[].Status
Outputs:
SnapshotId:
Type: String
Value: '{{ createSnapshot.SnapshotId }}'
{
"FormatVersion": "OOS-2019-06-01",
"Parameters": {
"DiskId": {
"Description": "the disk id of the snap shot to copy ",
"Type": "String"
},
"InstanceId": {
"Description": "the instances id of the snap shot to copy ",
"Type": "String"
},
"SnapshotName": {
"Description": "snap shot name of the new snap",
"Type": "String"
}
},
"Tasks": [
{
"Name": "checkInstanceReady",
"Action": "ACS::CheckFor",
"Properties": {
"Service": "ECS",
"API": "DescribeInstances",
"DesiredValues": ["Running", "Stopped"],
"Parameters": {
"InstanceIds": ["{{ InstanceId }} "]
},
"PropertySelector": "Instances.Instance[].Status"
}
},
{
"Name": "createSnapshot",
"Action": "ACS::ExecuteAPI",
"Properties": {
"Service": "ECS",
"API": "CreateSnapshot",
"Parameters": {
"DiskId": "{{ DiskId }}",
"SnapshotName": "{{ SnapshotName }}"
}
},
"Outputs": {
"SnapshotId": {
"Type": "String",
"ValueSelector": "SnapshotId"
}
}
},
{
"Name": "waitSnapshotsReady",
"Action": "ACS::WaitFor",
"Properties": {
"Service": "ECS",
"API": "DescribeSnapshots",
"DesiredValues": ["accomplished"],
"Parameters": {
"SnapshotIds": ["{{ createSnapshot.SnapshotId }}"]
},
"PropertySelector": "Snapshots.Snapshot[].Status"
}
}
],
"Outputs": {
"SnapshotId": {
"Type": "String",
"Value": "{{ createSnapshot.SnapshotId }}"
}
}
}