---
# Check ansible
- name: Check Ansible version
  assert:
    that: >
      (ansible_version.major == 2 and ansible_version.minor >= 3) or
      (ansible_version.major > 2)
    msg: "Ansible version must be at least 2.3"

# Check shade
- name: Try to import python module shade
  command: python -c "import shade"
  ignore_errors: yes
  register: shade_result
- name: Check if shade is installed
  assert:
    that: 'shade_result.rc == 0'
    msg: "Python module shade is not installed"

# Gather Neutron extension facts
- name: Check for Neutron trunk support
  os_network_extensions:

# Gather OpenStack service catalog endpoint facts
- name: Check for cloud service catalog endpoints
  os_service_catalog:

# Check trunk support
- fail:
    msg: "Trunk ports enabled but support lacking in Neutron"
  when: (use_trunk_ports | default(False)) and
        ('trunk' not in openstack_network_extensions)

# Check jmespath
- name: Try to import python module shade
  command: python -c "import jmespath"
  ignore_errors: yes
  register: jmespath_result
- name: Check if jmespath is installed
  assert:
    that: 'jmespath_result.rc == 0'
    msg: "Python module jmespath is not installed"

# Check python-dns
- name: Try to import python DNS module
  command: python -c "import dns"
  ignore_errors: yes
  register: pythondns_result
  when: openshift_openstack_external_nsupdate_keys is defined
- name: Check if python-dns is installed
  assert:
    that: 'pythondns_result.rc == 0'
    msg: "Python module python-dns is not installed"
  when: openshift_openstack_external_nsupdate_keys is defined

# Check jinja2
- name: Try to import jinja2 module
  command: python -c "import jinja2"
  ignore_errors: yes
  register: jinja_result
- name: Check if jinja2 is installed
  assert:
    that: 'jinja_result.rc == 0'
    msg: "Python module jinja2 is not installed"

# Check network name
- name: Try to get network facts
  os_networks_facts:
    name: "{{ openshift_openstack_external_network_name }}"
  register: network_result
  when: not openshift_openstack_provider_network_name|default(None)
- name: Check that network is available
  assert:
    that: "network_result.ansible_facts.openstack_networks"
    msg: "Network {{ openshift_openstack_external_network_name }} is not available"
  when: not openshift_openstack_provider_network_name|default(None)

# Check keypair
# TODO kpilatov: there is no Ansible module for getting OS keypairs
#                (os_keypair is not suitable for this)
#                this method does not force python-openstackclient dependency
- name: Try to show keypair
  command: >
           python -c 'import shade; cloud = shade.openstack_cloud();
           exit(cloud.get_keypair("{{ openshift_openstack_keypair_name }}") is None)'
  ignore_errors: yes
  register: key_result
- name: Check that keypair is available
  assert:
    that: 'key_result.rc == 0'
    msg: "Keypair {{ openshift_openstack_keypair_name }} is not available"

# Check flavors and images
- include_tasks: image-and-flavor-check.yml
  with_items:
  - { image: "{{ openshift_openstack_default_image_name }}", flavor: "{{ openshift_openstack_default_flavor }}" }
  - { image: "{{ openshift_openstack_master_image }}", flavor: "{{ openshift_openstack_master_flavor }}" }
  - { image: "{{ openshift_openstack_infra_image }}", flavor: "{{ openshift_openstack_infra_flavor }}" }
  - { image: "{{ openshift_openstack_cns_image }}", flavor: "{{ openshift_openstack_cns_flavor }}" }
  - { image: "{{ openshift_openstack_node_image }}", flavor: "{{ openshift_openstack_node_flavor }}" }
  - { image: "{{ openshift_openstack_lb_image }}", flavor: "{{ openshift_openstack_lb_flavor }}" }
  - { image: "{{ openshift_openstack_etcd_image }}", flavor: "{{ openshift_openstack_etcd_flavor }}" }

- name: Check Load Balancer options
  fail:
    msg: >
      Only one of `openshift_openstack_use_lbaas_load_balancer` and
      `openshift_openstack_use_vm_load_balancer` can be true at a time.
  when:
  - openshift_openstack_use_lbaas_load_balancer
  - openshift_openstack_use_vm_load_balancer

- name: Check LBaaS Load Balancer providers
  fail:
    msg: >
      Your configured openshift_openstack_lbaasv2_provider is not supported
      by your cloud.
  when: (openshift_openstack_use_lbaas_load_balancer and openshift_openstack_lbaasv2_provider == 'Neutron::LBaaS' and 'lbaasv2' not in openstack_network_extensions) or
        (openshift_openstack_use_lbaas_load_balancer and openshift_openstack_lbaasv2_provider == 'Octavia' and 'load-balancer' not in openstack_service_catalog)

- name: Verify the nsupdate zone is a subset of the full cluster domain
  fail:
    msg: >
      The `openshift_openstack_nsupdate_zone` ({{ openshift_openstack_nsupdate_zone }})
      must be a substring of `openshift_openstack_full_dns_domain ({{ openshift_openstack_full_dns_domain }})`
  when:
  - openshift_openstack_nsupdate_zone is defined
  - openshift_openstack_full_dns_domain is defined
  - openshift_openstack_full_dns_domain is not match("(.*\.)*" + openshift_openstack_nsupdate_zone + "$")
