From 2217caed37a94c8b85df0f262684cce632a9c11e Mon Sep 17 00:00:00 2001 From: tengqm Date: Fri, 6 Feb 2015 20:29:36 +0800 Subject: [PATCH] Support to cluster-node-add operation --- senlinclient/v1/client.py | 26 +++++++++----------------- senlinclient/v1/models.py | 24 ++++++++++++++++++------ senlinclient/v1/shell.py | 31 +++++++++++++++---------------- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/senlinclient/v1/client.py b/senlinclient/v1/client.py index 2f49ab0..ca867f4 100644 --- a/senlinclient/v1/client.py +++ b/senlinclient/v1/client.py @@ -106,19 +106,10 @@ class Client(object): return obj def action(self, cls, options): - '''Examples: - --data '{"alarm_id": "33109eea-24dd-45ff-93f7-82292d1dd38c", - "action": "change_state", - "action_args": {"next_state": "insufficient data"}' - - --data '{"id": "a1369557-748f-429c-bd3e-fc385aacaec7", - "action": "reboot", - "action_args": {"reboot_type": "SOFT"}}' - ''' def filter_args(method, params): expected_args = inspect.getargspec(method).args accepted_args = ([a for a in expected_args if a != 'self']) - filtered_args = [{d: params[d]} for d in accepted_args] + filtered_args = dict((d, params[d]) for d in accepted_args) return filtered_args def invoke_method(target, method_name, params): @@ -127,14 +118,15 @@ class Client(object): reply = action(**filtered_args) return reply - kwargs = self.get_options(options) - action = kwargs.pop('action') - if 'action_args' in kwargs: - args = kwargs.pop('action_args') + action = options.pop('action') + if 'action_args' in options: + args = options.pop('action_args') else: args = {} args.update(session=self.session) - obj = cls.new(**kwargs) - reply = invoke_method(obj, action, args) - return reply + obj = cls.new(**options) + try: + return invoke_method(obj, action, args) + except Exception as ex: + client_exc.parse_exception(ex) diff --git a/senlinclient/v1/models.py b/senlinclient/v1/models.py index 8dac1b2..5dbe587 100644 --- a/senlinclient/v1/models.py +++ b/senlinclient/v1/models.py @@ -10,6 +10,8 @@ # License for the specific language governing permissions and limitations # under the License. +from openstack import utils + from senlinclient.common import sdk as resource from senlinclient.openstack.clustering import clustering_service @@ -208,7 +210,20 @@ class Cluster(resource.Resource): nodes = resource.prop('nodes') profile_name = resource.prop('profile_name') - action = resource.prop('action') + # action = resource.prop('action') + + def action(self, session, body): + url = utils.urljoin(self.base_path, self.id, 'action') + resp = session.put(url, service=self.service, json=body).body + return resp + + def add_nodes(self, session, nodes): + body = { + 'add_nodes': { + 'nodes': nodes, + } + } + return self.action(session, body) def to_dict(self): info = { @@ -256,7 +271,7 @@ class ClusterPolicy(resource.Resource): class ClusterNode(resource.Resource): - resources_key = 'policies' + resources_key = 'nodes' base_path = '/clusters/%(cluster_id)s/nodes' service = clustering_service.ClusteringService() @@ -268,10 +283,7 @@ class ClusterNode(resource.Resource): # Properties id = resource.prop('id') cluster_id = resource.prop('cluster_id') - policy_id = resource.prop('policy_id') - cooldown = resource.prop('cooldown') - level = resource.prop('level', type=int) - enabled = resource.prop('enabled') + policy_id = resource.prop('node_id') class Node(resource.Resource): diff --git a/senlinclient/v1/shell.py b/senlinclient/v1/shell.py index d9658c9..dd793f9 100644 --- a/senlinclient/v1/shell.py +++ b/senlinclient/v1/shell.py @@ -13,6 +13,7 @@ import logging from oslo_serialization import jsonutils +from oslo_utils import encodeutils from senlinclient.common import exc from senlinclient.common.i18n import _ @@ -430,25 +431,23 @@ def do_cluster_node_list(sc, args): utils.print_list(nodes, fields, formatters=formatters, sortby_index=5) -@utils.arg('-n', '--nodes', metavar='', - help=_('ID of nodes to be added.')) -@utils.arg('id', metavar='', +@utils.arg('-n', '--nodes', metavar='', + help=_('ID of nodes to be added; multiple nodes can be separated ' + 'with ","')) +@utils.arg('id', metavar='', help=_('Name or ID of cluster to operate on.')) def do_cluster_node_add(sc, args): '''Add specified nodes to cluster.''' - failure_count = 0 - for nid in args.nodes: - try: - params = {'cluster_id': args.id, 'id': nid} - sc.create(models.ClusterNode, params) - except Exception as ex: - failure_count += 1 - print(ex) - if failure_count == len(args.nodes): - msg = _('Failed to add any of the specified nodes.') - raise exc.CommandError(msg) - - do_cluster_node_list(sc, id=args.id) + node_ids = args.nodes.split(',') + params = { + 'id': args.id, + 'action': 'add_nodes', + 'action_args': { + 'nodes': node_ids, + } + } + resp = sc.action(models.Cluster, params) + print(encodeutils.safe_decode(resp)) @utils.arg('-n', '--nodes', metavar='',