---
log_guid: "routing_api"
max_ttl: <%= p("routing_api.max_ttl") %>
system_domain: <%= p("routing_api.system_domain") %>
metron_config:
  address: "localhost"
  port: <%= p("metron.port") %>
metrics_reporting_interval: <%= p("routing_api.metrics_reporting_interval") %>
statsd_endpoint: <%= p("routing_api.statsd_endpoint") %>
oauth:
  token_endpoint: <%= p("uaa.token_endpoint") %>
  port: <%= p("uaa.tls_port") %>
  skip_ssl_validation: <%= p("skip_ssl_validation") %>
  <% if p("uaa.ca_cert") != "" %>
  ca_certs: "/var/vcap/jobs/routing-api/config/certs/uaa/ca.crt"
  <% end %>
<%-
def parse_ip (ip, var_name)
  unless ip.empty?
    ip = ip.split(":")[0]
      begin
        parsed = IPAddr.new ip
      rescue  => e
        raise "Invalid #{var_name} '#{ip}': #{e}"
      end
  end
end

def fail_on_router_port_conflicts
  if_p('routing_api.fail_on_router_port_conflicts') do |prop|
    return prop
  end.else do
    if_link('tcp_router') do |link|
      link.if_p('tcp_router.fail_on_router_port_conflicts') do |prop|
        return prop
      end
    end
  end

  return 'false' # There is a default on the tcp_router property. This should only happen when tcp_router is not deployed.
end

parse_ip(p('routing_api.debug_address'), 'routing_api.debug_address')
-%>
debug_address: <%= p("routing_api.debug_address") %>
statsd_client_flush_interval: <%= p("routing_api.statsd_client_flush_interval") %>
router_groups: <%= p("routing_api.router_groups").to_yaml.gsub("---","") %>
reserved_system_component_ports: <%= p("routing_api.reserved_system_component_ports").map(&:to_i) %>
fail_on_router_port_conflicts: <%= fail_on_router_port_conflicts %>
uuid: <%= spec.id %>
admin_port: <%= p("routing_api.admin_port") %>

api:
  listen_port: <%= p("routing_api.port") %>
<% case p("routing_api.enabled_api_endpoints")
  when "mtls" -%>
  http_enabled: false
<% when "both" -%>
  http_enabled: true
<% else -%>
  <% raise "expected routing_api.enabled_api_endpoints to be one of 'mtls' or 'both' but got '#{p('routing_api.enabled_api_endpoints')}'" -%>
<% end -%>
  mtls_listen_port: <%= p("routing_api.mtls_port") %>
  mtls_client_ca_file: "/var/vcap/jobs/routing-api/config/certs/routing-api/client_ca.crt"
  mtls_server_cert_file: "/var/vcap/jobs/routing-api/config/certs/routing-api/server.crt"
  mtls_server_key_file: "/var/vcap/jobs/routing-api/config/certs/routing-api/server.key"
  <%
    # Used to ensure that properties for bosh links are included for consumers
    p("routing_api.mtls_client_cert")
    p("routing_api.mtls_client_key")
  %>

<%=
  sqldb = {
    'sqldb' => {
      'host' => p("routing_api.sqldb.host"),
      'port' => p("routing_api.sqldb.port"),
      'type' => p("routing_api.sqldb.type"),
      'schema' => p("routing_api.sqldb.schema"),
      'username' => p("routing_api.sqldb.username"),
      'password' => p("routing_api.sqldb.password"),
      'skip_hostname_validation' => p("routing_api.sqldb.skip_hostname_validation"),
      'max_open_connections' => p("routing_api.sqldb.max_open_connections"),
      'max_idle_connections' => p("routing_api.sqldb.max_idle_connections"),
      'connections_max_lifetime_seconds' => p("routing_api.sqldb.connections_max_lifetime_seconds"),
    }
  }
  if_p("routing_api.sqldb.ca_cert") do |cert|
    sqldb['sqldb']['ca_cert'] = cert
  end
  sqldb.to_yaml[4..-2]
%>

lock_ttl: <%= p("routing_api.lock_ttl") %>
retry_interval: <%= p("routing_api.lock_retry_interval") %>

locket:
  locket_address: <%= p("routing_api.locket.api_location") %>
  locket_ca_cert_file: "/var/vcap/jobs/routing-api/config/certs/locket/ca.crt"
  locket_client_cert_file: "/var/vcap/jobs/routing-api/config/certs/locket/client.crt"
  locket_client_key_file: "/var/vcap/jobs/routing-api/config/certs/locket/client.key"
