From 33226737b020f09f2dec7a40ecd4340040d25dbb Mon Sep 17 00:00:00 2001 From: Feilong Wang Date: Tue, 20 Jun 2017 11:56:46 +1200 Subject: [PATCH] Support /credits API Currently, Distil supports getting credits of current user. This patch adds the support for Distil client and test cases are added together. Change-Id: Ibbfd4858d205f78de7a3cda8748b130151a1e266 --- distilclient/tests/unit/v2/test_credits.py | 43 ++++++++++++++++++++++ distilclient/v2/client.py | 2 + distilclient/v2/credits.py | 32 ++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 distilclient/tests/unit/v2/test_credits.py create mode 100644 distilclient/v2/credits.py diff --git a/distilclient/tests/unit/v2/test_credits.py b/distilclient/tests/unit/v2/test_credits.py new file mode 100644 index 0000000..4d2b30f --- /dev/null +++ b/distilclient/tests/unit/v2/test_credits.py @@ -0,0 +1,43 @@ +# Copyright (c) 2017 Catalyst IT Ltd. +# +# 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. + +import mock +import uuid + +import distilclient +from distilclient import base +from distilclient.tests.unit import utils +from distilclient.v2 import client + + +class InvoicesTest(utils.TestCase): + + def setUp(self): + super(InvoicesTest, self).setUp() + self.client = client.Client(session=client.session.Session(), + api_version=distilclient.API_MAX_VERSION, + distil_url=uuid.uuid4().hex, retries=3, + input_auth_token='token') + + @mock.patch.object(base.Manager, '_list') + def test_list_with_project_id(self, mock_list): + self.client.credits.list('project_id') + mock_list.assert_called_with('/v2/credits?project_id=project_id', + 'credits') + + @mock.patch.object(base.Manager, '_list') + def test_list_without_project_id(self, mock_list): + self.client.credits.list() + mock_list.assert_called_with('/v2/credits', 'credits') diff --git a/distilclient/v2/client.py b/distilclient/v2/client.py index ab2fa3f..0fbf688 100644 --- a/distilclient/v2/client.py +++ b/distilclient/v2/client.py @@ -20,6 +20,7 @@ import six from distilclient.common import httpclient from distilclient import exceptions +from distilclient.v2 import credits from distilclient.v2 import invoices from distilclient.v2 import measurements from distilclient.v2 import quotations @@ -190,6 +191,7 @@ class Client(object): self.measurements = measurements.MeasurementManager(self) self.invoices = invoices.InvoiceManager(self) self.quotations = quotations.QuotationManager(self) + self.credits = credits.CreditManager(self) self._load_extensions(extensions) diff --git a/distilclient/v2/credits.py b/distilclient/v2/credits.py new file mode 100644 index 0000000..9634001 --- /dev/null +++ b/distilclient/v2/credits.py @@ -0,0 +1,32 @@ +# Copyright 2017 Catalyst IT Ltd. +# +# 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. + +from distilclient import base + + +class CreditManager(base.Manager): + + def list(self, project_id=None): + """Retrieve a list of credits. + + :param project_id: Project ID, there there is no project id given, + Distil will use the project ID from token. + :returns: A list of credits. + """ + + url = "/v2/credits" + if project_id: + url = url + "?project_id=" + project_id + + return self._list(url, "credits")