Add command to create an instance

This commit is contained in:
Frédéric Guillot 2017-05-17 13:47:35 -04:00
parent c6faa327dd
commit 39f7a8bbbb
8 changed files with 169 additions and 1 deletions

@ -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'

@ -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'

@ -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,

@ -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'})

@ -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))

@ -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)

@ -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)

@ -50,6 +50,31 @@ Usage: :code:`almanach list-entities <tenant_id> <start> <end>`
| 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 <tenant_id> <instance_id> <name> <flavor> <start> --image-meta <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 <instance_id> --end <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
-----------------