---
  - name: check if name is a symlink
    stat:
      path: "{{ image_filename }}"
    register: symlnk_check

  - name: bypass image naming logic if image name is provided (backwards-compatibility)
    set_fact:
      amphora_image: "{{ amphora_image_name }}"
    when: amphora_image_name is defined and not amphora_image_name == ''

  - name: set the actual glance image name if it is a symlink
    set_fact:
      amphora_image: "{{ (symlnk_check.stat.lnk_target | basename | splitext)[0] }}"
    when: amphora_image is not defined and symlnk_check is defined and symlnk_check.stat.islnk

  - name: set the actual glance image name if it is not a symlink
    set_fact:
      amphora_image: "{{ (image_file_result.stat.path | basename | splitext)[0] }}"
    when: amphora_image is not defined and image_file_result.stat.exists and not symlnk_check.stat.islnk

  - name: gather facts about the service project
    shell: |
      openstack project show "{{ auth_project_name }}" -c id -f value
    register: project_id_result

  - name: check there's an image in glance already
    shell: |
      openstack image list --property owner={{ project_id_result.stdout }} --private --name {{ amphora_image }} -c ID -f value
    environment:
      OS_USERNAME: "{{ auth_username }}"
      OS_PASSWORD: "{{ auth_password }}"
      OS_PROJECT_NAME: "{{ auth_project_name }}"
    register: glance_id_result
    ignore_errors: true

  - name: set image id fact
    set_fact:
      image_id: "{{ glance_id_result.stdout }}"
    when: glance_id_result.rc == 0

  - name: get checksum if there's an image in glance already
    shell: |
      openstack image show {{ glance_id_result.stdout }} -c checksum -f value
    environment:
      OS_USERNAME: "{{ auth_username }}"
      OS_PASSWORD: "{{ auth_password }}"
      OS_PROJECT_NAME: "{{ auth_project_name }}"
    when: image_id is defined
    register: glance_results
    ignore_errors: true

  - name: set current_md5 fact from glance if image already exists there
    set_fact:
      current_md5: "{{ glance_results.stdout }}"
    when: glance_results.rc == 0

  - name: determine if the image needs to be replaced
    set_fact:
      replace_image: "{{ current_md5 != image_file_result.stat.md5 }}"
    when: current_md5 is defined

  - name: move existing image if the names match and the md5s are not the same
    shell: |
      ts=`openstack image show {{ image_id }} -f value -c created_at`
      ts=${ts//:/}
      ts=${ts//-/}
      openstack image set {{ image_id }} --name "{{ amphora_image }}_$ts"
    environment:
      OS_USERNAME: "{{ auth_username }}"
      OS_PASSWORD: "{{ auth_password }}"
      OS_PROJECT_NAME: "{{ auth_project_name }}"
    when: replace_image is defined and replace_image

  - name: decide whether to upload new image
    set_fact:
      upload_image: true
    when: current_md5 is not defined or replace_image

  - block:
      - name: create temporary directory
        tempfile:
          state: directory
        register: amp_tmp_dir

      - name: set RAW file name
        set_fact:
          raw_filename: "{{ amp_tmp_dir.path }}/{{ image_filename|splitext|first|basename }}.img"

      - name: convert image from qcow2 to raw
        shell: |
          qemu-img convert -f qcow2 -O raw {{ image_filename }} {{ raw_filename }}

      - name: setting amphora format to raw
        set_fact:
          raw_format: raw
    when: amp_to_raw

  - name: upload image to glance
    shell: |
      openstack image create --disk-format {{ raw_format|default('qcow2') }} \
        --container-format bare --tag {{ amp_image_tag }} \
        --file {{ raw_filename|default(image_filename) }} \
        --property hw_architecture={{ amp_hw_arch }} \
        --private {{ amphora_image }}
    environment:
      OS_USERNAME: "{{ auth_username }}"
      OS_PASSWORD: "{{ auth_password }}"
      OS_PROJECT_NAME: "{{ auth_project_name }}"
    register: image_result
    changed_when: "image_result.stdout != ''"
    when: image_file_result.stat.exists and upload_image is defined

  - name: delete converted raw image
    file:
      path: "{{ amp_tmp_dir.path }}"
      state: absent
    when: amp_to_raw
