QubitProducts / bamboo

HAProxy auto configuration and auto service discovery for Mesos Marathon
Apache License 2.0
793 stars 214 forks source link

'if eq' in go template #187

Closed Boran closed 8 years ago

Boran commented 8 years ago

While trying to add conditional lines to the frontend http-in section of haproxy_template.cfg based on an Env variable from marathon, the following code:

{{ if eq $app.Env.SOME_VAR "bar" }}
        acl {{ $app.EscapedId }}-aclrule {{ $service.Acl}}
        use_backend {{ $app.EscapedId }}-cluster if {{ $app.EscapedId }}-aclrule
        {{ else }}
        # ignoring, SOME_VAR={{app.Env.SOME_VAR}}
        {{ end }}

gives "error calling eq: invalid type for comparison"

Searching the net has not been fruitful, any suggestions please?

scbunn commented 8 years ago

+1 for this; Right now it seems like I can only tell if an ENV exists or not. I have not been able to figure out any way to do comparisons.

scbunn commented 8 years ago

FYI, for anybody that might have a similar issue

  {{ if $app.Env.BAMBOO_STICK_SESSION }}
    {{ if eq $app.Env.BAMBOO_STICK_SESSION "foobar" }}
       # foobar
    {{ end }}
  {{ end }}

This works. You have to check for the existence of the var prior to comparing it.

Boran commented 8 years ago

Ah, good point! thx.

rbucker commented 5 years ago

FYI, for anybody that might have a similar issue THIS ALSO WORKS

    {{ if eq (or $app.Env.BAMBOO_STICK_SESSION "some useless value or nothing at all") "foobar" }}
       # foobar
    {{ end }}

This works. You have to check for the existence of the var prior to comparing it.

what seems to be happening... is that {{ if eq $app.Env.BAMBOO_STICK_SESSION "foobar" }} if $app.Env.BAMBOO_STICK_SESSION resolves to undefined then an exception is thrown, however, in my example if the variable is empty or nil then it is assigned the or value.