From 28263e2c7a39885feb9d5ce644b632daa973652e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Magimel?= Date: Fri, 22 Aug 2014 17:50:44 +0200 Subject: [PATCH] Add the billing/quote request Change-Id: I9dcffe181108aa9f854fa696862b0b47b0f907f6 --- cloudkittyclient/tests/v1/billing/__init__.py | 0 .../tests/v1/billing/test_quote.py | 60 +++++++++++++++++ cloudkittyclient/v1/billing/__init__.py | 0 cloudkittyclient/v1/billing/quote.py | 64 +++++++++++++++++++ cloudkittyclient/v1/client.py | 7 ++ 5 files changed, 131 insertions(+) create mode 100644 cloudkittyclient/tests/v1/billing/__init__.py create mode 100644 cloudkittyclient/tests/v1/billing/test_quote.py create mode 100644 cloudkittyclient/v1/billing/__init__.py create mode 100644 cloudkittyclient/v1/billing/quote.py diff --git a/cloudkittyclient/tests/v1/billing/__init__.py b/cloudkittyclient/tests/v1/billing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cloudkittyclient/tests/v1/billing/test_quote.py b/cloudkittyclient/tests/v1/billing/test_quote.py new file mode 100644 index 0000000..b19b319 --- /dev/null +++ b/cloudkittyclient/tests/v1/billing/test_quote.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# Copyright 2014 Objectif Libre +# +# 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. +# +# @author: François Magimel (linkid) + +""" +Tests for the manager billing.quote `cloudkittyclient.v1.billing.quote`. +""" + +from cloudkittyclient.openstack.common.apiclient import fake_client +from cloudkittyclient.tests import base +from cloudkittyclient.v1.billing import quote +from cloudkittyclient.v1 import client + + +compute = { + 'desc': { + 'image_id': "a41fba37-2429-4f15-aa00-b5bc4bf557bf", + }, + 'service': "compute", + 'volume': 1 +} + +fixtures = { + '/v1/billing/quote': { + 'POST': ( + {}, + '4.2' + ), + } +} + + +class QuoteManagerTest(base.TestCase): + def setUp(self): + super(QuoteManagerTest, self).setUp() + fake_http_client = fake_client.FakeHTTPClient(fixtures=fixtures) + api_client = client.Client(fake_http_client) + self.mgr = quote.QuoteManager(api_client) + + def test_post(self): + _quote = self.mgr.post(json=compute) + self.assertIn('Quote', repr(_quote)) + self.assertEqual(4.2, _quote.price) + + def test_post_raw(self): + _quote = self.mgr.post(json=compute, return_raw=True) + self.assertEqual(4.2, _quote) diff --git a/cloudkittyclient/v1/billing/__init__.py b/cloudkittyclient/v1/billing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cloudkittyclient/v1/billing/quote.py b/cloudkittyclient/v1/billing/quote.py new file mode 100644 index 0000000..641f0a8 --- /dev/null +++ b/cloudkittyclient/v1/billing/quote.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +# Copyright 2014 Objectif Libre +# +# 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. +# +# @author: François Magimel (linkid) + +""" +Quote resource and manager. +""" + +from cloudkittyclient.openstack.common.apiclient import base + + +class Quote(base.Resource): + """A resource represents a particular instance of an object (tenant, user, + etc). This is pretty much just a bag for attributes. + + :param manager: Manager object + :param info: dictionary representing resource attributes + :param loaded: prevent lazy-loading if set to True + """ + + def _add_details(self, info): + try: + setattr(self, 'price', info) + except AttributeError: + # In this case we already defined the attribute on the class + pass + + +class QuoteManager(base.CrudManager): + """Managers interact with a particular type of API and provide CRUD + operations for them. + """ + + resource_class = Quote + collection_key = 'billing/quote' + key = 'quote' + + def _post(self, url, json, response_key=None, return_raw=False): + """Create an object.""" + body = self.client.post(url, json=json).json() + if return_raw: + return body + return self.resource_class(self, body) + + def post(self, **kwargs): + """Get the price corresponding to resources attributes.""" + kwargs = self._filter_kwargs(kwargs) + return self._post( + url=self.build_url(base_url='/v1', **kwargs), + json=kwargs.get('json'), + return_raw=kwargs.get('return_raw')) diff --git a/cloudkittyclient/v1/client.py b/cloudkittyclient/v1/client.py index a499f47..146e587 100644 --- a/cloudkittyclient/v1/client.py +++ b/cloudkittyclient/v1/client.py @@ -20,6 +20,7 @@ OpenStack Client interface. Handles the REST calls and responses. """ from cloudkittyclient.openstack.common.apiclient import client +from cloudkittyclient.v1.billing import quote from cloudkittyclient.v1 import report @@ -30,4 +31,10 @@ class Client(client.BaseClient): """Initialize a new client for the Cloudkitty v1 API.""" super(Client, self).__init__(http_client, extensions) + self.billing = Billing(self) self.report = report.ReportManager(self) + + +class Billing(object): + def __init__(self, http_client): + self.quote = quote.QuoteManager(http_client)