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 21a873041c)
This commit is contained in:
Rafael Weingärtner 2021-09-14 13:27:54 -03:00 committed by Pierre Riteau
parent 9533fdbb60
commit 28f8bd4a73
3 changed files with 48 additions and 9 deletions

View File

@ -14,6 +14,7 @@
# under the License.
#
from oslo_concurrency import lockutils
from oslo_log import log
import pecan
from pecan import rest
from stevedore import extension
@ -26,6 +27,8 @@ from cloudkitty import utils as ck_utils
PROCESSORS_NAMESPACE = 'cloudkitty.rating.processors'
LOG = log.getLogger(__name__)
class RatingModulesMixin(object):
def reload_extensions(self):
@ -202,7 +205,8 @@ class RatingController(rest.RestController):
json_data = res.to_json()
res_dict[res.service].extend(json_data[res.service])
res = client.call({}, 'quote', res_data=[{'usage': res_dict}])
LOG.debug("Calling quote method with data: [%s].", res_dict)
res = client.call({}, 'quote', res_data={'usage': res_dict})
return res
@wsme_pecan.wsexpose(None)

View File

@ -115,9 +115,41 @@ class RatingEndpoint(object):
return module_list
def quote(self, ctxt, res_data):
LOG.debug('Received quote from RPC.')
LOG.debug('Received quote request [%s] from RPC.', res_data)
worker = APIWorker()
return str(worker.quote(res_data))
start = tzutils.localized_now()
end = tzutils.add_delta(start, timedelta(seconds=CONF.collect.period))
# Need to prepare data to support the V2 processing format
usage = {}
for k in res_data['usage']:
all_data_points_for_metric = []
all_quote_data_entries = res_data['usage'][k]
for p in all_quote_data_entries:
vol = p['vol']
desc = p.get('desc', {})
data_point = dataframe.DataPoint(
vol['unit'],
vol['qty'],
0,
desc.get('groupby', []),
desc.get('metadata', []),
)
all_data_points_for_metric.append(data_point)
usage[k] = all_data_points_for_metric
frame = dataframe.DataFrame(
start=start,
end=end,
usage=usage,
)
quote_result = worker.quote(frame)
LOG.debug("Quote result [%s] for input data [%s].",
quote_result, res_data)
return str(quote_result)
def reload_modules(self, ctxt):
LOG.info('Received reload modules command.')
@ -219,15 +251,13 @@ class APIWorker(BaseWorker):
super(APIWorker, self).__init__(tenant_id)
def quote(self, res_data):
quote_result = res_data
for processor in self._processors:
processor.obj.quote(res_data)
quote_result = processor.obj.quote(quote_result)
price = decimal.Decimal(0)
for res in res_data:
for res_usage in res['usage'].values():
for data in res_usage:
price += data.get('rating', {}).get('price',
decimal.Decimal(0))
for _, point in quote_result.iterpoints():
price += point.price
return price

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Fixes the quote API method. See story 2009022
<https://storyboard.openstack.org/#!/story/2009022>`_ for more details.