---
# Copyright 2016, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Test Octavia
  hosts: localhost
  connection: local
  gather_facts: no
  vars_files:
    - common/test-vars.yml
  vars:
    octavia_service_user_name: octavia
    octavia_service_project_name: service
    octavia_service_user_domain_id: default
    octavia_service_project_domain_id: default
    env:
      OS_ENDPOINT_TYPE: internalURL
      OS_INTERFACE: internalURL
      OS_USERNAME: admin
      OS_PASSWORD: "{{ keystone_auth_admin_password }}"
      OS_PROJECT_NAME: admin
      OS_TENANT_NAME: admin
      OS_AUTH_URL: "http://{{ test_keystone_host }}:5000/v3"
      OS_NO_CACHE: 1
      OS_USER_DOMAIN_NAME: Default
      OS_PROJECT_DOMAIN_NAME: Default
      OS_REGION_NAME: RegionOne
      OS_IDENTITY_API_VERSION: 3
      OS_AUTH_VERSION: 3
  tasks:
    - name: Ensure public network exists
      os_network:
        auth:
          auth_url: "{{ keystone_service_adminurl }}"
          username: "{{ octavia_service_user_name }}"
          password: "{{ octavia_service_password }}"
          project_name: "{{ octavia_service_project_name }}"
          user_domain_name: "{{ octavia_service_user_domain_id }}"
          project_domain_name: "{{ octavia_service_project_domain_id }}"
        interface: admin
        state: present
        name: "public"
        provider_network_type: "flat"
        provider_physical_network: "flat"
        external: True

    - name:  Ensure public subnet exists
      os_subnet:
        auth:
          auth_url: "{{ keystone_service_adminurl }}"
          username: "{{ octavia_service_user_name }}"
          password: "{{ octavia_service_password }}"
          project_name: "{{ octavia_service_project_name }}"
          user_domain_name: "{{ octavia_service_user_domain_id }}"
          project_domain_name: "{{ octavia_service_project_domain_id }}"
        interface: admin
        state: present
        network_name: "public"
        name: "public-subnet"
        cidr: "10.1.3.0/24"

    - name: Create a loadbalancer
      shell: >
         openstack --debug loadbalancer create  --name test-lb --vip-subnet-id public-subnet
      environment: "{{ env }}"
      tags:
        - skip_ansible_lint

    - name: Wait until LB is active
      shell: >
         openstack loadbalancer show test-lb -c provisioning_status -f value
      environment: "{{ env }}"
      register: lb_active
      until: lb_active.stdout == "ACTIVE"
      failed_when: lb_active.stdout == "ERROR"
      retries: 50
      delay: 10
      tags:
        - skip_ansible_lint

    - name: Create a listener
      shell: >
       openstack loadbalancer listener create --protocol HTTP --protocol-port 80 --name listener test-lb
      environment: "{{ env }}"
      tags:
        - skip_ansible_lint

    - name: Wait until Listener is active
      shell: >
         openstack loadbalancer show test-lb -c provisioning_status -f value
      environment: "{{ env }}"
      register: lb_active
      until: lb_active.stdout == "ACTIVE"
      failed_when: lb_active.stdout == "ERROR"
      retries: 10
      delay: 10
      tags:
        - skip_ansible_lint

    - name: Run Show (for debugging)
      shell: >
        openstack loadbalancer show test-lb
      environment: "{{ env }}"
      when:
        - debug | bool
      tags:
        - skip_ansible_lint

    - name: Register VIP IP # this is likely changing with a newer version of octaviaclient
      shell: >
         openstack loadbalancer show test-lb -c vip_address -f value
      environment: "{{ env }}"
      register: vip_output
      tags:
        - skip_ansible_lint

    - name: Test the Listener
      uri:
        url: "http://{{ vip_output.stdout }}"
        status_code: 503
      when: test_octavia_amphora | bool

    - name: Delete LoadBalancer
      shell: >
        openstack loadbalancer delete --cascade test-lb
      environment: "{{ env }}"
      register: lb_deleted
      until: lb_deleted is success
      retries: 10
      delay: 15
      tags:
        - skip_ansible_lint