{{/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}

{{- define "neutron.configmap.etc" }}
{{- $configMapName := index . 0 }}
{{- $envAll := index . 1 }}
{{- with $envAll }}

{{- if empty $envAll.Values.conf.neutron.keystone_authtoken.auth_uri -}}
{{- $_ := tuple "identity" "internal" "api" . | include "helm-toolkit.endpoints.keystone_endpoint_uri_lookup"| set $envAll.Values.conf.neutron.keystone_authtoken "auth_uri" -}}
{{- end }}

{{- if empty $envAll.Values.conf.neutron.keystone_authtoken.auth_url -}}
{{- $_ := tuple "identity" "internal" "api" . | include "helm-toolkit.endpoints.keystone_endpoint_uri_lookup"| set $envAll.Values.conf.neutron.keystone_authtoken "auth_url" -}}
{{- end }}

{{- if empty .Values.conf.neutron.keystone_authtoken.project_name -}}
{{- $_ := set .Values.conf.neutron.keystone_authtoken "project_name" .Values.endpoints.identity.auth.neutron.project_name -}}
{{- end -}}
{{- if empty $envAll.Values.conf.neutron.keystone_authtoken.project_domain_name -}}
{{- $_ := set $envAll.Values.conf.neutron.keystone_authtoken "project_domain_name" $envAll.Values.endpoints.identity.auth.neutron.project_domain_name -}}
{{- end -}}
{{- if empty $envAll.Values.conf.neutron.keystone_authtoken.user_domain_name -}}
{{- $_ := set $envAll.Values.conf.neutron.keystone_authtoken "user_domain_name" $envAll.Values.endpoints.identity.auth.neutron.user_domain_name -}}
{{- end -}}
{{- if empty $envAll.Values.conf.neutron.keystone_authtoken.username -}}
{{- $_ := set $envAll.Values.conf.neutron.keystone_authtoken "username" $envAll.Values.endpoints.identity.auth.neutron.username -}}
{{- end -}}
{{- if empty $envAll.Values.conf.neutron.keystone_authtoken.password -}}
{{- $_ := set $envAll.Values.conf.neutron.keystone_authtoken "password" $envAll.Values.endpoints.identity.auth.neutron.password -}}
{{- end -}}
{{- if empty $envAll.Values.conf.neutron.keystone_authtoken.region_name -}}
{{- $_ := set $envAll.Values.conf.neutron.keystone_authtoken "region_name" $envAll.Values.endpoints.identity.auth.neutron.region_name -}}
{{- end -}}

{{- if empty $envAll.Values.conf.neutron.keystone_authtoken.memcached_servers -}}
{{- $_ := tuple "oslo_cache" "internal" "memcache" . | include "helm-toolkit.endpoints.host_and_port_endpoint_uri_lookup" | set $envAll.Values.conf.neutron.keystone_authtoken "memcached_servers" -}}
{{- end }}
{{- if empty .Values.conf.neutron.keystone_authtoken.memcache_secret_key -}}
{{- $_ := set .Values.conf.neutron.keystone_authtoken "memcache_secret_key" ( default ( randAlphaNum 64 ) .Values.endpoints.oslo_cache.auth.memcache_secret_key ) -}}
{{- end -}}

{{- if empty $envAll.Values.conf.neutron.database.connection -}}
{{- $connection := tuple "oslo_db" "internal" "neutron" "mysql" . | include "helm-toolkit.endpoints.authenticated_endpoint_uri_lookup" -}}
{{- if .Values.manifests.certificates -}}
{{- $_ := (printf "%s?charset=utf8&ssl_ca=/etc/mysql/certs/ca.crt&ssl_key=/etc/mysql/certs/tls.key&ssl_cert=/etc/mysql/certs/tls.crt&ssl_verify_cert" $connection ) | set .Values.conf.neutron.database "connection" -}}
{{- else -}}
{{- $_ := set .Values.conf.neutron.database "connection" $connection -}}
{{- end -}}
{{- end }}

{{- if empty $envAll.Values.conf.neutron.DEFAULT.transport_url -}}
{{- $_ := tuple "oslo_messaging" "internal" "neutron" "amqp" . | include "helm-toolkit.endpoints.authenticated_transport_endpoint_uri_lookup" | set $envAll.Values.conf.neutron.DEFAULT "transport_url" -}}
{{- end }}

{{- if empty $envAll.Values.conf.neutron.nova.auth_url -}}
{{- $_ := tuple "identity" "internal" "api" . | include "helm-toolkit.endpoints.keystone_endpoint_uri_lookup"| set $envAll.Values.conf.neutron.nova "auth_url" -}}
{{- end }}

{{- if empty $envAll.Values.conf.neutron.nova.region_name -}}
{{- $_ := set $envAll.Values.conf.neutron.nova "region_name" $envAll.Values.endpoints.identity.auth.nova.region_name -}}
{{- end -}}
{{- if empty $envAll.Values.conf.neutron.nova.project_name -}}
{{- $_ := set $envAll.Values.conf.neutron.nova "project_name" $envAll.Values.endpoints.identity.auth.nova.project_name -}}
{{- end -}}
{{- if empty $envAll.Values.conf.neutron.nova.project_domain_name -}}
{{- $_ := set $envAll.Values.conf.neutron.nova "project_domain_name" $envAll.Values.endpoints.identity.auth.nova.project_domain_name -}}
{{- end -}}
{{- if empty $envAll.Values.conf.neutron.nova.user_domain_name -}}
{{- $_ := set $envAll.Values.conf.neutron.nova "user_domain_name" $envAll.Values.endpoints.identity.auth.nova.user_domain_name -}}
{{- end -}}
{{- if empty $envAll.Values.conf.neutron.nova.username -}}
{{- $_ := set $envAll.Values.conf.neutron.nova "username" $envAll.Values.endpoints.identity.auth.nova.username -}}
{{- end -}}
{{- if empty $envAll.Values.conf.neutron.nova.password -}}
{{- $_ := set $envAll.Values.conf.neutron.nova "password" $envAll.Values.endpoints.identity.auth.nova.password -}}
{{- end -}}

{{- if empty $envAll.Values.conf.neutron.octavia.base_url -}}
{{- $_ := tuple "load_balancer" "internal" "api" . | include "helm-toolkit.endpoints.keystone_endpoint_uri_lookup"| set $envAll.Values.conf.neutron.octavia "base_url" -}}
{{- end }}

{{/*
nova_metadata_ip can go away when Newton is no longer supported, otherwise
just set it along with nova_metadata_host.
*/}}
{{- if empty $envAll.Values.conf.metadata_agent.DEFAULT.nova_metadata_ip -}}
{{- $_ := tuple "compute_metadata" "public" . | include "helm-toolkit.endpoints.hostname_fqdn_endpoint_lookup" | set $envAll.Values.conf.metadata_agent.DEFAULT "nova_metadata_ip" -}}
{{- end -}}
{{- if empty $envAll.Values.conf.metadata_agent.DEFAULT.nova_metadata_host -}}
{{- $_ := tuple "compute_metadata" "public" . | include "helm-toolkit.endpoints.hostname_fqdn_endpoint_lookup" | set $envAll.Values.conf.metadata_agent.DEFAULT "nova_metadata_host" -}}
{{- end -}}
{{- if empty $envAll.Values.conf.metadata_agent.DEFAULT.nova_metadata_port -}}
{{- $_ := set $envAll.Values.conf.metadata_agent.DEFAULT "nova_metadata_port" 80 -}}
{{- end -}}
{{- if empty $envAll.Values.conf.metadata_agent.cache.memcache_servers -}}
{{- $_ := tuple "oslo_cache" "internal" "memcache" . | include "helm-toolkit.endpoints.host_and_port_endpoint_uri_lookup" | set $envAll.Values.conf.metadata_agent.cache "memcache_servers" -}}
{{- end -}}

{{- if empty $envAll.Values.conf.neutron.DEFAULT.interface_driver -}}
{{- $_ := set $envAll.Values "__interface_driver" ( list ) }}
{{- if ( has "openvswitch" $envAll.Values.network.backend ) -}}
{{ $__interface_driver := append $envAll.Values.__interface_driver "openvswitch" }}
{{- $_ := set $envAll.Values "__interface_driver" $__interface_driver }}
{{- end -}}
{{- if ( has "linuxbridge" $envAll.Values.network.backend ) -}}
{{ $__interface_driver := append $envAll.Values.__interface_driver "linuxbridge" }}
{{- $_ := set $envAll.Values "__interface_driver" $__interface_driver }}
{{- end -}}
{{- $_ := set $envAll.Values.conf.neutron.DEFAULT "interface_driver" $envAll.Values.__interface_driver -}}
{{- end -}}

{{- if empty $envAll.Values.conf.dhcp_agent.DEFAULT.interface_driver -}}
{{- $_ := set $envAll.Values "__interface_driver" ( list ) }}
{{- if ( has "openvswitch" $envAll.Values.network.backend ) -}}
{{ $__interface_driver := append $envAll.Values.__interface_driver "openvswitch" }}
{{- $_ := set $envAll.Values "__interface_driver" $__interface_driver }}
{{- end -}}
{{- if ( has "linuxbridge" $envAll.Values.network.backend ) -}}
{{ $__interface_driver := append $envAll.Values.__interface_driver "linuxbridge" }}
{{- $_ := set $envAll.Values "__interface_driver" $__interface_driver }}
{{- end -}}
{{- $_ := set $envAll.Values.conf.dhcp_agent.DEFAULT "interface_driver" $envAll.Values.__interface_driver -}}
{{- end -}}

{{- if empty $envAll.Values.conf.l3_agent.DEFAULT.interface_driver -}}
{{- $_ := set $envAll.Values "__interface_driver" ( list ) }}
{{- if ( has "openvswitch" $envAll.Values.network.backend ) -}}
{{ $__interface_driver := append $envAll.Values.__interface_driver "openvswitch" }}
{{- $_ := set $envAll.Values "__interface_driver" $__interface_driver }}
{{- end -}}
{{- if ( has "linuxbridge" $envAll.Values.network.backend ) -}}
{{ $__interface_driver := append $envAll.Values.__interface_driver "linuxbridge" }}
{{- $_ := set $envAll.Values "__interface_driver" $__interface_driver }}
{{- end -}}
{{- $_ := set $envAll.Values.conf.l3_agent.DEFAULT "interface_driver" $envAll.Values.__interface_driver -}}
{{- end -}}

{{- if empty $envAll.Values.conf.plugins.ml2_conf.ml2.mechanism_drivers -}}
{{- if (contains "vxlan" $envAll.Values.conf.plugins.ml2_conf.ml2.tenant_network_types) -}}
{{- $_ := set $envAll.Values "__mechanism_drivers" (append $envAll.Values.network.backend "l2population") -}}
{{- end -}}
{{- $_ := set $envAll.Values.conf.plugins.ml2_conf.ml2 "mechanism_drivers" ($envAll.Values.__mechanism_drivers | default $envAll.Values.network.backend | uniq) -}}
{{- end -}}

{{- if empty .Values.conf.neutron.DEFAULT.bind_port -}}
{{- $_ := tuple "network" "internal" "api" . | include "helm-toolkit.endpoints.endpoint_port_lookup" | set .Values.conf.neutron.DEFAULT "bind_port" -}}
{{- end -}}

{{- if and (empty .Values.conf.logging.handler_fluent) (has "fluent" .Values.conf.logging.handlers.keys) -}}
{{- $fluentd_host := tuple "fluentd" "internal" $envAll | include "helm-toolkit.endpoints.hostname_namespaced_endpoint_lookup" }}
{{- $fluentd_port := tuple "fluentd" "internal" "service" $envAll | include "helm-toolkit.endpoints.endpoint_port_lookup" }}
{{- $fluent_args := printf "('%s.%s', '%s', %s)" .Release.Namespace .Release.Name $fluentd_host $fluentd_port }}
{{- $handler_fluent := dict "class" "fluent.handler.FluentHandler" "formatter" "fluent" "args" $fluent_args -}}
{{- $_ := set .Values.conf.logging "handler_fluent" $handler_fluent -}}
{{- end -}}

{{- if and (empty .Values.conf.logging.formatter_fluent) (has "fluent" .Values.conf.logging.formatters.keys) -}}
{{- $formatter_fluent := dict "class" "oslo_log.formatters.FluentFormatter" -}}
{{- $_ := set .Values.conf.logging "formatter_fluent" $formatter_fluent -}}
{{- end -}}

{{- if .Values.conf.ovs_dpdk.enabled -}}
{{- $_ := set $envAll.Values.conf.plugins.openvswitch_agent.ovs "datapath_type" "netdev" -}}
{{- if empty $envAll.Values.conf.plugins.openvswitch_agent.ovs.vhostuser_socket_dir -}}
{{- $_ := set $envAll.Values.conf.plugins.openvswitch_agent.ovs "vhostuser_socket_dir" "/run/openvswitch/vhostuser" -}}
{{- end -}}
{{- end -}}

{{/* Designate DNS driver */}}
{{- if eq (.Values.conf.neutron.DEFAULT.external_dns_driver | default "") "designate" -}}
{{- if empty .Values.conf.neutron.designate.project_name -}}
{{- $_ := set .Values.conf.neutron.designate "project_name" .Values.endpoints.identity.auth.designate.project_name -}}
{{- end -}}
{{- if empty .Values.conf.neutron.designate.project_domain_name -}}
{{- $_ := set .Values.conf.neutron.designate "project_domain_name" .Values.endpoints.identity.auth.designate.project_domain_name -}}
{{- end -}}
{{- if empty .Values.conf.neutron.designate.user_domain_name -}}
{{- $_ := set .Values.conf.neutron.designate "user_domain_name" .Values.endpoints.identity.auth.designate.user_domain_name -}}
{{- end -}}
{{- if empty .Values.conf.neutron.designate.username -}}
{{- $_ := set .Values.conf.neutron.designate "username" .Values.endpoints.identity.auth.designate.username -}}
{{- end -}}
{{- if empty .Values.conf.neutron.designate.password -}}
{{- $_ := set .Values.conf.neutron.designate "password" .Values.endpoints.identity.auth.designate.password -}}
{{- end -}}
{{- if empty .Values.conf.neutron.designate.url -}}
{{- $_ := tuple "dns" "internal" "api" . | include "helm-toolkit.endpoints.keystone_endpoint_uri_lookup" | set .Values.conf.neutron.designate "url" -}}
{{- end -}}
{{- end }}

{{- if (has "baremetal" .Values.network.backend) -}}
{{- if empty .Values.conf.neutron.ironic.project_name -}}
{{- $_ := set .Values.conf.neutron.ironic "project_name" .Values.endpoints.identity.auth.ironic.project_name -}}
{{- end -}}
{{- if empty .Values.conf.neutron.ironic.project_domain_name -}}
{{- $_ := set .Values.conf.neutron.ironic "project_domain_name" .Values.endpoints.identity.auth.ironic.project_domain_name -}}
{{- end -}}
{{- if empty .Values.conf.neutron.ironic.user_domain_name -}}
{{- $_ := set .Values.conf.neutron.ironic "user_domain_name" .Values.endpoints.identity.auth.ironic.user_domain_name -}}
{{- end -}}
{{- if empty .Values.conf.neutron.ironic.username -}}
{{- $_ := set .Values.conf.neutron.ironic "username" .Values.endpoints.identity.auth.ironic.username -}}
{{- end -}}
{{- if empty .Values.conf.neutron.ironic.password -}}
{{- $_ := set .Values.conf.neutron.ironic "password" .Values.endpoints.identity.auth.ironic.password -}}
{{- end -}}
{{- end -}}

{{- if ( has "tungstenfabric" .Values.network.backend ) -}}
{{- if empty .Values.conf.plugins.tungstenfabric.KEYSTONE.auth_url -}}
{{- $_ := tuple "identity" "internal" "api" . | include "helm-toolkit.endpoints.keystone_endpoint_uri_lookup" | set .Values.conf.plugins.tungstenfabric.KEYSTONE "auth_url" -}}
{{- end -}}
{{- if empty .Values.conf.plugins.tungstenfabric.KEYSTONE.admin_user -}}
{{- $_ := set .Values.conf.plugins.tungstenfabric.KEYSTONE "admin_user" .Values.endpoints.identity.auth.admin.username -}}
{{- end -}}
{{- if empty .Values.conf.plugins.tungstenfabric.KEYSTONE.admin_password -}}
{{- $_ := set .Values.conf.plugins.tungstenfabric.KEYSTONE "admin_password" .Values.endpoints.identity.auth.admin.password -}}
{{- end -}}
{{- if empty .Values.conf.plugins.tungstenfabric.KEYSTONE.admin_tenant_name -}}
{{- $_ := set .Values.conf.plugins.tungstenfabric.KEYSTONE "admin_tenant_name" .Values.endpoints.identity.auth.admin.project_name -}}
{{- end -}}
{{- if empty .Values.conf.plugins.tf_vnc_api_lib.auth.AUTHN_SERVER -}}
{{- $_ := tuple "identity" . | include "helm-toolkit.endpoints.keystone_endpoint_name_lookup" | set .Values.conf.plugins.tf_vnc_api_lib.auth "AUTHN_SERVER" -}}
{{- end -}}
{{- if empty .Values.conf.plugins.tf_vnc_api_lib.auth.AUTHN_PORT -}}
{{- $_ := tuple "identity" "internal" "api" . | include "helm-toolkit.endpoints.endpoint_port_lookup" | set .Values.conf.plugins.tf_vnc_api_lib.auth "AUTHN_PORT" -}}
{{- end -}}
{{- if empty .Values.conf.plugins.tf_vnc_api_lib.auth.AUTHN_DOMAIN -}}
{{- $_ := set .Values.conf.plugins.tf_vnc_api_lib.auth "AUTHN_DOMAIN" .Values.endpoints.identity.auth.neutron.project_domain_name -}}
{{- end -}}
{{- end -}}

---
apiVersion: v1
kind: Secret
metadata:
  name: {{ $configMapName }}
type: Opaque
data:
  rally_tests.yaml: {{ toYaml $envAll.Values.conf.rally_tests.tests | b64enc }}
  api-paste.ini: {{ include "helm-toolkit.utils.to_ini" $envAll.Values.conf.paste | b64enc }}
  policy.yaml: {{ toYaml $envAll.Values.conf.policy | b64enc }}
  neutron.conf: {{ include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.neutron | b64enc }}
  logging.conf: {{ include "helm-toolkit.utils.to_oslo_conf" .Values.conf.logging | b64enc }}
  api_audit_map.conf: {{ include "helm-toolkit.utils.to_oslo_conf" .Values.conf.api_audit_map | b64enc }}
  dhcp_agent.ini: {{ include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.dhcp_agent | b64enc }}
  l3_agent.ini: {{ include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.l3_agent | b64enc }}
  metadata_agent.ini: {{ include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.metadata_agent | b64enc }}
  metering_agent.ini: {{ default "\"\"" (include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.metering_agent | b64enc) }}
  taas_plugin.ini: {{ default "\"\"" (include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.taas_plugin | b64enc) }}
  ml2_conf.ini: {{ include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.plugins.ml2_conf | b64enc }}
  ml2_conf_sriov.ini: {{ default "\"\"" (include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.plugins.ml2_conf_sriov | b64enc) }}
  taas.ini: {{ include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.plugins.taas | b64enc }}
  l2gw_plugin.ini: {{ default "\"\"" (include "helm-toolkit.utils.to_oslo_conf" .Values.conf.plugins.l2gateway | b64enc) }}
  macvtap_agent.ini: {{ default "\"\"" (include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.plugins.macvtap_agent | b64enc) }}
  linuxbridge_agent.ini: {{ include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.plugins.linuxbridge_agent | b64enc }}
  openvswitch_agent.ini: {{ include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.plugins.openvswitch_agent | b64enc }}
  sriov_agent.ini: {{ include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.plugins.sriov_agent | b64enc }}
  l2gw_agent.ini: {{ default "\"\"" (include "helm-toolkit.utils.to_oslo_conf" .Values.conf.l2gateway_agent | b64enc) }}
  bagpipe_bgp.conf: {{ default "\"\"" (include "helm-toolkit.utils.to_oslo_conf" .Values.conf.bagpipe_bgp | b64enc) }}
  tf_plugin.ini: {{ default "\"\"" (include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.plugins.tungstenfabric | b64enc) }}
  vnc_api_lib.ini: {{ default "\"\"" (include "helm-toolkit.utils.to_oslo_conf" $envAll.Values.conf.plugins.tf_vnc_api_lib | b64enc) }}
  dnsmasq.conf: ""
  neutron_sudoers: {{ $envAll.Values.conf.neutron_sudoers | b64enc }}
  rootwrap.conf: {{ $envAll.Values.conf.rootwrap | b64enc }}
  auto_bridge_add: {{ toJson $envAll.Values.conf.auto_bridge_add | b64enc }}
  dpdk.conf: {{ toJson $envAll.Values.conf.ovs_dpdk | b64enc }}
  update_dpdk_bond_config: {{ $envAll.Values.conf.ovs_dpdk.update_dpdk_bond_config | toString | b64enc }}
{{-  if .Values.manifests.certificates }}
{{- include "helm-toolkit.snippets.values_template_renderer" ( dict "envAll" $envAll "template" .Values.conf.nginx "key" "nginx.conf" "format" "Secret" ) | indent 2 }}
{{- end }}
{{- range $key, $value := $envAll.Values.conf.rootwrap_filters }}
{{- $filePrefix := replace "_" "-"  $key }}
  {{ printf "%s.filters" $filePrefix }}: {{ $value.content | b64enc }}
{{- end }}
{{- end }}
{{- end }}

{{- if .Values.manifests.configmap_etc }}
{{- list "neutron-etc" . | include "neutron.configmap.etc" }}
{{- end }}
