---

### tags:
###   update_configurations

- name: "Running all block tasks on locahost"
  delegate_to: 127.0.0.1
  become: false
  block:

  - name: "Checking connect is running on <{{ connect_base_url }}>"
    uri:
      url: "{{ http_s }}://{{ connect_base_url }}/api/profile-info"
      method: GET
      headers:
        "Smardigo-User-Token": "{{ smardigo_auth_token_value }}"
      status_code: [200]
    register: connect_profile_info
    delay: 15
    retries: 10
    until: connect_profile_info.status in [200]
    tags:
      - update_configurations

  - name: "Creating archives of smardigo configuration"
    ansible.builtin.tempfile:
      state: directory
      suffix: temp
    register: temp
    tags:
      - update_configurations
  
  - name: "Creating archives of smardigo configuration"
    archive:
      path: "./smardigo/{{ item }}"
      dest: "{{ temp.path }}/{{ item }}.zip"
      format: zip
      mode: '0644'
    with_items: "{{ management_configurations }}"
    tags:
      - update_configurations
  
  - name: "Upload configuration zip file to <{{ connect_base_url }}>"
    uri:
      url: "{{ http_s }}://{{ connect_base_url }}/api/v1/config/import-zip"
      method: POST
      headers:
        Smardigo-User-Token: "{{ smardigo_auth_token_value }}"
      body_format: form-multipart
      body:
        file:
          filename: "{{ temp.path }}/{{ item }}.zip"
          mime_type: "application/zip"
      status_code: [200]
    register: config_import_result
    with_items: "{{ management_configurations }}"
    tags:
      - update_configurations
  
  - name: "Upload configuration zip file to <{{ connect_base_url }}>"
    debug:
      msg: '{{ config_import_result }}'
    tags:
      - update_configurations
