def create_server_hardware_as_ironic_node(server_hardware_info):
    if (service_manager.is_nova_flavor_available(server_hardware_info)):
        LOG.debug('There is a flavor available for server hardware '
                  '%(server_hardware)s',
                  {'server_hardware': server_hardware_info.get('name')})

        node = create_ironic_node(
            server_hardware_info=server_hardware_info
        )

        LOG.debug('Node %(node)s created', {'node': node})

        service_manager.update_ironic_node_state(
            node, server_hardware_info.get('server_hardware_uri')
        )
        is_server_hardware_available = server_hardware_info.get(
            'server_profile_uri') is not None
        if is_server_hardware_available:
            LOG.debug('Server hardware %(server_hardware)s has a '
                      'server profile',
                      {'server_hardware': server_hardware_info.get('name')})
            LOG.info('Setting server hardware %(server_hardware)s to '
                     'maintenance mode',
                     {'server_hardware': server_hardware_info.get('name')})
            service_manager.get_ironic_client().node.set_maintenance(
                node.uuid, "true", maint_reason=REASON
            )
def delete_ironic_node(server_hardware_uri):
    nodes = service_manager.get_ironic_client().node.list(detail=True)
    for node in nodes:
        LOG.debug('Node %(node_uuid)s using driver %(driver)s',
                      {'node_uuid': node.uuid, 'driver': node.driver})
        if node.driver in drivers:
            node_server_hardware_uri = node.extra.get("server_hardware_uri")
            if (server_hardware_uri == node_server_hardware_uri):
                service_manager.get_ironic_client().node.delete(node.uuid)
def _get_old_ov_server_hardwares():
    old_server_hardwares = []
    nodes = service_manager.get_ironic_client().node.list(detail=True)

    for server_hardware in service_manager.get_ov_server_hardware_list():
        if _is_ov_server_hardware_in_ironic(
            server_hardware=server_hardware, nodes=nodes):
            old_server_hardwares.append(server_hardware)

    return old_server_hardwares
def _is_ov_server_hardware_in_ironic(server_hardware, nodes=None):
    if nodes is None:
        nodes = service_manager.get_ironic_client().node.list(detail=True)
    for node in nodes:
        node_server_hardware_uri = node.extra.get("server_hardware_uri")
        if node_server_hardware_uri == server_hardware.get("uri"):
            LOG.info("Server Hardware %(server_hardware_name)s has a "
                     "corresponding node in Ironic",
                     {'server_hardware_name': server_hardware.get("name")})
            return True

    return False
def create_ironic_nodes():
    new_server_hardwares = _get_new_ov_server_hardwares()

    for server_hardware in new_server_hardwares:
        server_hardware_info = service_manager.parse_server_hardware_to_dict(
                                   server_hardware)
        create_server_hardware_as_ironic_node(server_hardware_info)
    ironic_client = service_manager.get_ironic_client()
    old_server_hardwares = _get_old_ov_server_hardwares()
    nodes = ironic_client.node.list()

    for server_hardware in old_server_hardwares:
        is_hardware_available = server_hardware_info.get(
                                    'server_profile_uri') is not None
        if is_hardware_available:
            update_ironic_node_maintenance_state(server_hardware, nodes)
def create_ironic_node(server_hardware_info):
    LOG.info('Creating node in Ironic reflecting server hardware '
             '%(server_hardware)s',
             {'server_hardware': server_hardware_info})

    sync_conf = service_manager.get_config_options()
    kwargs_node = {
                   # TODO: name should be hostname safe (WHYYY?)
                   # 'name': server_hardware_info.get('name')
                   # Uncomment as soon as final design decision from
                   # Ironic is released
                   'driver': sync_conf.ironic.default_sync_driver,
                   'driver_info': {'deploy_kernel':
                                   sync_conf.ironic.default_deploy_kernel_id,
                                   'deploy_ramdisk':
                                   sync_conf.ironic.default_deploy_ramdisk_id},
                   'properties': {'cpus': server_hardware_info.get('cpus'),
                                  'memory_mb': server_hardware_info.get(
                                                   'memory_mb'),
                                  'local_gb': server_hardware_info.get(
                                                   'local_gb'),
                                  'cpu_arch': server_hardware_info.get(
                                                   'cpu_arch'),
                                  'capabilities': 'server_hardware_type_uri:'
                                                  '%s,enclosure_group_uri:%s,'
                                                  'server_profile_template'
                                                  '_uri:%s' %
                                                  (server_hardware_info.get(
                                                   'server_hardware_type_uri'),
                                                   server_hardware_info.get(
                                                   'enclosure_group_uri'),
                                                   'None')},
                   'extra': {'server_hardware_uri': server_hardware_info.get(
                                                    'server_hardware_uri')},
                  }
    ironic_client = service_manager.get_ironic_client()
    node = ironic_client.node.create(**kwargs_node)
    LOG.info('Creating port for Node %(node_uuid)s using mac %(mac)s',
             {'node_uuid': node.uuid,
              'mac': server_hardware_info.get('mac')})
    ironic_client.port.create(node_uuid=node.uuid,
                              address=server_hardware_info.get('mac'))
    return node