Add new commands to create and delete volume types

This commit is contained in:
Frédéric Guillot 2017-05-16 14:07:22 -04:00
parent 5cf365abc9
commit eedec59003
13 changed files with 211 additions and 6 deletions

View File

@ -0,0 +1,30 @@
# 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 cliff.command import Command
class CreateVolumeTypeCommand(Command):
"""Create volume type"""
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument('volume_type_id', help='Volume Type ID')
parser.add_argument('volume_type_name', help='Volume Type Name')
return parser
def take_action(self, parsed_args):
self.app.get_client().create_volume_type(parsed_args.volume_type_id,
parsed_args.volume_type_name)
return 'Success'

View File

@ -0,0 +1,28 @@
# 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 cliff.command import Command
class DeleteVolumeTypeCommand(Command):
"""Delete volume type"""
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument('volume_type_id', help='Volume Type ID')
return parser
def take_action(self, parsed_args):
self.app.get_client().delete_volume_type(parsed_args.volume_type_id)
return 'Success'

View File

@ -19,4 +19,4 @@ class EndpointCommand(Command):
"""Show Almanach Endpoint URL"""
def take_action(self, parsed_args):
self.app.stdout.write('{}\n'.format(self.app.get_client().get_url()))
return self.app.get_client().get_url()

View File

@ -20,4 +20,4 @@ class VersionCommand(Command):
def take_action(self, parsed_args):
info = self.app.get_client().get_info()
self.app.stdout.write('{}\n'.format(info.get('info', {}).get('version')))
return info.get('info', {}).get('version')

View File

@ -41,8 +41,19 @@ class HttpClient(metaclass=abc.ABCMeta):
params=params,
data=json.dumps(data)))
def _post(self, url, data, params=None):
logger.debug(url)
return self._parse_response(requests.post(url,
headers=self._get_headers(),
params=params,
data=json.dumps(data)), 201)
def _delete(self, url, params=None):
logger.debug(url)
return self._parse_response(requests.delete(url, headers=self._get_headers(), params=params), 202)
def _parse_response(self, response, expected_status=200):
body = response.json()
body = response.json() if len(response.text) > 0 else ''
if response.status_code != expected_status:
raise exceptions.HTTPError('{} ({})'.format(body.get('error') or 'HTTP Error', response.status_code))

View File

@ -18,9 +18,11 @@ import sys
from cliff import app
from cliff import commandmanager
from almanachclient.commands.create_volume_type import CreateVolumeTypeCommand
from almanachclient.commands.delete_volume_type import DeleteVolumeTypeCommand
from almanachclient.commands.endpoint import EndpointCommand
from almanachclient.commands.get_volume_type import GetVolumeTypeCommand
from almanachclient.commands.list_entities import ListEntityCommand
from almanachclient.commands.list_entity import ListEntityCommand
from almanachclient.commands.list_volume_type import ListVolumeTypeCommand
from almanachclient.commands.update_instance_entity import UpdateInstanceEntityCommand
from almanachclient.commands.version import VersionCommand
@ -33,6 +35,8 @@ class AlmanachCommandManager(commandmanager.CommandManager):
SHELL_COMMANDS = {
'version': VersionCommand,
'endpoint': EndpointCommand,
'create-volume-type': CreateVolumeTypeCommand,
'delete-volume-type': DeleteVolumeTypeCommand,
'list-volume-types': ListVolumeTypeCommand,
'get-volume-type': GetVolumeTypeCommand,
'list-entities': ListEntityCommand,

View File

@ -0,0 +1,37 @@
# 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
from unittest import mock
from almanachclient.commands.create_volume_type import CreateVolumeTypeCommand
from almanachclient.tests import base
class TestCreateVolumeTypeCommand(base.TestCase):
def setUp(self):
super().setUp()
self.app = mock.Mock()
self.app_args = mock.Mock()
self.args = Namespace(volume_type_id='some uuid', volume_type_name='some name')
self.client = mock.Mock()
self.app.get_client.return_value = self.client
self.command = CreateVolumeTypeCommand(self.app, self.app_args)
def test_execute_command(self):
self.assertTrue(self.command.take_action(self.args))
self.client.create_volume_type.assert_called_once_with('some uuid', 'some name')

View File

@ -0,0 +1,37 @@
# 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
from unittest import mock
from almanachclient.commands.delete_volume_type import DeleteVolumeTypeCommand
from almanachclient.tests import base
class TestDeleteVolumeTypeCommand(base.TestCase):
def setUp(self):
super().setUp()
self.app = mock.Mock()
self.app_args = mock.Mock()
self.args = Namespace(volume_type_id='some uuid')
self.client = mock.Mock()
self.app.get_client.return_value = self.client
self.command = DeleteVolumeTypeCommand(self.app, self.app_args)
def test_execute_command(self):
self.assertTrue(self.command.take_action(self.args))
self.client.delete_volume_type.assert_called_once_with('some uuid')

View File

@ -16,7 +16,7 @@ from argparse import Namespace
import datetime
from unittest import mock
from almanachclient.commands.list_entities import ListEntityCommand
from almanachclient.commands.list_entity import ListEntityCommand
from almanachclient.tests import base

View File

@ -25,7 +25,7 @@ class TestClient(base.TestCase):
def setUp(self):
super().setUp()
self.response = mock.Mock()
self.response = mock.Mock(text='sample data')
self.url = 'http://almanach_url'
self.token = 'token'
self.headers = {'Content-Type': 'application/json',
@ -112,3 +112,29 @@ class TestClient(base.TestCase):
self.assertEqual(expected, self.client.get_volume_type('some-uuid'))
requests.assert_called_once_with('{}{}'.format(self.url, '/v1/volume_type/some-uuid'),
headers=self.headers, params=None)
@mock.patch('requests.post')
def test_create_volume_type(self, requests):
data = {'type_id': 'some uuid', 'type_name': 'some name'}
self.response.text = ''
requests.return_value = self.response
self.response.status_code = 201
self.assertTrue(self.client.create_volume_type('some uuid', 'some name'))
requests.assert_called_once_with('{}{}'.format(self.url, '/v1/volume_type'),
headers=self.headers,
data=json.dumps(data),
params=None)
@mock.patch('requests.delete')
def test_delete_volume_type(self, requests):
self.response.text = ''
requests.return_value = self.response
self.response.status_code = 202
self.assertTrue(self.client.delete_volume_type('some uuid'))
requests.assert_called_once_with('{}{}'.format(self.url, '/v1/volume_type/some uuid'),
headers=self.headers,
params=None)

View File

@ -33,6 +33,16 @@ class Client(HttpClient):
def get_volume_type(self, volume_type_id):
return self._get('{}/{}/volume_type/{}'.format(self.url, self.api_version, volume_type_id))
def create_volume_type(self, volume_type_id, volume_type_name):
url = '{}/{}/volume_type'.format(self.url, self.api_version)
data = {'type_id': volume_type_id, 'type_name': volume_type_name}
self._post(url, data)
return True
def delete_volume_type(self, volume_type_id):
self._delete('{}/{}/volume_type/{}'.format(self.url, self.api_version, volume_type_id))
return True
def get_tenant_entities(self, tenant_id, start, end):
url = '{}/{}/project/{}/entities'.format(self.url, self.api_version, tenant_id)
params = {'start': self._format_qs_datetime(start), 'end': self._format_qs_datetime(end)}

View File

@ -109,3 +109,25 @@ Usage: :code:`almanach-client get-volume-type <volume_type_id>`
| Volume Type ID | f3786e9f-f8e6-4944-a3bc-e11b9f112706 |
| Volume Type Name | solidfire0 |
+------------------+--------------------------------------+
Create Volume Type
------------------
Usage: :code:`almanach-client create-volume-type <volume_type_id> <volume_type_name>`
.. code:: bash
almanach-client create-volume-type f1c2db7b-946e-47a4-b443-914a669a6672 my_volume_type
Success
Delete Volume Type
------------------
Usage: :code:`almanach-client delete-volume-type <volume_type_id>`
.. code:: bash
almanach-client delete-volume-type f1c2db7b-946e-47a4-b443-914a669a6672
Success