- set_fact:
    file: '/ansible_test_file.txt'
    directory: '/ansible_test_directory/'
    vsphere_connection: &vsphere_conn
      host: '{{ vcenter_ipaddress }}'
      username: '{{ vcenter_username }}'
      password: '{{ vcenter_password }}'
      datacenter: '{{ vcenter_datacenter }}'
      datastore: '{{ vcenter_datastore }}'
      validate_certs: false

- set_fact:
    vsphere_conection_file: &vsphere_conn_file
      <<: *vsphere_conn
      path: '{{ file }}'
    vsphere_conection_dir: &vsphere_conn_dir
      <<: *vsphere_conn
      path: '{{ directory }}'

# Clean up environment
- name: Delete file
  vsphere_file:
    <<: *vsphere_conn_file
    state: absent
  ignore_errors: true

- name: Delete directory
  vsphere_file:
    <<: *vsphere_conn_dir
    state: absent
  ignore_errors: true

# Test file operations
- name: Test file at start (check_mode)
  vsphere_file:
    <<: *vsphere_conn_file
    state: file
  check_mode: true
  ignore_errors: true
  register: cm_test_file_start

- name: Verify cm_test_file_start
  assert:
    that:
    - cm_test_file_start is failed
    - cm_test_file_start.state == 'absent'
    - cm_test_file_start.status == 404

- name: Test file at start (normal mode)
  vsphere_file:
    <<: *vsphere_conn_file
    state: file
  register: nm_test_file_start
  ignore_errors: true

- name: Verify nm_test_file_start
  assert:
    that:
    - nm_test_file_start is failed
    - nm_test_file_start.state == 'absent'
    - nm_test_file_start.status == 404

- name: Touch file (check_mode)
  vsphere_file:
    <<: *vsphere_conn_file
    state: touch
  check_mode: true
  register: cm_touch_file

- name: Verify cm_touch_file
  assert:
    that:
    - cm_touch_file is success
    - cm_touch_file is changed
    - cm_touch_file.reason == 'Created'
    - cm_touch_file.size == 0
    #- cm_touch_file.state == 'file'  # FIXME
    - cm_touch_file.status == 201

- name: Touch file (normal mode)
  vsphere_file:
    <<: *vsphere_conn_file
    state: touch
  register: nm_touch_file

- name: Verify nm_touch_file
  assert:
    that:
    - nm_touch_file is success
    - nm_touch_file is changed
    - nm_touch_file.reason == 'Created'
    - nm_touch_file.size == 0
    #- nm_touch_file.state == 'file'  # FIXME
    - nm_touch_file.status == 201

- name: Test file after touch (check_mode)
  vsphere_file:
    <<: *vsphere_conn_file
    state: file
  check_mode: true
  register: cm_test_file_touch

- name: Verify cm_test_file_touch
  assert:
    that:
    - cm_test_file_touch is success
    - cm_test_file_touch is not changed
    - cm_test_file_touch.size == 0
    #- cm_test_file_touch.state == 'file'  # FIXME
    - cm_test_file_touch.status == 200

- name: Test file after touch (normal mode)
  vsphere_file:
    <<: *vsphere_conn_file
    state: file
  register: nm_test_file_touch

- name: Verify nm_test_file_touch
  assert:
    that:
    - nm_test_file_touch is success
    - nm_test_file_touch is not changed
    - nm_test_file_touch.size == 0
    #- nm_test_file_touch.state == 'file'  # FIXME
    - nm_test_file_touch.status == 200

- name: Delete file (check_mode)
  vsphere_file:
    <<: *vsphere_conn_file
    state: absent
  check_mode: true
  register: cm_delete_file

- name: Verify cm_delete_file
  assert:
    that:
    - cm_delete_file is success
    - cm_delete_file is changed
    - cm_delete_file.reason == 'No Content'
    - cm_delete_file.size == None
    - cm_delete_file.state == 'absent'
    - cm_delete_file.status == 204

- name: Delete file (normal mode)
  vsphere_file:
    <<: *vsphere_conn_file
    state: absent
  register: nm_delete_file

- name: Verify nm_delete_file
  assert:
    that:
    - nm_delete_file is success
    - nm_delete_file is changed
    - nm_delete_file.reason == 'No Content'
    - nm_delete_file.size == None
    - nm_delete_file.state == 'absent'
    - nm_delete_file.status == 204

- name: Test file after delete (check_mode)
  vsphere_file:
    <<: *vsphere_conn_file
    state: file
  check_mode: true
  ignore_errors: true
  register: cm_test_file_delete

- name: Verify cm_test_file_delete
  assert:
    that:
    - cm_test_file_delete is failed
    - cm_test_file_delete.size == None
    - cm_test_file_delete.state == 'absent'
    - cm_test_file_delete.status == 404

- name: Test file after delete (normal mode)
  vsphere_file:
    <<: *vsphere_conn_file
    state: file
  ignore_errors: true
  register: nm_test_file_delete

- name: Verify nm_test_file_delete
  assert:
    that:
    - nm_test_file_delete is failed
    - nm_test_file_delete.size == None
    - nm_test_file_delete.state == 'absent'
    - nm_test_file_delete.status == 404

# Test directory operations
- name: Test directory at start (check_mode)
  vsphere_file:
    <<: *vsphere_conn_dir
    state: file
  check_mode: true
  ignore_errors: true
  register: cm_test_dir_start

- name: Verify cm_test_dir_start
  assert:
    that:
    - cm_test_dir_start is failed
    - cm_test_dir_start.size == None
    - cm_test_dir_start.state == 'absent'
    - cm_test_dir_start.status == 404

- name: Test directory at start (normal mode)
  vsphere_file:
    <<: *vsphere_conn_dir
    state: file
  ignore_errors: true
  register: nm_test_dir_start

# NOTE: Deleting directories is not implemented.
- name: Verify nm_test_dir_start
  assert:
    that:
    - nm_test_dir_start is failed
    - nm_test_dir_start.size == None
    - nm_test_dir_start.state == 'absent'
    - nm_test_dir_start.status == 404

- name: Create directory (check_mode)
  vsphere_file:
    <<: *vsphere_conn_dir
    path: '{{ directory }}'
    state: directory
  check_mode: true
  register: cm_create_dir

- name: Verify cm_create_dir
  assert:
    that:
    - cm_create_dir is success
    - cm_create_dir is changed
    - cm_create_dir.reason == 'Created'
    - cm_create_dir.size == None
    #- cm_create_dir.state == 'directory'  # FIXME
    - cm_create_dir.status == 201

- name: Create directory (normal mode)
  vsphere_file:
    <<: *vsphere_conn_dir
    path: '{{ directory }}'
    state: directory
  register: nm_create_dir

- name: Verify nm_create_dir
  assert:
    that:
    - nm_create_dir is success
    - nm_create_dir is changed
    - nm_create_dir.reason == 'Created'
    - nm_create_dir.size == None
    #- nm_create_dir.state == 'directory'  # FIXME
    - nm_create_dir.status == 201

- name: Test directory after create (check_mode)
  vsphere_file:
    <<: *vsphere_conn_dir
    path: '{{ directory }}'
    state: file
  check_mode: true
  register: cm_test_dir_create

- name: Verify cm_test_dir_create
  assert:
    that:
    - cm_test_dir_create is success
    - cm_test_dir_create is not changed
    #- cm_test_dir_create.size == 0
    #- cm_test_dir_create.state == 'file'  # FIXME
    - cm_test_dir_create.status == 200

- name: Test directory after create (normal mode)
  vsphere_file:
    <<: *vsphere_conn_dir
    path: '{{ directory }}'
    state: file
  register: nm_test_dir_create

- name: Verify nm_test_dir_create
  assert:
    that:
    - nm_test_dir_create is success
    - nm_test_dir_create is not changed
    #- nm_test_dir_create.size == 0
    #- nm_test_dir_create.state == 'file'  # FIXME
    - nm_test_dir_create.status == 200

- name: Delete directory (check_mode)
  vsphere_file:
    <<: *vsphere_conn_dir
    state: absent
  check_mode: true
  ignore_errors: true
  register: cm_delete_dir

- name: Verify cm_delete_dir
  assert:
    that:
    - cm_delete_dir is success
    - cm_delete_dir is changed
    - cm_delete_dir.reason == 'No Content'
    - cm_delete_dir.size == None
    - cm_delete_dir.state == 'absent'
    - cm_delete_dir.status == 204

- name: Delete directory (normal mode)
  vsphere_file:
    <<: *vsphere_conn_dir
    path: '{{ directory }}'
    state: absent
  ignore_errors: true
  register: nm_delete_dir

# NOTE: Deleting directories is not implemented
- name: Verify nm_delete_dir
  assert:
    that:
    - nm_delete_dir is failed  # FIXME
    #- nm_delete_dir is success
    #- nm_delete_dir is changed
    - nm_delete_dir.reason == 'Method Not Allowed'  # FIXME
    #- cm_delete_dir.reason == 'No Content'
    #- nm_delete_dir.size == None
    #- cm_delete_dir.state == 'absent'
    - nm_delete_dir.status == 405  # FIXME
    #- cm_delete_dir.status == 204

- name: Test directory after delete (check_mode)
  vsphere_file:
    <<: *vsphere_conn_dir
    path: '{{ directory }}'
    state: file
  check_mode: true
  ignore_errors: true
  register: cm_test_dir_delete

- name: Verify cm_test_dir_delete
  assert:
    that:
    - cm_test_dir_delete is success  # FIXME
    - cm_test_dir_delete is not changed  #FIXME
    #- cm_test_dir_delete is failed
    #- cm_test_dir_delete.size == None
    #- cm_test_dir_delete.state == 'file'
    - cm_test_dir_delete.status == 200  # FIXME
    #- nm_test_dir_delete.status == 404

- name: Test directory after delete (normal mode)
  vsphere_file:
    <<: *vsphere_conn_dir
    path: '{{ directory }}'
    state: file
  ignore_errors: true
  register: nm_test_dir_delete

- name: Verify nm_test_dir_delete
  assert:
    that:
    - nm_test_dir_delete is success  # FIXME
    - nm_test_dir_delete is not changed  #FIXME
    #- nm_test_dir_delete is failed
    #- nm_test_dir_delete.size == None
    #- nm_test_dir_delete.state == 'file'
    - nm_test_dir_delete.status == 200  # FIXME
    #- nm_test_dir_delete.status == 404
