from usage.exc import UnknownConversionError from pkg_resources import iter_entry_points from usage.log import logging logger = logging.getLogger('conversions') CONVERSIONS = {} for entry_point in iter_entry_points(group='usage.conversions'): try: CONVERSIONS[entry_point.name] = entry_point.load() except: logger.warn('Unable to load conversion {}'.format(entry_point.name)) def convert(name, value): """Converts value with conversion name. :param name: Name of the conversion, like seconds_to_hours :type name: String :param value: Value to convert :type value: object :returns: Converted value :rtype: object: """ if name not in CONVERSIONS: raise UnknownConversionError(name) try: new_value = CONVERSIONS[name](value) except:
from usage.domain_cache import get_domain_name from usage.log import logging logger = logging.getLogger('usage.licensing.common') INDENTSIZE = 4 MAXLINESIZE = 79 FILL = {0: ' ', 1: ' '} class Licenser(object): """Base licenser class with common functionality.""" def __init__(self, project_id_field='Project Id', groupby=None): """Inits the licenser :param project_id_field: Name of the field in a csv report containing the project id of a resource. :type project_id_field: Str :param groupby: List of fields in a report to group results by. :type groupby: List """ self._project_id_field = project_id_field if groupby is None: groupby = ['domain'] self._groupby = groupby self._data = {} def _relevant(self, row): """Determine if row is relevant to licenser.
from common import CountLicenser as BaseCountLicenser from common import HourLicenser as BaseHourLicenser from common import INDENTSIZE from common import MAXLINESIZE from usage.log import logging logger = logging.getLogger('usage.licensing.windows') _DISTRO_KEY = 'image:OS Distro' _VERSION_KEY = 'image:OS Version' _GROUPBY = ['domain', _DISTRO_KEY, _VERSION_KEY] _RELEVANT_OSES = ['windows'] def _relevant(row): """Determine if row is relevant to windows licensers. :param row: Row :type row: Dict :returns: True if row is relevant. :rtype: Bool """ if row.get(_DISTRO_KEY).lower() in _RELEVANT_OSES: if row.get(_VERSION_KEY): return True return False def _row_cost(row, costs, quantity): """Determine cose of the row.
import ast import volume_types from usage import tag from usage.conversions.time_units import seconds_to_hours from usage.log import logging _ALLOWED_RESOURCE_ATTRS = ['resource_id', 'project_id', 'metadata'] logger = logging.getLogger('usage.fields') def _i_map(data): """Map lowercase insensitive keys to sensitive keys. :param data: Data to map :type data: Dict :returns: Map of insensitive to sensitive keys :rtype: Dict """ return {key.lower(): key for key in data.iterkeys()} def _i_get(data, i_map, key): """Get an item from data insensitively. :param data: Data with sensitive keys :type data: Dict :param i_map: Map of lowercase keys to real keys. :type i_map: Dict :param key: Sensitive key
from common import CountLicenser as BaseCountLicenser from common import HourLicenser as BaseHourLicenser from common import INDENTSIZE from common import MAXLINESIZE from usage.log import logging logger = logging.getLogger('usage.licensing.windows') _DISTRO_KEY = 'image:OS Distro' _VERSION_KEY = 'image:OS Version' _GROUPBY = [ 'domain', _DISTRO_KEY, _VERSION_KEY ] _RELEVANT_OSES = [ 'windows' ] def _relevant(row): """Determine if row is relevant to windows licensers. :param row: Row :type row: Dict :returns: True if row is relevant. :rtype: Bool """ if row.get(_DISTRO_KEY).lower() in _RELEVANT_OSES: if row.get(_VERSION_KEY): return True
import ast from usage.log import logging _ALLOWED_RESOURCE_ATTRS = ['resource_id', 'project_id', 'metadata'] logger = logging.getLogger('usage.fields') def _get_reading_attr(reading, attr): """Get an attribute of a reading. :param reading: Meter reading. :type reading: usage.reading.Reading :param attr: Name of the attr :type attr: String :returns: Attribute if an allowed attribute or None :rtype: Object|None """ if attr in _ALLOWED_RESOURCE_ATTRS: return getattr(reading, attr) return None def metadata_field(key, r): """Get value of metadata field if present. Special field function that is not called like the others. Only accepts a key parameter and a reading. :param key: Metadata key. Looks like metadata:<real key>
from usage.exc import UnknownFieldFunctionError from pkg_resources import iter_entry_points from usage.log import logging logger = logging.getLogger('fields') FIELD_FUNCTIONS = {} for entry_point in iter_entry_points(group='usage.fields'): try: FIELD_FUNCTIONS[entry_point.name] = entry_point.load() except: logger.warn('Unable to load field function {}'.format( entry_point.name)) def field_function(name, definition, item, reading): """Run a field function. Always send defintion, item, and reading. :param name: Name of the field function :type name: String :param definition: Report definition :type definition: dict :param item: Item in report definition. Describes how to make a row. :type item: dict :param reading: Meter reading for the item. :type reading: usage.reading.Reading :returns: Field function result :rtype: object: """ if name not in FIELD_FUNCTIONS: raise UnknownFieldFunctionError(name)
from common import CountLicenser as BaseCountLicenser from common import HourLicenser as BaseHourLicenser from common import INDENTSIZE from common import MAXLINESIZE from usage.log import logging logger = logging.getLogger('usage.licensing.sqlserver') _EDITION_KEY = 'image:SQLServer Edition' _VERSION_KEY = 'image:SQLServer Version' _GROUPBY = [ 'domain', _EDITION_KEY, _VERSION_KEY ] def _relevant(row): """Determine if row is relevant to SqlServer licensers. :param row: Row :type row: Dict :returns: True if row is relevant. :rtype: Bool """ if row.get(_EDITION_KEY): if row.get(_VERSION_KEY): return True return False
from usage.exc import UnknownFieldFunctionError from pkg_resources import iter_entry_points from usage.log import logging logger = logging.getLogger('fields') FIELD_FUNCTIONS = {} for entry_point in iter_entry_points(group='usage.fields'): try: FIELD_FUNCTIONS[entry_point.name] = entry_point.load() except: logger.warn( 'Unable to load field function {}'.format(entry_point.name)) def field_function(name, definition, item, reading): """Run a field function. Always send defintion, item, and reading. :param name: Name of the field function :type name: String :param definition: Report definition :type definition: dict :param item: Item in report definition. Describes how to make a row. :type item: dict :param reading: Meter reading for the item. :type reading: usage.reading.Reading :returns: Field function result :rtype: object: """ if name not in FIELD_FUNCTIONS:
from common import CountLicenser as BaseCountLicenser from common import HourLicenser as BaseHourLicenser from common import INDENTSIZE from common import MAXLINESIZE from usage.log import logging logger = logging.getLogger('usage.licensing.oracle') _EDITION_KEY = 'image:Oracle Edition' _VERSION_KEY = 'image:Oracle Version' _GROUPBY = [ 'domain', _EDITION_KEY, _VERSION_KEY ] def _relevant(row): """Determine if row is relevant to oracle licensers. :param row: Row :type row: Dict :returns: True if row is relevant. :rtype: Bool """ if row.get(_EDITION_KEY): if row.get(_VERSION_KEY): return True return False
from common import CountLicenser as BaseCountLicenser from common import HourLicenser as BaseHourLicenser from common import INDENTSIZE from common import MAXLINESIZE from usage.log import logging logger = logging.getLogger('usage.licensing.sqlserver') _EDITION_KEY = 'image:SQLServer Edition' _VERSION_KEY = 'image:SQLServer Version' _GROUPBY = ['domain', _EDITION_KEY, _VERSION_KEY] def _relevant(row): """Determine if row is relevant to SqlServer licensers. :param row: Row :type row: Dict :returns: True if row is relevant. :rtype: Bool """ if row.get(_EDITION_KEY): if row.get(_VERSION_KEY): return True return False def _row_cost(row, costs, quantity): """Determine cose of the row. :param row: Row
from common import CountLicenser as BaseCountLicenser from common import HourLicenser as BaseHourLicenser from common import INDENTSIZE from common import MAXLINESIZE from usage.log import logging logger = logging.getLogger('usage.licensing.oracle') _EDITION_KEY = 'image:Oracle Edition' _VERSION_KEY = 'image:Oracle Version' _GROUPBY = ['domain', _EDITION_KEY, _VERSION_KEY] def _relevant(row): """Determine if row is relevant to oracle licensers. :param row: Row :type row: Dict :returns: True if row is relevant. :rtype: Bool """ if row.get(_EDITION_KEY): if row.get(_VERSION_KEY): return True return False def _row_cost(row, costs, quantity): """Determine cose of the row. :param row: Row
import csv import yaml from pkg_resources import iter_entry_points from usage.log import logging logger = logging.getLogger('usage.licensing') _LICENSERS = {} for entry_point in iter_entry_points(group='usage.licensers'): try: _LICENSERS[entry_point.name] = entry_point.load() except: logger.exception('Unable to load licenser {}'.format(entry_point.name)) class Licensing: """ This class will create licensing summaries according to a definition. """ def __init__(self, domain_client=None, input_file=None, definition_file=None): """Inits the licensing object. :param domain_client: Domain client instance. :type domain_client: usage.clients.DomainClient :param input_file: Name of the input file. :type input_file: String :param definition_file: Name of the definition file