Skip to content

Commit 6a25347

Browse files
rafaelweingartnerpriteau
authored andcommitted
Fix quote API
The quote API method has been broken for a while now. This patch fixes the API and makes it work again. To use it, one can issue the following command. curl -s -X POST "http://cloudkitty_server_and_port/v1/rating/quote" -H "Accept: application/json" -H "User-Agent: python-keystoneclient" -H "Content-Type: application/json" -H "X-Auth-Token: ${ACCESS_TOKEN_KEYSTONE}" -d '{"resources": [{"service": "<cloudkitty_service_name>", "volume": "<quantity_to_quote>", "desc": {"metadata": {"<field_name_used_to_define_price>": "<value_to_activate_a_price>"}}}]}' Change-Id: Ia6ef6543a5100445b203196d39b404bc74b75ba0 Story: 2009022 Task: 42744 (cherry picked from commit 21a8730)
1 parent c64c641 commit 6a25347

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

cloudkitty/api/v1/controllers/rating.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# under the License.
1515
#
1616
from oslo_concurrency import lockutils
17+
from oslo_log import log
1718
import pecan
1819
from pecan import rest
1920
from stevedore import extension
@@ -26,6 +27,8 @@
2627

2728
PROCESSORS_NAMESPACE = 'cloudkitty.rating.processors'
2829

30+
LOG = log.getLogger(__name__)
31+
2932

3033
class RatingModulesMixin(object):
3134
def reload_extensions(self):
@@ -202,7 +205,8 @@ def quote(self, res_data):
202205
json_data = res.to_json()
203206
res_dict[res.service].extend(json_data[res.service])
204207

205-
res = client.call({}, 'quote', res_data=[{'usage': res_dict}])
208+
LOG.debug("Calling quote method with data: [%s].", res_dict)
209+
res = client.call({}, 'quote', res_data={'usage': res_dict})
206210
return res
207211

208212
@wsme_pecan.wsexpose(None)

cloudkitty/orchestrator.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,41 @@ def get_module_state(self):
115115
return module_list
116116

117117
def quote(self, ctxt, res_data):
118-
LOG.debug('Received quote from RPC.')
118+
LOG.debug('Received quote request [%s] from RPC.', res_data)
119119
worker = APIWorker()
120-
return str(worker.quote(res_data))
120+
121+
start = tzutils.localized_now()
122+
end = tzutils.add_delta(start, timedelta(seconds=CONF.collect.period))
123+
124+
# Need to prepare data to support the V2 processing format
125+
usage = {}
126+
for k in res_data['usage']:
127+
all_data_points_for_metric = []
128+
all_quote_data_entries = res_data['usage'][k]
129+
for p in all_quote_data_entries:
130+
vol = p['vol']
131+
desc = p.get('desc', {})
132+
133+
data_point = dataframe.DataPoint(
134+
vol['unit'],
135+
vol['qty'],
136+
0,
137+
desc.get('groupby', []),
138+
desc.get('metadata', []),
139+
)
140+
all_data_points_for_metric.append(data_point)
141+
usage[k] = all_data_points_for_metric
142+
143+
frame = dataframe.DataFrame(
144+
start=start,
145+
end=end,
146+
usage=usage,
147+
)
148+
149+
quote_result = worker.quote(frame)
150+
LOG.debug("Quote result [%s] for input data [%s].",
151+
quote_result, res_data)
152+
return str(quote_result)
121153

122154
def reload_modules(self, ctxt):
123155
LOG.info('Received reload modules command.')
@@ -220,15 +252,13 @@ def __init__(self, tenant_id=None):
220252
super(APIWorker, self).__init__(tenant_id)
221253

222254
def quote(self, res_data):
255+
quote_result = res_data
223256
for processor in self._processors:
224-
processor.obj.quote(res_data)
257+
quote_result = processor.obj.quote(quote_result)
225258

226259
price = decimal.Decimal(0)
227-
for res in res_data:
228-
for res_usage in res['usage'].values():
229-
for data in res_usage:
230-
price += data.get('rating', {}).get('price',
231-
decimal.Decimal(0))
260+
for _, point in quote_result.iterpoints():
261+
price += point.price
232262
return price
233263

234264

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
Fixes the quote API method. See story 2009022
5+
<https://storyboard.openstack.org/#!/story/2009022>`_ for more details.

0 commit comments

Comments
 (0)