Beispiel #1
0
def openstack(**kwargs):
    if settings.OPENSTACK_URL is None:
        return False, 'not configured.', kwargs
    stack = OpenStack(
                settings.OPENSTACK_URL,
                settings.OPENSTACK_USER,
                settings.OPENSTACK_PASS,
            )
    end = kwargs.get('end') or datetime.datetime.today().replace(
                hour=0, minute=0, second=0, microsecond=0)
    start = kwargs.get('start') or end - datetime.timedelta(days=1)
    tenants = collections.defaultdict(dict)
    for data in stack.simple_tenant_usage(start, end):
        tenants[data['tenant_id']].update(data)
    for url, query in getattr(settings, 'OPENSTACK_EXTRA_QUERIES', []):
        for data in stack.query(query, url=url,
                start=start.strftime('%Y-%m-%dT%H:%M:%S'),
                end=end.strftime('%Y-%m-%dT%H:%M:%S'),
            ):
            tenants[data['tenant_id']].update(data)
    for tenant_id, data in tenants.iteritems():
        dev, cost = make_tenant(data)
        dev.historycost_set.filter(start=start).delete()
        margin_in_percent = dev.get_margin() or 0
        total_cost = cost * (1 + margin_in_percent / 100)
        hc = HistoryCost(
            device=dev,
            venture=dev.venture,
            start=start,
            end=end,
            daily_cost=total_cost,
        )
        hc.save()
    return True, 'loaded from %s to %s.' % (start, end), kwargs
Beispiel #2
0
def openstack(**kwargs):
    if settings.OPENSTACK_URL is None:
        return False, 'not configured.', kwargs
    tenants = collections.defaultdict(lambda: collections.defaultdict(dict))
    end = kwargs.get('end') or datetime.datetime.today().replace(
        hour=0, minute=0, second=0, microsecond=0)
    start = kwargs.get('start') or end - datetime.timedelta(days=1)
    for region in getattr(settings, 'OPENSTACK_REGIONS', ['']):
        stack = OpenStack(
            settings.OPENSTACK_URL,
            settings.OPENSTACK_USER,
            settings.OPENSTACK_PASS,
            region=region,
        )
        for data in stack.simple_tenant_usage(start, end):
            tenants[data['tenant_id']][region].update(data)
    for url, query in getattr(settings, 'OPENSTACK_EXTRA_QUERIES', []):
        for data in stack.query(
                query,
                url=url,
                start=start.strftime('%Y-%m-%dT%H:%M:%S'),
                end=end.strftime('%Y-%m-%dT%H:%M:%S'),
        ):
            tenants[data['tenant_id']][url].update(data)
    for tenant_id, regions in tenants.iteritems():
        dev = make_tenant(tenant_id)
        dev.historycost_set.filter(start=start).delete()
        margin_in_percent = dev.get_margin() or 0
        total_cost = 0
        for region, data in regions.iteritems():
            cost = make_components(data, dev, region)
            total_cost += cost * (1 + margin_in_percent / 100)
        hc = HistoryCost(
            device=dev,
            venture=dev.venture,
            start=start,
            end=end,
            daily_cost=total_cost,
        )
        hc.save()
    return True, 'loaded from %s to %s.' % (start, end), kwargs