- name: Create random domain name
  set_fact:
    domain_name: "{{ resource_group | hash('md5') | truncate(16, True, '') + (65535 | random | string) }}"

- name: Create a DNS zone
  azure_rm_dnszone:
    resource_group: "{{ resource_group }}"
    name: "{{ domain_name }}.com"
    state: present
  register: results

- name: Assert that DNS zone was created
  assert:
    that: results.changed

- name: create "A" record set with multiple records
  azure_rm_dnsrecordset:
    resource_group: "{{ resource_group }}"
    relative_name: www
    zone_name: "{{ domain_name }}.com"
    record_type: A
    records:
      - entry: 192.168.100.101
      - entry: 192.168.100.102
      - entry: 192.168.100.103
  register: results

- name: Assert that A record set was created
  assert:
    that: results.changed

- name: re-run "A" record with same values
  azure_rm_dnsrecordset:
    resource_group: "{{ resource_group }}"
    relative_name: www
    zone_name: "{{ domain_name }}.com"
    record_type: A
    records:
      - entry: 192.168.100.101
      - entry: 192.168.100.102
      - entry: 192.168.100.103
  register: results

- name: Assert that A record set was not changed
  assert:
    that: not results.changed

- name: Update "A" record set with additional record
  azure_rm_dnsrecordset:
    resource_group: "{{ resource_group }}"
    relative_name: www
    zone_name: "{{ domain_name }}.com"
    record_type: A
    record_mode: append
    records:
      - entry: 192.168.100.104
  register: results

- name: Assert that new record was appended
  assert:
    that: 
      - results.changed

- name: re-update "A" record set with additional record
  azure_rm_dnsrecordset:
    resource_group: "{{ resource_group }}"
    relative_name: www
    zone_name: "{{ domain_name }}.com"
    record_type: A
    record_mode: append
    records:
      - entry: 192.168.100.104
  register: results

- name: Assert that A record set was not changed
  assert:
    that:
      - not results.changed

- name: Remove 1 record from record set
  azure_rm_dnsrecordset:
    resource_group: "{{ resource_group }}"
    relative_name: www
    zone_name: "{{ domain_name }}.com"
    record_type: A
    records:
      - entry: 192.168.100.101
      - entry: 192.168.100.102
      - entry: 192.168.100.103
  register: results

- name: Assert that record was deleted
  assert:
    that:
      - results.changed

- name: Check_mode test
  azure_rm_dnsrecordset:
    resource_group: "{{ resource_group }}"
    relative_name: www
    zone_name: "{{ domain_name }}.com"
    record_type: A
    records:
      - entry: 192.168.100.105
  check_mode: yes
  register: results

- name: Assert that check_mode returns new state
  assert:
    that: 
      - results.changed

# FUTURE: add facts module calls to ensure that we really didn't touch anything

- name: delete a record set
  azure_rm_dnsrecordset:
    resource_group: "{{ resource_group }}"
    relative_name: www
    zone_name: "{{ domain_name }}.com"
    record_type: A
    state: absent
  register: results

- name: Assert that record set deleted
  assert:
    that: results.changed

- name: (idempotence test) re-run record set absent
  azure_rm_dnsrecordset:
    resource_group: "{{ resource_group }}"
    relative_name: www
    zone_name: "{{ domain_name }}.com"
    record_type: A
    state: absent
  register: results

- name:
  assert:
    that: not results.changed

- name: create SRV records in a new record set
  azure_rm_dnsrecordset:
    resource_group: "{{ resource_group }}"
    relative_name: "_sip._tcp.{{ domain_name }}.com"
    zone_name: "{{ domain_name }}.com"
    time_to_live: 7200
    record_type: SRV
    state: present
    records:
    - entry: sip.{{ domain_name }}.com
      priority: 20
      weight: 10
      port: 5060
  register: results

- name: Assert that SRV record set was created
  assert:
    that: 
      - results.changed

- name: Delete DNS zone
  azure_rm_dnszone:
    resource_group: "{{ resource_group }}"
    name: "{{ domain_name }}.com"
    state: absent
