"Fossies" - the Fresh Open Source Software Archive

Member "cloudkitty-9.0.0/cloudkitty/storage_state/__init__.py" (10 Apr 2019, 4341 Bytes) of package /linux/misc/openstack/cloudkitty-9.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "__init__.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 8.0.0_vs_9.0.0.

    1 # -*- coding: utf-8 -*-
    2 # Copyright 2018 Objectif Libre
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 #
   16 # @author: Luka Peschke
   17 #
   18 from oslo_config import cfg
   19 from oslo_db.sqlalchemy import utils
   20 from oslo_log import log
   21 
   22 from cloudkitty import db
   23 from cloudkitty.storage_state import migration
   24 from cloudkitty.storage_state import models
   25 from cloudkitty import utils as ck_utils
   26 
   27 
   28 LOG = log.getLogger(__name__)
   29 
   30 
   31 CONF = cfg.CONF
   32 # NOTE(peschk_l): Required for defaults
   33 CONF.import_opt('backend', 'cloudkitty.fetcher', 'fetcher')
   34 CONF.import_opt('collector', 'cloudkitty.collector', 'collect')
   35 CONF.import_opt('scope_key', 'cloudkitty.collector', 'collect')
   36 
   37 
   38 class StateManager(object):
   39     """Class allowing state management in CloudKitty"""
   40 
   41     model = models.IdentifierState
   42 
   43     def _get_db_item(self, session, identifier,
   44                      fetcher=None, collector=None, scope_key=None):
   45         fetcher = fetcher or CONF.fetcher.backend
   46         collector = collector or CONF.collect.collector
   47         scope_key = scope_key or CONF.collect.scope_key
   48 
   49         q = utils.model_query(self.model, session)
   50         r = q.filter(self.model.identifier == identifier). \
   51             filter(self.model.scope_key == scope_key). \
   52             filter(self.model.fetcher == fetcher). \
   53             filter(self.model.collector == collector). \
   54             first()
   55 
   56         # In case the identifier exists with empty columns, update them
   57         if not r:
   58             # NOTE(peschk_l): We must use == instead of 'is' because sqlachmey
   59             # overloads this operator
   60             r = q.filter(self.model.identifier == identifier). \
   61                 filter(self.model.scope_key == None). \
   62                 filter(self.model.fetcher == None). \
   63                 filter(self.model.collector == None). \
   64                 first()  # noqa
   65             if r:
   66                 r.scope_key = scope_key
   67                 r.collector = collector
   68                 r.fetcher = fetcher
   69                 LOG.info('Updating identifier "{i}" with scope_key "{sk}", '
   70                          'collector "{c}" and fetcher "{f}"'.format(
   71                              i=identifier,
   72                              sk=scope_key,
   73                              c=collector,
   74                              f=fetcher))
   75                 session.commit()
   76         return r
   77 
   78     def set_state(self, identifier, state,
   79                   fetcher=None, collector=None, scope_key=None):
   80         if isinstance(state, int):
   81             state = ck_utils.ts2dt(state)
   82         session = db.get_session()
   83         session.begin()
   84         r = self._get_db_item(
   85             session, identifier, fetcher, collector, scope_key)
   86         if r and r.state != state:
   87             r.state = state
   88             session.commit()
   89         else:
   90             state_object = self.model(
   91                 identifier=identifier,
   92                 state=state,
   93                 fetcher=fetcher,
   94                 collector=collector,
   95                 scope_key=scope_key,
   96             )
   97             session.add(state_object)
   98             session.commit()
   99         session.close()
  100 
  101     def get_state(self, identifier,
  102                   fetcher=None, collector=None, scope_key=None):
  103         session = db.get_session()
  104         session.begin()
  105         r = self._get_db_item(
  106             session, identifier, fetcher, collector, scope_key)
  107         session.close()
  108         return ck_utils.dt2ts(r.state) if r else None
  109 
  110     def init(self):
  111         migration.upgrade('head')
  112 
  113     # This is made in order to stay compatible with legacy behavior but
  114     # shouldn't be used
  115     def get_tenants(self, begin=None, end=None):
  116         session = db.get_session()
  117         session.begin()
  118         q = utils.model_query(self.model, session)
  119         session.close()
  120         return [tenant.identifier for tenant in q]