Ejemplo n.º 1
0
    def to_cloudkitty(self):
        # Rating informations
        rating_dict = {}
        rating_dict['price'] = self.rate

        # Volume informations
        vol_dict = {}
        vol_dict['qty'] = self.qty.normalize()
        vol_dict['unit'] = self.unit
        res_dict = {}

        # Encapsulate informations in a resource dict
        res_dict['rating'] = rating_dict
        res_dict['desc'] = json.loads(self.desc)
        res_dict['vol'] = vol_dict
        res_dict['tenant_id'] = self.tenant_id

        # Add resource to the usage dict
        usage_dict = {}
        usage_dict[self.res_type] = [res_dict]

        # Time informations
        period_dict = {}
        period_dict['begin'] = ck_utils.dt2iso(self.begin)
        period_dict['end'] = ck_utils.dt2iso(self.end)

        # Add period to the resource informations
        ck_dict = {}
        ck_dict['period'] = period_dict
        ck_dict['usage'] = usage_dict
        return ck_dict
Ejemplo n.º 2
0
    def to_cloudkitty(self, collector=None):
        # Rating informations
        rating_dict = {}
        rating_dict['price'] = self.rate

        # Volume informations
        vol_dict = {}
        vol_dict['qty'] = self.qty.normalize()
        vol_dict['unit'] = self.unit
        res_dict = {}

        # Encapsulate informations in a resource dict
        res_dict['rating'] = rating_dict
        res_dict['desc'] = json.loads(self.desc)
        res_dict['vol'] = vol_dict
        res_dict['tenant_id'] = self.tenant_id

        # Add resource to the usage dict
        usage_dict = {}
        usage_dict[self.res_type] = [res_dict]

        # Time informations
        period_dict = {}
        period_dict['begin'] = ck_utils.dt2iso(self.begin)
        period_dict['end'] = ck_utils.dt2iso(self.end)

        # Add period to the resource informations
        ck_dict = {}
        ck_dict['period'] = period_dict
        ck_dict['usage'] = usage_dict
        return ck_dict
Ejemplo n.º 3
0
def split_storage_data(raw_data):
    final_data = []
    for frame in raw_data:
        frame['period']['begin'] = ck_utils.dt2iso(frame['period']['begin'])
        frame['period']['end'] = ck_utils.dt2iso(frame['period']['end'])
        usage_buffer = frame.pop('usage')
        # Sort to have a consistent result as we are converting it to a list
        for service, data in sorted(usage_buffer.items()):
            new_frame = copy.deepcopy(frame)
            new_frame['usage'] = {service: data}
            new_frame['usage'][service][0]['tenant_id'] = TENANT
            final_data.append(new_frame)
    return final_data
Ejemplo n.º 4
0
    def _format_frame(self, res_type, resource, desc, measure, tenant_id):
        res_type_info = self._resource_type_data.get(res_type, None)
        if not res_type_info:
            return dict()

        start = measure[0]
        stop = start + datetime.timedelta(seconds=self._period)

        # Getting price
        price = decimal.Decimal(measure[2])
        price_dict = {'price': float(price)}

        # Getting vol
        if isinstance(res_type_info['qty_metric'], six.string_types):
            try:
                qty = self._conn.metric.get_measures(
                    resource['metrics'][res_type_info['qty_metric']],
                    aggregation='sum',
                    start=start,
                    stop=stop,
                    refresh=True)[-1][2]
            except IndexError:
                qty = 0
        else:
            qty = res_type_info['qty_metric']
        vol_dict = {'qty': decimal.Decimal(qty), 'unit': resource['unit']}

        # Period
        period_dict = {
            'begin': ck_utils.dt2iso(start),
            'end': ck_utils.dt2iso(stop),
        }

        # Formatting
        res_dict = dict()
        res_dict['desc'] = desc
        res_dict['vol'] = vol_dict
        res_dict['rating'] = price_dict
        res_dict['tenant_id'] = tenant_id

        return {
            'usage': {
                res_type: [res_dict]
            },
            'period': period_dict,
        }
Ejemplo n.º 5
0
    def to_cloudkitty(self):

        invoice_dict = {}
        invoice_dict['id'] = self.id
        invoice_dict['invoice_date'] = ck_utils.dt2iso(self.invoice_date)
        invoice_dict['invoice_period_from'] = ck_utils.dt2iso(self.invoice_period_from)
        invoice_dict['invoice_period_to'] = ck_utils.dt2iso(self.invoice_period_to)
        invoice_dict['tenant_id'] = self.tenant_id
        invoice_dict['tenant_name'] = self.tenant_name
        invoice_dict['invoice_id'] = self.invoice_id
        invoice_dict['invoice_data'] = json.loads(self.invoice_data)
        invoice_dict['total_cost'] = json.dumps(self.total_cost, use_decimal=True)
        invoice_dict['paid_cost'] = json.dumps(self.paid_cost, use_decimal=True)
        invoice_dict['balance_cost'] = json.dumps(self.balance_cost, use_decimal=True)
        invoice_dict['payment_status'] = self.payment_status

        return invoice_dict
Ejemplo n.º 6
0
    def _format_frame(self, res_type, resource, desc, measure, tenant_id):
        res_type_info = self._resource_type_data.get(res_type, None)
        if not res_type_info:
            return dict()

        start = measure[0]
        stop = start + datetime.timedelta(seconds=self._period)

        # Getting price
        price = decimal.Decimal(measure[2])
        price_dict = {'price': float(price)}

        # Getting vol
        if isinstance(res_type_info['qty_metric'], six.text_type):
            try:
                qty = self._conn.metric.get_measures(
                    resource['metrics'][res_type_info['qty_metric']],
                    aggregation='sum',
                    start=start, stop=stop,
                    refresh=True)[-1][2]
            except IndexError:
                qty = 0
        else:
            qty = res_type_info['qty_metric']
        vol_dict = {'qty': decimal.Decimal(qty), 'unit': resource['unit']}

        # Period
        period_dict = {
            'begin': ck_utils.dt2iso(start),
            'end': ck_utils.dt2iso(stop),
        }

        # Formatting
        res_dict = dict()
        res_dict['desc'] = desc
        res_dict['vol'] = vol_dict
        res_dict['rating'] = price_dict
        res_dict['tenant_id'] = tenant_id

        return {
            'usage': {res_type: [res_dict]},
            'period': period_dict,
        }
Ejemplo n.º 7
0
    def _to_cloudkitty(self, res_type, resource, cost_measure, qty_measure):

        start = cost_measure[0]
        stop = start + datetime.timedelta(seconds=cost_measure[1])

        # Period
        period_dict = {
            'begin': ck_utils.dt2iso(start),
            'end': ck_utils.dt2iso(stop),
        }

        return {
            'usage': {
                res_type: [
                    self._get_resource_frame(cost_measure, qty_measure,
                                             resource)
                ],
            },
            'period': period_dict,
        }
Ejemplo n.º 8
0
    def to_cloudkitty(self):

        invoice_dict = {}
        invoice_dict['id'] = self.id
        invoice_dict['invoice_date'] = ck_utils.dt2iso(self.invoice_date)
        invoice_dict['invoice_period_from'] = ck_utils.dt2iso(
            self.invoice_period_from)
        invoice_dict['invoice_period_to'] = ck_utils.dt2iso(
            self.invoice_period_to)
        invoice_dict['tenant_id'] = self.tenant_id
        invoice_dict['tenant_name'] = self.tenant_name
        invoice_dict['invoice_id'] = self.invoice_id
        invoice_dict['invoice_data'] = json.loads(self.invoice_data)
        invoice_dict['total_cost'] = json.dumps(self.total_cost,
                                                use_decimal=True)
        invoice_dict['paid_cost'] = json.dumps(self.paid_cost,
                                               use_decimal=True)
        invoice_dict['balance_cost'] = json.dumps(self.balance_cost,
                                                  use_decimal=True)
        invoice_dict['payment_status'] = self.payment_status

        return invoice_dict
Ejemplo n.º 9
0
    def to_cloudkitty(self, collector=None):
        if not collector:
            raise Exception('Gnocchi storage needs a reference '
                            'to the collector.')
        # Rating informations
        rating_dict = {}
        rating_dict['price'] = self.rate

        # Resource information from gnocchi
        resource_data = collector.resource_info(resource_name=self.res_type,
                                                start=self.begin,
                                                end=self.end,
                                                resource_id=self.resource_ref,
                                                project_id=self.tenant_id)

        # Encapsulate informations in a resource dict
        res_dict = {}
        resource_data = resource_data[0]
        res_dict['desc'] = resource_data['desc']
        res_dict['vol'] = resource_data['vol']
        res_dict['rating'] = rating_dict
        res_dict['tenant_id'] = self.tenant_id

        # Add resource to the usage dict
        usage_dict = {}
        usage_dict[self.res_type] = [res_dict]

        # Time informations
        period_dict = {}
        period_dict['begin'] = ck_utils.dt2iso(self.begin)
        period_dict['end'] = ck_utils.dt2iso(self.end)

        # Add period to the resource informations
        ck_dict = {}
        ck_dict['period'] = period_dict
        ck_dict['usage'] = usage_dict
        return ck_dict
Ejemplo n.º 10
0
    def to_cloudkitty(self, collector=None):
        if not collector:
            raise Exception("Gnocchi storage needs a reference " "to the collector.")
        # Rating informations
        rating_dict = {}
        rating_dict["price"] = self.rate

        # Resource information from gnocchi
        resource_data = collector.resource_info(
            resource_type=self.res_type,
            start=self.begin,
            end=self.end,
            resource_id=self.resource_ref,
            project_id=self.tenant_id,
        )

        # Encapsulate informations in a resource dict
        res_dict = {}
        res_dict["desc"] = resource_data["desc"]
        res_dict["vol"] = resource_data["vol"]
        res_dict["rating"] = rating_dict
        res_dict["tenant_id"] = self.tenant_id

        # Add resource to the usage dict
        usage_dict = {}
        usage_dict[self.res_type] = [res_dict]

        # Time informations
        period_dict = {}
        period_dict["begin"] = ck_utils.dt2iso(self.begin)
        period_dict["end"] = ck_utils.dt2iso(self.end)

        # Add period to the resource informations
        ck_dict = {}
        ck_dict["period"] = period_dict
        ck_dict["usage"] = usage_dict
        return ck_dict
Ejemplo n.º 11
0
    def to_cloudkitty(self, collector=None):
        if not collector:
            raise Exception('Gnocchi storage needs a reference '
                            'to the collector.')
        # Rating informations
        rating_dict = {}
        rating_dict['price'] = self.rate

        # Resource information from gnocchi
        resource_data = collector.resource_info(
            resource_type=self.res_type,
            start=self.begin,
            end=self.end,
            resource_id=self.resource_ref,
            project_id=self.tenant_id)

        # Encapsulate informations in a resource dict
        res_dict = {}
        res_dict['desc'] = resource_data['desc']
        res_dict['vol'] = resource_data['vol']
        res_dict['rating'] = rating_dict
        res_dict['tenant_id'] = self.tenant_id

        # Add resource to the usage dict
        usage_dict = {}
        usage_dict[self.res_type] = [res_dict]

        # Time informations
        period_dict = {}
        period_dict['begin'] = ck_utils.dt2iso(self.begin)
        period_dict['end'] = ck_utils.dt2iso(self.end)

        # Add period to the resource informations
        ck_dict = {}
        ck_dict['period'] = period_dict
        ck_dict['usage'] = usage_dict
        return ck_dict
Ejemplo n.º 12
0
    def put(self,
            all_scopes=False,
            scope_id=None,
            scope_key=None,
            fetcher=None,
            collector=None,
            state=None):

        policy.authorize(
            flask.request.context, 'scope:reset_state',
            {'tenant_id': scope_id or flask.request.context.project_id})

        if not all_scopes and scope_id is None:
            raise http_exceptions.BadRequest(
                "Either all_scopes or a scope_id should be specified.")

        results = self._storage_state.get_all(
            identifier=scope_id,
            scope_key=scope_key,
            fetcher=fetcher,
            collector=collector,
        )

        if len(results) < 1:
            raise http_exceptions.NotFound(
                "No resource found for provided filters.")

        serialized_results = [{
            'scope_id': r.identifier,
            'scope_key': r.scope_key,
            'fetcher': r.fetcher,
            'collector': r.collector,
        } for r in results]

        self._client.cast({},
                          'reset_state',
                          res_data={
                              'scopes': serialized_results,
                              'state': ck_utils.dt2iso(state),
                          })

        return {}, 202
Ejemplo n.º 13
0
 def default(self, obj):
     if isinstance(obj, decimal.Decimal):
         return float(obj)
     elif isinstance(obj, datetime.datetime):
         return ck_utils.dt2iso(obj)
     return super(CloudkittyJSONEncoder, self).default(obj)
Ejemplo n.º 14
0
    def tenant_addon_rate(self):

        logging.info("Inside the tenant add-on rating section")

        # get todays date
        ta_today_date = datetime.datetime.today()

        # can edit date for debugging and verification
        # ta_today_date = ta_today_date.replace(day=01, month=06, year=2016, hour=00, minute=00, second=00, microsecond=0)

        # Take current month and year
        ta_month = ta_today_date.month
        ta_year = ta_today_date.year

        # set the begin period
        ta_begin_period = ta_today_date.replace(day=1,
                                                hour=0,
                                                minute=0,
                                                second=0,
                                                microsecond=0)
        print 'ta_begin_period', ta_begin_period
        # set the end period
        ta_end_date_period = calendar.monthrange(ta_year, ta_month)[1]
        ta_end_period = ta_today_date.replace(day=ta_end_date_period,
                                              hour=23,
                                              minute=59,
                                              second=59,
                                              microsecond=0)
        print 'ta_end_period', ta_end_period
        # take the services list
        list_services = ck.hashmap.services.list()
        print 'list_services', list_services
        # getting the relevant service id
        for services in list_services:

            # getting the service id for tenant.addon
            if services.name == 'tenant.addon':
                service_id = services.service_id

        # get the relevant fields for the service
        list_fields = ck.hashmap.fields.list(service_id=service_id)
        print 'list_fields', list_fields
        for fields in list_fields:

            if fields.name == 'tenant_id':
                field_id = fields.field_id

        # mappings available for particular field
        # getting the tenants needs to rated
        list_mappings = ck.hashmap.mappings.list(field_id=field_id)
        print 'list_mappings', list_mappings
        # for available tenant addons
        for mappings in list_mappings:

            mapping_details = mappings
            ta_tenant_id = mapping_details.value
            print 'begin=', ta_begin_period, 'end=', ta_end_period, 'tenant_id=', ta_tenant_id
            # get the available rated dataframes for tenant.addon

            #ta_begin_period = '2017-04-01 00:00:00'
            #ta_end_period = '2017-04-30 23:59:59'
            ta_tenant_id = ' 5a4fb03113d44f7590789f9aa9ff3619'

            list_rated_frames = ck.storage.dataframes.list(
                resource_type='tenant.addon',
                begin=ta_begin_period,
                end=ta_end_period,
                tenant_id=ta_tenant_id)
            print 'list_rated_frames', list_rated_frames
            # frame count to decide the course of action
            frames_count = len(list_rated_frames)
            print 'frames_count', frames_count
            # for adding necessary details
            rate = mapping_details.cost
            print 'rate', rate
            # get the tenant details
            try:

                ta_tenant = keystone.tenants.get(ta_tenant_id)
                dicts = {
                    'name': ta_tenant.name,
                    'description': ta_tenant.description,
                    'tenant_id': ta_tenant_id,
                    'creation_time': ta_tenant.creation_date,
                    'timezone': ta_tenant.timezone
                }
                print 'ta_tenant', ta_tenant
            except:

                dicts = {'tenant_id': ta_tenant_id}

            # if tenant addon is not rated for this month
            if frames_count == 0:

                logging.info(
                    "Adding the tenant addon data to rated_data_frames for tenant %s"
                    % (ta_tenant_id))
                # add dataframes
                print dicts

                create_dict = {
                    'res_type': 'tenant.addon',
                    'begin': ck_utils.dt2iso(ta_begin_period),
                    'end': ck_utils.dt2iso(ta_begin_period),
                    'tenant_id': ta_tenant_id,
                    'unit': 'tenant',
                    'qty': '1',
                    'rate': rate
                }
                print create_dict
                #add_rated_frames = ck.storage.dataframes.create(create_dict)

                add_rated_frames = ck.storage.dataframes.create(
                    res_type='tenant.addon',
                    begin=ck_utils.dt2iso(ta_begin_period),
                    end=ck_utils.dt2iso(ta_begin_period),
                    tenant_id=ta_tenant_id,
                    unit='tenant',
                    qty='1',
                    rate=rate,
                    desc=dicts)
Ejemplo n.º 15
0
 def test_dt2iso(self):
     date = datetime.datetime(**self.date_params)
     trans_iso = ck_utils.dt2iso(date)
     self.assertEqual(self.date_iso, trans_iso)
Ejemplo n.º 16
0
#
import copy
import datetime
import decimal

from oslo_utils import uuidutils

from cloudkitty import utils as ck_utils

# These have a different format in order to check that both forms are supported
TENANT = 'f266f30b11f246b589fd266f85eeec39'
OTHER_TENANT = '8d3ae500-89ea-4142-9c6e-1269db6a0b64'

INITIAL_TIMESTAMP = 1420070400
FIRST_PERIOD_BEGIN = ck_utils.ts2dt(INITIAL_TIMESTAMP)
FIRST_PERIOD_BEGIN_ISO = ck_utils.dt2iso(FIRST_PERIOD_BEGIN)
FIRST_PERIOD_END = FIRST_PERIOD_BEGIN + datetime.timedelta(seconds=3600)
FIRST_PERIOD_END_ISO = ck_utils.dt2iso(FIRST_PERIOD_END)
SECOND_PERIOD_BEGIN = FIRST_PERIOD_END
SECOND_PERIOD_BEGIN_ISO = ck_utils.dt2iso(SECOND_PERIOD_BEGIN)
SECOND_PERIOD_END = SECOND_PERIOD_BEGIN + datetime.timedelta(seconds=3600)
SECOND_PERIOD_END_ISO = ck_utils.dt2iso(SECOND_PERIOD_END)

COMPUTE_METADATA = {
    'availability_zone': 'nova',
    'flavor': 'm1.nano',
    'image_id': 'f5600101-8fa2-4864-899e-ebcb7ed6b568',
    'instance_id': '26c084e1-b8f1-4cbc-a7ec-e8b356788a17',
    'id': '1558f911-b55a-4fd2-9173-c8f1f23e5639',
    'resource_id': '1558f911-b55a-4fd2-9173-c8f1f23e5639',
    'memory': '64',
Ejemplo n.º 17
0
 def test_dt2iso(self):
     date = datetime.datetime(**self.date_params)
     trans_iso = ck_utils.dt2iso(date)
     self.assertEqual(self.date_iso, trans_iso)