From dffc18095bd37c9844d7389d8630fe92dc53f2f9 Mon Sep 17 00:00:00 2001 From: Fei Long Wang Date: Tue, 18 Apr 2017 12:02:57 +1200 Subject: [PATCH] Support /invoices and /quotations API According to the change of Distil API, this patch adds the support for /invoices and /quotations. And by the way, change the /measurements API as well. Unit tests are added based on changes. Change-Id: I251b534a4e4a41a76a3743118010be408116c27a --- distilclient/tests/unit/v2/test_client.py | 19 ++++--- distilclient/tests/unit/v2/test_invoices.py | 46 ++++++++++++++++ .../tests/unit/v2/test_measurements.py | 54 +++++++++++++------ distilclient/tests/unit/v2/test_quotations.py | 46 ++++++++++++++++ distilclient/v2/client.py | 4 ++ distilclient/v2/invoices.py | 36 +++++++++++++ distilclient/v2/measurements.py | 15 ++++-- distilclient/v2/quotations.py | 36 +++++++++++++ 8 files changed, 229 insertions(+), 27 deletions(-) create mode 100644 distilclient/tests/unit/v2/test_invoices.py create mode 100644 distilclient/tests/unit/v2/test_quotations.py create mode 100644 distilclient/v2/invoices.py create mode 100644 distilclient/v2/quotations.py diff --git a/distilclient/tests/unit/v2/test_client.py b/distilclient/tests/unit/v2/test_client.py index 082138c..5630d4c 100644 --- a/distilclient/tests/unit/v2/test_client.py +++ b/distilclient/tests/unit/v2/test_client.py @@ -1,14 +1,17 @@ -# 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 +# Copyright (c) 2017 Catalyst IT Ltd. # -# http://www.apache.org/licenses/LICENSE-2.0 +# 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. +# 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 ddt import uuid diff --git a/distilclient/tests/unit/v2/test_invoices.py b/distilclient/tests/unit/v2/test_invoices.py new file mode 100644 index 0000000..eb46178 --- /dev/null +++ b/distilclient/tests/unit/v2/test_invoices.py @@ -0,0 +1,46 @@ +# 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.invoices.list('2017-1-1', '2018-2-1', + 'project_id') + mock_list.assert_called_with('/v2/invoices?start=2017-1-1' + '&end=2018-2-1&project_id=project_id', + 'invoices') + + @mock.patch.object(base.Manager, '_list') + def test_list_without_project_id(self, mock_list): + self.client.invoices.list('2017-1-1', '2018-2-1') + mock_list.assert_called_with('/v2/invoices?start=2017-1-1' + '&end=2018-2-1', 'invoices') diff --git a/distilclient/tests/unit/v2/test_measurements.py b/distilclient/tests/unit/v2/test_measurements.py index df8c0a2..a8596d5 100644 --- a/distilclient/tests/unit/v2/test_measurements.py +++ b/distilclient/tests/unit/v2/test_measurements.py @@ -1,25 +1,47 @@ -# Copyright 2010 Jacob Kaplan-Moss +# 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. -# Copyright 2011 OpenStack Foundation -# All Rights Reserved. -# -# 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 MeasurementsTest(utils.TestCase): - # Testcases for class Share def setUp(self): super(MeasurementsTest, 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.measurements.list('2017-1-1', '2018-2-1', + 'project_id') + mock_list.assert_called_with('/v2/measurements?start=2017-1-1' + '&end=2018-2-1&project_id=project_id', + 'measurements') + + @mock.patch.object(base.Manager, '_list') + def test_list_without_project_id(self, mock_list): + self.client.measurements.list('2017-1-1', '2018-2-1') + mock_list.assert_called_with('/v2/measurements?start=2017-1-1' + '&end=2018-2-1', 'measurements') diff --git a/distilclient/tests/unit/v2/test_quotations.py b/distilclient/tests/unit/v2/test_quotations.py new file mode 100644 index 0000000..c907868 --- /dev/null +++ b/distilclient/tests/unit/v2/test_quotations.py @@ -0,0 +1,46 @@ +# 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 QuotationsTest(utils.TestCase): + + def setUp(self): + super(QuotationsTest, 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.quotations.list('2017-1-1', '2018-2-1', + 'project_id') + mock_list.assert_called_with('/v2/quotations?start=2017-1-1' + '&end=2018-2-1&project_id=project_id', + 'quotations') + + @mock.patch.object(base.Manager, '_list') + def test_list_without_project_id(self, mock_list): + self.client.quotations.list('2017-1-1', '2018-2-1') + mock_list.assert_called_with('/v2/quotations?start=2017-1-1' + '&end=2018-2-1', 'quotations') diff --git a/distilclient/v2/client.py b/distilclient/v2/client.py index 6ace015..ab2fa3f 100644 --- a/distilclient/v2/client.py +++ b/distilclient/v2/client.py @@ -20,7 +20,9 @@ import six from distilclient.common import httpclient from distilclient import exceptions +from distilclient.v2 import invoices from distilclient.v2 import measurements +from distilclient.v2 import quotations class Client(object): @@ -186,6 +188,8 @@ class Client(object): api_version=self.api_version) self.measurements = measurements.MeasurementManager(self) + self.invoices = invoices.InvoiceManager(self) + self.quotations = quotations.QuotationManager(self) self._load_extensions(extensions) diff --git a/distilclient/v2/invoices.py b/distilclient/v2/invoices.py new file mode 100644 index 0000000..eb9fd6b --- /dev/null +++ b/distilclient/v2/invoices.py @@ -0,0 +1,36 @@ +# 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 InvoiceManager(base.Manager): + + def list(self, start, end, project_id=None): + """Retrieve a list of invoices. + + :param start: Start date of the query + :param end: End date of the query + :param project_id: Project ID, there there is no project id given, + Distil will use the project ID from token. + :returns: A list of invoices. + """ + + url = "/v2/invoices?start={0}&end={1}" + if project_id: + url = url.format(start, end) + "&project_id=" + project_id + else: + url = url.format(start, end) + + return self._list(url, "invoices") diff --git a/distilclient/v2/measurements.py b/distilclient/v2/measurements.py index 5b7ea2b..fe79bd9 100644 --- a/distilclient/v2/measurements.py +++ b/distilclient/v2/measurements.py @@ -17,11 +17,20 @@ from distilclient import base class MeasurementManager(base.Manager): - def list(self, project_id, start, end): + def list(self, start, end, project_id=None): """Retrieve a list of measurements. + :param start: Start date of the query + :param end: End date of the query + :param project_id: Project ID, there there is no project id given, + Distil will use the project ID from token. :returns: A list of measurements. """ - url = "/v2/measurements?project_id={0}&start={1}&end={2}" - url = url.format(project_id, start, end) + + url = "/v2/measurements?start={0}&end={1}" + if project_id: + url = url.format(start, end) + "&project_id=" + project_id + else: + url = url.format(start, end) + return self._list(url, "measurements") diff --git a/distilclient/v2/quotations.py b/distilclient/v2/quotations.py new file mode 100644 index 0000000..7a0e60b --- /dev/null +++ b/distilclient/v2/quotations.py @@ -0,0 +1,36 @@ +# 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 QuotationManager(base.Manager): + + def list(self, start, end, project_id=None): + """Retrieve a list of quotations. + + :param start: Start date of the query + :param end: End date of the query + :param project_id: Project ID, there there is no project id given, + Distil will use the project ID from token. + :returns: A list of quotations. + """ + + url = "/v2/quotations?start={0}&end={1}" + if project_id: + url = url.format(start, end) + "&project_id=" + project_id + else: + url = url.format(start, end) + + return self._list(url, "quotations")