File: fetcher.rst

package info (click to toggle)
cloudkitty 23.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,528 kB
  • sloc: python: 21,803; sh: 528; makefile: 226; pascal: 54
file content (78 lines) | stat: -rw-r--r-- 2,118 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
===============
 Scope fetcher
===============

The fetcher retrieves a list of scopes to rate. These scopes are then passed to
the collector, in combination with each metric type.

Implementation
==============

Fetchers are extremely simple. A custom fetcher must implement the following
class:

.. autoclass:: cloudkitty.fetcher.BaseFetcher
   :members: get_tenants

The ``get_tenants`` method takes no parameters and returns a list of unique
scope_ids, represented as ``str``.

The name of the new fetcher must be specified as a class attribute.

Options for the new fetcher must be registered under the ``fetcher_<name>``
config section.

A new scope fetcher must be implemented in a new module, in
``cloudkitty.fetcher.<name>.py``. Its class must be called ``<Name>Fetcher``.

An entrypoint must be registered for new fetchers. This is done in the
``setup.cfg`` file, located at the root of the repository:

.. code-block:: ini

   cloudkitty.fetchers =
       keystone = cloudkitty.fetcher.keystone:KeystoneFetcher
       source = cloudkitty.fetcher.source:SourceFetcher
       # [...]
       custom = cloudkitty.fetcher.custom:CustomFetcher

Example
=======

The most simple scope fetcher is the ``SourceFetcher``. It simply returns a
list of scopes read from the configuration file:

.. code-block:: python

   # In cloudkitty/fetcher/source.py
   from oslo_config import cfg

   from cloudkitty import fetcher

   FETCHER_SOURCE_OPTS = 'fetcher_source'

   fetcher_source_opts = [
       cfg.ListOpt(
           'sources',
           default=list(),
           help='list of source identifiers',
       ),
   ]

   # Registering the 'sources' option in the 'fetcher_source' option
   cfg.CONF.register_opts(fetcher_source_opts, FETCHER_SOURCE_OPTS)

   CONF = cfg.CONF


   class SourceFetcher(fetcher.BaseFetcher):

       # Defining the name of the fetcher
       name = 'source'

       # Returning the list of scopes read from the configuration file
       def get_tenants(self):
           return CONF.fetcher_source.sources


More complex examples can be found in the ``cloudkitty/fetcher`` directory.