metal/inventory/inventory/inventory/common/vim_api.py
John Kung bd998017d5 SysInv Decoupling: Create Inventory Service
Create host inventory services (api, conductor and agent) and
python-inventoryclient.

The inventory service collects the host resources and provides a
REST API and client to expose the host resources.

Create plugin for integration with system configuration (sysinv)
service.

This is the initial inventory service infratructure commit.
Puppet configuration, SM integration and host integration with
sysinv(systemconfig) changes are pending and planned to be
delivered in future commits.

Tests Performed:
 Verify the changes are inert on config_controller installation
 and provisioning.
     Puppet and spec changes are required in order to create keystone,
     database and activate inventory services.

 Unit tests performed (when puppet configuration for keystone, database
 is applied):
     Trigger host configure_check, configure signals into
         systemconfig(sysinv).

     Verify python-inventoryclient and api service:
         Disks and related storage resources are pending.
         inventory host-cpu-list/show
         inventory host-device-list/show/modify
         inventory host-ethernetport-list/show
         inventory host-lldp-neighbor-list
         inventory host-lldp-agent-list/show
         inventory host-memory-list/show
         inventory host-node-list/show
         inventory host-port-list/show

     Tox Unit tests:
         inventory: pep8
         python-inventoryclient: py27, pep8, cover, pylint

Change-Id: I744ac0de098608c55b9356abf180cc36601cfb8d
Story: 2002950
Task: 22952
Signed-off-by: John Kung <john.kung@windriver.com>
2018-12-06 13:17:35 -05:00

157 lines
5.1 KiB
Python

#
# Copyright (c) 2015-2018 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
from inventory.common import constants
from inventory.common import k_host
from inventory import objects
import json
from keystoneauth1.access import service_catalog as k_service_catalog
from oslo_log import log
from rest_api import rest_api_request
LOG = log.getLogger(__name__)
def _get_region(context):
system = objects.System.get_one(context)
return system.region_name
def _get_endpoint(context):
# service_type, service_name, interface = \
# CONF.nfv.catalog_info.split(':')
region_name = _get_region(context)
sc = k_service_catalog.ServiceCatalogV2(context.service_catalog)
service_parameters = {'service_type': 'nfv',
'service_name': 'vim',
'interface': 'internalURL',
'region_name': region_name}
endpoint = sc.url_for(**service_parameters)
LOG.info("NFV endpoint=%s" % endpoint)
return endpoint
def vim_host_add(context, uuid, hostname, subfunctions,
admininistrative, operational, availability,
subfunction_oper, subfunction_avail,
timeout=constants.VIM_DEFAULT_TIMEOUT_IN_SECS):
"""
Requests VIM to add a host.
"""
LOG.info("vim_host_add hostname=%s, subfunctions=%s "
"%s-%s-%s subfunction_oper=%s subfunction_avail=%s" %
(hostname, subfunctions, admininistrative, operational,
availability, subfunction_oper, subfunction_avail))
api_cmd = _get_endpoint(context)
api_cmd += "/nfvi-plugins/v1/hosts/"
api_cmd_headers = dict()
api_cmd_headers['Content-type'] = "application/json"
api_cmd_headers['User-Agent'] = "inventory/1.0"
api_cmd_payload = dict()
api_cmd_payload['uuid'] = uuid
api_cmd_payload['hostname'] = hostname
api_cmd_payload['subfunctions'] = subfunctions
api_cmd_payload['administrative'] = admininistrative
api_cmd_payload['operational'] = operational
api_cmd_payload['availability'] = availability
api_cmd_payload['subfunction_oper'] = subfunction_oper
api_cmd_payload['subfunction_avail'] = subfunction_avail
LOG.warn("vim_host_add api_cmd=%s headers=%s payload=%s" %
(api_cmd, api_cmd_headers, api_cmd_payload))
response = rest_api_request(context, "POST", api_cmd, api_cmd_headers,
json.dumps(api_cmd_payload), timeout)
return response
def vim_host_action(context, uuid, hostname, action,
timeout=constants.VIM_DEFAULT_TIMEOUT_IN_SECS):
"""
Request VIM to perform host action.
"""
response = None
_valid_actions = [k_host.ACTION_UNLOCK,
k_host.ACTION_LOCK,
k_host.ACTION_FORCE_LOCK]
if action not in _valid_actions:
LOG.error("Unrecognized vim_host_action=%s" % action)
return response
LOG.warn("vim_host_action hostname=%s, action=%s" % (hostname, action))
api_cmd = _get_endpoint(context)
api_cmd += "/nfvi-plugins/v1/hosts/%s" % uuid
api_cmd_headers = dict()
api_cmd_headers['Content-type'] = "application/json"
api_cmd_headers['User-Agent'] = "inventory/1.0"
api_cmd_payload = dict()
api_cmd_payload['uuid'] = uuid
api_cmd_payload['hostname'] = hostname
api_cmd_payload['action'] = action
LOG.warn("vim_host_action hostname=%s, action=%s api_cmd=%s "
"headers=%s payload=%s" %
(hostname, action, api_cmd, api_cmd_headers, api_cmd_payload))
response = rest_api_request(context, "PATCH", api_cmd, api_cmd_headers,
json.dumps(api_cmd_payload), timeout)
return response
def vim_host_delete(context, uuid, hostname,
timeout=constants.VIM_DEFAULT_TIMEOUT_IN_SECS):
"""
Asks VIM to delete a host
"""
api_cmd = _get_endpoint(context)
api_cmd += "/nfvi-plugins/v1/hosts/%s" % uuid
api_cmd_headers = dict()
api_cmd_headers['Content-type'] = "application/json"
api_cmd_headers['User-Agent'] = "inventory/1.0"
api_cmd_payload = dict()
api_cmd_payload['uuid'] = uuid
api_cmd_payload['hostname'] = hostname
api_cmd_payload['action'] = 'delete'
response = rest_api_request(context, "DELETE", api_cmd,
api_cmd_headers,
json.dumps(api_cmd_payload),
timeout=timeout)
return response
def vim_host_get_instances(context, uuid, hostname,
timeout=constants.VIM_DEFAULT_TIMEOUT_IN_SECS):
"""
Returns instance counts for a given host
"""
response = None
api_cmd = _get_endpoint(context)
api_cmd += "/nfvi-plugins/v1/hosts"
api_cmd_headers = dict()
api_cmd_headers['Content-type'] = "application/json"
api_cmd_headers['User-Agent'] = "inventory/1.0"
api_cmd_payload = dict()
api_cmd_payload['uuid'] = uuid
api_cmd_payload['hostname'] = hostname
response = rest_api_request(context, "GET", api_cmd, api_cmd_headers,
json.dumps(api_cmd_payload), timeout)
return response