---
######## SRV record tests #################

- name: "Test: SRV record creation"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: SRV
    value: "srv1.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}."
    ttl: 150
    priority: 20
    service: srv1
    proto: tcp
    port: 3500
    weight: 5
  register: cloudflare_dns

- name: "Validate: SRV record creation"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == '5\t3500\tsrv1.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.data.target == 'srv1.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.data.port == 3500
      - cloudflare_dns.result.record.data.weight == 5
      - cloudflare_dns.result.record.data.priority == 20
      - cloudflare_dns.result.record.data.name == "{{ cloudflare_dns_record  }}"
      - cloudflare_dns.result.record.data.proto == '_tcp'
      - cloudflare_dns.result.record.data.service == '_srv1'
      - cloudflare_dns.result.record.type == 'SRV'
      - cloudflare_dns.result.record.name == "_srv1._tcp.{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: SRV record idempotency"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: SRV
    value: "srv1.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}."
    ttl: 150
    priority: 20
    service: srv1
    proto: tcp
    port: 3500
    weight: 5
  register: cloudflare_dns

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

# changing the following attributes creates a new record:
#  weight
#  port
#  value
#  service
#  proto
- name: "Test: SRV record update"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: SRV
    value: "srv1.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 300
    priority: 10
    service: srv1
    proto: tcp
    port: 3500
    weight: 5
  register: cloudflare_dns

- name: "Validate: SRV record update"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.ttl == 300
      - cloudflare_dns.result.record.data.target == 'srv1.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.data.port == 3500
      - cloudflare_dns.result.record.data.weight == 5
      - cloudflare_dns.result.record.data.priority == 10
      - cloudflare_dns.result.record.data.name == "{{ cloudflare_dns_record  }}"
      - cloudflare_dns.result.record.data.proto == '_tcp'
      - cloudflare_dns.result.record.data.service == '_srv1'

- name: "Test: SRV 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: SRV
    value: "srv2.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 150
    priority: 30
    service: srv1
    proto: tcp
    port: 9999
    weight: 19
  register: cloudflare_dns

- name: "Validate: SRV record duplicate (create new record)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == '19\t9999\tsrv2.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.data.target == 'srv2.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.data.port == 9999
      - cloudflare_dns.result.record.data.weight == 19
      - cloudflare_dns.result.record.data.priority == 30
      - cloudflare_dns.result.record.data.name == "{{ cloudflare_dns_record  }}"
      - cloudflare_dns.result.record.data.proto == '_tcp'
      - cloudflare_dns.result.record.data.service == '_srv1'
      - cloudflare_dns.result.record.type == 'SRV'
      - cloudflare_dns.result.record.name == "_srv1._tcp.{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: SRV 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: SRV
    value: "srv1.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 300
    priority: 10
    service: srv1
    proto: tcp
    port: 3500
    weight: 5
  register: cloudflare_dns

- name: "Validate: SRV record duplicate (old record present)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is not changed
      - cloudflare_dns.result.record.content == '5\t3500\tsrv1.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.ttl == 300
      - cloudflare_dns.result.record.data.target == 'srv1.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.data.port == 3500
      - cloudflare_dns.result.record.data.weight == 5
      - cloudflare_dns.result.record.data.priority == 10
      - cloudflare_dns.result.record.data.name == "{{ cloudflare_dns_record  }}"
      - cloudflare_dns.result.record.data.proto == '_tcp'
      - cloudflare_dns.result.record.data.service == '_srv1'
      - cloudflare_dns.result.record.type == 'SRV'
      - cloudflare_dns.result.record.name == "_srv1._tcp.{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: SRV 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: SRV
    value: "srv2.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 150
    priority: 30
    service: srv1
    proto: tcp
    port: 9999
    weight: 19
    solo: true
  register: cloudflare_dns

- name: "Validate: SRV record duplicate (make new record solo)"
  assert:
    that:
      - cloudflare_dns is successful
      - cloudflare_dns is changed
      - cloudflare_dns.result.record.content == '19\t9999\tsrv2.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.ttl == 150
      - cloudflare_dns.result.record.data.target == 'srv2.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}'
      - cloudflare_dns.result.record.data.port == 9999
      - cloudflare_dns.result.record.data.weight == 19
      - cloudflare_dns.result.record.data.priority == 30
      - cloudflare_dns.result.record.data.name == "{{ cloudflare_dns_record  }}"
      - cloudflare_dns.result.record.data.proto == '_tcp'
      - cloudflare_dns.result.record.data.service == '_srv1'
      - cloudflare_dns.result.record.type == 'SRV'
      - cloudflare_dns.result.record.name == "_srv1._tcp.{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"

- name: "Test: SRV 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: SRV
    value: "srv1.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 300
    priority: 10
    service: srv1
    proto: tcp
    port: 3500
    weight: 5
    state: absent
  register: cloudflare_dns

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

- name: "Test: SRV record deletion"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: SRV
    value: "srv2.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 150
    priority: 30
    service: srv1
    proto: tcp
    port: 9999
    weight: 19
    state: absent
  register: cloudflare_dns

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

- name: "Test: SRV record deletion succeeded"
  cloudflare_dns:
    account_email: "{{ cloudflare_email }}"
    account_api_token: "{{ cloudflare_api_token }}"
    zone: "{{ cloudflare_zone }}"
    record: "{{ cloudflare_dns_record  }}"
    type: SRV
    value: "srv2.{{ cloudflare_dns_record  }}.{{ cloudflare_zone }}"
    ttl: 150
    priority: 30
    service: srv1
    proto: tcp
    port: 9999
    weight: 19
    state: absent
  register: cloudflare_dns

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