Convert cloud layer to use COE proxy layer
We only have the Cluster resource modelled thus far, so that is all that's converted. Change-Id: I7ea63b2cdda881c621cbb0e212479328a96e73bd Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:

committed by
Artem Goncharov

parent
1b2a360d1c
commit
289e5c2d3c
@@ -30,20 +30,16 @@ class CoeCloudMixin:
|
||||
|
||||
@_utils.cache_on_arguments()
|
||||
def list_coe_clusters(self):
|
||||
"""List COE(Ccontainer Orchestration Engine) cluster.
|
||||
|
||||
:returns: a list of dicts containing the cluster.
|
||||
"""List COE (Container Orchestration Engine) cluster.
|
||||
|
||||
:returns: A list of container infrastructure management ``Cluster``
|
||||
objects.
|
||||
:raises: ``OpenStackCloudException``: if something goes wrong during
|
||||
the OpenStack API call.
|
||||
"""
|
||||
with _utils.shade_exceptions("Error fetching cluster list"):
|
||||
data = self._container_infra_client.get('/clusters')
|
||||
return self._normalize_coe_clusters(
|
||||
self._get_and_munchify('clusters', data))
|
||||
return list(self.container_infrastructure_management.clusters())
|
||||
|
||||
def search_coe_clusters(
|
||||
self, name_or_id=None, filters=None):
|
||||
def search_coe_clusters(self, name_or_id=None, filters=None):
|
||||
"""Search COE cluster.
|
||||
|
||||
:param name_or_id: cluster name or ID.
|
||||
@@ -51,14 +47,13 @@ class CoeCloudMixin:
|
||||
:param detail: a boolean to control if we need summarized or
|
||||
detailed output.
|
||||
|
||||
:returns: a list of dict containing the cluster
|
||||
|
||||
:returns: A list of container infrastructure management ``Cluster``
|
||||
objects.
|
||||
:raises: ``OpenStackCloudException``: if something goes wrong during
|
||||
the OpenStack API call.
|
||||
"""
|
||||
coe_clusters = self.list_coe_clusters()
|
||||
return _utils._filter_list(
|
||||
coe_clusters, name_or_id, filters)
|
||||
return _utils._filter_list(coe_clusters, name_or_id, filters)
|
||||
|
||||
def get_coe_cluster(self, name_or_id, filters=None):
|
||||
"""Get a COE cluster by name or ID.
|
||||
@@ -79,36 +74,34 @@ class CoeCloudMixin:
|
||||
A string containing a jmespath expression for further filtering.
|
||||
Example:: "[?last_name==`Smith`] | [?other.gender]==`Female`]"
|
||||
|
||||
:returns: A cluster dict or None if no matching cluster is found.
|
||||
:returns: A container infrastructure management ``Cluster`` object if
|
||||
found, else None.
|
||||
"""
|
||||
return _utils._get_entity(self, 'coe_cluster', name_or_id,
|
||||
filters=filters)
|
||||
return _utils._get_entity(self, 'coe_cluster', name_or_id, filters)
|
||||
|
||||
def create_coe_cluster(
|
||||
self, name, cluster_template_id, **kwargs):
|
||||
self, name, cluster_template_id, **kwargs,
|
||||
):
|
||||
"""Create a COE cluster based on given cluster template.
|
||||
|
||||
:param string name: Name of the cluster.
|
||||
:param string image_id: ID of the cluster template to use.
|
||||
Other arguments will be passed in kwargs.
|
||||
:param string cluster_template_id: ID of the cluster template to use.
|
||||
:param dict kwargs: Any other arguments to pass in.
|
||||
|
||||
:returns: a dict containing the cluster description
|
||||
|
||||
:returns: The created container infrastructure management ``Cluster``
|
||||
object.
|
||||
:raises: ``OpenStackCloudException`` if something goes wrong during
|
||||
the OpenStack API call
|
||||
"""
|
||||
error_message = ("Error creating cluster of name"
|
||||
" {cluster_name}".format(cluster_name=name))
|
||||
with _utils.shade_exceptions(error_message):
|
||||
body = kwargs.copy()
|
||||
body['name'] = name
|
||||
body['cluster_template_id'] = cluster_template_id
|
||||
|
||||
cluster = self._container_infra_client.post(
|
||||
'/clusters', json=body)
|
||||
cluster = self.container_infrastructure_management.create_cluster(
|
||||
name=name,
|
||||
cluster_template_id=cluster_template_id,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
self.list_coe_clusters.invalidate(self)
|
||||
return self._normalize_coe_cluster(cluster)
|
||||
return cluster
|
||||
|
||||
def delete_coe_cluster(self, name_or_id):
|
||||
"""Delete a COE cluster.
|
||||
@@ -126,25 +119,21 @@ class CoeCloudMixin:
|
||||
self.log.debug(
|
||||
"COE Cluster %(name_or_id)s does not exist",
|
||||
{'name_or_id': name_or_id},
|
||||
exc_info=True)
|
||||
exc_info=True,
|
||||
)
|
||||
return False
|
||||
|
||||
with _utils.shade_exceptions("Error in deleting COE cluster"):
|
||||
self._container_infra_client.delete(
|
||||
'/clusters/{id}'.format(id=cluster['id']))
|
||||
self.list_coe_clusters.invalidate(self)
|
||||
|
||||
self.container_infrastructure_management.delete_cluster(cluster)
|
||||
self.list_coe_clusters.invalidate(self)
|
||||
return True
|
||||
|
||||
@_utils.valid_kwargs('node_count')
|
||||
def update_coe_cluster(self, name_or_id, operation, **kwargs):
|
||||
def update_coe_cluster(self, name_or_id, **kwargs):
|
||||
"""Update a COE cluster.
|
||||
|
||||
:param name_or_id: Name or ID of the COE cluster being updated.
|
||||
:param operation: Operation to perform - add, remove, replace.
|
||||
Other arguments will be passed with kwargs.
|
||||
:param kwargs: Cluster attributes to be updated.
|
||||
|
||||
:returns: a dict representing the updated cluster.
|
||||
:returns: The updated cluster ``Cluster`` object.
|
||||
|
||||
:raises: OpenStackCloudException on operation error.
|
||||
"""
|
||||
@@ -154,23 +143,12 @@ class CoeCloudMixin:
|
||||
raise exc.OpenStackCloudException(
|
||||
"COE cluster %s not found." % name_or_id)
|
||||
|
||||
if operation not in ['add', 'replace', 'remove']:
|
||||
raise TypeError(
|
||||
"%s operation not in 'add', 'replace', 'remove'" % operation)
|
||||
cluster = self.container_infrastructure_management.update_cluster(
|
||||
cluster,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
patches = _utils.generate_patches_from_kwargs(operation, **kwargs)
|
||||
# No need to fire an API call if there is an empty patch
|
||||
if not patches:
|
||||
return cluster
|
||||
|
||||
with _utils.shade_exceptions(
|
||||
"Error updating COE cluster {0}".format(name_or_id)):
|
||||
self._container_infra_client.patch(
|
||||
'/clusters/{id}'.format(id=cluster['id']),
|
||||
json=patches)
|
||||
|
||||
new_cluster = self.get_coe_cluster(name_or_id)
|
||||
return new_cluster
|
||||
return cluster
|
||||
|
||||
def get_coe_cluster_certificate(self, cluster_id):
|
||||
"""Get details about the CA certificate for a cluster by name or ID.
|
||||
|
@@ -28,6 +28,9 @@ class Cluster(resource.Resource):
|
||||
allow_list = True
|
||||
allow_patch = True
|
||||
|
||||
commit_method = 'PATCH'
|
||||
commit_jsonpatch = True
|
||||
|
||||
#: The endpoint URL of COE API exposed to end-users.
|
||||
api_address = resource.Body('api_address')
|
||||
#: The UUID of the cluster template.
|
||||
|
@@ -33,7 +33,7 @@ class TestMagnumServices(base.BaseFunctionalTest):
|
||||
'''Test magnum services functionality'''
|
||||
|
||||
# Test that we can list services
|
||||
services = self.user_cloud.list_magnum_services()
|
||||
services = self.operator_cloud.list_magnum_services()
|
||||
|
||||
self.assertEqual(1, len(services))
|
||||
self.assertEqual(services[0]['id'], 1)
|
||||
|
@@ -11,11 +11,11 @@
|
||||
# under the License.
|
||||
|
||||
|
||||
import munch
|
||||
|
||||
from openstack.container_infrastructure_management.v1 import cluster
|
||||
from openstack.tests.unit import base
|
||||
|
||||
coe_cluster_obj = munch.Munch(
|
||||
|
||||
coe_cluster_obj = dict(
|
||||
status="CREATE_IN_PROGRESS",
|
||||
cluster_template_id="0562d357-8641-4759-8fed-8173f02c9633",
|
||||
uuid="731387cf-a92b-4c36-981e-3271d63e5597",
|
||||
@@ -38,130 +38,181 @@ coe_cluster_obj = munch.Munch(
|
||||
|
||||
|
||||
class TestCOEClusters(base.TestCase):
|
||||
def _compare_clusters(self, exp, real):
|
||||
self.assertDictEqual(
|
||||
cluster.Cluster(**exp).to_dict(computed=False),
|
||||
real.to_dict(computed=False),
|
||||
)
|
||||
|
||||
def get_mock_url(
|
||||
self,
|
||||
service_type='container-infrastructure-management',
|
||||
base_url_append=None, append=None, resource=None):
|
||||
self,
|
||||
service_type="container-infrastructure-management",
|
||||
base_url_append=None,
|
||||
append=None,
|
||||
resource=None,
|
||||
):
|
||||
return super(TestCOEClusters, self).get_mock_url(
|
||||
service_type=service_type, resource=resource,
|
||||
append=append, base_url_append=base_url_append)
|
||||
service_type=service_type,
|
||||
resource=resource,
|
||||
append=append,
|
||||
base_url_append=base_url_append,
|
||||
)
|
||||
|
||||
def test_list_coe_clusters(self):
|
||||
self.register_uris([dict(
|
||||
method='GET',
|
||||
uri=self.get_mock_url(resource='clusters'),
|
||||
json=dict(clusters=[coe_cluster_obj.toDict()]))])
|
||||
self.register_uris(
|
||||
[
|
||||
dict(
|
||||
method="GET",
|
||||
uri=self.get_mock_url(resource="clusters"),
|
||||
json=dict(clusters=[coe_cluster_obj]),
|
||||
)
|
||||
]
|
||||
)
|
||||
cluster_list = self.cloud.list_coe_clusters()
|
||||
self.assertEqual(
|
||||
self._compare_clusters(
|
||||
coe_cluster_obj,
|
||||
cluster_list[0],
|
||||
self.cloud._normalize_coe_cluster(coe_cluster_obj))
|
||||
)
|
||||
self.assert_calls()
|
||||
|
||||
def test_create_coe_cluster(self):
|
||||
json_response = dict(uuid=coe_cluster_obj.get('uuid'))
|
||||
kwargs = dict(name=coe_cluster_obj.name,
|
||||
cluster_template_id=coe_cluster_obj.cluster_template_id,
|
||||
master_count=coe_cluster_obj.master_count,
|
||||
node_count=coe_cluster_obj.node_count)
|
||||
self.register_uris([dict(
|
||||
method='POST',
|
||||
uri=self.get_mock_url(resource='clusters'),
|
||||
json=json_response,
|
||||
validate=dict(json=kwargs)),
|
||||
])
|
||||
expected = self.cloud._normalize_coe_cluster(json_response)
|
||||
json_response = dict(uuid=coe_cluster_obj.get("uuid"))
|
||||
kwargs = dict(
|
||||
name=coe_cluster_obj["name"],
|
||||
cluster_template_id=coe_cluster_obj["cluster_template_id"],
|
||||
master_count=coe_cluster_obj["master_count"],
|
||||
node_count=coe_cluster_obj["node_count"],
|
||||
)
|
||||
self.register_uris(
|
||||
[
|
||||
dict(
|
||||
method="POST",
|
||||
uri=self.get_mock_url(resource="clusters"),
|
||||
json=json_response,
|
||||
validate=dict(json=kwargs),
|
||||
),
|
||||
]
|
||||
)
|
||||
response = self.cloud.create_coe_cluster(**kwargs)
|
||||
self.assertEqual(response, expected)
|
||||
expected = kwargs.copy()
|
||||
expected.update(**json_response)
|
||||
self._compare_clusters(expected, response)
|
||||
self.assert_calls()
|
||||
|
||||
def test_search_coe_cluster_by_name(self):
|
||||
self.register_uris([dict(
|
||||
method='GET',
|
||||
uri=self.get_mock_url(resource='clusters'),
|
||||
json=dict(clusters=[coe_cluster_obj.toDict()]))])
|
||||
self.register_uris(
|
||||
[
|
||||
dict(
|
||||
method="GET",
|
||||
uri=self.get_mock_url(resource="clusters"),
|
||||
json=dict(clusters=[coe_cluster_obj]),
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
coe_clusters = self.cloud.search_coe_clusters(
|
||||
name_or_id='k8s')
|
||||
coe_clusters = self.cloud.search_coe_clusters(name_or_id="k8s")
|
||||
|
||||
self.assertEqual(1, len(coe_clusters))
|
||||
self.assertEqual(coe_cluster_obj.uuid, coe_clusters[0]['id'])
|
||||
self.assertEqual(coe_cluster_obj["uuid"], coe_clusters[0]["id"])
|
||||
self.assert_calls()
|
||||
|
||||
def test_search_coe_cluster_not_found(self):
|
||||
self.register_uris([dict(
|
||||
method='GET',
|
||||
uri=self.get_mock_url(resource='clusters'),
|
||||
json=dict(clusters=[coe_cluster_obj.toDict()]))])
|
||||
self.register_uris(
|
||||
[
|
||||
dict(
|
||||
method="GET",
|
||||
uri=self.get_mock_url(resource="clusters"),
|
||||
json=dict(clusters=[coe_cluster_obj]),
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
coe_clusters = self.cloud.search_coe_clusters(
|
||||
name_or_id='non-existent')
|
||||
name_or_id="non-existent"
|
||||
)
|
||||
|
||||
self.assertEqual(0, len(coe_clusters))
|
||||
self.assert_calls()
|
||||
|
||||
def test_get_coe_cluster(self):
|
||||
self.register_uris([dict(
|
||||
method='GET',
|
||||
uri=self.get_mock_url(resource='clusters'),
|
||||
json=dict(clusters=[coe_cluster_obj.toDict()]))])
|
||||
self.register_uris(
|
||||
[
|
||||
dict(
|
||||
method="GET",
|
||||
uri=self.get_mock_url(resource="clusters"),
|
||||
json=dict(clusters=[coe_cluster_obj]),
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
r = self.cloud.get_coe_cluster(coe_cluster_obj.name)
|
||||
r = self.cloud.get_coe_cluster(coe_cluster_obj["name"])
|
||||
self.assertIsNotNone(r)
|
||||
self.assertDictEqual(
|
||||
r, self.cloud._normalize_coe_cluster(coe_cluster_obj))
|
||||
self._compare_clusters(
|
||||
coe_cluster_obj,
|
||||
r,
|
||||
)
|
||||
self.assert_calls()
|
||||
|
||||
def test_get_coe_cluster_not_found(self):
|
||||
self.register_uris([dict(
|
||||
method='GET',
|
||||
uri=self.get_mock_url(resource='clusters'),
|
||||
json=dict(clusters=[]))])
|
||||
r = self.cloud.get_coe_cluster('doesNotExist')
|
||||
self.register_uris(
|
||||
[
|
||||
dict(
|
||||
method="GET",
|
||||
uri=self.get_mock_url(resource="clusters"),
|
||||
json=dict(clusters=[]),
|
||||
)
|
||||
]
|
||||
)
|
||||
r = self.cloud.get_coe_cluster("doesNotExist")
|
||||
self.assertIsNone(r)
|
||||
self.assert_calls()
|
||||
|
||||
def test_delete_coe_cluster(self):
|
||||
self.register_uris([
|
||||
dict(
|
||||
method='GET',
|
||||
uri=self.get_mock_url(resource='clusters'),
|
||||
json=dict(clusters=[coe_cluster_obj.toDict()])),
|
||||
dict(
|
||||
method='DELETE',
|
||||
uri=self.get_mock_url(
|
||||
resource='clusters',
|
||||
append=[coe_cluster_obj.uuid])),
|
||||
])
|
||||
self.cloud.delete_coe_cluster(coe_cluster_obj.uuid)
|
||||
self.register_uris(
|
||||
[
|
||||
dict(
|
||||
method="GET",
|
||||
uri=self.get_mock_url(resource="clusters"),
|
||||
json=dict(clusters=[coe_cluster_obj]),
|
||||
),
|
||||
dict(
|
||||
method="DELETE",
|
||||
uri=self.get_mock_url(
|
||||
resource="clusters", append=[coe_cluster_obj['uuid']]
|
||||
),
|
||||
),
|
||||
]
|
||||
)
|
||||
self.cloud.delete_coe_cluster(coe_cluster_obj["uuid"])
|
||||
self.assert_calls()
|
||||
|
||||
def test_update_coe_cluster(self):
|
||||
self.register_uris([
|
||||
dict(
|
||||
method='GET',
|
||||
uri=self.get_mock_url(resource='clusters'),
|
||||
json=dict(clusters=[coe_cluster_obj.toDict()])),
|
||||
dict(
|
||||
method='PATCH',
|
||||
uri=self.get_mock_url(
|
||||
resource='clusters',
|
||||
append=[coe_cluster_obj.uuid]),
|
||||
status_code=200,
|
||||
validate=dict(
|
||||
json=[{
|
||||
u'op': u'replace',
|
||||
u'path': u'/node_count',
|
||||
u'value': 3
|
||||
}]
|
||||
)),
|
||||
dict(
|
||||
method='GET',
|
||||
uri=self.get_mock_url(resource='clusters'),
|
||||
# This json value is not meaningful to the test - it just has
|
||||
# to be valid.
|
||||
json=dict(clusters=[coe_cluster_obj.toDict()])),
|
||||
])
|
||||
self.register_uris(
|
||||
[
|
||||
dict(
|
||||
method="GET",
|
||||
uri=self.get_mock_url(resource="clusters"),
|
||||
json=dict(clusters=[coe_cluster_obj]),
|
||||
),
|
||||
dict(
|
||||
method="PATCH",
|
||||
uri=self.get_mock_url(
|
||||
resource="clusters", append=[coe_cluster_obj["uuid"]]
|
||||
),
|
||||
status_code=200,
|
||||
validate=dict(
|
||||
json=[
|
||||
{
|
||||
"op": "replace",
|
||||
"path": "/node_count",
|
||||
"value": 3,
|
||||
}
|
||||
]
|
||||
),
|
||||
),
|
||||
]
|
||||
)
|
||||
self.cloud.update_coe_cluster(
|
||||
coe_cluster_obj.uuid, 'replace', node_count=3)
|
||||
coe_cluster_obj["uuid"], node_count=3
|
||||
)
|
||||
self.assert_calls()
|
||||
|
@@ -0,0 +1,51 @@
|
||||
# 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 openstack.container_infrastructure_management.v1 import _proxy
|
||||
from openstack.container_infrastructure_management.v1 import cluster
|
||||
from openstack.tests.unit import test_proxy_base
|
||||
|
||||
|
||||
class TestClusterProxy(test_proxy_base.TestProxyBase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.proxy = _proxy.Proxy(self.session)
|
||||
|
||||
|
||||
class TestCluster(TestClusterProxy):
|
||||
def test_cluster_get(self):
|
||||
self.verify_get(self.proxy.get_cluster, cluster.Cluster)
|
||||
|
||||
def test_cluster_find(self):
|
||||
self.verify_find(
|
||||
self.proxy.find_cluster,
|
||||
cluster.Cluster,
|
||||
method_kwargs={},
|
||||
expected_kwargs={},
|
||||
)
|
||||
|
||||
def test_clusters(self):
|
||||
self.verify_list(
|
||||
self.proxy.clusters,
|
||||
cluster.Cluster,
|
||||
method_kwargs={"query": 1},
|
||||
expected_kwargs={"query": 1},
|
||||
)
|
||||
|
||||
def test_cluster_create_attrs(self):
|
||||
self.verify_create(self.proxy.create_cluster, cluster.Cluster)
|
||||
|
||||
def test_cluster_delete(self):
|
||||
self.verify_delete(self.proxy.delete_cluster, cluster.Cluster, False)
|
||||
|
||||
def test_cluster_delete_ignore(self):
|
||||
self.verify_delete(self.proxy.delete_cluster, cluster.Cluster, True)
|
Reference in New Issue
Block a user