package d_app

const CmiiFrontendConfigMapTemplate = `
kind: ConfigMap
apiVersion: v1
metadata:
  name: tenant-prefix-{{ .ShortName }}
  namespace: {{ .Namespace }}
data:
  ingress-config.js: |-
    var __GlobalIngressConfig = {
      TenantEnvironment: "{{ .TenantEnv }}",
      {{- if .WebPort }}
      CloudHOST: "{{ .WebIP }}:{{ .WebPort }}",
      {{- else }}
      CloudHOST: "{{ .WebIP }}",
      {{- end }}
      {{- if eq .ShortName "pangu" }}
      ApplicationShortName: "",
      {{- else }}
      ApplicationShortName: "{{ .ShortName }}",
      {{- end }}
      AppClientId: "{{ .ClientId }}"
    }
`

const CmiiFrontendDefaultNginxConfTemplate = `
kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-cm
  namespace: {{ .Namespace }}
  labels:
    cmii.type: frontend
data:
  nginx.conf: |
    server {
        listen       9528;
        server_name  localhost;
        gzip on;

        location / {
            root   /home/cmii-platform/dist;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
`

const CmiiFrontendIngressTemplate = `
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: frontend-applications-ingress
  namespace: {{ .Namespace }}
  labels:
    type: frontend
    octopus.control: all-ingress-config-wdd
    app.kubernetes.io/managed-by: octopus-control
    app.kubernetes.io/version: "{{ .TagVersion }}"
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/configuration-snippet: |
	{{- range $key, $value := .FrontendShortNameMaps }}
      rewrite ^(/{{ $value }})$ $1/ redirect;
	{{- end }}
spec:
  rules:
    - host: fake-domain.{{ .Namespace }}.io
      http:
        paths:
          {{- if .TenantEnv }}
          {{- $tenantEnv := .TenantEnv }}
          - path: /{{ $tenantEnv }}/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uav-platform
              servicePort: 9528
          {{- range $key, $value := .FrontendShortNameMaps }}
          - path: /{{ $tenantEnv }}/{{ $value }}/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: {{ $key }}
              servicePort: 9528
          {{- end }}
          {{- else }}
          - path: /?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uav-platform
              servicePort: 9528
          {{- range $key, $value := .FrontendShortNameMaps }}
          - path: /{{ $value }}/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: {{ $key }}
              servicePort: 9528
          {{- end }}
          {{- end }}
`

const CmiiBackendIngressTemplate = `
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: backend-applications-ingress
  namespace: {{ .Namespace }}
  labels:
    type: backend
    octopus.control: all-ingress-config-wdd
    app.kubernetes.io/managed-by: octopus-control
    app.kubernetes.io/version: "{{ .TagVersion }}"
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/enable-cors: "true"
spec:
  rules:
    {{- if .TenantEnv }}
    {{- $tenantEnv := .TenantEnv }}
    {{- range $key, $value := .BackendImageVersionMap }}
    - host: {{ $key }}.uavcloud-{{ $tenantEnv }}.io
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              serviceName: {{  $key  }}
              servicePort: 8080
   {{- end }}
   {{- else }}
   {{- $tenantEnv := .Namespace }}
   {{- range $key, $value := .BackendImageVersionMap }}
   - host: {{ $key }}.uavcloud-{{ $tenantEnv }}.io
     http:
       paths:
         - path: /
           pathType: ImplementationSpecific
           backend:
             serviceName: {{  $key  }}
             servicePort: 8080
   {{- end }}
   {{- end }}
`

const CmiiGatewayIngressTemplate = `
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: all-gateways-ingress
  namespace: {{ .Namespace }}
  labels:
    type: api-gateway
    octopus.control: all-ingress-config-1.1.0
    app.kubernetes.io/managed-by: octopus-control
    app.kubernetes.io/version: "{{ .TagVersion }}"
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header upgradePrefix $http_upgrade;
      proxy_set_header Connection "upgradePrefix";
spec:
  rules:
    - host: fake-domain.{{ .Namespace }}.io
      http:
        paths:
          {{- if .TenantEnv }}
          {{- $tenantEnv := .TenantEnv }}
          - path: /{{ $tenantEnv }}/oms/api/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-admin-gateway
              servicePort: 8080
          - path: /{{ $tenantEnv }}/open/api/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-open-gateway
              servicePort: 8080
          - path: /{{ $tenantEnv }}/api/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uav-gateway
              servicePort: 8080
          - path: /{{ $tenantEnv }}/uas/api/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uas-gateway
              servicePort: 8080
          - path: /{{ $tenantEnv }}/converge/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-sky-converge
              servicePort: 8080
          {{- else }}
          - path: /oms/api/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-admin-gateway
              servicePort: 8080
          - path: /open/api/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-open-gateway
              servicePort: 8080
          - path: /api/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uav-gateway
              servicePort: 8080
          - path: /uas/api/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-uas-gateway
              servicePort: 8080
          - path: /converge/?(.*)
            pathType: ImplementationSpecific
            backend:
              serviceName: cmii-sky-converge
              servicePort: 8080
         {{- end }}
`
