---
- name: setup ensure the test network is absent
  cs_network:
    name: ipaddr_test_network
    state: absent
    zone: "{{ cs_common_zone_adv }}"

- name: setup create the test network
  cs_network:
    name: ipaddr_test_network
    network_offering: DefaultIsolatedNetworkOfferingWithSourceNatService
    state: present
    zone: "{{ cs_common_zone_adv }}"
  register: base_network
- name: setup verify create the test network
  assert:
    that:
    - base_network is successful

- name: setup instance to get network in implementation state
  cs_instance:
    name: "{{ cs_resource_prefix }}-vm-cs-ip-address"
    template: "{{ cs_common_template }}"
    service_offering: "{{ cs_common_service_offering }}"
    zone: "{{ cs_common_zone_adv }}"
    networks:
      - "{{ base_network.name }}"
  register: instance
  until: instance is success
  retries: 20
  delay: 5
- name: verify instance setup
  assert:
    that:
    - instance is successful

- name: setup clean ip_address with tags
  cs_ip_address:
    state: absent
    tags:
      - key: unique_id
        value: "adacd65e-7868-5ebf-9f8b-e6e0ea779861"
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"

- name: setup associate ip_address for SNAT
  cs_ip_address:
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"
  register: ip_address_snat

- name: test associate ip_address in check mode
  cs_ip_address:
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"
  check_mode: true
  register: ip_address
- name: verify test associate ip_address in check mode
  assert:
    that:
    - ip_address is successful
    - ip_address is changed

- name: test associate ip_address
  cs_ip_address:
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"
  register: ip_address
- name: verify test associate ip_address
  assert:
    that:
    - ip_address is successful
    - ip_address is changed
    - ip_address.ip_address is defined

- name: test associate ip_address with tags in check mode
  cs_ip_address:
    network: ipaddr_test_network
    tags:
      - key: unique_id
        value: "adacd65e-7868-5ebf-9f8b-e6e0ea779861"
    zone: "{{ cs_common_zone_adv }}"
  register: ip_address_tag
  check_mode: true
- name: verify test associate ip_address with tags in check mode
  assert:
    that:
    - ip_address_tag is successful
    - ip_address_tag is changed

- name: test associate ip_address with tags
  cs_ip_address:
    network: ipaddr_test_network
    tags:
      - key: unique_id
        value: "adacd65e-7868-5ebf-9f8b-e6e0ea779861"
    zone: "{{ cs_common_zone_adv }}"
  register: ip_address_tag
- name: verify test associate ip_address with tags
  assert:
    that:
    - ip_address_tag is successful
    - ip_address_tag is changed
    - ip_address_tag.ip_address is defined
    - ip_address_tag.tags.0.key == "unique_id"
    - ip_address_tag.tags.0.value == "adacd65e-7868-5ebf-9f8b-e6e0ea779861"

- name: test associate ip_address with tags idempotence
  cs_ip_address:
    network: ipaddr_test_network
    tags:
      - key: unique_id
        value: "adacd65e-7868-5ebf-9f8b-e6e0ea779861"
    zone: "{{ cs_common_zone_adv }}"
  register: ip_address_tag
- name: verify test associate ip_address with tags idempotence
  assert:
    that:
    - ip_address_tag is successful
    - ip_address_tag is not changed
    - ip_address_tag.ip_address is defined
    - ip_address_tag.state == "Allocated"
    - ip_address_tag.tags.0.key == "unique_id"
    - ip_address_tag.tags.0.value == "adacd65e-7868-5ebf-9f8b-e6e0ea779861"

- name: test disassiociate ip_address with missing param ip_address
  cs_ip_address:
    state: absent
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"
  ignore_errors: true
  register: ip_address_err
- name: verify test disassiociate ip_address with missing param ip_address
  assert:
    that:
    - ip_address_err is failed
    - 'ip_address_err.msg == "state is absent but any of the following are missing: ip_address, tags"'

- name: test disassociate ip_address in check mode
  cs_ip_address:
    state: absent
    ip_address: "{{ ip_address.ip_address }}"
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"
  check_mode: true
  register: ip_address
- name: verify test disassociate ip_address in check mode
  assert:
    that:
    - ip_address is successful
    - ip_address is changed

- name: test disassociate ip_address
  cs_ip_address:
    state: absent
    ip_address: "{{ ip_address.ip_address }}"
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"
  register: ip_address
- name: verify test disassociate ip_address
  assert:
    that:
    - ip_address is successful
    - ip_address is changed

- name: test disassociate ip_address idempotence
  cs_ip_address:
    state: absent
    ip_address: "{{ ip_address.ip_address }}"
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"
  register: ip_address
- name: verify test disassociate ip_address idempotence
  assert:
    that:
    - ip_address is successful
    - ip_address is not changed

- name: test disassociate ip_address with tags with check mode
  cs_ip_address:
    state: absent
    tags:
      - key: unique_id
        value: "adacd65e-7868-5ebf-9f8b-e6e0ea779861"
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"
  check_mode: true
  register: ip_address
- name: verify test disassociate ip_address with tags in check mode
  assert:
    that:
    - ip_address is successful
    - ip_address is changed

- name: test disassociate ip_address with tags
  cs_ip_address:
    state: absent
    tags:
      - key: unique_id
        value: "adacd65e-7868-5ebf-9f8b-e6e0ea779861"
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"
  register: ip_address
- name: verify test disassociate ip_address with tags
  assert:
    that:
    - ip_address is successful
    - ip_address is changed

- name: test disassociate ip_address with tags idempotence
  cs_ip_address:
    state: absent
    tags:
      - key: unique_id
        value: "adacd65e-7868-5ebf-9f8b-e6e0ea779861"
    network: ipaddr_test_network
    zone: "{{ cs_common_zone_adv }}"
  register: ip_address
- name: verify test disassociate ip_address with tags idempotence
  assert:
    that:
    - ip_address is successful
    - ip_address is not changed

- name: cleanup instance
  cs_instance:
    name: "{{ cs_resource_prefix }}-vm-cs-ip-address"
    zone: "{{ cs_common_zone_adv }}"
    state: expunged
  register: instance
- name: verify instance cleanup
  assert:
    that:
    - instance is successful

- name: clean the test network
  cs_network:
    name: ipaddr_test_network
    state: absent
    zone: "{{ cs_common_zone_adv }}"
