From 819526591ee2cdbf7f138a08f9c38b9c804e5d31 Mon Sep 17 00:00:00 2001
From: Dean Troyer <dtroyer@gmail.com>
Date: Sat, 14 Jan 2017 12:57:09 -0600
Subject: [PATCH] Fix quota show --default command

Work around a bug in OpenStack SDK 0.9.11 and 0.9.12 that causes
quota show --default to fail.  This can be removed when the proposed
SDK fix (https://review.openstack.org/420301) is reelased and in the
minimum SDK version in global requirements.

quota set --network is still broken, I can't fix it at the moment...

Closes-bug: 1656572
Change-Id: Ice77e14782c33e672176afbab36bba95b73d7a11
---
 openstackclient/common/quota.py               | 36 ++++++++++++++++++-
 .../tests/functional/common/test_quota.py     |  3 --
 .../notes/bug-1656572-b40303ae50a41000.yaml   |  6 ++++
 3 files changed, 41 insertions(+), 4 deletions(-)
 create mode 100644 releasenotes/notes/bug-1656572-b40303ae50a41000.yaml

diff --git a/openstackclient/common/quota.py b/openstackclient/common/quota.py
index afc6195f93..fa6c576552 100644
--- a/openstackclient/common/quota.py
+++ b/openstackclient/common/quota.py
@@ -18,6 +18,8 @@
 import itertools
 import sys
 
+from openstack import exceptions as sdk_exceptions
+from openstack.network.v2 import quota as _quota
 from osc_lib.command import command
 from osc_lib import utils
 import six
@@ -251,7 +253,39 @@ class ShowQuota(command.ShowOne):
             project = self._get_project(parsed_args)
             client = self.app.client_manager.network
             if parsed_args.default:
-                network_quota = client.get_quota_default(project)
+                # TODO(dtroyer): Remove the top of this if block once the
+                #                fixed SDK QuotaDefault class is the minimum
+                #                required version.  This is expected to be
+                #                SDK release 0.9.13
+                if hasattr(_quota.QuotaDefault, 'project'):
+                    # hack 0.9.11+
+                    quotadef_obj = client._get_resource(
+                        _quota.QuotaDefault,
+                        project,
+                    )
+                    quotadef_obj.base_path = quotadef_obj.base_path % {
+                        'project': project,
+                    }
+                    try:
+                        network_quota = quotadef_obj.get(
+                            client.session,
+                            requires_id=False,
+                        )
+                    except sdk_exceptions.NotFoundException as e:
+                        raise sdk_exceptions.ResourceNotFound(
+                            message="No %s found for %s" %
+                                    (_quota.QuotaDefault.__name__, project),
+                            details=e.details,
+                            response=e.response,
+                            request_id=e.request_id,
+                            url=e.url,
+                            method=e.method,
+                            http_status=e.http_status,
+                            cause=e.cause,
+                        )
+                    # end hack-around
+                else:
+                    network_quota = client.get_quota_default(project)
             else:
                 network_quota = client.get_quota(project)
             return network_quota
diff --git a/openstackclient/tests/functional/common/test_quota.py b/openstackclient/tests/functional/common/test_quota.py
index fbb8e56367..b2b198afb4 100644
--- a/openstackclient/tests/functional/common/test_quota.py
+++ b/openstackclient/tests/functional/common/test_quota.py
@@ -35,19 +35,16 @@ class QuotaTests(base.TestCase):
         raw_output = self.openstack('quota show ' + self.PROJECT_NAME + opts)
         self.assertEqual("11\n11\n11\n", raw_output)
 
-    @testtools.skip('broken SDK testing')
     def test_quota_show(self):
         raw_output = self.openstack('quota show ' + self.PROJECT_NAME)
         for expected_field in self.EXPECTED_FIELDS:
             self.assertIn(expected_field, raw_output)
 
-    @testtools.skip('broken SDK testing')
     def test_quota_show_default_project(self):
         raw_output = self.openstack('quota show')
         for expected_field in self.EXPECTED_FIELDS:
             self.assertIn(expected_field, raw_output)
 
-    @testtools.skip('broken SDK testing')
     def test_quota_show_with_default_option(self):
         raw_output = self.openstack('quota show --default')
         for expected_field in self.EXPECTED_FIELDS:
diff --git a/releasenotes/notes/bug-1656572-b40303ae50a41000.yaml b/releasenotes/notes/bug-1656572-b40303ae50a41000.yaml
new file mode 100644
index 0000000000..e6d0bcc593
--- /dev/null
+++ b/releasenotes/notes/bug-1656572-b40303ae50a41000.yaml
@@ -0,0 +1,6 @@
+---
+fixes:
+  - |
+    Work around a bug in OpenStackSDK 0.9.11 and 0.9.12 that causes
+    ``quota show --default`` to fail.
+    [Bug `1656572 <https://bugs.launchpad.net/python-openstackclient/+bug/1656572>`_]