OpenStack Heat模板内部函数

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

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

 

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开源团队工程师陈曾

原文地址:https://www.cnblogs.com/openstackteam/p/5533111.html