本站文章大部分为作者原创,非商业用途转载无需作者授权,但务必在文章标题下面注明作者 刘世民(Sammy Liu)以及可点击的本博客地址超级链接 http://www.cnblogs.com/sammyliu/ ,谢谢合作
随笔 - 206  文章 - 46  评论 - 755  阅读 - 270万

Heat模板内部函数

Heat模板内部函数又称为Intrinsic functions。

注:Intrinsic functions只能用在 resource 的 properties 段 和 outputs 中。   

 http://www.ysshang.com/openstackteam/p/5533111.html

get_attr

作用:获取所创建资源的属性。

语法:

get_attr:
  - <resource name>
  - <attribute name>
  - <key/index 1> (optional)
  - <key/index 2> (optional)
  - ...

Resource name:必须是模板 resouce 段中指定的资源。

Attribute name:要获取的属性,如果属性对应的值是list 或map, 则可以指定key/index来获取具体的值。

示例:

复制代码
resources:
  my_instance:
    type: OS::Nova::Server
# ...
 
outputs:
  instance_ip:
    description: IP address of the deployed compute instance
    value: { get_attr: [my_instance, first_address] }
  instance_private_ip:
    description: Private IP address of the deployed compute instance
    value: { get_attr: [my_instance, networks, private, 0] }
复制代码

  

get_file

作用:获取文件的内容。

语法:

get_file: <content key>

示例:

复制代码
resources:
  my_instance:
    type: OS::Nova::Server
    properties:
      # general properties ...
      user_data:
        get_file: my_instance_user_data.sh
  my_other_instance:
    type: OS::Nova::Server
    properties:
      # general properties ...
      user_data:
        get_file: http://example.com/my_other_instance_user_data.sh
复制代码

 

get_param

作用:引用模板中指定的参数。

语法:

get_param:
 - <parameter name>
 - <key/index 1> (optional)
 - <key/index 2> (optional)
 - ...

示例:

复制代码
parameters:
   instance_type:
    type: string
    label: Instance Type
    description: Instance type to be used.
  server_data:
    type: json
 
resources:
  my_instance:
    type: OS::Nova::Server
    properties:
      flavor: { get_param: instance_type}
      metadata: { get_param: [ server_data, metadata ] }
      key_name: { get_param: [ server_data, keys, 0 ] }
复制代码

输入参数是:

{"instance_type": "m1.tiny",
{"server_data": {"metadata": {"foo": "bar"},
                 "keys": ["a_key","other_key"]}}}

 

get_resource 

作用:获取模板中指定的资源。

语法:

get_resource: <resource ID>

示例:

复制代码
resources:
  instance_port:
    type: OS::Neutron::Port
    properties: ...
 
  instance:
    type: OS::Nova::Server
    properties:
      ...
      networks:
        port: { get_resource: instance_port }
复制代码

 

list_join

作用:使用指定的分隔符将一个list中的字符串合成一个字符串。

语法:

list_join:
- <delimiter>
- <list to join>

示例输出:one, two, and three。

list_join: [', ', ['one', 'two', 'and three']]

 

digest           

作用:在指定的值上使用algorithm。

语法:

digest:
  - <algorithm>
  - <value>

algorithm 可用的值是hashlib(md5, sha1, sha224, sha256, sha384, and sha512) 或openssl的相关值

示例:

# from a user supplied parameter
pwd_hash: { digest: ['sha512', { get_param: raw_password }] }

 

repeat

作用:迭代fore_each中的列表,按照template的格式生成一个list。

语法:

repeat:
  template:
    <template>
  for_each:
    <var>: <list>

示例:

复制代码
parameters:
  ports:
    type: comma_delimited_list
    label: ports
    default: "80,443,8080"
  protocols:
    type: comma_delimited_list
    label: protocols
    default: "tcp,udp"
 
resources:
  security_group:
    type: OS::Neutron::SecurityGroup
    properties:
      name: web_server_security_group
      rules:
        repeat:
          for_each:
            <%port%>: { get_param: ports }
            <%protocol%>: { get_param: protocols }
          template:
            protocol: <%protocol%>
            port_range_min: <%port%>
复制代码

结果是[{‘protocal’:tpc, ‘prot_range_min’:80},

          {‘protocal’:tpc, ‘prot_range_min’:443},

          {‘protocal’:tpc, ‘prot_range_min’:8080},

          {‘protocal’:udp, ‘prot_range_min’:80},

          {‘protocal’:udp, ‘prot_range_min’:443},

          {‘protocal’:udp, ‘prot_range_min’:8080}]

 

resource_facade

作用:检索资源的数据。

语法:

resource_facade: <data type>

data type:metadata、deletion_policy、update_policy

 

9 str_replace

作用:使用params中的值替换template中的占位符,从而构造一个新的字符串。

语法:

str_replace:
  template: <template string>
  params: <parameter mappings>

示例:

复制代码
resources:
  my_instance:
    type: OS::Nova::Server
    # general metadata and properties ...
 
outputs:
  Login_URL:
    description: The URL to log into the deployed application
    value:
      str_replace:
        template: http://host/MyApplication
        params:
          host: { get_attr: [ my_instance, first_address ] }
复制代码

template 中 host 将会被替换。

 

10 str_split

作用:将一个字符串按照分隔符分隔成一个list

语法:

str_split:
  - ','
  - string,to,split

示例:

str_split: [',', 'string,to,split']

结果是['string', 'to', 'split']

 

11 map_merge

作用:合并多个map,且后面的map会覆盖前面map中同一个key的值。

语法:

map_merge:
- <map 1>
- <map 2>
- ...

示例:

map_merge: [{'k1': 'v1', 'k2': 'v2'}, {'k1': 'v2'}]

结果是:{'k1': 'v2', 'k2': 'v2'}。

 

编者注:本文来自OpenStack开源团队工程师陈曾

 

http://docs.openstack.org/developer/heat/template_guide/hot_spec.html

2015-10-15 Heat 版本支持的内置函数

get_attr
get_file
get_param
get_resource
list_join
repeat
digest
resource_facade
str_replace
str_split
posted on   SammyLiu  阅读(459)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示