Add the billing/quote request

Change-Id: I9dcffe181108aa9f854fa696862b0b47b0f907f6
This commit is contained in:
François Magimel
2014-08-22 17:50:44 +02:00
parent ac57d0b974
commit 28263e2c7a
5 changed files with 131 additions and 0 deletions

View File

@@ -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)

View File

View File

@@ -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'))

View File

@@ -20,6 +20,7 @@ OpenStack Client interface. Handles the REST calls and responses.
""" """
from cloudkittyclient.openstack.common.apiclient import client from cloudkittyclient.openstack.common.apiclient import client
from cloudkittyclient.v1.billing import quote
from cloudkittyclient.v1 import report from cloudkittyclient.v1 import report
@@ -30,4 +31,10 @@ class Client(client.BaseClient):
"""Initialize a new client for the Cloudkitty v1 API.""" """Initialize a new client for the Cloudkitty v1 API."""
super(Client, self).__init__(http_client, extensions) super(Client, self).__init__(http_client, extensions)
self.billing = Billing(self)
self.report = report.ReportManager(self) self.report = report.ReportManager(self)
class Billing(object):
def __init__(self, http_client):
self.quote = quote.QuoteManager(http_client)