---
- name: "Creating smardigo user token"
  smardigo_user_token:
    secret:  "{{ connect_jwt_secret }}"
    user_id: "{{ connect_jwt_username }}"
  register: smardigo_user_token_result
  delegate_to: 127.0.0.1
  become: false
  tags:
    - update_deployment
  when:
    - scope_id is defined
    - process_instance_id is defined
    - smardigo_management_action is defined

- name: "Setting smardigo_auth_token_value as fact"
  set_fact:
    smardigo_auth_token_value: "{{ smardigo_user_token_result.token }}"
  tags:
    - update_deployment
  when:
    - scope_id is defined
    - process_instance_id is defined
    - smardigo_management_action is defined

- name: "Sending smardigo management message <{{ smardigo_management_action }}> to <{{ scope_id }}/{{ process_instance_id }}>"
  uri:
    url: "{{ shared_service_url_management }}/api/v1/scopes/{{ scope_id }}/processes/{{ process_instance_id }}/messages"
    method: POST
    body_format: json
    body: "{{ lookup('template','smardigo-management-message.json.j2') }}"
    headers:
      accept: "*/*"
      Content-Type: "application/json"
      Smardigo-User-Token: "{{ smardigo_auth_token_value }}"
    status_code: [200]
  delegate_to: 127.0.0.1
  register: sma_message
  retries: 5
  delay: 5
  until: sma_message.status in [200]
  when:
    - scope_id is defined
    - process_instance_id is defined
    - smardigo_management_action is defined
