---
######## A record tests #################

- name: "Test: A record creation"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 127.0.0.1
    ttl: 150
  register: cloudflare_dns

- name: "Validate: A record creation"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == '127.0.0.1'
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.type == 'A'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: A record idempotency"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 127.0.0.1
    ttl: 150
  register: cloudflare_dns

- name: "Validate: A record idempotency"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed

- name: "Test: A record update"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 127.0.0.1
    ttl: 300
  register: cloudflare_dns

- name: "Validate: A record update"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.ttl == 300

- name: "Test: A record duplicate (create new record)"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 127.0.1.1
    ttl: 150
  register: cloudflare_dns

- name: "Validate: A record duplicate (create new record)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == '127.0.1.1'
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.type == 'A'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: A record duplicate (old record present)"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 127.0.0.1
    ttl: 300
  register: cloudflare_dns

- name: "Validate: A record duplicate (old record present)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed
      - cloudflare_dns.result.record.content == '127.0.0.1'
      - cloudflare_dns.result.record.ttl == 300
      - cloudflare_dns.result.record.type == 'A'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: A record duplicate (make new record solo)"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 127.0.1.1
    ttl: 150
    solo: true
  register: cloudflare_dns

- name: "Validate: A record duplicate (make new record solo)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == '127.0.1.1'
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.type == 'A'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: A record duplicate (old record absent)"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 127.0.0.1
    ttl: 300
    state: absent
  register: cloudflare_dns

- name: "Validate: A record duplicate (old record absent)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed

- name: "Test: A record deletion"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 127.0.1.1
    ttl: 150
    state: absent
  register: cloudflare_dns

- name: "Validate: A record deletion"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed

- name: "Test: A record deletion succeeded"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 127.0.1.1
    ttl: 150
    state: absent
  register: cloudflare_dns

- name: "Validate: A record deletion succeeded"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed

- name: "Test: proxiable A record creation"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 192.0.2.47
    ttl: 150
  register: cloudflare_dns

- name: "Validate: proxiable A record creation"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == '192.0.2.47'
      - cloudflare_dns.result.record.proxiable == true
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.type == 'A'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: proxiable A record creation succeeded"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 192.0.2.47
    ttl: 150
  register: cloudflare_dns

- name: "Validate: proxiable A record creation succeeded"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed

- name: "Test: Enable A record proxied status"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 192.0.2.47
    proxied: yes
  register: cloudflare_dns

- name: "Validate: Enable A record proxied status"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == '192.0.2.47'
      - cloudflare_dns.result.record.proxied == true
      - cloudflare_dns.result.record.type == 'A'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: Enable A record proxied status succeeded"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 192.0.2.47
    proxied: yes
  register: cloudflare_dns

- name: "Validate: Enable A record proxied status succeeded"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed

- name: "Test: Disable A record proxied status"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 192.0.2.47
    proxied: no
  register: cloudflare_dns

- name: "Validate: Enable A record proxied status"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == '192.0.2.47'
      - cloudflare_dns.result.record.proxied == false
      - cloudflare_dns.result.record.type == 'A'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: Disable A record proxied status succeeded"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    value: 192.0.2.47
    proxied: no
  register: cloudflare_dns

- name: "Validate: Enable A record proxied status succeeded"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed

- name: "Test: Full A record deletion"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    state: absent
  register: cloudflare_dns

- name: "Validate: Full A record deletion"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed

- name: "Test: Full A record deletion succeeded"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: A
    state: absent
  register: cloudflare_dns

- name: "Validate: Full A record deletion succeeded"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed
