---
platform: linux
image_resource:
  type: docker-image
  source:
    repository: governmentpaas/cf-cli
    tag: 51c4648c56a18e28a295e73963b65fdbfe76e52f
inputs:
  - name: paas-cf
  - name: cf-manifest
  - name: admin-creds
run:
  path: sh
  args:
    - -e
    - -u
    - -c
    - |
      if [ "${DISABLE_ADMIN_USER_CREATION:-}" = "true" ]; then
        echo "Temporary user creation is disabled (DISABLE_ADMIN_USER_CREATION=${DISABLE_ADMIN_USER_CREATION}). Skipping."
        exit 0
      fi

      VAL_FROM_YAML=$(pwd)/paas-cf/concourse/scripts/val_from_yaml.rb
      SYSTEM_DNS_ZONE_NAME=$($VAL_FROM_YAML instance_groups.api.jobs.cloud_controller_ng.properties.system_domain cf-manifest/cf-manifest.yml)
      API_ENDPOINT="https://api.${SYSTEM_DNS_ZONE_NAME}"
      USERNAME=$(cat admin-creds/username)
      PASSWORD=$(cat admin-creds/password)

      echo "Removing user ${USERNAME}"

      echo | cf login -a "${API_ENDPOINT}" -u "${USERNAME}" -p "${PASSWORD}"

      cf delete-user "${USERNAME}" -f

---
platform: linux
image_resource:
  type: docker-image
  source:
    repository: governmentpaas/cf-cli
    tag: 51c4648c56a18e28a295e73963b65fdbfe76e52f
inputs:
  - name: paas-cf
  - name: cf-manifest
  - name: admin-creds
  - name: cf-vars-store
run:
  path: sh
  args:
    - -e
    - -u
    - -c
    - |
      if [ "${DISABLE_ADMIN_USER_CREATION:-}" = "true" ]; then
        echo "Temporary user creation is disabled (DISABLE_ADMIN_USER_CREATION=${DISABLE_ADMIN_USER_CREATION}). Skipping."
        exit 0
      fi

      VAL_FROM_YAML=$(pwd)/paas-cf/concourse/scripts/val_from_yaml.rb
      SYSTEM_DNS_ZONE_NAME=$($VAL_FROM_YAML instance_groups.api.jobs.cloud_controller_ng.properties.system_domain cf-manifest/cf-manifest.yml)
      API_ENDPOINT="https://api.${SYSTEM_DNS_ZONE_NAME}"
      CF_ADMIN=admin
      CF_PASS=$($VAL_FROM_YAML cf_admin_password cf-vars-store/cf-vars-store.yml)
      USERNAME=$(cat admin-creds/username)

      echo "Removing user ${USERNAME}"

      echo | cf login -a "${API_ENDPOINT}" -u "${CF_ADMIN}" -p "${CF_PASS}"

      cf delete-user "${USERNAME}" -f

Delete the temporary admin user with the main admin user
Delete the temporary admin user with the main admin user

Based on observation if you delete a CF user with itself then the user will
only be deleted from UAA and CF fails to remove the user. The user is still
listed in the API, but without a username field.
