- name: install the messaging packages
  package: name={{ item }} state=present
  with_items:
  - fedora-messaging
  tags:
  - fedora-messaging

- name: create rabitmq directory
  file: path=/etc/pki/rabbitmq/ owner=root group=root mode=0755 state=directory
  tags:
  - fedora-messaging

- name: "create key/cert directory"
  file: path=/etc/pki/rabbitmq/{{ item.username }}/
        owner={{ item.username }} group={{ item.username }}
        mode=0700 state=directory
  with_items: "{{ messaging.certificates }}"
  tags:
  - fedora-messaging

- name: "copy fedora messaging key"
  copy: src={{ private }}/files/rabbitmq/{{ env }}/pki/private/{{ item.key }}{% if env == 'staging' %}.stg{% endif %}.key
        dest=/etc/pki/rabbitmq/{{ item.username }}/{{ item.key }}.key
        owner={{ item.username }} group=root mode=0600
  with_items: "{{ messaging.certificates }}"
  tags:
  - fedora-messaging

- name: "copy fedora messaging certificate"
  copy: src={{ private }}/files/rabbitmq/{{ env }}/pki/issued/{{ item.key }}{% if env == 'staging' %}.stg{% endif %}.crt
        dest=/etc/pki/rabbitmq/{{ item.username }}/{{ item.key }}.crt
        owner={{ item.username }} group=root mode=0644
  with_items: "{{ messaging.certificates }}"
  tags:
  - fedora-messaging

- name: "copy fedora messaging ca.crt for readers"
  copy: src={{ private }}/files/rabbitmq/{{ env }}/pki/ca.crt
        dest=/etc/pki/rabbitmq/{{ item.username }}/ca.crt
        owner={{ item.username }} group=root mode=0644
  with_items: "{{ messaging.certificates }}"
  tags:
  - fedora-messaging

- name: "install the toml configuration file"
  template: src="messaging.toml.j2" dest="/etc/fedora-messaging/{{ item.username }}.toml"
            owner=root group=root mode=644
  vars:
  - msg_username: "{{ item.username }}"
  - msg_appname: "{{ item.app_name }}"
  with_items: "{{ messaging.certificates }}"
  tags:
  - fedora-messaging

- name: "make sure the user exists on broker"
  include_role: name=rabbit/user
  vars:
  - username: "{{ item.username }}{{ env_suffix }}"
    sent_topics: "{{ item.sent_topics }}"
  with_items: "{{ messaging.certificates }}"
  tags:
  - fedora-messaging
