Merge "Add get/create/delete resource provider"
This commit is contained in:
commit
66b1af9058
|
@ -11,14 +11,17 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
import mock
|
||||
|
||||
from blazar import tests
|
||||
from blazar.tests import fake_requests
|
||||
from blazar.utils.openstack import exceptions
|
||||
from blazar.utils.openstack import placement
|
||||
|
||||
from oslo_config import cfg
|
||||
from oslo_config import fixture as conf_fixture
|
||||
from oslo_utils import uuidutils
|
||||
|
||||
CONF = cfg.CONF
|
||||
PLACEMENT_MICROVERSION = 1.29
|
||||
|
@ -91,3 +94,109 @@ class TestPlacementClient(tests.TestCase):
|
|||
'interface': 'public'},
|
||||
headers={'accept': 'application/json'},
|
||||
microversion=PLACEMENT_MICROVERSION, raise_exc=False)
|
||||
|
||||
@mock.patch('keystoneauth1.session.Session.request')
|
||||
def test_get_resource_provider(self, kss_req):
|
||||
rp_name = 'blazar'
|
||||
rp_uuid = uuidutils.generate_uuid()
|
||||
parent_uuid = uuidutils.generate_uuid()
|
||||
|
||||
mock_json_data = {
|
||||
'resource_providers': [
|
||||
{
|
||||
'uuid': rp_uuid,
|
||||
'name': rp_name,
|
||||
'generation': 0,
|
||||
'parent_provider_uuid': parent_uuid
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
kss_req.return_value = fake_requests.FakeResponse(
|
||||
200, content=json.dumps(mock_json_data))
|
||||
|
||||
result = self.client.get_resource_provider(rp_name)
|
||||
|
||||
expected_url = '/resource_providers?name=blazar'
|
||||
kss_req.assert_called_once_with(
|
||||
expected_url, 'GET',
|
||||
endpoint_filter={'service_type': 'placement',
|
||||
'interface': 'public'},
|
||||
headers={'accept': 'application/json'},
|
||||
microversion=PLACEMENT_MICROVERSION, raise_exc=False)
|
||||
expected = {'uuid': rp_uuid,
|
||||
'name': rp_name,
|
||||
'generation': 0,
|
||||
'parent_provider_uuid': parent_uuid}
|
||||
self.assertEqual(expected, result)
|
||||
|
||||
@mock.patch('keystoneauth1.session.Session.request')
|
||||
def test_get_resource_provider_fail(self, kss_req):
|
||||
rp_name = 'blazar'
|
||||
kss_req.return_value = fake_requests.FakeResponse(404)
|
||||
|
||||
self.assertRaises(
|
||||
exceptions.ResourceProviderRetrievalFailed,
|
||||
self.client.get_resource_provider, rp_name)
|
||||
|
||||
@mock.patch('keystoneauth1.session.Session.request')
|
||||
def test_create_resource_provider(self, kss_req):
|
||||
rp_name = 'Blazar'
|
||||
rp_uuid = uuidutils.generate_uuid()
|
||||
parent_uuid = uuidutils.generate_uuid()
|
||||
|
||||
mock_json_data = {'uuid': rp_uuid,
|
||||
'name': rp_name,
|
||||
'generation': 0,
|
||||
'parent_provider_uuid': parent_uuid}
|
||||
|
||||
kss_req.return_value = fake_requests.FakeResponse(
|
||||
200, content=json.dumps(mock_json_data))
|
||||
|
||||
result = self.client.create_resource_provider(
|
||||
rp_name, rp_uuid=rp_uuid, parent_uuid=parent_uuid)
|
||||
|
||||
expected_url = '/resource_providers'
|
||||
kss_req.assert_called_once_with(
|
||||
expected_url, 'POST',
|
||||
json={'uuid': rp_uuid,
|
||||
'name': rp_name,
|
||||
'parent_provider_uuid': parent_uuid},
|
||||
endpoint_filter={'service_type': 'placement',
|
||||
'interface': 'public'},
|
||||
headers={'accept': 'application/json'},
|
||||
microversion=PLACEMENT_MICROVERSION, raise_exc=False)
|
||||
self.assertEqual(mock_json_data, result)
|
||||
|
||||
@mock.patch('keystoneauth1.session.Session.request')
|
||||
def test_create_resource_provider_fail(self, kss_req):
|
||||
rp_name = 'Blazar'
|
||||
kss_req.return_value = fake_requests.FakeResponse(404)
|
||||
|
||||
self.assertRaises(
|
||||
exceptions.ResourceProviderCreationFailed,
|
||||
self.client.create_resource_provider, rp_name)
|
||||
|
||||
@mock.patch('keystoneauth1.session.Session.request')
|
||||
def test_delete_resource_provider(self, kss_req):
|
||||
rp_uuid = uuidutils.generate_uuid()
|
||||
kss_req.return_value = fake_requests.FakeResponse(200)
|
||||
|
||||
self.client.delete_resource_provider(rp_uuid)
|
||||
|
||||
expected_url = '/resource_providers/' + str(rp_uuid)
|
||||
kss_req.assert_called_once_with(
|
||||
expected_url, 'DELETE',
|
||||
endpoint_filter={'service_type': 'placement',
|
||||
'interface': 'public'},
|
||||
headers={'accept': 'application/json'},
|
||||
microversion=PLACEMENT_MICROVERSION, raise_exc=False)
|
||||
|
||||
@mock.patch('keystoneauth1.session.Session.request')
|
||||
def test_delete_resource_provider_fail(self, kss_req):
|
||||
rp_uuid = uuidutils.generate_uuid()
|
||||
kss_req.return_value = fake_requests.FakeResponse(404)
|
||||
|
||||
self.assertRaises(
|
||||
exceptions.ResourceProviderDeletionFailed,
|
||||
self.client.delete_resource_provider, rp_uuid)
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
# 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.
|
||||
|
||||
from blazar import exceptions
|
||||
from blazar.i18n import _
|
||||
|
||||
|
||||
class ResourceProviderRetrievalFailed(exceptions.BlazarException):
|
||||
msg_fmt = _("Failed to get resource provider %(name)s")
|
||||
|
||||
|
||||
class ResourceProviderCreationFailed(exceptions.BlazarException):
|
||||
msg_fmt = _("Failed to create resource provider %(name)s")
|
||||
|
||||
|
||||
class ResourceProviderDeletionFailed(exceptions.BlazarException):
|
||||
msg_fmt = _("Failed to delete resource provider %(uuid)s")
|
|
@ -18,6 +18,8 @@ from keystoneauth1 import session
|
|||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
|
||||
from blazar.utils.openstack import exceptions
|
||||
|
||||
CONF = cfg.CONF
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
@ -91,3 +93,87 @@ class BlazarPlacementClient(object):
|
|||
def delete(self, url, microversion=PLACEMENT_MICROVERSION):
|
||||
return self._client.delete(url, raise_exc=False,
|
||||
microversion=microversion)
|
||||
|
||||
def get_resource_provider(self, rp_name):
|
||||
"""Calls the placement API for a resource provider record.
|
||||
|
||||
:param rp_name: Name of the resource provider
|
||||
:return: A dict of resource provider information.
|
||||
:raise: ResourceProviderRetrievalFailed on error.
|
||||
"""
|
||||
url = "/resource_providers?name=%s" % rp_name
|
||||
resp = self.get(url)
|
||||
if resp:
|
||||
json_resp = resp.json()
|
||||
return json_resp['resource_providers'][0]
|
||||
|
||||
msg = ("Failed to get resource provider %(name)s. "
|
||||
"Got %(status_code)d: %(err_text)s.")
|
||||
args = {
|
||||
'name': rp_name,
|
||||
'status_code': resp.status_code,
|
||||
'err_text': resp.text,
|
||||
}
|
||||
LOG.error(msg, args)
|
||||
raise exceptions.ResourceProviderRetrievalFailed(name=rp_name)
|
||||
|
||||
def create_resource_provider(self, rp_name, rp_uuid=None,
|
||||
parent_uuid=None):
|
||||
"""Calls the placement API to create a new resource provider record.
|
||||
|
||||
:param rp_name: Name of the resource provider
|
||||
:param rp_uuid: Optional UUID of the new resource provider
|
||||
:param parent_uuid: Optional UUID of the parent resource provider
|
||||
:return: A dict of resource provider information object representing
|
||||
the newly-created resource provider.
|
||||
:raise: ResourceProviderCreationFailed error.
|
||||
"""
|
||||
url = "/resource_providers"
|
||||
payload = {'name': rp_name}
|
||||
if rp_uuid is not None:
|
||||
payload['uuid'] = rp_uuid
|
||||
if parent_uuid is not None:
|
||||
payload['parent_provider_uuid'] = parent_uuid
|
||||
|
||||
resp = self.post(url, payload)
|
||||
|
||||
if resp:
|
||||
msg = ("Created resource provider record via placement API for "
|
||||
"resource provider %(name)s.")
|
||||
args = {'name': rp_name}
|
||||
LOG.info(msg, args)
|
||||
return resp.json()
|
||||
|
||||
msg = ("Failed to create resource provider record in placement API "
|
||||
"for resource provider %(name)s. "
|
||||
"Got %(status_code)d: %(err_text)s.")
|
||||
args = {
|
||||
'name': rp_name,
|
||||
'status_code': resp.status_code,
|
||||
'err_text': resp.text,
|
||||
}
|
||||
LOG.error(msg, args)
|
||||
raise exceptions.ResourceProviderCreationFailed(name=rp_name)
|
||||
|
||||
def delete_resource_provider(self, rp_uuid):
|
||||
"""Calls the placement API to delete a resource provider.
|
||||
|
||||
:param rp_uuid: UUID of the resource provider to delete
|
||||
:raise: ResourceProviderDeletionFailed error
|
||||
"""
|
||||
url = '/resource_providers/%s' % rp_uuid
|
||||
resp = self.delete(url)
|
||||
|
||||
if resp:
|
||||
LOG.info("Deleted resource provider %s", rp_uuid)
|
||||
return
|
||||
|
||||
msg = ("Failed to delete resource provider with UUID %(uuid)s from "
|
||||
"the placement API. Got %(status_code)d: %(err_text)s.")
|
||||
args = {
|
||||
'uuid': rp_uuid,
|
||||
'status_code': resp.status_code,
|
||||
'err_text': resp.text
|
||||
}
|
||||
LOG.error(msg, args)
|
||||
raise exceptions.ResourceProviderDeletionFailed(uuid=rp_uuid)
|
||||
|
|
Loading…
Reference in New Issue