---
# 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: Run post glance upgrade fix
  hosts: galera_all[0]
  gather_facts: false
  user: root
  tasks:
    - name: Get image_locations backup status
      command: >
        mysql --verbose --unbuffered -e
        "desc image_locations_keystone_v3_mig_pre_liberty" glance
      ignore_errors: yes
      register: glance_backup_existing

    - name: Backup glance image_locations
      command: >
        mysql --verbose --unbuffered -e
        "create table image_locations_keystone_v3_mig_pre_liberty (select * from image_locations);" glance
      when:
        - glance_backup_existing.rc  == 1
        - "{{ glance_swift_store_auth_version | default(3) }}  ==  3"

    - name: Run database fix for glance
      command: >
        mysql --verbose --unbuffered -e
        "update image_locations set value=replace(value, '{{ keystone_service_port }}/v2.0/', '{{ keystone_service_port }}/v3/')
         where value like 'swift%{{ internal_lb_vip_address }}:{{ keystone_service_port }}/v2.0/%' and status='active';" glance
      when:
        - "(glance_swift_store_auth_version | default(3)) == 3"