ACS::CheckFor

用法

该动作用来检查云产品资源是否符合预期状态,如是预期状态,则任务执行成功。否则,任务执行失败。通常用来做前置检查。如要停止某ECS实例,则可以先检查该ECS实例的状态是运行中状态(Running),如果不是Running,则不执行停止操作。

语法

此语法格式除了TypeACS::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 }}"
        }
    }
}