OpenStack Heat模板內部函數

Heat模板內部函數又稱爲Intrinsic functions。web

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

 

get_attr

做用:獲取所建立資源的屬性。app

語法:tcp

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

Resource name:必須是模板 resouce 段中指定的資源。函數

Attribute name:要獲取的屬性,若是屬性對應的值是list 或map, 則能夠指定key/index來獲取具體的值。spa

示例:code

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

做用:獲取文件的內容。server

語法:blog

get_file: <content key>

示例:three

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開源團隊工程師陳曾

相關文章
相關標籤/搜索