Optional properties 编辑
Optional properties
Sometimes, a property of a component takes its value from an expression, which can be a simple expression such as a parameter reference, or a more complex one. Setting this property value is optional in the component. You can choose to set the property value only if the expression returns an actual value, else you can choose not to set this property.
For example, consider that one of the properties you want to set is the lbmethod
(load balancing algorithm) of a component whose type is ns::lbvserver. The value of the property lbmethod
is taken from a parameter value provided by the user, as shown below:
components
-
name: lbvserver_comp
type: ns::lbvserver
properties:
name: $parameters.lb-appname + "-lb"
servicetype: $parameters.lb-service-type
ipv46: $parameters.lb-virtual-ip
port: 80
lbmethod: $parameters.lb-advanced.algorithm
<!--NeedCopy-->
Now, consider that the parameter lb-advanced.algorithm is an optional parameter. And, if the user does not provide a value for this parameter because it is optional, the expression $parameters.lb-advanced.algorithm evaluates to blank value. Therefore, an invalid value is passed for the lbmethod
property. In order to avoid such a situation, you can annotate the property as optional by suffixing its name with “?” as follows:
components
-
name: lbvserver_comp
type: ns::lbvserver
properties:
name: $parameters.lb-appname + "-lb"
servicetype: $parameters.lb-service-type
ipv46: $parameters.lb-virtual-ip
port: 80
lbmethod?: $parameters.lb-advanced.algorithm
<!--NeedCopy-->
The use of “?” omits the property if the expression on the right evaluates to nothing, which would be equivalent, in this case, to a component defined as follows:
components
-
name: lbvserver_comp
type: ns::lbvserver
properties:
name: $parameters.lb-appname + "-lb"
servicetype: $parameters.lb-service-type
ipv46: $parameters.lb-virtual-ip
port: 80
<!--NeedCopy-->
Because lbmethod is optional, omitting it still makes a valid component. Note that lbmethod
might take its default value if one is defined in its type “ns::lbvserver.”
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论