Пример #1
0
def main(notify, hour, minute):
    """Runs billing report. Optionally sends notifications to billing"""

    # Read the config file and get the admin context
    config_opts = ['--config-file', '/etc/neutron/neutron.conf']
    config.init(config_opts)
    # Have to load the billing module _after_ config is parsed so
    # that we get the right network strategy
    network_strategy.STRATEGY.load()
    billing.PUBLIC_NETWORK_ID = network_strategy.STRATEGY.get_public_net_id()
    config.setup_logging()
    context = neutron_context.get_admin_context()

    # A query to get all IPAddress objects from the db
    query = context.session.query(models.IPAddress)

    (period_start, period_end) = billing.calc_periods(hour, minute)

    full_day_ips = billing.build_full_day_ips(query, period_start, period_end)
    partial_day_ips = billing.build_partial_day_ips(query, period_start,
                                                    period_end)

    if notify:
        # '==================== Full Day ============================='
        for ipaddress in full_day_ips:
            click.echo('start: {}, end: {}'.format(period_start, period_end))
            payload = billing.build_payload(ipaddress,
                                            'ip.exists',
                                            start_time=period_start,
                                            end_time=period_end)
            billing.do_notify(context, 'ip.exists', payload)
        # '==================== Part Day ============================='
        for ipaddress in partial_day_ips:
            click.echo('start: {}, end: {}'.format(period_start, period_end))
            payload = billing.build_payload(ipaddress,
                                            'ip.exists',
                                            start_time=ipaddress.allocated_at,
                                            end_time=period_end)
            billing.do_notify(context, 'ip.exists', payload)
    else:
        click.echo('Case 1 ({}):\n'.format(len(full_day_ips)))
        for ipaddress in full_day_ips:
            pp(
                billing.build_payload(ipaddress,
                                      'ip.exists',
                                      start_time=period_start,
                                      end_time=period_end))

        click.echo('\n===============================================\n')

        click.echo('Case 2 ({}):\n'.format(len(partial_day_ips)))
        for ipaddress in partial_day_ips:
            pp(
                billing.build_payload(ipaddress,
                                      'ip.exists',
                                      start_time=ipaddress.allocated_at,
                                      end_time=period_end))
Пример #2
0
 def test_fixed_payload(self):
     start_time = datetime.datetime.utcnow().replace(microsecond=0) -\
         datetime.timedelta(days=1)
     end_time = datetime.datetime.utcnow().replace(microsecond=0)
     ipaddress = get_fake_fixed_address()
     ipaddress.allocated_at = start_time
     ipaddress.deallocated_at = end_time
     ipaddress.address_type = 'fixed'
     payload = billing.build_payload(ipaddress,
                                     'ip.exists',
                                     start_time=start_time,
                                     end_time=end_time)
     self.assertEqual(payload['event_type'], u'USAGE',
                      'event_type is wrong')
     self.assertEqual(payload['tenant_id'], TENANT_ID, 'tenant_id is wrong')
     self.assertEqual(payload['ip_address'], IP_READABLE,
                      'ip_address is wrong')
     self.assertEqual(payload['subnet_id'], SUBNET_ID, 'subnet_id is wrong')
     self.assertEqual(payload['network_id'], PUB_NETWORK_ID,
                      'network_id is wrong')
     self.assertEqual(payload['public'], True, 'public should be true')
     self.assertEqual(payload['ip_version'], 4, 'ip_version should be 4')
     self.assertEqual(payload['ip_type'], 'fixed',
                      'ip_type should be fixed')
     self.assertEqual(payload['id'], IP_ID, 'ip_id is wrong')
     self.assertEqual(payload['startTime'],
                      billing.convert_timestamp(start_time),
                      'startTime is wrong')
     self.assertEqual(payload['endTime'],
                      billing.convert_timestamp(end_time),
                      'endTime is wrong')
Пример #3
0
 def test_disassociate_flip_payload(self):
     event_time = datetime.datetime.utcnow().replace(microsecond=0)
     ipaddress = get_fake_fixed_address()
     # allocated_at and deallocated_at could be anything for testing this
     ipaddress.allocated_at = event_time
     ipaddress.deallocated_at = event_time
     ipaddress.address_type = 'floating'
     payload = billing.build_payload(ipaddress,
                                     'ip.disassociate',
                                     event_time=event_time)
     self.assertEqual(payload['event_type'], u'DOWN', 'event_type is wrong')
     self.assertEqual(payload['tenant_id'], TENANT_ID, 'tenant_id is wrong')
     self.assertEqual(payload['ip_address'], IP_READABLE,
                      'ip_address is wrong')
     self.assertEqual(payload['subnet_id'], SUBNET_ID, 'subnet_id is wrong')
     self.assertEqual(payload['network_id'], PUB_NETWORK_ID,
                      'network_id is wrong')
     self.assertEqual(payload['public'], True, 'public should be true')
     self.assertEqual(payload['ip_version'], 4, 'ip_version should be 4')
     self.assertEqual(payload['ip_type'], 'floating',
                      'ip_type should be fixed')
     self.assertEqual(payload['id'], IP_ID, 'ip_id is wrong')
     self.assertEqual(payload['eventTime'],
                      billing.convert_timestamp(event_time),
                      'eventTime is wrong')
Пример #4
0
 def test_fixed_payload(self):
     start_time = datetime.datetime.utcnow().replace(microsecond=0) -\
         datetime.timedelta(days=1)
     end_time = datetime.datetime.utcnow().replace(microsecond=0)
     ipaddress = get_fake_fixed_address()
     ipaddress.allocated_at = start_time
     ipaddress.deallocated_at = end_time
     ipaddress.address_type = 'fixed'
     payload = billing.build_payload(ipaddress, billing.IP_EXISTS,
                                     start_time=start_time,
                                     end_time=end_time)
     self.assertEqual(payload['event_type'], billing.IP_EXISTS,
                      'event_type is wrong')
     self.assertEqual(payload['tenant_id'], TENANT_ID,
                      'tenant_id is wrong')
     self.assertEqual(payload['ip_address'], IP_READABLE,
                      'ip_address is wrong')
     self.assertEqual(payload['ip_version'], 4,
                      'ip_version should be 4')
     self.assertEqual(payload['ip_type'], 'fixed',
                      'ip_type should be fixed')
     self.assertEqual(payload['id'], IP_ID, 'ip_id is wrong')
     self.assertEqual(payload['startTime'],
                      billing.convert_timestamp(start_time),
                      'startTime is wrong')
     self.assertEqual(payload['endTime'],
                      billing.convert_timestamp(end_time),
                      'endTime is wrong')
Пример #5
0
 def test_disassociate_flip_payload(self):
     event_time = datetime.datetime.utcnow().replace(microsecond=0)
     ipaddress = get_fake_fixed_address()
     # allocated_at and deallocated_at could be anything for testing this
     ipaddress.allocated_at = event_time
     ipaddress.deallocated_at = event_time
     ipaddress.address_type = 'floating'
     payload = billing.build_payload(ipaddress, billing.IP_DISASSOC,
                                     event_time=event_time)
     self.assertEqual(payload['event_type'], billing.IP_DISASSOC,
                      'event_type is wrong')
     self.assertEqual(payload['tenant_id'], TENANT_ID, 'tenant_id is wrong')
     self.assertEqual(payload['ip_address'], IP_READABLE,
                      'ip_address is wrong')
     self.assertEqual(payload['subnet_id'], SUBNET_ID, 'subnet_id is wrong')
     self.assertEqual(payload['network_id'], PUB_NETWORK_ID,
                      'network_id is wrong')
     self.assertEqual(payload['public'], True, 'public should be true')
     self.assertEqual(payload['ip_version'], 4, 'ip_version should be 4')
     self.assertEqual(payload['ip_type'], 'floating',
                      'ip_type should be fixed')
     self.assertEqual(payload['id'], IP_ID, 'ip_id is wrong')
     self.assertEqual(payload['eventTime'],
                      billing.convert_timestamp(event_time),
                      'eventTime is wrong')
Пример #6
0
 def test_disassociate_flip_payload(self):
     event_time = datetime.datetime.utcnow().replace(microsecond=0)
     ipaddress = get_fake_fixed_address()
     # allocated_at and deallocated_at could be anything for testing this
     ipaddress.allocated_at = event_time
     ipaddress.deallocated_at = event_time
     ipaddress.address_type = "floating"
     payload = billing.build_payload(ipaddress, "ip.disassociate", event_time=event_time)
     self.assertEqual(payload["event_type"], u"DOWN", "event_type is wrong")
     self.assertEqual(payload["tenant_id"], TENANT_ID, "tenant_id is wrong")
     self.assertEqual(payload["ip_address"], IP_READABLE, "ip_address is wrong")
     self.assertEqual(payload["subnet_id"], SUBNET_ID, "subnet_id is wrong")
     self.assertEqual(payload["network_id"], PUB_NETWORK_ID, "network_id is wrong")
     self.assertEqual(payload["public"], True, "public should be true")
     self.assertEqual(payload["ip_version"], 4, "ip_version should be 4")
     self.assertEqual(payload["ip_type"], "floating", "ip_type should be fixed")
     self.assertEqual(payload["id"], IP_ID, "ip_id is wrong")
     self.assertEqual(payload["eventTime"], billing.convert_timestamp(event_time), "eventTime is wrong")
Пример #7
0
 def test_fixed_payload(self):
     start_time = datetime.datetime.utcnow().replace(microsecond=0) - datetime.timedelta(days=1)
     end_time = datetime.datetime.utcnow().replace(microsecond=0)
     ipaddress = get_fake_fixed_address()
     ipaddress.allocated_at = start_time
     ipaddress.deallocated_at = end_time
     ipaddress.address_type = "fixed"
     payload = billing.build_payload(ipaddress, "ip.exists", start_time=start_time, end_time=end_time)
     self.assertEqual(payload["event_type"], u"USAGE", "event_type is wrong")
     self.assertEqual(payload["tenant_id"], TENANT_ID, "tenant_id is wrong")
     self.assertEqual(payload["ip_address"], IP_READABLE, "ip_address is wrong")
     self.assertEqual(payload["subnet_id"], SUBNET_ID, "subnet_id is wrong")
     self.assertEqual(payload["network_id"], PUB_NETWORK_ID, "network_id is wrong")
     self.assertEqual(payload["public"], True, "public should be true")
     self.assertEqual(payload["ip_version"], 4, "ip_version should be 4")
     self.assertEqual(payload["ip_type"], "fixed", "ip_type should be fixed")
     self.assertEqual(payload["id"], IP_ID, "ip_id is wrong")
     self.assertEqual(payload["startTime"], billing.convert_timestamp(start_time), "startTime is wrong")
     self.assertEqual(payload["endTime"], billing.convert_timestamp(end_time), "endTime is wrong")
Пример #8
0
def main(notify, hour, minute):
    """Runs billing report. Optionally sends notifications to billing"""

    # Read the config file and get the admin context
    config_opts = ['--config-file', '/etc/neutron/neutron.conf']
    config.init(config_opts)
    # Have to load the billing module _after_ config is parsed so
    # that we get the right network strategy
    network_strategy.STRATEGY.load()
    billing.PUBLIC_NETWORK_ID = network_strategy.STRATEGY.get_public_net_id()
    config.setup_logging()
    context = neutron_context.get_admin_context()

    # A query to get all IPAddress objects from the db
    query = context.session.query(models.IPAddress)

    (period_start, period_end) = billing.calc_periods(hour, minute)

    full_day_ips = billing.build_full_day_ips(query,
                                              period_start,
                                              period_end)
    partial_day_ips = billing.build_partial_day_ips(query,
                                                    period_start,
                                                    period_end)

    if notify:
        # '==================== Full Day ============================='
        for ipaddress in full_day_ips:
            click.echo('start: {}, end: {}'.format(period_start, period_end))
            payload = billing.build_payload(ipaddress,
                                            'ip.exists',
                                            start_time=period_start,
                                            end_time=period_end)
            billing.do_notify(context,
                              'ip.exists',
                              payload)
        # '==================== Part Day ============================='
        for ipaddress in partial_day_ips:
            click.echo('start: {}, end: {}'.format(period_start, period_end))
            payload = billing.build_payload(ipaddress,
                                            'ip.exists',
                                            start_time=ipaddress.allocated_at,
                                            end_time=period_end)
            billing.do_notify(context,
                              'ip.exists',
                              payload)
    else:
        click.echo('Case 1 ({}):\n'.format(len(full_day_ips)))
        for ipaddress in full_day_ips:
            pp(billing.build_payload(ipaddress,
                                     'ip.exists',
                                     start_time=period_start,
                                     end_time=period_end))

        click.echo('\n===============================================\n')

        click.echo('Case 2 ({}):\n'.format(len(partial_day_ips)))
        for ipaddress in partial_day_ips:
            pp(billing.build_payload(ipaddress,
                                     'ip.exists',
                                     start_time=ipaddress.allocated_at,
                                     end_time=period_end))