diff --git a/proliantutils/ilo/client.py b/proliantutils/ilo/client.py index e641f4b..cd3e0fa 100644 --- a/proliantutils/ilo/client.py +++ b/proliantutils/ilo/client.py @@ -17,6 +17,7 @@ from proliantutils.ilo import ipmi from proliantutils.ilo import operations from proliantutils.ilo import ribcl from proliantutils.ilo import ris +from proliantutils import log SUPPORTED_RIS_METHODS = [ 'activate_license', @@ -45,6 +46,8 @@ SUPPORTED_RIS_METHODS = [ 'update_persistent_boot', ] +LOG = log.get_logger(__name__) + class IloClient(operations.IloOperations): @@ -57,13 +60,20 @@ class IloClient(operations.IloOperations): cacert=cacert) self.info = {'address': host, 'username': login, 'password': password} self.model = self.ribcl.get_product_name() + LOG.debug("IloClient object created for host {} [model: {}]".format( + host, self.model)) def _call_method(self, method_name, *args, **kwargs): """Call the corresponding method using either RIBCL or RIS.""" - object = self.ribcl + the_operation_object = self.ribcl if ('Gen9' in self.model) and (method_name in SUPPORTED_RIS_METHODS): - object = self.ris - method = getattr(object, method_name) + the_operation_object = self.ris + method = getattr(the_operation_object, method_name) + + LOG.debug("Calling {} method of {} for host {}".format( + method_name, type(the_operation_object).__name__, + the_operation_object.host)) + return method(*args, **kwargs) def get_all_licenses(self): diff --git a/proliantutils/ilo/ribcl.py b/proliantutils/ilo/ribcl.py index 3bfa8e1..ff0dc58 100644 --- a/proliantutils/ilo/ribcl.py +++ b/proliantutils/ilo/ribcl.py @@ -29,6 +29,7 @@ import six from proliantutils import exception from proliantutils.ilo import common from proliantutils.ilo import operations +from proliantutils import log POWER_STATE = { @@ -43,6 +44,8 @@ BOOT_MODE_CMDS = [ 'SET_PENDING_BOOT_MODE' ] +LOG = log.get_logger(__name__) + class RIBCLOperations(operations.IloOperations): """iLO class for RIBCL interface for iLO. @@ -66,6 +69,9 @@ class RIBCLOperations(operations.IloOperations): if self.cacert is None: urllib3.disable_warnings(urllib3_exceptions.InsecureRequestWarning) + LOG.debug("RIBCLOperations object created for host {}".format( + self.host)) + def _request_ilo(self, root): """Send RIBCL XML data to iLO. diff --git a/proliantutils/ilo/ris.py b/proliantutils/ilo/ris.py index 0ff9021..d449842 100755 --- a/proliantutils/ilo/ris.py +++ b/proliantutils/ilo/ris.py @@ -28,6 +28,7 @@ from six.moves.urllib import parse as urlparse from proliantutils import exception from proliantutils.ilo import common from proliantutils.ilo import operations +from proliantutils import log """ Currently this class supports only secure boot and firmware settings related API's . @@ -41,6 +42,8 @@ DEVICE_COMMON_TO_RIS = {'NETWORK': 'Pxe', DEVICE_RIS_TO_COMMON = dict( (v, k) for (k, v) in DEVICE_COMMON_TO_RIS.items()) +LOG = log.get_logger(__name__) + class RISOperations(operations.IloOperations): @@ -61,6 +64,9 @@ class RISOperations(operations.IloOperations): if self.cacert is None: urllib3.disable_warnings(urllib3_exceptions.InsecureRequestWarning) + LOG.debug("RISOperations object created for host {}".format( + self.host)) + def _rest_op(self, operation, suburi, request_headers, request_body): """Generic REST Operation handler.""" diff --git a/proliantutils/log.py b/proliantutils/log.py new file mode 100644 index 0000000..d5e9794 --- /dev/null +++ b/proliantutils/log.py @@ -0,0 +1,36 @@ +# Copyright 2015 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Logger utility for proliantutils""" + +import logging + + +base_logger = logging.getLogger('proliantutils') +base_logger.addHandler(logging.NullHandler()) + + +def get_logger(name): + """Return a logger with the specified name + + If no name is specified then it returns the base logger + with the name, 'proliantutils'. + + :param name: logger name + """ + if not name: + return base_logger + + logger = logging.getLogger(name) + return logger diff --git a/proliantutils/tests/test_log.py b/proliantutils/tests/test_log.py new file mode 100644 index 0000000..45559ba --- /dev/null +++ b/proliantutils/tests/test_log.py @@ -0,0 +1,60 @@ +# Copyright 2015 Hewlett-Packard Development Company, L.P. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +"""Test Class for Log.""" + +import unittest + +import ddt + +from proliantutils import log + + +@ddt.ddt +class LogTestCase(unittest.TestCase): + + def setUp(self): + super(LogTestCase, self).setUp() + + @ddt.data(('pear',), + ('apple',), + ('banana',),) + @ddt.unpack + def test_get_logger_returns_the_same_logger_for_a_given_name( + self, logger_name): + # ----------------------------------------------------------------------- + # WHEN + # ----------------------------------------------------------------------- + logger1 = log.get_logger(logger_name) + logger2 = log.get_logger(logger_name) + # ----------------------------------------------------------------------- + # THEN + # ----------------------------------------------------------------------- + self.assertIs(logger1, logger2) + + def test_get_logger_returns_the_base_logger_for_no_name(self): + # ----------------------------------------------------------------------- + # GIVEN + # ----------------------------------------------------------------------- + base_logger = log.get_logger('proliantutils') + # ----------------------------------------------------------------------- + # WHEN + # ----------------------------------------------------------------------- + logger1 = log.get_logger(None) + logger2 = log.get_logger('') + # ----------------------------------------------------------------------- + # THEN + # ----------------------------------------------------------------------- + self.assertIs(logger1, base_logger) + self.assertIs(logger2, base_logger) diff --git a/test-requirements.txt b/test-requirements.txt index fbaba5c..5577c7a 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -2,3 +2,4 @@ mock hacking>=0.9.2,<0.10 testrepository>=0.0.18 testtools>=0.9.36,!=1.2.0 +ddt