From 39f7a8bbbbca47100cefb868bcc8369876a39a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Wed, 17 May 2017 13:47:35 -0400 Subject: [PATCH] Add command to create an instance --- almanachclient/commands/create_instance.py | 43 +++++++++++++++++ almanachclient/commands/delete_instance.py | 4 +- almanachclient/shell.py | 2 + .../tests/commands/test_create_instance.py | 48 +++++++++++++++++++ .../tests/commands/test_delete_instance.py | 6 +++ almanachclient/tests/v1/test_client.py | 23 +++++++++ almanachclient/v1/client.py | 14 ++++++ doc/source/usage.rst | 30 ++++++++++++ 8 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 almanachclient/commands/create_instance.py create mode 100644 almanachclient/tests/commands/test_create_instance.py diff --git a/almanachclient/commands/create_instance.py b/almanachclient/commands/create_instance.py new file mode 100644 index 0000000..e308aca --- /dev/null +++ b/almanachclient/commands/create_instance.py @@ -0,0 +1,43 @@ +# Copyright 2017 INAP +# +# 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 dateutil import parser as date_parser +import json + +from cliff.command import Command + + +class CreateInstanceCommand(Command): + """Create instance""" + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + parser.add_argument('tenant_id', help='Tenant ID') + parser.add_argument('instance_id', help='Instance ID') + parser.add_argument('name', help='Instance name') + parser.add_argument('flavor', help='Flavor') + parser.add_argument('start', help='Start date') + parser.add_argument('--image-meta', help='Image metadata') + return parser + + def take_action(self, parsed_args): + image_meta = json.loads(parsed_args.image_meta) if parsed_args.image_meta else None + + self.app.get_client().create_instance(parsed_args.tenant_id, + parsed_args.instance_id, + parsed_args.name, + parsed_args.flavor, + date_parser.parse(parsed_args.start), + image_meta) + return 'Success' diff --git a/almanachclient/commands/delete_instance.py b/almanachclient/commands/delete_instance.py index 2817461..6e1bb3f 100644 --- a/almanachclient/commands/delete_instance.py +++ b/almanachclient/commands/delete_instance.py @@ -13,6 +13,7 @@ # limitations under the License. from cliff.command import Command +from dateutil import parser as date_parser class DeleteInstanceCommand(Command): @@ -25,5 +26,6 @@ class DeleteInstanceCommand(Command): return parser def take_action(self, parsed_args): - self.app.get_client().delete_instance(parsed_args.instance_id, parsed_args.end) + self.app.get_client().delete_instance(parsed_args.instance_id, + date_parser.parse(parsed_args.end) if parsed_args.end else None) return 'Success' diff --git a/almanachclient/shell.py b/almanachclient/shell.py index e952d56..b6e6a05 100644 --- a/almanachclient/shell.py +++ b/almanachclient/shell.py @@ -18,6 +18,7 @@ import sys from cliff import app from cliff import commandmanager +from almanachclient.commands.create_instance import CreateInstanceCommand from almanachclient.commands.create_volume_type import CreateVolumeTypeCommand from almanachclient.commands.delete_instance import DeleteInstanceCommand from almanachclient.commands.delete_volume_type import DeleteVolumeTypeCommand @@ -40,6 +41,7 @@ class AlmanachCommandManager(commandmanager.CommandManager): 'delete-volume-type': DeleteVolumeTypeCommand, 'list-volume-types': ListVolumeTypeCommand, 'get-volume-type': GetVolumeTypeCommand, + 'create-instance': CreateInstanceCommand, 'delete-instance': DeleteInstanceCommand, 'list-entities': ListEntityCommand, 'update instance': UpdateInstanceEntityCommand, diff --git a/almanachclient/tests/commands/test_create_instance.py b/almanachclient/tests/commands/test_create_instance.py new file mode 100644 index 0000000..eb9c8d0 --- /dev/null +++ b/almanachclient/tests/commands/test_create_instance.py @@ -0,0 +1,48 @@ +# Copyright 2017 INAP +# +# 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 argparse import Namespace +import datetime +from unittest import mock + +from almanachclient.commands.create_instance import CreateInstanceCommand + +from almanachclient.tests import base + + +class TestCreateInstanceCommand(base.TestCase): + + def setUp(self): + super().setUp() + self.app = mock.Mock() + self.app_args = mock.Mock() + self.args = Namespace(tenant_id='tenant uuid', + instance_id='instance uuid', + name='vm', + flavor='flavor', + start='2017-01-01', + image_meta='{"type": "linux"}') + + self.client = mock.Mock() + self.app.get_client.return_value = self.client + self.command = CreateInstanceCommand(self.app, self.app_args) + + def test_execute_command(self): + self.assertEqual('Success', self.command.take_action(self.args)) + self.client.create_instance.assert_called_once_with('tenant uuid', + 'instance uuid', + 'vm', + 'flavor', + datetime.datetime(2017, 1, 1, 0, 0), + {'type': 'linux'}) diff --git a/almanachclient/tests/commands/test_delete_instance.py b/almanachclient/tests/commands/test_delete_instance.py index bdd0f81..e0cde8c 100644 --- a/almanachclient/tests/commands/test_delete_instance.py +++ b/almanachclient/tests/commands/test_delete_instance.py @@ -13,6 +13,7 @@ # limitations under the License. from argparse import Namespace +import datetime from unittest import mock from almanachclient.commands.delete_instance import DeleteInstanceCommand @@ -35,3 +36,8 @@ class TestDeleteInstanceCommand(base.TestCase): def test_execute_command(self): self.assertEqual('Success', self.command.take_action(self.args)) self.client.delete_instance.assert_called_once_with('some uuid', None) + + def test_execute_command_with_date(self): + self.args.end = '2017-01-01' + self.assertEqual('Success', self.command.take_action(self.args)) + self.client.delete_instance.assert_called_once_with('some uuid', datetime.datetime(2017, 1, 1, 0, 0)) diff --git a/almanachclient/tests/v1/test_client.py b/almanachclient/tests/v1/test_client.py index ef325e1..34f7c00 100644 --- a/almanachclient/tests/v1/test_client.py +++ b/almanachclient/tests/v1/test_client.py @@ -153,3 +153,26 @@ class TestClient(base.TestCase): headers=self.headers, data=json.dumps({'date': date.strftime(Client.DATE_FORMAT_BODY)}), params=None) + + @mock.patch('requests.post') + def test_create_instance(self, requests): + self.response.text = '' + date = datetime.now() + payload = { + "flavor": "flavor", + "id": "instance_id", + "name": "name", + "created_at": date.strftime(Client.DATE_FORMAT_BODY), + "os_distro": None, + "os_type": None, + "os_version": None, + } + + requests.return_value = self.response + self.response.status_code = 201 + + self.assertTrue(self.client.create_instance('tenant_id', 'instance_id', 'name', 'flavor', date)) + requests.assert_called_once_with('{}{}'.format(self.url, '/v1/project/tenant_id/instance'), + headers=self.headers, + data=json.dumps(payload), + params=None) diff --git a/almanachclient/v1/client.py b/almanachclient/v1/client.py index 1771335..08bc13d 100644 --- a/almanachclient/v1/client.py +++ b/almanachclient/v1/client.py @@ -45,6 +45,20 @@ class Client(HttpClient): self._delete('{}/{}/volume_type/{}'.format(self.url, self.api_version, volume_type_id)) return True + def create_instance(self, tenant_id, instance_id, name, flavor, start, image_meta=None): + url = '{}/{}/project/{}/instance'.format(self.url, self.api_version, tenant_id) + image_meta = image_meta or {} + self._post(url, data={ + 'id': instance_id, + 'created_at': self._format_body_datetime(start), + 'name': name, + 'flavor': flavor, + 'os_distro': image_meta.get('distro'), + 'os_version': image_meta.get('version'), + 'os_type': image_meta.get('type'), + }) + return True + def delete_instance(self, instance_id, end=None): data = {'date': self._format_body_datetime(end or datetime.now())} self._delete('{}/{}/instance/{}'.format(self.url, self.api_version, instance_id), data=data) diff --git a/doc/source/usage.rst b/doc/source/usage.rst index b1db0c8..e0b0c36 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -50,6 +50,31 @@ Usage: :code:`almanach list-entities ` | 3e3b22e6-a10c-4c00-b8e5-05fcc8422b11 | volume | vol01 | 2017-05-15 19:11:14+00:00 | None | {'attached_to': [], 'volume_type': 'solidfire0'} | +--------------------------------------+----------+--------+---------------------------+------+---------------------------------------------------------------------------------------+ +Create Instance Entity +---------------------- + +Usage: :code:`almanach create_instance --image-meta ` + +Example: + +.. code:: bash + + almanach create-instance bca89ae64dba46b8b74653d8d9ae8364 \ + 8d8d0dc7-5f06-40aa-aba8-c4ff02aeb866 \ + my-instance \ + my-flavor \ + 2017-01-01 \ + --image-meta '{"distro": "centos7", "type": "linux"}' + + Success + +* :code:`tenant_id`: Tenant ID (UUID) +* :code:`instance_id`: Instance ID (UUID) +* :code:`start`: Start date (ISO8601 format) +* :code:`name`: Instance name (string) +* :code:`flavor`: Flavor (string) +* :code:`image_meta`: Image metadata (dict as JSON string) + Update Instance Entity ---------------------- @@ -92,6 +117,11 @@ Usage: :code:`almanach delete-instance --end * :code:`end`: End date, if not specified the current date time is used (ISO8601 format) +Arguments: + +* :code:`instance_id`: Instance ID (UUID) +* :code:`end`: End date (ISO8601 format) + List Volume Types -----------------