---
######## CNAME record tests #################

# the '.' at the end of the value is intentional
# it must be verified that the '.' will be stripped
- name: "Test: CNAME record creation"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: CNAME
    value: "srv1.{{ cloudflare_zone }}."
    ttl: 150
  register: cloudflare_dns

- name: "Validate: CNAME record creation"
  assert:
    that:
      - cloudflare_dns|success
      - cloudflare_dns|changed
      - cloudflare_dns.result.record.content == "srv1.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.type == 'CNAME'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

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

- name: "Validate: CNAME record idempotency"
  assert:
    that:
      - cloudflare_dns|success
      - not cloudflare_dns|changed

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

- name: "Validate: CNAME record update"
  assert:
    that:
      - cloudflare_dns|success
      - cloudflare_dns|changed
      - cloudflare_dns.result.record.content == "srv2.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.ttl == 300

- name: "Test: CNAME 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: CNAME
    value: "srv3.{{ cloudflare_zone }}"
    ttl: 600
    solo: true
  register: cloudflare_dns

- name: "Validate: CNAME record duplicate (make new record solo)"
  assert:
    that:
      - cloudflare_dns|success
      - cloudflare_dns|changed
      - cloudflare_dns.result.record.content == "srv3.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.ttl == 600
      - cloudflare_dns.result.record.type == 'CNAME'
      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: CNAME 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: CNAME
    value: "srv2.{{ cloudflare_zone }}"
    ttl: 300
    state: absent
  register: cloudflare_dns

- name: "Validate: CNAME record duplicate (old record absent)"
  assert:
    that:
      - cloudflare_dns|success
      - not cloudflare_dns|changed

- name: "Test: CNAME record deletion"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: CNAME
    value: "srv3.{{ cloudflare_zone }}"
    ttl: 600
    state: absent
  register: cloudflare_dns

- name: "Validate: CNAME record deletion"
  assert:
    that:
      - cloudflare_dns|success
      - cloudflare_dns|changed

- name: "Test: CNAME record deletion succeeded"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: CNAME
    value: "srv3.{{ cloudflare_zone }}"
    ttl: 600
    state: absent
  register: cloudflare_dns

- name: "Validate: CNAME record deletion succeeded"
  assert:
    that:
      - cloudflare_dns|success
      - not cloudflare_dns|changed
