# gce_net Integration Test
# Note: 'pause' is used during the Delete phase of the test.
# This is to ensure that the subnetwork is removed before removing
# the custom network.

# ============================================================
- name: test Create Legacy Network (change=true)
  gce_net:
    name: "{{ network_name_legacy }}"
    ipv4_range: '10.240.17.0/24'
    mode: legacy
    state: "present"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result

- name: "assert legacy network created"
  assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'

# ============================================================
- name: test Create Auto Network (change=true)
  gce_net:
    name: "{{ network_name_auto }}"
    mode: auto
    state: "present"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result

- name: "assert auto network created"
  assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'

# ============================================================
- name: test Create Network with custom Subnetwork (change=true)
  gce_net:
    name: "{{ network_name_custom }}"
    mode: custom
    subnet_name: "{{ subnetwork_name_custom }}"
    subnet_region: "{{ subnetwork_region }}"
    ipv4_range: '10.240.16.0/24'
    state: "present"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result

- name: "assert subnetwork network created"
  assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'

# ============================================================
- name: Create Firewall Rule (change=true)
  gce_net:
    name: default
    fwname: "{{ firewall_name }}"
    allowed: tcp:80
    state: "present"
    src_tags: "foo,bar"
    src_range: ['10.2.1.1/32']
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result

- name: "assert fw created"
  assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'

# ============================================================
- name: Update Firewall Rule (change=true)
  gce_net:
    name: default
    fwname: "{{ firewall_name }}"
    allowed: tcp:81
    state: "present"
    src_tags: "foo,bar,baz"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result

- name: "assert fw updated"
  assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'
      - 'result.src_tags|length == 3'

# ============================================================
- name: Update Firewall Rule w/icmp (no port) (change=true)
  gce_net:
    name: default
    fwname: "{{ firewall_name }}"
    allowed: tcp:81;icmp
    state: "present"
    src_tags: "foo,bar,baz"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result

- name: "assert fw updated"
  assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'
      - 'result.src_tags|length == 3'

# ============================================================
- name: Update Firewall Rule w/source range (change=true)
  gce_net:
    name: default
    fwname: "{{ firewall_name }}"
    allowed: tcp:81
    state: "present"
    src_tags: "foo,bar,baz"
    src_range: ['10.1.1.1/32']
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result

- name: "assert fw updated source range"
  assert:
    that:
      - 'result.changed'
      - 'result.state == "present"'
      - 'result.src_range|length == 1'

# ============================================================
- name: Delete Legacy Network (change=true)
  gce_net:
    name: "{{ network_name_legacy }}"
    state: "absent"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result
  tags: delete

- name: "assert legacy network deleted"
  tags: delete
  assert:
    that:
      - 'result.changed'
      - 'result.state == "absent"'
# ============================================================
- name: Delete auto Network (change=true)
  gce_net:
    name: "{{ network_name_auto }}"
    state: "absent"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result
  tags: delete

- name: "assert auto network deleted"
  tags: delete
  assert:
    that:
      - 'result.changed'
      - 'result.state == "absent"'

# ============================================================
- name: Delete SubNetwork of Custom Network (change=true)
  gce_net:
    name: "{{ network_name_custom }}"
    subnet_name: "{{ subnetwork_name_custom }}"
    subnet_region: "{{ subnetwork_region }}"
    state: "absent"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result
  tags: delete


- name: "assert custom subnetwork deleted"
  tags: delete
  assert:
    that:
      - 'result.changed'
      - 'result.state == "absent"'

# ============================================================
- pause: seconds=30

# ============================================================
- name: Delete Custom Network (change=true)
  gce_net:
    name: "{{ network_name_custom }}"
    state: "absent"
    service_account_email: "{{ service_account_email }}"
    pem_file: "{{ pem_file }}"
    project_id: "{{ project_id }}"
  register: result
  tags: delete

- name: "assert custom network deleted"
  tags: delete
  assert:
    that:
      - 'result.changed'
      - 'result.state == "absent"'