Updated on 2022-02-22 GMT+08:00

Condition Function

Condition functions are usually used to define whether elements need to be deployed, including cond_eq, cond_not, cond_and, cond_or, and cond_if. Except cond_if, other condition functions can only be used in the conditions section. The cond_if function can be used in the conditions, node_templates, and outputs sections.

For example, vm_deploy is used to determine whether to deploy a VM.

tosca_definitions_version: cloud_tosca_version_1_0
conditions:
  condition_vm_deploy:    #The conditions can be met only when inputs parameters are matched. 
    cond_eq:
      - get_input: vm_deploy
      - true
inputs:
  image:
    description: ID of the image used by the ECS
    type: Cloud.ECS.Image.Id
  instance:
    default: 1
    description: number of ECSs to be created
  subnet:
    description: ID of the subnet to which the ECS belongs
..vm_deploy:    #Determines whether to deploy the VM.
    default: true
    type: boolean
  vpc:
    description: ID of the VPC to which the ECS belongs
node_templates:
  vm:
    condition: condition_vm_deploy     # The VM will be deployed only when the conditions are met. 
    type: Cloud.ECS.CloudServer
    properties:
      availabilityZone: ae-ad-1a
      imageId:
        get_input: image
      flavor: s3.small.1
      instances:
        get_input: instance
      name: my-ecs
      nics:
        - subnetId:
            get_input: subnet
      rootVolume:
        size: 40
        volumeType: SATA
      vpcId:
        get_input: vpc
  myecs:
    type: Cloud.ECS.CloudServer
    properties:
      name: my-ecs
      instances:
        get_input: instance
      imageId:
        get_input: image
      flavor: s3.small.1
      vpcId:
        get_input: vpc
      availabilityZone: ae-ad-1a
      nics:
        - subnetId:
            get_input: subnet
      rootVolume:
        volumeType: SSD
        size: 40

cond_eq

The cond_eq function is used to determine whether an equal condition is met. It is generally used to determine whether an input parameter is consistent with an expected value.

Table 1 cond_eq

Syntax

Parameter Description

Return Value

cond_eq: [cond1, cond2]

  • cond1: Condition 1, which can be a number, string, Boolean value, or variable obtained using the get_input function.
  • cond2: Condition 2, which can be a number, string, Boolean value, or variable obtained using the get_input function.

When the value of cond1 is the same as that of cond2, true is returned; otherwise, false is returned.

The following describes how to use the cond_eq function to determine whether the input parameter is consistent with an expected value:

inputs:
  a:
    type: string
    default: 10
conditions:
  matchA:
    cond_eq: [{get_input: a}, 10]

cond_not

The cond_not function is used to reverse the calculation result and is usually nested with other condition functions.

Table 2 cond_not

Syntax

Parameter Description

Return Value

cond_not: cond

  • cond: Condition expression, which can be a Boolean value, Boolean variable obtained using the get_input function, or nested condition function such as cond_eq or cond_not.

If the calculation result of the condition expression is true, false is returned. If the result is false, true is returned.

The following describes how to use the cond_not function to determine whether the input parameter is consistent with an expected value:

inputs:
  a:
    type: boolean
    default: true
conditions:
  matchA:
    cond_not: {get_input: a}    

cond_and

The cond_and function is used to check whether multiple conditions are met. This function supports 2 to 10 conditions.

Table 3 cond_and

Syntax

Parameter Description

Return Value

cond_and: [cond1, cond2...condn]

  • cond1: Condition 1, which can be a Boolean value, Boolean variable obtained using the get_input function, or nested condition function such as cond_eq or cond_not.
  • cond2: Condition 2, which can be a Boolean value, Boolean variable obtained using the get_input function, or nested condition function such as cond_eq or cond_not.
  • condn: Condition n (3 ≤ n ≤ 10), which is optional and can be defined as required. The parameter type is the same as that of cond1 or cond2.

If all parameter conditions are met, true is returned; otherwise, false is returned.

The following describes how to use the cond_and function to check whether the combination conditions are met:

inputs:
  a:
    type: integer
    default: 10
  b:
    type: string
    default: debug
conditions:
  matchAnd:
    cond_and: [{cond_eq: [{get_input: a}, 10]}, {cond_eq: [{get_input: b}, debug]}]   # The condition of matchAnd can be met only when both conditions 1 and 2 are met.

cond_or

The cond_or function is used to determine whether any of multiple conditions is met. This function supports 2 to 10 conditions.

Table 4 cond_or

Syntax

Parameter Description

Return Value

cond_or: [cond1, cond2...condn]

  • cond1: Condition 1, which can be a Boolean value, Boolean variable obtained using the get_input function, or nested condition function such as cond_eq or cond_not.
  • cond2: Condition 2, which can be a Boolean value, Boolean variable obtained using the get_input function, or nested condition function such as cond_eq or cond_not.
  • condn: Condition n (3 ≤ n ≤ 10), which is optional and can be defined as required. The parameter type is the same as that of cond1 or cond2.

If any condition is met, true is returned. If no condition is met, false is returned.

The following describes how to use the cond_or function to check whether the combination conditions are met:

inputs:
  a:
    type: integer
    default: 10
  b:
    type: string
    default: debug
conditions:
  matchOr:
    cond_or: [{cond_eq: [{get_input: a}, 8]}, {cond_eq: [{get_input: b}, debug]}]  # The condition of matchOr can be met when either condition is met.

cond_if

The cond_if function is a triplet expression used to assign values to properties. It is generally used in the property structure of node_templates.

Table 5 cond_if

Syntax

Parameter Description

Return Value

cond_if: [condition, value_true, value_false]

  • condition: Condition name, which must be defined in the conditions section.
  • value_true: Value assigned when a condition is met.
  • value_false: Value assigned when a condition is not met.

If the condition is met, value_true is returned. If the condition is not met, value_false is returned.

The following describes how to use the cond_if function to define property values:

inputs:
  a:
    type: integer
    default: 10
  b:
    type: string
    default: debug
conditions:
  matchOr:
    cond_or: [{cond_eq: [{get_input: a}, 8]}, {cond_eq: [{get_input: b}, debug]}]  # The condition of matchOr can be met when either condition is met.
node_templates:
  vm:
    type: Cloud.ECS.CloudServer     
    properties:
      vpcId: vpc-id-123
      name: myvm
      nics:
        - subnetId: subnet-id-123
      imageId: {cond_if: [matchOr, image-debug, image-product]}   # cond_if is used to define a condition. If the debugging mode is used, debugging images are used; otherwise, product images are used.
      instances: 1
      availabilityZone: ae-ad-1a
      rootVolume:
        volumeType: SATA
        size: 40
      flavor: flavor-1