Openstack HEAT condition for resource properties

1.4k views Asked by At

"Conditions (..) They can be associated with resources and resource properties in the resources section (..)" - as the official openstack's docs said I can do that. But attached examples do not contains these with "resource properties".

I have my example, when user can set parameter to NOT create port2 AND not attach port2 (because port2 does not exist):

parameters:
  global_port2_create:
    description: Do you want eth1 (port2)
    type: string
    default: true

conditions:
  create_port2: {equals : [{get_param: global_port2_create}, "true"]}

resources:
  node_port1:
    type: OS::Neutron::Port
    properties:
      network_id: {get_param: global_port1_net_id }
      fixed_ips:
        - subnet_id: {get_param: global_port1_net_id }
        - ip_address: {get_param: node_port1_ip }
      security_groups: {get_param: global_port1_security_groups_ids}
  node_port2:
    type: OS::Neutron::Port
    condition: create_port2
    properties:
      network_id: {get_param: global_port_net_id }
      fixed_ips:
        - subnet_id: {get_param: global_port2_net_id }
        - ip_address: {get_param: node5_port2_ip }
      security_groups: {get_param: global_port2_security_groups_ids}
  node5_server:
    type: OS::Nova::Server
    depends_on: [ node5_port1, node5_port2 ]
    properties:
      name: some_name
      image: { get_param: global_image }
      availability_zone: some_az
      networks:
        - port: { get_resource: node5_port1 }
        - port: { get_resource: node5_port2 }  #How to use a condition here?

I know, i can do a ResourceGroup with both ports and iterate them, but I do not want this resolution. Maybe like this?

      networks:
        - port: { get_resource: node5_port1 }
        - port:
          condition: create_port2
          get_resource: node5_port2

Anyone have any ideas how to accomplish this?

1

There are 1 answers

0
Mago On BEST ANSWER

I had a similar use case and found a solution. However, say goodbye to readability if this was still a concern:

      networks: {if: [ "create_port2", [port: { get_resource: node5_port1 }, port: { get_resource: node5_port2 }], [port: { get_resource: node5_port1 }]}

which you can also write:

      networks:
        if: 
        - "create_port2"
        - [port: { get_resource: node5_port1 }, port: { get_resource: node5_port2 }]
        - [port: { get_resource: node5_port1 }]

Or even:

      networks:
        if: 
        - "create_port2"
        - - port: { get_resource: node5_port1 }
          - port: { get_resource: node5_port2 }
        - - port: { get_resource: node5_port1 }

Choose your poison!