From 1bd2bf67dab86bce06da89db65f2b532edf8e35e Mon Sep 17 00:00:00 2001
From: Imtiaz Chowdhury <imtiaz.chowdhury@workday.com>
Date: Tue, 27 Dec 2016 14:04:29 -0800
Subject: [PATCH] Fixes image api URL endpoint for certain scenario

openstackclient fails to get image list when the image api endpoint
has 'v2' substring in the URL. Instead of checking whether the api
endpoint URL terminates with '/v2', the current logic is checking
whether 'v2' appears anywhere in the endpoint string.

This issue was discovered on a production setup where certain
server names had 'v2' in their names. For example, when a hostname
is gopher.dev20.com, the image list APIs fail.

This commit updates the unit test to reflect this scenario. Without
the change in openstackclient/api/image_v2.py, all the unit tests
fail.

Co-Authored-By: sergio.carvalho@workday.com
Change-Id: I26b85afd646938272dbabe8e045b337b7df58c7d
Closes-Bug: 1652827
---
 openstackclient/api/image_v1.py                 | 6 +++---
 openstackclient/api/image_v2.py                 | 6 +++---
 openstackclient/tests/unit/api/test_image_v1.py | 2 +-
 openstackclient/tests/unit/api/test_image_v2.py | 2 +-
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/openstackclient/api/image_v1.py b/openstackclient/api/image_v1.py
index 534c775069..e15d825a31 100644
--- a/openstackclient/api/image_v1.py
+++ b/openstackclient/api/image_v1.py
@@ -19,7 +19,7 @@ from openstackclient.api import api
 class APIv1(api.BaseAPI):
     """Image v1 API"""
 
-    _endpoint_suffix = 'v1'
+    _endpoint_suffix = '/v1'
 
     def __init__(self, endpoint=None, **kwargs):
         super(APIv1, self).__init__(endpoint=endpoint, **kwargs)
@@ -29,8 +29,8 @@ class APIv1(api.BaseAPI):
 
     def _munge_url(self):
         # Hack this until discovery is up
-        if self._endpoint_suffix not in self.endpoint.split('/')[-1]:
-            self.endpoint = '/'.join([self.endpoint, self._endpoint_suffix])
+        if not self.endpoint.endswith(self._endpoint_suffix):
+            self.endpoint = self.endpoint + self._endpoint_suffix
 
     def image_list(
         self,
diff --git a/openstackclient/api/image_v2.py b/openstackclient/api/image_v2.py
index 026498fa1d..c36281212c 100644
--- a/openstackclient/api/image_v2.py
+++ b/openstackclient/api/image_v2.py
@@ -19,12 +19,12 @@ from openstackclient.api import image_v1
 class APIv2(image_v1.APIv1):
     """Image v2 API"""
 
-    _endpoint_suffix = 'v2'
+    _endpoint_suffix = '/v2'
 
     def _munge_url(self):
         # Hack this until discovery is up, and ignore parent endpoint setting
-        if 'v2' not in self.endpoint.split('/')[-1]:
-            self.endpoint = '/'.join([self.endpoint, 'v2'])
+        if not self.endpoint.endswith(self._endpoint_suffix):
+            self.endpoint = self.endpoint + self._endpoint_suffix
 
     def image_list(
         self,
diff --git a/openstackclient/tests/unit/api/test_image_v1.py b/openstackclient/tests/unit/api/test_image_v1.py
index e02ef3812b..6ce3ddeac0 100644
--- a/openstackclient/tests/unit/api/test_image_v1.py
+++ b/openstackclient/tests/unit/api/test_image_v1.py
@@ -21,7 +21,7 @@ from openstackclient.tests.unit import utils
 
 
 FAKE_PROJECT = 'xyzpdq'
-FAKE_URL = 'http://gopher.com'
+FAKE_URL = 'http://gopher.dev10.com'
 
 
 class TestImageAPIv1(utils.TestCase):
diff --git a/openstackclient/tests/unit/api/test_image_v2.py b/openstackclient/tests/unit/api/test_image_v2.py
index 5dbb51e03c..22490e4632 100644
--- a/openstackclient/tests/unit/api/test_image_v2.py
+++ b/openstackclient/tests/unit/api/test_image_v2.py
@@ -21,7 +21,7 @@ from openstackclient.tests.unit import utils
 
 
 FAKE_PROJECT = 'xyzpdq'
-FAKE_URL = 'http://gopher.com'
+FAKE_URL = 'http://gopher.dev20.com'
 
 
 class TestImageAPIv2(utils.TestCase):