<commit_before># 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.

"""
Ironic oVirt power manager and management interface.

Provides basic power control and management of virtual machines
via oVirt sdk API.

For use in dev and test environments.
"""

from ironic.common import boot_devices
from ironic.common import exception
from ironic.common.i18n import _
from ironic.common import states
from ironic.conductor import task_manager
from ironic.drivers import base
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils

from ironic_staging_drivers.common import exception as staging_exception

ovirtsdk = importutils.try_import('ovirtsdk4')
if ovirtsdk:
    import ovirtsdk4 as sdk
    import ovirtsdk4.types as otypes

IRONIC_TO_OVIRT_DEVICE_MAPPING = {
    boot_devices.PXE: 'network',
    boot_devices.DISK: 'hd',
    boot_devices.CDROM: 'cdrom',
}
OVIRT_TO_IRONIC_DEVICE_MAPPING = {v: k for k, v in
                                  IRONIC_TO_OVIRT_DEVICE_MAPPING.items()}

OVIRT_TO_IRONIC_POWER_MAPPING = {
    'down': states.POWER_OFF,
    'error': states.ERROR,
    'image_locked': states.POWER_OFF,
    'migrating': states.POWER_ON,
    'not_responding': states.ERROR,
    'paused': states.ERROR,
    'powering_down': states.POWER_OFF,
    'powering_up': states.POWER_ON,
    'reboot_in_progress': states.POWER_ON,
    'wait_for_launch': states.POWER_ON,
    'up': states.POWER_ON
}

opts = [
    cfg.StrOpt('address',
               default='127.0.0.1',
               help='oVirt address'),
    cfg.StrOpt('username',
               default='admin@internal',
               help='oVirt username'),
    cfg.StrOpt('password',
               help='oVirt password'),
    cfg.StrOpt('insecure',
               default=False,
               help='Skips verification of the oVirt host certificate'),
    cfg.StrOpt('ca_file',
               help='oVirt path to a CA file'),
]
CONF = cfg.CONF
CONF.register_opts(opts, group='ovirt')

LOG = logging.getLogger(__name__)

PROPERTIES = {
    'ovirt_address': _("Address of the oVirt Manager"),
    'ovirt_username': _("oVirt username"),
    'ovirt_password': _("<PASSWORD>"),
    'ovirt_insecure': _("Skips oVirt host certificate's verification"),
    'ovirt_ca_file': _("oVirt path to a CA file"),
    'ovirt_vm_name': _("Name of the VM in oVirt. Required."),
}


def _parse_driver_info(node):
    """Gets the driver specific node driver info.

    This method validates whether the 'driver_info' property of the
    supplied node contains the required information for this driver.

    :param node: an Ironic Node object.
    :returns: a dict containing information from driver_info (or where
        applicable, config values).
    :raises: MissingParameterValue, if some required parameter(s) are missing
        in the node's driver_info.
    :raises: InvalidParameterValue, if some parameter(s) have invalid value(s)
        in the node's driver_info.
    """
    conf_info = {attr: getattr(CONF.ovirt, attr) for attr in CONF.ovirt}
    node_info = node.driver_info or {}
    driver_info = {}
    for prop in PROPERTIES:
        node_value = node_info.get(prop)
        conf_value = conf_info.get(prop.replace('ovirt_', ''))
        value = node_value if node_value is not None else conf_value
        if value is None and prop not in ['ovirt_ca_file', 'ovirt_insecure']:
            raise exception.MissingParameterValue(
                _("%(prop)s is not set either in the configuration or"
                    "in the node's driver_info"))
        else:
            driver_info[prop] = value
    insecure = driver_info['ovirt_insecure']
    ovirt_ca_file = driver_info['ovirt_ca_file']
    if not insecure and ovirt_ca_file is None:
            msg = _("Missing ovirt_ca_file in the node's driver_info")
            raise exception.MissingParameterValue(msg)
    return driver_info


def _getvm(driver_info):
    address = driver_info['ovirt_address']
    username = driver_info['ovirt_username']
    password = <PASSWORD>['<PASSWORD>']
    insecure = driver_info['ovirt_insecure']
    ca_file = driver_info['ovirt_ca_file']
    name = driver_info['ovirt_vm_name'].encode('ascii', 'ignore')
    url = "https://%s/ovirt-engine/api" % address
    try:
        connection = sdk.Connection(url=url, username=username,
                                    password=password, insecure=insecure,
                                    ca_file=ca_file)<commit_msg>Fix ovirt property error logging<commit_after># 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.

"""
Ironic oVirt power manager and management interface.

Provides basic power control and management of virtual machines
via oVirt sdk API.

For use in dev and test environments.
"""

from ironic.common import boot_devices
from ironic.common import exception
from ironic.common.i18n import _
from ironic.common import states
from ironic.conductor import task_manager
from ironic.drivers import base
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import importutils

from ironic_staging_drivers.common import exception as staging_exception

ovirtsdk = importutils.try_import('ovirtsdk4')
if ovirtsdk:
    import ovirtsdk4 as sdk
    import ovirtsdk4.types as otypes

IRONIC_TO_OVIRT_DEVICE_MAPPING = {
    boot_devices.PXE: 'network',
    boot_devices.DISK: 'hd',
    boot_devices.CDROM: 'cdrom',
}
OVIRT_TO_IRONIC_DEVICE_MAPPING = {v: k for k, v in
                                  IRONIC_TO_OVIRT_DEVICE_MAPPING.items()}

OVIRT_TO_IRONIC_POWER_MAPPING = {
    'down': states.POWER_OFF,
    'error': states.ERROR,
    'image_locked': states.POWER_OFF,
    'migrating': states.POWER_ON,
    'not_responding': states.ERROR,
    'paused': states.ERROR,
    'powering_down': states.POWER_OFF,
    'powering_up': states.POWER_ON,
    'reboot_in_progress': states.POWER_ON,
    'wait_for_launch': states.POWER_ON,
    'up': states.POWER_ON
}

opts = [
    cfg.StrOpt('address',
               default='127.0.0.1',
               help='oVirt address'),
    cfg.StrOpt('username',
               default='admin@internal',
               help='oVirt username'),
    cfg.StrOpt('password',
               help='oVirt password'),
    cfg.StrOpt('insecure',
               default=False,
               help='Skips verification of the oVirt host certificate'),
    cfg.StrOpt('ca_file',
               help='oVirt path to a CA file'),
]
CONF = cfg.CONF
CONF.register_opts(opts, group='ovirt')

LOG = logging.getLogger(__name__)

PROPERTIES = {
    'ovirt_address': _("Address of the oVirt Manager"),
    'ovirt_username': _("oVirt username"),
    'ovirt_password': _("<PASSWORD>"),
    'ovirt_insecure': _("Skips oVirt host certificate's verification"),
    'ovirt_ca_file': _("oVirt path to a CA file"),
    'ovirt_vm_name': _("Name of the VM in oVirt. Required."),
}


def _parse_driver_info(node):
    """Gets the driver specific node driver info.

    This method validates whether the 'driver_info' property of the
    supplied node contains the required information for this driver.

    :param node: an Ironic Node object.
    :returns: a dict containing information from driver_info (or where
        applicable, config values).
    :raises: MissingParameterValue, if some required parameter(s) are missing
        in the node's driver_info.
    :raises: InvalidParameterValue, if some parameter(s) have invalid value(s)
        in the node's driver_info.
    """
    conf_info = {attr: getattr(CONF.ovirt, attr) for attr in CONF.ovirt}
    node_info = node.driver_info or {}
    driver_info = {}
    for prop in PROPERTIES:
        node_value = node_info.get(prop)
        conf_value = conf_info.get(prop.replace('ovirt_', ''))
        value = node_value if node_value is not None else conf_value
        if value is None and prop not in ['ovirt_ca_file', 'ovirt_insecure']:
            msg = _("%s is not set in either the configuration or "
                    "in the node's driver_info") % prop
            raise exception.MissingParameterValue(msg)
        else:
            driver_info[prop] = value
    insecure = driver_info['ovirt_insecure']
    ovirt_ca_file = driver_info['ovirt_ca_file']
    if not insecure and ovirt_ca_file is None:
            msg = _("Missing ovirt_ca_file in the node's driver_info")
            raise exception.MissingParameterValue(msg)
    return driver_info


def _getvm(driver_info):
    address = driver_info['ovirt_address']
    username = driver_info['ovirt_username']
    password = driver_info['ovirt_password']
    insecure = driver_info['ovirt_insecure']
    ca_file = driver_info['ovirt_ca_file']
    name = driver_info['ovirt_vm_name'].encode('ascii', 'ignore')
    url = "https://%s/ovirt-engine/api" % address
    try:
        connection = sdk.Connection(url=url, username=username,
                                    password=password, insecure=insecure,
                                    ca_file=ca_file)
{'fraction_non_alphanumeric': 0.09401107196779064, 'fraction_numerical': 0.003019627579265224, 'mean_word_length': 3.2280851063829785, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2540588', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2962, 'n_words': 883}