条件函数
条件函数通常用于定义元素是否需要部署,包括cond_eq、cond_not、cond_and、cond_or和cond_if。除了cond_if函数外,其他条件函数只允许在conditions段内定义使用。cond_if函数除了在conditions段内定义使用外,还可以在node_templates段和outputs段内定义使用。
例如,通过vm_deploy属性控制vm是否部署:
tosca_definitions_version: cloud_tosca_version_1_0
conditions:
condition_vm_deploy: #inputs参数满足匹配时,条件才能满足
cond_eq:
- get_input: vm_deploy
- true
inputs:
image:
description: 云服务器使用的镜像ID
type: Cloud.ECS.Image.Id
instance:
default: 1
description: 创建云服务器数量
subnet:
description: 云服务器子网ID
vm_deploy: #输入参数,控制vm对象是否部署。
default: true
type: boolean
vpc:
description: 云服务器VPC的ID
node_templates:
vm:
condition: condition_vm_deploy #条件满足时,vm对象才会部署
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
判断相等条件是否满足,一般用于判断输入参数与预期是否一致。
|
语法 |
参数说明 |
返回值 |
|---|---|---|
|
cond_eq: [cond1, cond2] |
|
cond1与cond2一致时,返回true;否则返回false。 |
通过cond_eq判断输入参数是否为期望值示例如下:
inputs:
a:
type: string
default: 10
conditions:
matchA:
cond_eq: [{get_input: a}, 10]
cond_not
对计算结果求反,一般用于嵌套其他条件函数。
|
语法 |
参数说明 |
返回值 |
|---|---|---|
|
cond_not: cond |
|
条件表达式计算结果为true时,返回false;结果为false时,返回true。 |
通过cond_not判断输入参数是否为期望值示例如下:
inputs:
a:
type: boolean
default: true
conditions:
matchA:
cond_not: {get_input: a}
cond_and
判断连续多个条件是否都满足,一般用于多重判断条件下,至少要有2个条件,最多支持10个条件。
|
语法 |
参数说明 |
返回值 |
|---|---|---|
|
cond_and: [cond1, cond2...condn] |
|
当所有参数条件都满足时,返回为true;否则返回false。 |
通过cond_and判断组合条件是否满足示例如下:
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]}] #条件1满足,条件2满足,matchAnd的条件才满足
cond_or
判断多个条件中任一条件满足即可。一般用于多重判断条件下,至少要有2个条件,最多支持10个条件。
|
语法 |
参数说明 |
返回值 |
|---|---|---|
|
cond_or: [cond1, cond2...condn] |
|
任一条件满足时,即返回为true;所有条件都不满足时,返回false。 |
通过cond_or判断组合条件是否满足示例如下:
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]}] #条件1不满足,条件2满足,matchOr的条件即可满足
cond_if
If条件是一个三元表达式,主要用于对属性进行赋值,一般用于node_templates的属性结构中。
|
语法 |
参数说明 |
返回值 |
|---|---|---|
|
cond_if: [condition, value_true, value_false] |
|
条件满足时,返回value_true;条件不满足时,返回value_false。 |
通过cond_if定义属性值示例如下:
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]}] #条件1不满足,条件2满足,matchOr的条件即可满足
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定义,若满足debug模式使用调试镜像,不满足使用产品镜像
instances: 1
availabilityZone: ae-ad-1a
rootVolume:
volumeType: SATA
size: 40
flavor: flavor-1