---
- name: Get Project Detail
  shell: "{{ openshift_oc_bin }} get project {{ openshift_project }}"
  register: project_details
  ignore_errors: yes

- name: Create AWX Openshift Project
  shell: "{{ openshift_oc_bin }} new-project {{ openshift_project }}"
  when: project_details.rc != 0

- name: Ensure PostgreSQL PVC is available
  block:
    - name: Check PVC status
      command: "{{ openshift_oc_bin }} get pvc {{ openshift_pg_pvc_name }} -n {{ openshift_project }} -o=jsonpath='{.status.phase}'"
      register: pg_pvc_status
      ignore_errors: yes

    - name: Ensure PostgreSQL PVC is available
      assert:
        that:
          - pg_pvc_status.stdout == "Bound"
        msg: "Ensure a PVC named '{{ openshift_pg_pvc_name }}' is created and bound in the '{{ openshift_project }}' namespace."
  when:
    - pg_hostname is not defined or pg_hostname == ''
    - openshift_pg_emptydir is defined and (openshift_pg_emptydir | bool) != true

- name: Set postgresql service name
  set_fact:
    postgresql_service_name: "postgresql"
  when: "pg_hostname is not defined or pg_hostname == ''"

- name: Add privileged SCC to service account
  shell: |
    {{ openshift_oc_bin }} adm policy add-scc-to-user privileged system:serviceaccount:{{ openshift_project }}:awx
