def test_with_project(self):
        counter1 = counter.Counter('instance',
                                   'cumulative',
                                   '',
                                   1,
                                   'user-id',
                                   'project-id',
                                   'resource-id',
                                   timestamp=datetime.datetime(
                                       2012, 7, 2, 10, 40),
                                   resource_metadata={
                                       'display_name': 'test-server',
                                       'tag': 'self.counter',
                                   })
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test_list_resources',
        )
        self.conn.record_metering_data(msg)

        counter2 = counter.Counter('instance',
                                   'cumulative',
                                   '',
                                   1,
                                   'user-id2',
                                   'project-id2',
                                   'resource-id-alternate',
                                   timestamp=datetime.datetime(
                                       2012, 7, 2, 10, 41),
                                   resource_metadata={
                                       'display_name': 'test-server',
                                       'tag': 'self.counter2',
                                   })
        msg2 = meter.meter_message_from_counter(
            counter2,
            cfg.CONF.publisher_meter.metering_secret,
            'not-test',
        )
        self.conn.record_metering_data(msg2)

        data = self.get_json('/resources',
                             q=[{
                                 'field': 'project_id',
                                 'value': 'project-id',
                             }])
        ids = [r['resource_id'] for r in data]
        self.assertEquals(['resource-id'], ids)
    def test_metadata(self):
        counter1 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id',
            'project-id',
            'resource-id',
            timestamp=datetime.datetime(2012, 7, 2, 10, 40),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter',
                               'ignored_dict': {'key': 'value'},
                               'ignored_list': ['not-returned'],
                               }
        )
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test',
        )
        self.conn.record_metering_data(msg)

        data = self.get_json('/resources')
        metadata = data[0]['metadata']
        self.assertEqual(
            list(sorted(metadata.iteritems())),
            [('display_name', 'test-server'),
             ('tag', 'self.counter'),
             ])
    def test_with_user_wrong_tenant(self):
        counter1 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id2',
            'project-id2',
            'resource-id-alternate',
            timestamp=datetime.datetime(2012, 7, 2, 10, 41),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter1',
                               }
        )
        msg2 = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'not-test',
        )
        self.conn.record_metering_data(msg2)

        data = self.get_json('/resources',
                             headers={"X-Roles": "Member",
                                      "X-Tenant-Id": "project-wrong"})
        ids = set(r['resource_id'] for r in data)
        self.assertEquals(set(), ids)
    def test_resource_meter_links(self):
        counter1 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id',
            'project-id',
            'resource-id',
            timestamp=datetime.datetime(2012, 7, 2, 10, 40),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter',
                               }
        )
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test_list_resources',
        )
        self.conn.record_metering_data(msg)

        data = self.get_json('/resources')
        links = data[0]['links']
        self.assertEqual(len(links), 2)
        self.assertEqual(links[0]['rel'], 'self')
        self.assertTrue((self.PATH_PREFIX + '/resources/resource-id')
                        in links[0]['href'])
        self.assertEqual(links[1]['rel'], 'instance')
        self.assertTrue((self.PATH_PREFIX + '/meters/instance?'
                         'q.field=resource_id&q.value=resource-id')
                        in links[1]['href'])
    def setUp(self):
        super(TestMaxResourceVolume, self).setUp()
        require_map_reduce(self.conn)

        self.counters = []
        for i in range(3):
            c = counter.Counter(
                'volume.size',
                'gauge',
                'GiB',
                5 + i,
                'user-id',
                'project1',
                'resource-id',
                timestamp=datetime.datetime(2012, 9, 25, 10 + i, 30 + i),
                resource_metadata={'display_name': 'test-volume',
                                   'tag': 'self.counter',
                                   }
            )
            self.counters.append(c)
            msg = meter.meter_message_from_counter(
                c,
                cfg.CONF.publisher_meter.metering_secret,
                'source1',
            )
            self.conn.record_metering_data(msg)
    def test_with_user_wrong_tenant(self):
        counter1 = counter.Counter('instance',
                                   'cumulative',
                                   '',
                                   1,
                                   'user-id2',
                                   'project-id2',
                                   'resource-id-alternate',
                                   timestamp=datetime.datetime(
                                       2012, 7, 2, 10, 41),
                                   resource_metadata={
                                       'display_name': 'test-server',
                                       'tag': 'self.counter1',
                                   })
        msg2 = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'not-test',
        )
        self.conn.record_metering_data(msg2)

        data = self.get_json('/resources',
                             headers={
                                 "X-Roles": "Member",
                                 "X-Tenant-Id": "project-wrong"
                             })
        ids = set(r['resource_id'] for r in data)
        self.assertEquals(set(), ids)
    def test_metadata(self):
        counter1 = counter.Counter('instance',
                                   'cumulative',
                                   '',
                                   1,
                                   'user-id',
                                   'project-id',
                                   'resource-id',
                                   timestamp=datetime.datetime(
                                       2012, 7, 2, 10, 40),
                                   resource_metadata={
                                       'display_name': 'test-server',
                                       'tag': 'self.counter',
                                       'ignored_dict': {
                                           'key': 'value'
                                       },
                                       'ignored_list': ['not-returned'],
                                   })
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test',
        )
        self.conn.record_metering_data(msg)

        data = self.get_json('/resources')
        metadata = data[0]['metadata']
        self.assertEqual(list(sorted(metadata.iteritems())), [
            ('display_name', 'test-server'),
            ('tag', 'self.counter'),
        ])
示例#8
0
    def setUp(self):
        super(TestAPIACL, self).setUp()
        self.environ = {'fake.cache': FakeMemcache()}

        for cnt in [
                counter.Counter(
                    'meter.test',
                    'cumulative',
                    '',
                    1,
                    'user-good',
                    'project-good',
                    'resource-good',
                    timestamp=datetime.datetime(2012, 7, 2, 10, 40),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'self.counter'}),
                counter.Counter(
                    'meter.mine',
                    'gauge',
                    '',
                    1,
                    'user-fred',
                    'project-good',
                    'resource-56',
                    timestamp=datetime.datetime(2012, 7, 2, 10, 43),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'self.counter4'})]:
            msg = meter.meter_message_from_counter(
                cnt,
                cfg.CONF.publisher_meter.metering_secret,
                'test_source')
            self.conn.record_metering_data(msg)
示例#9
0
    def setUp(self):
        super(TestSumResourceVolume, self).setUp()
        require_map_reduce(self.conn)

        self.counters = []
        for i in range(3):
            c = counter.Counter('volume.size',
                                'gauge',
                                'GiB',
                                5 + i,
                                'user-id',
                                'project1',
                                'resource-id',
                                timestamp=datetime.datetime(
                                    2012, 9, 25, 10 + i, 30 + i),
                                resource_metadata={
                                    'display_name': 'test-volume',
                                    'tag': 'self.counter',
                                })
            self.counters.append(c)
            msg = meter.meter_message_from_counter(
                c,
                cfg.CONF.publisher_meter.metering_secret,
                'source1',
            )
            self.conn.record_metering_data(msg)
示例#10
0
    def test_resource_meter_links(self):
        counter1 = counter.Counter('instance',
                                   'cumulative',
                                   '',
                                   1,
                                   'user-id',
                                   'project-id',
                                   'resource-id',
                                   timestamp=datetime.datetime(
                                       2012, 7, 2, 10, 40),
                                   resource_metadata={
                                       'display_name': 'test-server',
                                       'tag': 'self.counter',
                                   })
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test_list_resources',
        )
        self.conn.record_metering_data(msg)

        data = self.get_json('/resources')
        links = data[0]['links']
        self.assertEqual(len(links), 2)
        self.assertEqual(links[0]['rel'], 'self')
        self.assertTrue((self.PATH_PREFIX +
                         '/resources/resource-id') in links[0]['href'])
        self.assertEqual(links[1]['rel'], 'instance')
        self.assertTrue(
            (self.PATH_PREFIX + '/meters/instance?'
             'q.field=resource_id&q.value=resource-id') in links[1]['href'])
示例#11
0
    def test_with_project(self):
        counter1 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id',
            'project-id',
            'resource-id',
            timestamp=datetime.datetime(2012, 7, 2, 10, 40),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter',
                               }
        )
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test_list_resources',
        )
        self.conn.record_metering_data(msg)

        counter2 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id2',
            'project-id2',
            'resource-id-alternate',
            timestamp=datetime.datetime(2012, 7, 2, 10, 41),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter2',
                               }
        )
        msg2 = meter.meter_message_from_counter(
            counter2,
            cfg.CONF.publisher_meter.metering_secret,
            'not-test',
        )
        self.conn.record_metering_data(msg2)

        data = self.get_json('/resources', q=[{'field': 'project_id',
                                               'value': 'project-id',
                                               }])
        ids = [r['resource_id'] for r in data]
        self.assertEquals(['resource-id'], ids)
示例#12
0
    def setUp(self):
        super(TestListResourcesBase, self).setUp()

        for cnt in [
                counter.Counter(
                    'instance',
                    'cumulative',
                    '',
                    1,
                    'user-id',
                    'project-id',
                    'resource-id',
                    timestamp=datetime.datetime(2012, 7, 2, 10, 40),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'self.counter'},
                ),
                counter.Counter(
                    'instance',
                    'cumulative',
                    '',
                    1,
                    'user-id',
                    'project-id',
                    'resource-id-alternate',
                    timestamp=datetime.datetime(2012, 7, 2, 10, 41),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'self.counter2'},
                ),
                counter.Counter(
                    'instance',
                    'cumulative',
                    '',
                    1,
                    'user-id2',
                    'project-id2',
                    'resource-id2',
                    timestamp=datetime.datetime(2012, 7, 2, 10, 42),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'self.counter3'},
                ),
                counter.Counter(
                    'instance',
                    'cumulative',
                    '',
                    1,
                    'user-id',
                    'project-id',
                    'resource-id',
                    timestamp=datetime.datetime(2012, 7, 2, 10, 43),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'self.counter4'}
                )]:
            msg = meter.meter_message_from_counter(
                cnt,
                cfg.CONF.publisher_meter.metering_secret,
                'test_list_resources')
            self.conn.record_metering_data(msg)
示例#13
0
    def test_instances(self):
        counter1 = counter.Counter('instance',
                                   'cumulative',
                                   '',
                                   1,
                                   'user-id',
                                   'project-id',
                                   'resource-id',
                                   timestamp=datetime.datetime(
                                       2012, 7, 2, 10, 40),
                                   resource_metadata={
                                       'display_name': 'test-server',
                                       'tag': 'self.counter',
                                   })
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test',
        )
        self.conn.record_metering_data(msg)

        counter2 = counter.Counter('instance',
                                   'cumulative',
                                   '',
                                   1,
                                   'user-id',
                                   'project-id',
                                   'resource-id-alternate',
                                   timestamp=datetime.datetime(
                                       2012, 7, 2, 10, 41),
                                   resource_metadata={
                                       'display_name': 'test-server',
                                       'tag': 'self.counter2',
                                   })
        msg2 = meter.meter_message_from_counter(
            counter2,
            cfg.CONF.publisher_meter.metering_secret,
            'test',
        )
        self.conn.record_metering_data(msg2)

        data = self.get_json('/resources')
        self.assertEquals(2, len(data))
示例#14
0
    def setUp(self):
        super(TestListEvents, self).setUp()
        self.counter1 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id',
            'project1',
            'resource-id',
            timestamp=datetime.datetime(2012, 7, 2, 10, 40),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter',
                               'ignored_dict': {'key': 'value'},
                               'ignored_list': ['not-returned'],
                               }
        )
        msg = meter.meter_message_from_counter(
            self.counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test_source',
        )
        self.conn.record_metering_data(msg)

        self.counter2 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id2',
            'project2',
            'resource-id-alternate',
            timestamp=datetime.datetime(2012, 7, 2, 10, 41),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter2',
                               }
        )
        msg2 = meter.meter_message_from_counter(
            self.counter2,
            cfg.CONF.publisher_meter.metering_secret,
            'source2',
        )
        self.conn.record_metering_data(msg2)
示例#15
0
    def test_instances(self):
        counter1 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id',
            'project-id',
            'resource-id',
            timestamp=datetime.datetime(2012, 7, 2, 10, 40),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter',
                               }
        )
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test',
        )
        self.conn.record_metering_data(msg)

        counter2 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id',
            'project-id',
            'resource-id-alternate',
            timestamp=datetime.datetime(2012, 7, 2, 10, 41),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter2',
                               }
        )
        msg2 = meter.meter_message_from_counter(
            counter2,
            cfg.CONF.publisher_meter.metering_secret,
            'test',
        )
        self.conn.record_metering_data(msg2)

        data = self.get_json('/resources')
        self.assertEquals(2, len(data))
示例#16
0
    def setUp(self):
        super(TestListUsers, self).setUp()

        counter1 = counter.Counter(
            'instance',
            'cumulative',
            'instance',
            1,
            'user-id',
            'project-id',
            'resource-id',
            timestamp=datetime.datetime(2012, 7, 2, 10, 40),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter',
                               }
        )
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test_list_users',
        )
        self.conn.record_metering_data(msg)

        counter2 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id2',
            'project-id',
            'resource-id-alternate',
            timestamp=datetime.datetime(2012, 7, 2, 10, 41),
            resource_metadata={'display_name': 'test-server',
                               'tag': 'self.counter2',
                               }
        )
        msg2 = meter.meter_message_from_counter(
            counter2,
            cfg.CONF.publisher_meter.metering_secret,
            'not-test',
        )
        self.conn.record_metering_data(msg2)
示例#17
0
    def setUp(self):
        super(TestListUsers, self).setUp()

        counter1 = counter.Counter('instance',
                                   'cumulative',
                                   'instance',
                                   1,
                                   'user-id',
                                   'project-id',
                                   'resource-id',
                                   timestamp=datetime.datetime(
                                       2012, 7, 2, 10, 40),
                                   resource_metadata={
                                       'display_name': 'test-server',
                                       'tag': 'self.counter',
                                   })
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test_list_users',
        )
        self.conn.record_metering_data(msg)

        counter2 = counter.Counter('instance',
                                   'cumulative',
                                   '',
                                   1,
                                   'user-id2',
                                   'project-id',
                                   'resource-id-alternate',
                                   timestamp=datetime.datetime(
                                       2012, 7, 2, 10, 41),
                                   resource_metadata={
                                       'display_name': 'test-server',
                                       'tag': 'self.counter2',
                                   })
        msg2 = meter.meter_message_from_counter(
            counter2,
            cfg.CONF.publisher_meter.metering_secret,
            'not-test',
        )
        self.conn.record_metering_data(msg2)
示例#18
0
def test_meter_message_from_counter_field():
    def compare(f, c, msg_f, msg):
        assert msg == c
    msg = meter.meter_message_from_counter(
        TEST_COUNTER, 'not-so-secret',
        'src')
    name_map = {'name': 'counter_name',
                'type': 'counter_type',
                'unit': 'counter_unit',
                'volume': 'counter_volume',
                }
    for f in TEST_COUNTER._fields:
        msg_f = name_map.get(f, f)
        yield compare, f, getattr(TEST_COUNTER, f), msg_f, msg[msg_f]
示例#19
0
def test_meter_message_from_counter_field():
    def compare(f, c, msg_f, msg):
        assert msg == c

    msg = meter.meter_message_from_counter(TEST_COUNTER, 'not-so-secret',
                                           'src')
    name_map = {
        'name': 'counter_name',
        'type': 'counter_type',
        'unit': 'counter_unit',
        'volume': 'counter_volume',
    }
    for f in TEST_COUNTER._fields:
        msg_f = name_map.get(f, f)
        yield compare, f, getattr(TEST_COUNTER, f), msg_f, msg[msg_f]
示例#20
0
    def test_instance_no_metadata(self):
        counter1 = counter.Counter('instance',
                                   'cumulative',
                                   '',
                                   1,
                                   'user-id',
                                   'project-id',
                                   'resource-id',
                                   timestamp=datetime.datetime(
                                       2012, 7, 2, 10, 40),
                                   resource_metadata=None)
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test',
        )
        self.conn.record_metering_data(msg)

        data = self.get_json('/resources')
        self.assertEquals(1, len(data))
示例#21
0
    def test_instance_no_metadata(self):
        counter1 = counter.Counter(
            'instance',
            'cumulative',
            '',
            1,
            'user-id',
            'project-id',
            'resource-id',
            timestamp=datetime.datetime(2012, 7, 2, 10, 40),
            resource_metadata=None
        )
        msg = meter.meter_message_from_counter(
            counter1,
            cfg.CONF.publisher_meter.metering_secret,
            'test',
        )
        self.conn.record_metering_data(msg)

        data = self.get_json('/resources')
        self.assertEquals(1, len(data))
示例#22
0
    def setUp(self):
        super(TestListMeters, self).setUp()

        for cnt in [
                counter.Counter(
                    'meter.test',
                    'cumulative',
                    '',
                    1,
                    'user-id',
                    'project-id',
                    'resource-id',
                    timestamp=datetime.datetime(2012, 7, 2, 10, 40),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'self.counter'}),
                counter.Counter(
                    'meter.test',
                    'cumulative',
                    '',
                    3,
                    'user-id',
                    'project-id',
                    'resource-id',
                    timestamp=datetime.datetime(2012, 7, 2, 11, 40),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'self.counter'}),
                counter.Counter(
                    'meter.mine',
                    'gauge',
                    '',
                    1,
                    'user-id',
                    'project-id',
                    'resource-id2',
                    timestamp=datetime.datetime(2012, 7, 2, 10, 41),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'two.counter'}),
                counter.Counter(
                    'meter.test',
                    'cumulative',
                    '',
                    1,
                    'user-id2',
                    'project-id2',
                    'resource-id3',
                    timestamp=datetime.datetime(2012, 7, 2, 10, 42),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'three.counter'}),
                counter.Counter(
                    'meter.mine',
                    'gauge',
                    '',
                    1,
                    'user-id4',
                    'project-id2',
                    'resource-id4',
                    timestamp=datetime.datetime(2012, 7, 2, 10, 43),
                    resource_metadata={'display_name': 'test-server',
                                       'tag': 'four.counter'})]:
            msg = meter.meter_message_from_counter(
                cnt,
                cfg.CONF.publisher_meter.metering_secret,
                'test_list_resources')
            self.conn.record_metering_data(msg)
示例#23
0
def main():
    cfg.CONF([], project='ceilometer')

    parser = argparse.ArgumentParser(
        description='generate metering data',
        )
    parser.add_argument(
        '--interval',
        default=10,
        type=int,
        help='the period between events, in minutes',
        )
    parser.add_argument(
        '--start',
        default=31,
        help='the number of days in the past to start timestamps',
        )
    parser.add_argument(
        '--end',
        default=2,
        help='the number of days into the future to continue timestamps',
        )
    parser.add_argument(
        '--type',
        choices=('gauge', 'cumulative'),
        default='gauge',
        help='counter type',
        )
    parser.add_argument(
        '--unit',
        default=None,
        help='counter unit',
        )
    parser.add_argument(
        '--project',
        help='project id of owner',
        )
    parser.add_argument(
        '--user',
        help='user id of owner',
        )
    parser.add_argument(
        'resource',
        help='the resource id for the meter data',
        )
    parser.add_argument(
        'counter',
        help='the counter name for the meter data',
        )
    parser.add_argument(
        'volume',
        help='the amount to attach to the meter',
        type=int,
        default=1,
        )
    args = parser.parse_args()

    # Set up logging to use the console
    console = logging.StreamHandler(sys.stderr)
    console.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(message)s')
    console.setFormatter(formatter)
    root_logger = logging.getLogger('')
    root_logger.addHandler(console)
    root_logger.setLevel(logging.DEBUG)

    # Connect to the metering database
    conn = storage.get_connection(cfg.CONF)

    # Find the user and/or project for a real resource
    if not (args.user or args.project):
        for r in conn.get_resources():
            if r['resource_id'] == args.resource:
                args.user = r['user_id']
                args.project = r['project_id']
                break

    # Compute start and end timestamps for the
    # new data.
    timestamp = timeutils.parse_isotime(args.start)
    end = timeutils.parse_isotime(args.end)
    increment = datetime.timedelta(minutes=args.interval)

    # Generate events
    n = 0
    while timestamp <= end:
        c = counter.Counter(name=args.counter,
                            type=args.type,
                            unit=args.unit,
                            volume=args.volume,
                            user_id=args.user,
                            project_id=args.project,
                            resource_id=args.resource,
                            timestamp=timestamp,
                            resource_metadata={},
                            )
        data = meter.meter_message_from_counter(
            c,
            cfg.CONF.publisher_meter.metering_secret,
            'artificial')
        conn.record_metering_data(data)
        n += 1
        timestamp = timestamp + increment

    print 'Added %d new events' % n

    return 0
示例#24
0
def test_meter_message_from_counter_signed():
    msg = meter.meter_message_from_counter(
        TEST_COUNTER,
        'not-so-secret',
        'src')
    assert 'message_signature' in msg
示例#25
0
def test_meter_message_from_counter_signed():
    msg = meter.meter_message_from_counter(TEST_COUNTER, 'not-so-secret',
                                           'src')
    assert 'message_signature' in msg
示例#26
0
    def prepare_data(self):
        def old_record_metering_data(self, data):
            self.db.user.update(
                {'_id': data['user_id']},
                {'$addToSet': {'source': data['source'],
                               },
                 },
                upsert=True,
            )
            self.db.project.update(
                {'_id': data['project_id']},
                {'$addToSet': {'source': data['source'],
                               },
                 },
                upsert=True,
            )
            received_timestamp = datetime.datetime.utcnow()
            self.db.resource.update(
                {'_id': data['resource_id']},
                {'$set': {'project_id': data['project_id'],
                          'user_id': data['user_id'],
                          # Current metadata being used and when it was
                          # last updated.
                          'timestamp': data['timestamp'],
                          'received_timestamp': received_timestamp,
                          'metadata': data['resource_metadata'],
                          'source': data['source'],
                          },
                 '$addToSet': {'meter': {'counter_name': data['counter_name'],
                                         'counter_type': data['counter_type'],
                                         },
                               },
                 },
                upsert=True,
            )

            record = copy.copy(data)
            self.db.meter.insert(record)
            return

        # Stubout with the old version DB schema, the one w/o 'counter_unit'
        self.stubs.Set(self.conn,
                       'record_metering_data',
                       old_record_metering_data)
        self.counters = []
        c = counter.Counter(
            'volume.size',
            'gauge',
            'GiB',
            5,
            'user-id',
            'project1',
            'resource-id',
            timestamp=datetime.datetime(2012, 9, 25, 10, 30),
            resource_metadata={'display_name': 'test-volume',
                               'tag': 'self.counter',
                               }
        )
        self.counters.append(c)
        msg = meter.meter_message_from_counter(
            c,
            secret='not-so-secret',
            source='test')
        self.conn.record_metering_data(self.conn, msg)
示例#27
0
def main():
    cfg.CONF([], project='ceilometer')

    parser = argparse.ArgumentParser(description='generate metering data', )
    parser.add_argument(
        '--interval',
        default=10,
        type=int,
        help='the period between events, in minutes',
    )
    parser.add_argument(
        '--start',
        default=31,
        help='the number of days in the past to start timestamps',
    )
    parser.add_argument(
        '--end',
        default=2,
        help='the number of days into the future to continue timestamps',
    )
    parser.add_argument(
        '--type',
        choices=('gauge', 'cumulative'),
        default='gauge',
        help='counter type',
    )
    parser.add_argument(
        '--unit',
        default=None,
        help='counter unit',
    )
    parser.add_argument(
        '--project',
        help='project id of owner',
    )
    parser.add_argument(
        '--user',
        help='user id of owner',
    )
    parser.add_argument(
        'resource',
        help='the resource id for the meter data',
    )
    parser.add_argument(
        'counter',
        help='the counter name for the meter data',
    )
    parser.add_argument(
        'volume',
        help='the amount to attach to the meter',
        type=int,
        default=1,
    )
    args = parser.parse_args()

    # Set up logging to use the console
    console = logging.StreamHandler(sys.stderr)
    console.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(message)s')
    console.setFormatter(formatter)
    root_logger = logging.getLogger('')
    root_logger.addHandler(console)
    root_logger.setLevel(logging.DEBUG)

    # Connect to the metering database
    conn = storage.get_connection(cfg.CONF)

    # Find the user and/or project for a real resource
    if not (args.user or args.project):
        for r in conn.get_resources():
            if r['resource_id'] == args.resource:
                args.user = r['user_id']
                args.project = r['project_id']
                break

    # Compute start and end timestamps for the
    # new data.
    timestamp = timeutils.parse_isotime(args.start)
    end = timeutils.parse_isotime(args.end)
    increment = datetime.timedelta(minutes=args.interval)

    # Generate events
    n = 0
    while timestamp <= end:
        c = counter.Counter(
            name=args.counter,
            type=args.type,
            unit=args.unit,
            volume=args.volume,
            user_id=args.user,
            project_id=args.project,
            resource_id=args.resource,
            timestamp=timestamp,
            resource_metadata={},
        )
        data = meter.meter_message_from_counter(
            c, cfg.CONF.publisher_meter.metering_secret, 'artificial')
        conn.record_metering_data(data)
        n += 1
        timestamp = timestamp + increment

    print 'Added %d new events' % n

    return 0