---

### properties:
###   postgres_acls:
###     - name
###     - password
###     - trusted_cidr_entry [shared_service_network]

- name: "Updating pg_hba.conf entries for users/nodes/schemas"
  lineinfile:
    state: '{{ database_state }}'
    regex: '^hostssl[ ]+{{ item.name }}[ ]+{{ item.name }}'
    line: 'hostssl    {{ item.name }}    {{ item.name }}    {{ item.trusted_cidr_entry | default(shared_service_network) }} md5'
    path: /etc/postgresql/{{ default_postgres_version }}/main/pg_hba.conf
  with_items: "{{ postgres_acls }}"

- name: "Checking roles exist" # noqa command-instead-of-shell
  shell: "/usr/bin/psql -Atc \"SELECT count(rolname) FROM pg_roles where rolname='{{ item.name }}'\""
  with_items: "{{ postgres_acls }}"
  register: role_check
  changed_when: "role_check.stdout == '0'"
  become_user: postgres
  become: yes

- name: "Checking roles exist"
  debug:
    msg: "{{ role_check }}"
  when:
   - debug

- name: "Creating roles if necessary"
  shell: "/usr/bin/psql -c 'CREATE ROLE {{ item.item.name }} LOGIN;'"
  with_items: "{{ role_check.results }}"
  become_user: postgres
  become: yes
  when:
    - database_state == 'present'
    - item.stdout == '0'

- name: "Checking database exist"
  shell: "/usr/bin/psql -Atc \"SELECT count(*) FROM pg_database WHERE datname = '{{ item.name }}'\""
  with_items: "{{ postgres_acls }}"
  register: database_check
  changed_when: "database_check.stdout == '0'"
  become_user: postgres
  become: yes

- name: "Check databases exist result"
  debug:
    msg: "{{ database_check }}"
  when:
   - debug

- name: "Creating Databases if necessary"
  shell: "/usr/bin/psql -c \"CREATE DATABASE {{ item.item.name }};\""
  with_items: "{{ database_check.results }}"
  become_user: postgres
  become: yes
  when:
    - database_state == 'present'
    - item.stdout == '0'

- name: "Deleting Databases if necessary"
  shell: '/usr/bin/psql -c "DROP DATABASE {{ item.item.name }} WITH (FORCE);"'
  with_items: "{{ database_check.results }}"
  become_user: postgres
  become: yes
  when: 
    - database_state == 'absent'
    - item.stdout == '1'

- name: "Deleting roles if necessary"
  shell: '/usr/bin/psql -c "DROP ROLE {{ item.item.name }};"'
  with_items: "{{ role_check.results }}"
  become_user: postgres
  become: yes
  when:
    - database_state == 'absent'
    - item.stdout == '1'

- name: "Changing password with scram-sha-256! for users and set password"
  shell: "/usr/bin/psql -c \"set password_encryption = 'scram-sha-256';ALTER ROLE {{ item.name }} WITH PASSWORD '{{ item.password }}';\""
  with_items: "{{ postgres_acls }}"
  become_user: postgres
  become: yes
  when:
    - database_state == 'present'

- name: "Changing owners for databases"
  shell: "/usr/bin/psql -c \"ALTER DATABASE {{ item.name }} OWNER TO {{ item.name }};\""
  with_items: "{{ postgres_acls }}"
  become_user: postgres
  become: yes
  when:
    - database_state == 'present'

- name: pg_reload_conf # noqa no-changed-when
  become: yes
  become_user: postgres
  shell: '/usr/bin/psql -c "SELECT pg_reload_conf();"'
