# gcdns Integration Test Playbook
# ============================================================
- name: Creating a zone without parameters
  gcdns_zone:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result
  ignore_errors: true
  tags:
    - param-check
- name: Assert created a zone without parameters
  assert:
    that:
       - 'result.changed == False'
       - 'result.msg == "missing required arguments: zone"'
# ============================================================
- name: Creating a zone with a description
  gcdns_zone:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone: "{{ dnszone_name }}"
    description: "This is an awesome zone"
  register: result
  tags:
    - zone
- name: Assert created a zone with a description
  assert:
    that:
       - 'result.changed'
       - 'result.zone == "{{ dnszone_name }}"'
       - 'result.description == "This is an awesome zone"'

# ============================================================
- name: Create an A record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone: "{{ dnszone_name }}"
    record: "www1.{{ dnszone_name }}"
    type: A
    value: '1.2.3.4'
  register: result
  tags:
    - A
    - record
- name: Assert creation of an A record
  assert:
    that:
       - 'result.changed'
       - 'result.ttl == 300'
       - 'result.type == "A"'
       - 'result.record == "www1.{{ dnszone_name }}"'
       - 'result.record_data|length == 1'
       - 'result.record_data[0] == "1.2.3.4"'

# ============================================================
- name: Overwrite an A record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone: "{{ dnszone_name }}"
    record: "www1.{{ dnszone_name }}"
    type: A
    value: '5.6.7.8'
    overwrite: true
  register: result
  tags:
    - A
    - record
- name: Assert overwrite of an A record
  assert:
    that:
       - 'result.changed'
       - 'result.ttl == 300'
       - 'result.type == "A"'
       - 'result.record == "www1.{{ dnszone_name }}"'
       - 'result.record_data|length == 1'
       - 'result.record_data[0] == "5.6.7.8"'

# ============================================================       
- name: Create a CNAME record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone_id: "{{ dnszone_id }}"
    record: "www2.{{ dnszone_name }}"
    type: CNAME
    value: "www.{{ dnszone_name }}"
  register: result
  tags:
    - CNAME
    - record
- name: Assert creation of CNAME record
  assert:
    that:
       - 'result.changed'
       - 'result.type == "CNAME"'
       - 'result.zone_id == "{{ dnszone_id }}"'
       - 'result.record == "www2.{{ dnszone_name }}"'
       - 'result.record_data[0] == "www.{{ dnszone_name }}"'

# ============================================================    
- name: Create an MX record with a custom TTL
  gcdns_record:
      service_account_email: "{{ service_account_email }}"
      pem_file: "{{ pem_file }}"
      project_id: "{{ project_id }}"
      zone_id: "{{ dnszone_id }}"
      record: "{{ dnszone_name }}"
      type: MX
      ttl: 3600
      value: "10 mail.{{ dnszone_name }}"
  register: result
  tags:
    - MX
    - record
- name: Assert creation of MX record
  assert:
    that:
       - 'result.changed'
       - 'result.type == "MX"'
       - 'result.zone_id == "{{ dnszone_id }}"'
       - 'result.record_data[0] == "10 mail.{{ dnszone_name }}"'

# ============================================================    
- name: Create multiple A records with the same name
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone_id: "{{ dnszone_id }}"
    record: "{{ dnszone_name }}"
    type: A
    record_data:
      - '192.0.2.23'
      - '10.4.5.6'
      - '198.51.100.5'
      - '203.0.113.10'
  register: result
  tags:
    - multi-A
    - record
- name: Assert creation of multiple A records with the same name
  assert:
    that:
       - 'result.changed'
       - 'result.zone == "{{ dnszone_name }}"'
       - 'result.record == "{{ dnszone_name }}"'
       - 'result.record_data|length == 4'

# ============================================================      
- name: Change the value of an existing record with multiple values
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone_id: "{{ dnszone_id }}"
    record: "{{ dnszone_name }}"
    type: A
    record_data:
      - '192.0.2.23'
      - '192.0.2.42'
      - '198.51.100.5'
      - '203.0.113.10'
    overwrite: true
  register: result
  tags:
    - multi-A
    - record

- name: Assert 
  assert:
    that:
       - 'result.changed'
       - 'result.record_data|length == 4'

# ============================================================    
- name: Create an AAAA record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone: "{{ dnszone_name }}"
    record: "www1.{{ dnszone_name }}"
    type: AAAA
    value: "fd00:db8::1"
  register: result
  tags:
    - AAAA
    - record
- name: Assert creation of AAAA record
  assert:
    that:
       - 'result.changed'
       - 'result.type == "AAAA"'
       - 'result.zone == "{{ dnszone_name }}"'
       - 'result.zone_id == "{{ dnszone_id }}"'
       - 'result.record == "www1.{{ dnszone_name }}"'
       - 'result.record_data[0] == "fd00:db8::1"'

# ============================================================    
- name: Create a PTR record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone: "{{ dnszone_name }}"
    record: "{{ dnszone_name }}"
    type: PTR
    value: "10.5.168.192.{{ dnszone_name }}arpa."
  register: result
  tags:
    - PTR
    - record
- name: Assert creation of PTR record
  assert:
    that:
       - 'result.changed'
       - 'result.record_data[0] == "10.5.168.192.{{ dnszone_name }}arpa."'

# ============================================================    
- name: Create an NS record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone: "{{ dnszone_name }}"
    record: "subdomain.{{ dnszone_name }}"
    type: NS
    ttl: 21600
    record_data:
      - 'ns-cloud-d1.googledomains.com.'
      - 'ns-cloud-d2.googledomains.com.'
      - 'ns-cloud-d3.googledomains.com.'
      - 'ns-cloud-d4.googledomains.com.'
  register: result
  tags:
    - NS
    - record
- name: Assert creation of NS record
  assert:
    that:
       - 'result.changed'
       - 'result.record_data|length == 4'
       - 'result.ttl == 21600'
       - 'result.type == "NS"'
# ============================================================
- name: Create a TXT record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone_id: "{{ dnszone_id }}"
    record: "{{ dnszone_name }}"
    type: TXT
    record_data:
      - '"v=spf1 include:_spf.google.com -all"'
      - '"hello " "world"'
  register: result
  tags:
    - TXT
    - record
- name: Assert creation of TXT record
  assert:
    that:
       - 'result.changed'
       - 'result.zone_id == "{{ dnszone_id }}"'
       - 'result.record_data|length == 2'

# ============================================================
- name: Remove an A record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone: "{{ dnszone_name }}"
    record: "www1.{{ dnszone_name }}"
    type: A
    value: "5.6.7.8"
    state: absent
  register: result
  tags:
    - A
    - record
- name: Assert removal of A record
  assert:
    that:
       - 'result.changed'
       - 'result.state == "absent"'
# ============================================================
- name: Remove a CNAME record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone_id: "{{ dnszone_id }}"
    record: "www2.{{ dnszone_name }}"
    type: CNAME
    value: "www.{{ dnszone_name }}"
    state: absent
  register: result
  tags:
    - CNAME
    - record
- name: Assert removal of CNAME record
  assert:
    that:
       - 'result.changed'
       - 'result.state == "absent"'

# ============================================================    
- name: Remove an MX record with a custom TTL.
  gcdns_record:
      service_account_email: "{{ service_account_email }}"
      pem_file: "{{ pem_file }}"
      project_id: "{{ project_id }}"
      zone_id: "{{ dnszone_id }}"
      record: "{{ dnszone_name }}"
      type: MX
      ttl: 3600
      value: "10 mail.{{ dnszone_name }}"
      state: absent
  register: result
  tags:
    - MX
    - record
- name: Assert removal of MX record
  assert:
    that:
       - 'result.changed'
       - 'result.state == "absent"'

# ============================================================        
- name: Remove multiple A records with the same name.
  gcdns_record:
      service_account_email: "{{ service_account_email }}"
      pem_file: "{{ pem_file }}"
      project_id: "{{ project_id }}"
      zone_id: "{{ dnszone_id }}"
      record: "{{ dnszone_name }}"
      type: A
      record_data:
        - '192.0.2.23'
        - '192.0.2.42'
        - '198.51.100.5'
        - '203.0.113.10'
      state: absent
  register: result
  tags:
    - multi-A
    - record
- name: Assert 
  assert:
    that:
       - 'result.changed'
       - 'result.state == "absent"'


# ============================================================    
- name: Remove an AAAA record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    record: "www1.{{ dnszone_name }}"
    zone: "{{ dnszone_name }}"
    type: AAAA
    value: 'fd00:db8::1'
    state: absent
  register: result
  tags:
    - AAAA
    - record
- name: Assert 
  assert:
    that:
       - 'result.changed'
       - 'result.state == "absent"'

# ============================================================    
- name: Remove a PTR record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    record: "{{ dnszone_name }}"
    zone: "{{ dnszone_name }}"
    type: PTR
    value: "10.5.168.192.{{ dnszone_name }}arpa."
    state: absent
  register: result
  tags:
    - PTR
    - record
- name: Assert remove a PTR record
  assert:
    that:
       - 'result.changed'
       - 'result.state == "absent"'

# ============================================================    
- name: Remove an NS record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone: "{{ dnszone_name }}"
    record: "subdomain.{{ dnszone_name }}"
    type: NS
    ttl: 21600
    record_data:
      - 'ns-cloud-d1.googledomains.com.'
      - 'ns-cloud-d2.googledomains.com.'
      - 'ns-cloud-d3.googledomains.com.'
      - 'ns-cloud-d4.googledomains.com.'
    state: absent
  register: result
  tags:
    - NS
    - record
- name: Assert removal of NS record
  assert:
    that:
       - 'result.changed'
       - 'result.state == "absent"'
# ============================================================
- name: Remove a TXT record
  gcdns_record:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    record: '{{ dnszone_name }}'
    zone_id: "{{ dnszone_id }}"
    type: TXT
    record_data:
      - '"v=spf1 include:_spf.google.com -all"'
      - '"hello " "world"'
    state: absent
  register: result
  tags:
    - TXT
    - record
- name: Assert removed  TXT record
  assert:
    that:
       - 'result.changed'
       - 'result.state == "absent"'
       - 'result.record_data|length == 2'

# ============================================================    
- name: Remove the zone
  gcdns_zone:
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
    zone: "{{ dnszone_name }}"
    state: absent
  register: result
  ignore_errors: true
  tags:
    - zone
- name: Assert removal of zone
  assert:
    that:
       - 'result.changed'
       - 'result.state == "absent"'
