Merge "Update podmanager controller to use generic interface"

This commit is contained in:
Jenkins 2017-09-22 00:50:53 +00:00 committed by Gerrit Code Review
commit 7b1b261a18
6 changed files with 36 additions and 97 deletions

View File

@ -36,7 +36,7 @@ class PodManagersList(flask_restful.Resource):
def post(self):
values = flask.request.get_json()
return utils.make_response(http_client.OK,
podmanagers.create_podm(values))
podmanagers.create_podmanager(values))
class PodManager(flask_restful.Resource):
@ -48,9 +48,10 @@ class PodManager(flask_restful.Resource):
def patch(self, podm_uuid):
values = flask.request.get_json()
return utils.make_response(http_client.OK,
podmanagers.update_podm(podm_uuid, values))
podmanagers.update_podmanager(podm_uuid,
values))
def delete(self, podm_uuid):
podmanagers.delete_podm_by_uuid(podm_uuid)
podmanagers.delete_podmanager(podm_uuid)
resp_dict = exception.confirmation(confirm_detail="DELETED")
return utils.make_response(http_client.OK, resp_dict)

View File

@ -118,7 +118,7 @@ def get_basic_auth_credentials(authentication):
"""parse out the basic auth from podm's authentication array properties
:param authentication: podm's authentication
:return: username, password to connect to the podm
:return: username, password to connect to podmanager
"""
for auth_property in authentication:
if auth_property['type'] == constants.PODM_AUTH_BASIC_TYPE:

View File

@ -14,10 +14,11 @@
import logging
from valence.common import constants
from valence.common import exception
from valence.common import utils
from valence.controller import nodes
from valence.db import api as db_api
from valence.redfish import redfish
from valence.podmanagers import manager
LOG = logging.getLogger(__name__)
@ -25,7 +26,6 @@ LOG = logging.getLogger(__name__)
def _check_creation(values):
"""Checking args when creating a new pod manager
authentication: should follow the format
name: can not be duplicated
url: can not be duplicated
@ -38,30 +38,10 @@ def _check_creation(values):
urls = [podm['url'] for podm in pod_manager_list]
if values['name'] in names or values['url'] in urls:
raise exception.BadRequest('duplicated name or url !')
# If podmanager 'driver' is None, update as "redfishv1" which is default
# driver used to manage resources.
# 'driver' can take values like "redfishv1", "redfishv2" etc.
values['driver'] = values.get('driver', 'redfishv1')
# input status
values['status'] = get_podm_status(values['url'], values['authentication'])
return values
def _check_updation(values):
"""Checking args when updating a exist pod manager
:values: The properties of pod manager to be updated
:returns: improved values that could be updated
"""
# uuid, url can not be modified
if 'uuid' in values:
values.pop('uuid')
if 'url' in values:
values.pop('url')
return values
@ -73,35 +53,28 @@ def get_podm_by_uuid(uuid):
return db_api.Connection.get_podmanager_by_uuid(uuid).as_dict()
def create_podm(values):
def create_podmanager(values):
values = _check_creation(values)
username, password = utils.get_basic_auth_credentials(
values['authentication'])
# Retreive podm connection to get the status of podmanager
mng = manager.Manager(values['url'], username, password, values['driver'])
values['status'] = mng.podm.get_status()
return db_api.Connection.create_podmanager(values).as_dict()
def update_podm(uuid, values):
values = _check_updation(values)
def update_podmanager(uuid, values):
# Remove uuid and url from values as they can't be updated
for key in ['uuid', 'url']:
values.pop(key, None)
return db_api.Connection.update_podmanager(uuid, values).as_dict()
def delete_podm_by_uuid(uuid):
# TODO(hubian) this need to break the links between podm and its Nodes
def delete_podmanager(uuid):
# For any cleanup driver needs to do (nodes cleanup), before deleting podm
p_nodes = db_api.Connection.list_composed_nodes({'podm_id': uuid})
# Delete the nodes w.r.t podmanager from valence DB
for node in p_nodes:
nodes.Node(node['uuid']).delete_composed_node(node['uuid'])
return db_api.Connection.delete_podmanager(uuid)
def get_podm_status(url, authentication):
"""get pod manager running status by its url and auth
:param url: The url of pod manager.
:param authentication: array, The auth(s) info of pod manager.
:returns: status of the pod manager
"""
for auth in authentication:
# TODO(Hubian) Only consider and support basic auth type here.
# After decided to support other auth type this would be improved.
if auth['type'] == constants.PODM_AUTH_BASIC_TYPE:
username = auth['auth_items']['username']
password = auth['auth_items']['password']
return redfish.pod_status(url, username, password)
return constants.PODM_STATUS_UNKNOWN

View File

@ -23,6 +23,10 @@ class PodManagerBase(object):
password=password,
base_url=podm_url)
# TODO(ramineni): rebase on nate's patch
def get_status(self):
pass
def get_podm_info(self):
return self.get_resource_info_by_url(self.podm_url)

View File

@ -15,7 +15,6 @@ import unittest
import mock
from valence.common import constants
from valence.common.exception import BadRequest
from valence.controller import podmanagers
@ -23,15 +22,13 @@ from valence.controller import podmanagers
class TestPodManagers(unittest.TestCase):
@mock.patch('valence.controller.podmanagers.get_podm_list')
@mock.patch('valence.controller.podmanagers.get_podm_status')
def test_check_creation(self, mock_get_podm_status, mock_get_podm_list):
def test_check_creation(self, mock_get_podm_list):
mock_get_podm_list.return_value = [
{"name": "test1",
"url": "https://10.0.0.1"},
{"name": "test2",
"url": "https://10.0.0.2"}
]
mock_get_podm_status.return_value = constants.PODM_STATUS_ONLINE
values = {"name": "podm_name",
"url": "https://10.240.212.123",
@ -46,12 +43,9 @@ class TestPodManagers(unittest.TestCase):
]}
result_values = copy.deepcopy(values)
result_values['status'] = constants.PODM_STATUS_ONLINE
result_values['driver'] = 'redfishv1'
self.assertEqual(podmanagers._check_creation(values), result_values)
mock_get_podm_status.assert_called_once_with(values['url'],
values['authentication'])
mock_get_podm_list.assert_called_once_with()
@mock.patch('valence.controller.podmanagers.get_podm_list')
@ -97,7 +91,8 @@ class TestPodManagers(unittest.TestCase):
url_duplicate_values)
self.assertEqual(mock_get_podm_list.call_count, 2)
def test_check_updation_ignore_url_uuid(self):
@mock.patch('valence.db.api.Connection.update_podmanager')
def test_check_updation_ignore_url_uuid(self, mock_db_update):
values = {
"uuid": "uuid",
"url": "url",
@ -107,39 +102,5 @@ class TestPodManagers(unittest.TestCase):
result_values.pop('url')
result_values.pop('uuid')
self.assertEqual(podmanagers._check_updation(values), result_values)
@mock.patch('valence.redfish.redfish.pod_status')
def test_get_podm_status(self, mock_pod_status):
mock_pod_status.return_value = constants.PODM_STATUS_ONLINE
authentication = [
{
"type": "basic",
"auth_items": {
"username": "username",
"password": "password"
}
}
]
self.assertEqual(podmanagers.get_podm_status('url', authentication),
constants.PODM_STATUS_ONLINE)
mock_pod_status.asset_called_once_with('url', "username", "password")
def test_get_podm_status_unknown(self):
"""not basic type authentication podm status set value to be unknown"""
authentication = [
{
"type": "CertificateAuthority",
"auth_items": {
"public_key": "xxxxxxx"
}
},
{
"type": "DynamicCode",
"auth_items": {
"code": "xxxxxxx"
}
}
]
self.assertEqual(podmanagers.get_podm_status('url', authentication),
constants.PODM_STATUS_UNKNOWN)
podmanagers.update_podmanager('fake-podm-id', values)
mock_db_update.assert_called_once_with('fake-podm-id', result_values)

View File

@ -74,9 +74,9 @@ class TestFlavorApi(TestApiValidation):
class TestPodmanagerApi(TestApiValidation):
@mock.patch('valence.controller.podmanagers.create_podm')
@mock.patch('valence.controller.podmanagers.create_podmanager')
@mock.patch('valence.controller.podmanagers.get_podm_list')
@mock.patch('valence.controller.podmanagers.get_podm_status')
@mock.patch('valence.podmanagers.podm_base.PodManagerBase.get_status')
def test_podmanager_create(self, pstatus_mock, plist_mock, pcreate_mock):
pstatus_mock.return_value = constants.PODM_STATUS_ONLINE
plist_mock.return_value = []