From 94380f9ad5e452eb176e7116182e024576db66df Mon Sep 17 00:00:00 2001 From: Feilong Wang Date: Wed, 20 Mar 2019 09:21:16 +1300 Subject: [PATCH] Support resize api Task: 29572 Story: 2005054 Change-Id: Ic9ede21bbf87883d7dbdf9fde02e7dba24440ce7 --- magnumclient/osc/plugin.py | 10 +++--- magnumclient/osc/v1/clusters.py | 43 ++++++++++++++++++++++++++ magnumclient/tests/v1/test_clusters.py | 21 +++++++++++++ magnumclient/v1/clusters.py | 15 +++++++++ setup.cfg | 1 + 5 files changed, 86 insertions(+), 4 deletions(-) diff --git a/magnumclient/osc/plugin.py b/magnumclient/osc/plugin.py index dde56fd7..49309c29 100644 --- a/magnumclient/osc/plugin.py +++ b/magnumclient/osc/plugin.py @@ -17,7 +17,8 @@ from osc_lib import utils LOG = logging.getLogger(__name__) -DEFAULT_API_VERSION = '1' +DEFAULT_MAJOR_API_VERSION = '1' +DEFAULT_MAGNUM_API_VERSION = 'latest' API_VERSION_OPTION = 'os_container_infra_api_version' API_NAME = 'container_infra' API_VERSIONS = { @@ -37,7 +38,8 @@ def make_client(instance): region_name=instance._region_name, interface=instance._interface, insecure=instance._insecure, - ca_cert=instance._cacert) + ca_cert=instance._cacert, + api_version=DEFAULT_MAGNUM_API_VERSION) return client @@ -49,8 +51,8 @@ def build_option_parser(parser): metavar='', default=utils.env( 'OS_CONTAINER_INFRA_API_VERSION', - default=DEFAULT_API_VERSION), + default=DEFAULT_MAJOR_API_VERSION), help='Container-Infra API version, default=' + - DEFAULT_API_VERSION + + DEFAULT_MAJOR_API_VERSION + ' (Env: OS_CONTAINER_INFRA_API_VERSION)') return parser diff --git a/magnumclient/osc/v1/clusters.py b/magnumclient/osc/v1/clusters.py index fa2bdcbc..3a34ab88 100644 --- a/magnumclient/osc/v1/clusters.py +++ b/magnumclient/osc/v1/clusters.py @@ -370,3 +370,46 @@ class ConfigCluster(command.Command): cluster, cluster_template, parsed_args.dir, force=parsed_args.force, certs=tls, use_keystone=parsed_args.use_keystone)) + + +class ResizeCluster(command.Command): + _description = _("Resize a Cluster") + + def get_parser(self, prog_name): + parser = super(ResizeCluster, self).get_parser(prog_name) + parser.add_argument( + 'cluster', + metavar='', + help=_('The name or UUID of cluster to update')) + + parser.add_argument( + 'node_count', + type=int, + help=_("Desired node count of the cluser.")) + + parser.add_argument( + '--nodes-to-remove', + metavar='', + action='append', + help=_("Server ID of the nodes to be removed. Repeat to add" + "more server ID")) + + parser.add_argument( + '--nodegroup', + metavar='', + help=_('The name or UUID of the nodegroup of current cluster.')) + + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + + mag_client = self.app.client_manager.container_infra + cluster = mag_client.clusters.get(parsed_args.cluster) + + mag_client.clusters.resize(cluster.uuid, + parsed_args.node_count, + parsed_args.nodes_to_remove, + parsed_args.nodegroup) + print("Request to resize cluster %s has been accepted." % + parsed_args.cluster) diff --git a/magnumclient/tests/v1/test_clusters.py b/magnumclient/tests/v1/test_clusters.py index dc1a1004..2f01745b 100644 --- a/magnumclient/tests/v1/test_clusters.py +++ b/magnumclient/tests/v1/test_clusters.py @@ -54,6 +54,10 @@ UPDATED_CLUSTER = copy.deepcopy(CLUSTER1) NEW_NAME = 'newcluster' UPDATED_CLUSTER['name'] = NEW_NAME +RESIZED_CLUSTER = copy.deepcopy(CLUSTER1) +RESIZED_NODE_COUNT = 5 +UPDATED_CLUSTER['node_count'] = RESIZED_NODE_COUNT + fake_responses = { '/v1/clusters': { @@ -145,6 +149,13 @@ fake_responses = { {'clusters': [CLUSTER2, CLUSTER1]}, ), }, + '/v1/clusters/%s/actions/resize' % CLUSTER1['uuid']: + { + 'POST': ( + {}, + UPDATED_CLUSTER + ), + } } @@ -355,3 +366,13 @@ class ClusterManagerTest(testtools.TestCase): ] self.assertEqual(expect, self.api.calls) self.assertEqual(NEW_NAME, cluster.name) + + def test_cluster_resize(self): + body = {'node_count': RESIZED_NODE_COUNT} + cluster = self.mgr.resize(CLUSTER1["uuid"], **body) + expect = [ + ('POST', '/v1/clusters/%s/actions/resize' % CLUSTER1['uuid'], + {}, body), + ] + self.assertEqual(expect, self.api.calls) + self.assertEqual(RESIZED_NODE_COUNT, cluster.node_count) diff --git a/magnumclient/v1/clusters.py b/magnumclient/v1/clusters.py index cdb599a5..c0456039 100644 --- a/magnumclient/v1/clusters.py +++ b/magnumclient/v1/clusters.py @@ -32,3 +32,18 @@ class Cluster(baseunit.BaseTemplate): class ClusterManager(baseunit.BaseTemplateManager): resource_class = Cluster template_name = 'clusters' + + def resize(self, cluster_uuid, node_count, + nodes_to_remove=[], nodegroup=None): + url = self._path(cluster_uuid) + "/actions/resize" + + post_body = {"node_count": node_count} + if nodes_to_remove: + post_body.update({"nodes_to_remove": nodes_to_remove}) + if nodegroup: + post_body.update({"nodegroup": nodegroup}) + + resp, resp_body = self.api.json_request("POST", url, body=post_body) + + if resp_body: + return self.resource_class(self, resp_body) diff --git a/setup.cfg b/setup.cfg index c8141804..84ae627c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -43,6 +43,7 @@ openstack.container_infra.v1 = coe_cluster_show = magnumclient.osc.v1.clusters:ShowCluster coe_cluster_update = magnumclient.osc.v1.clusters:UpdateCluster coe_cluster_config = magnumclient.osc.v1.clusters:ConfigCluster + coe_cluster_resize = magnumclient.osc.v1.clusters:ResizeCluster coe_ca_rotate = magnumclient.osc.v1.certificates:RotateCa coe_ca_show = magnumclient.osc.v1.certificates:ShowCa coe_ca_sign = magnumclient.osc.v1.certificates:SignCa