# TODO: need tests for read/write mode.

# ============================================================
- name: test missing name
  gce_pd:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result
  ignore_errors: true

- name: assert failure when called with no parameters
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "missing required arguments: name"'

# ============================================================
- name: test state=present (expected changed=true)
  gce_pd:
    name: "{{ instance_name }}"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: present
  register: result

- name: assert state=present (expected changed=true)
  assert:
    that:
       - 'result.changed'
       - 'result.name == "{{ instance_name }}"'
       - 'result.size_gb == 10'  # default size
       - 'result.zone == "us-central1-b"'  # default zone
       - 'result.state == "present"'

# ============================================================
- name: test state=present (expected changed=false)
  gce_pd:
    name: "{{ instance_name }}"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: present
  register: result

- name: assert state=present (expected changed=false)
  assert:
    that:
       - 'not result.changed'
       - 'result.name == "{{ instance_name }}"'
       - 'result.state == "present"'

# ============================================================
- name: test state=absent (expected changed=true)
  gce_pd:
    name: "{{ instance_name }}"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: absent
  register: result

- name: assert state=absent (expected changed=true)
  assert:
    that:
       - 'result.changed'
       - 'result.name == "{{ instance_name }}"'
       - 'result.state == "absent"'

# ============================================================
- name: test state=absent (expected changed=false)
  gce_pd:
    name: "{{ instance_name }}"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: absent
  register: result

- name: assert state=absent (expected changed=false)
  assert:
    that:
       - 'not result.changed'
       - 'result.name == "{{ instance_name }}"'
       - 'result.state == "absent"'

# ============================================================
- name: test non-default size/zone
  gce_pd:
    name: "{{ instance_name }}"
    size_gb: 5
    zone: us-central1-a
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: present
  register: result

- name: assert non-default size/zone
  assert:
    that:
       - 'result.changed'
       - 'result.name == "{{ instance_name }}"'
       - 'result.size_gb == 5'
       - 'result.zone == "us-central1-a"'
       - 'result.state == "present"'

# ============================================================
- name: test non-default size/zone (state=absent)
  gce_pd:
    name: "{{ instance_name }}"
    size_gb: 5
    zone: us-central1-a
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: absent
  register: result

- name: assert non-default size/zone (state=absent)
  assert:
    that:
       - 'result.changed'
       - 'result.name == "{{ instance_name }}"'
       - 'result.state == "absent"'

# ============================================================
- name: test image given (state=present)
  gce_pd:
    name: "{{ instance_name }}"
    image: debian-7
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: present
  register: result

- name: assert image given (state=present)
  assert:
    that:
       - 'result.changed'
       - 'result.name == "{{ instance_name }}"'
       - 'result.image == "debian-7"'
       - 'result.state == "present"'

# ============================================================
- name: test image given (state=absent)
  gce_pd:
    name: "{{ instance_name }}"
    image: debian-7
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: absent
  register: result

- name: assert image given (state=absent)
  assert:
    that:
       - 'result.changed'
       - 'result.name == "{{ instance_name }}"'
       - 'result.state == "absent"'

# ============================================================
- name: test snapshot given (state=present)
  gce_pd:
    name: "{{ instance_name }}"
    snapshot: "{{ instance_name }}-snapshot"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: present
  register: result

- name: assert image given (state=present)
  assert:
    that:
       - 'result.changed'
       - 'result.name == "{{ instance_name }}"'
       - 'result.snapshot == "{{ instance_name }}-snapshot"'
       - 'result.state == "present"'

# ============================================================
- name: test snapshot given (state=absent)
  gce_pd:
    name: "{{ instance_name }}"
    snapshot: "{{ instance_name }}-snapshot"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: absent
  register: result

- name: assert image given (state=absent)
  assert:
    that:
       - 'result.changed'
       - 'result.name == "{{ instance_name }}"'
       - 'result.state == "absent"'

# ============================================================
- name: test both image and snapshot given
  gce_pd:
    name: "{{ instance_name }}"
    image: "debian-7"
    snapshot: "{{ instance_name }}-snapshot"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    state: present
  register: result
  ignore_errors: true

- name: assert image given (state=present)
  assert:
    that:
       - 'result.failed'
       - 'result.msg == "Cannot give both image (debian-7) and snapshot ({{ instance_name }}-snapshot)"'
