#
# Copyright SecureKey Technologies Inc. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

---
kind: Job
apiVersion: batch/v1
metadata:
  name: hydra-create-client
spec:
  template:
    spec:
      restartPolicy: Never
      initContainers:
        - name: healthcheck-ready
          image: busybox
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh"]
          args:
            - "-c"
            - |
              while [[ "$(wget -T 5 -S --spider http://${HUB_HYDRA_ADMIN_SERVICE_HOST}/health/ready 2>&1 | grep '200 OK')" == "" ]];
              do echo "waiting for endpoint";
              sleep 5;
              done;
      containers:
      - name: hydra-clients-create
        image: "oryd/hydra:v1.3.2-alpine"
        imagePullPolicy: IfNotPresent
        command: ["/bin/sh"]
        args:
          - "-c"
          - |
            err_resp=$(hydra clients create --endpoint http://${HUB_HYDRA_ADMIN_SERVICE_HOST} --fake-tls-termination --id user-agent --secret user-agent-secret --grant-types authorization_code,refresh_token --response-types code,id_token --scope openid,profile,email --skip-tls-verify --callbacks https://wallet-support.||DOMAIN||/oidc/callback 2>&1 > /dev/null);
            echo $err_resp;
            if [ -z "$err_resp" ] || [ -n "$(echo ${err_resp} | grep already )" ];then echo "hydra client add successful"; exit 0;else echo "hydra client add failed"; exit 1;fi
