diff --git a/fuelclient/fuelclient/cli/actions/environment.py b/fuelclient/fuelclient/cli/actions/environment.py index 6bc1c7c376..9c00524c5c 100644 --- a/fuelclient/fuelclient/cli/actions/environment.py +++ b/fuelclient/fuelclient/cli/actions/environment.py @@ -14,7 +14,6 @@ from fuelclient.cli.actions.base import Action from fuelclient.cli.actions.base import check_all -from fuelclient.cli.actions.base import check_any import fuelclient.cli.arguments as Args from fuelclient.cli.arguments import group from fuelclient.cli.formatting import format_table @@ -43,6 +42,9 @@ class EnvironmentAction(Action): Args.get_create_arg( "Create a new environment with " "specific release id and name." + ), + Args.get_update_arg( + "Update OS to specified release id for given env." ) ), Args.get_release_arg( @@ -65,6 +67,7 @@ class EnvironmentAction(Action): ("create", self.create), ("set", self.set), ("delete", self.delete), + ("update", self.update), (None, self.list) ) @@ -84,8 +87,9 @@ class EnvironmentAction(Action): params.net, net_segment_type=params.nst ) + if params.mode: - data = env.set(mode=params.mode) + data = env.set({'mode': params.mode}) else: data = env.get_fresh_data() @@ -97,24 +101,35 @@ class EnvironmentAction(Action): ) @check_all("env") - @check_any("name", "mode") def set(self, params): """For changing environments name, mode or network mode exists set action: fuel --env 1 env set --name NewEmvName --mode ha_compact """ + acceptable_params = ('mode', 'name', 'pending_release_id') + env = Environment(params.env, params=params) - data = env.set(name=params.name, mode=params.mode) - msg_templates = [] - if params.name: - msg_templates.append( - "Environment with id={id} was renamed to '{name}'.") - if params.mode: - msg_templates.append( - "Mode of environment with id={id} was set to '{mode}'.") + + # forming message for output and data structure for request body + # TODO(aroma): make it less ugly + msg_template = ("Following attributes are changed for " + "the environment: {env_attributes}") + + env_attributes = [] + update_kwargs = dict() + for param_name in acceptable_params: + attr_value = getattr(params, param_name, None) + if attr_value: + update_kwargs[param_name] = attr_value + env_attributes.append( + ''.join([param_name, '=', str(attr_value)]) + ) + + data = env.set(update_kwargs) + env_attributes = ', '.join(env_attributes) self.serializer.print_to_output( data, - "\n".join(msg_templates).format(**data) + msg_template.format(env_attributes=env_attributes) ) @check_all("env") @@ -135,7 +150,7 @@ class EnvironmentAction(Action): fuel env """ acceptable_keys = ("id", "status", "name", "mode", - "release_id", "changes") + "release_id", "changes", "pending_release_id") data = Environment.get_all_data() if params.env: data = filter( @@ -149,3 +164,21 @@ class EnvironmentAction(Action): acceptable_keys=acceptable_keys ) ) + + def update(self, params): + """Update environment to given OS release + fuel env --env 1 --update --release 1 + """ + params.pending_release_id = params.release + self.set(params) + + env = Environment(params.env, params=params) + update_task = env.update_env() + + msg = ("Update process for environment has been started. " + "Update task id is {0}".format(update_task.id)) + + self.serializer.print_to_output( + {}, + msg + ) diff --git a/fuelclient/fuelclient/cli/arguments.py b/fuelclient/fuelclient/cli/arguments.py index f132292053..ff1cf856ed 100644 --- a/fuelclient/fuelclient/cli/arguments.py +++ b/fuelclient/fuelclient/cli/arguments.py @@ -35,7 +35,7 @@ substitutions = { "create": "--create", "remove": "--delete", "config": "--config", - "--roles": "--role" + "--roles": "--role", } @@ -289,6 +289,11 @@ def get_list_arg(help_msg): return get_boolean_arg("list", flags=("-l",), help=help_msg) +def get_update_arg(help_msg): + return get_boolean_arg("update", + flags=("--env-update",), help=help_msg) + + def get_dir_arg(help_msg): return get_str_arg("dir", default=os.curdir, help=help_msg) diff --git a/fuelclient/fuelclient/objects/environment.py b/fuelclient/fuelclient/objects/environment.py index 13f59b8e4f..0d3905c9da 100644 --- a/fuelclient/fuelclient/objects/environment.py +++ b/fuelclient/fuelclient/objects/environment.py @@ -22,6 +22,7 @@ from fuelclient.cli.error import ServerDataException from fuelclient.cli.serializers import listdir_without_extensions from fuelclient.objects.base import BaseObject from fuelclient.objects.task import DeployTask +from fuelclient.objects.task import Task class Environment(BaseObject): @@ -49,18 +50,24 @@ class Environment(BaseObject): data = cls.connection.post_request("clusters/", data) return cls.init_with_data(data) - def set(self, name=None, mode=None): - data = {} - if mode: + def set(self, data): + if data.get('mode'): data["mode"] = "ha_compact" \ - if mode.lower() == "ha" else "multinode" - if name: - data["name"] = name + if data['mode'].lower() == "ha" else "multinode" + return self.connection.put_request( "clusters/{0}/".format(self.id), data ) + def update_env(self): + return Task.init_with_data( + self.connection.put_request( + "clusters/{0}/update/".format(self.id), + {} + ) + ) + def delete(self): return self.connection.delete_request( "clusters/{0}/".format(self.id) @@ -322,7 +329,6 @@ class Environment(BaseObject): ) def stop(self): - from fuelclient.objects.task import Task return Task.init_with_data( self.connection.put_request( "clusters/{0}/stop_deployment/".format(self.id), @@ -331,7 +337,6 @@ class Environment(BaseObject): ) def reset(self): - from fuelclient.objects.task import Task return Task.init_with_data( self.connection.put_request( "clusters/{0}/reset/".format(self.id), @@ -346,7 +351,6 @@ class Environment(BaseObject): ','.join(map(lambda n: str(n.id), nodes))) def install_selected_nodes(self, method_type, nodes): - from fuelclient.objects.task import Task return Task.init_with_data( self.connection.put_request( self._get_method_url(method_type, nodes), diff --git a/fuelclient/tests/test_client.py b/fuelclient/tests/test_client.py index d46e13bfbc..305acfd41b 100644 --- a/fuelclient/tests/test_client.py +++ b/fuelclient/tests/test_client.py @@ -25,12 +25,12 @@ class TestHandlers(BaseTestCase): def test_env_action(self): #check env help help_msgs = ["usage: fuel environment [-h]", - "[--list | --set | --delete | --create]", + "[--list | --set | --delete | --create | --update]", "optional arguments:", "--help", "--list", "--set", "--delete", "--rel", "--env-create", "--create", "--name", "--env-name", "--mode", "--net", "--network-mode", "--nst", "--net-segment-type", - "--deployment-mode"] + "--deployment-mode", "--update", "--env-update"] self.check_all_in_msg("env --help", help_msgs) #no clusters self.check_for_rows_in_table("env") @@ -41,15 +41,17 @@ class TestHandlers(BaseTestCase): #list of tuples (, ) expected_stdout = \ [( - "env create --name=TestEnv --release=1", + "env --create --name=TestEnv --release=1", "Environment 'TestEnv' with id=1, mode=ha_compact and " "network-mode=nova_network was created!\n" ), ( "--env-id=1 env set --name=NewEnv", - "Environment with id=1 was renamed to 'NewEnv'.\n" + ("Following attributes are changed for " + "the environment: name=NewEnv\n") ), ( "--env-id=1 env set --mode=multinode", - "Mode of environment with id=1 was set to 'multinode'.\n" + ("Following attributes are changed for " + "the environment: mode=multinode\n") )] for cmd, msg in expected_stdout: