Add delete volume command

Change-Id: I3aa5b3cbee94154537cca1a717e23237ed5b50c6
This commit is contained in:
Frédéric Guillot 2017-05-29 16:09:32 -04:00
parent e4033ce967
commit 805e804fad
5 changed files with 121 additions and 5 deletions

View File

@ -0,0 +1,31 @@
# 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
from dateutil import parser as date_parser
class DeleteVolumeCommand(Command):
"""Delete volume"""
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument('volume_id', help='Volume ID')
parser.add_argument('--end', help='End date')
return parser
def take_action(self, parsed_args):
self.app.get_client().delete_volume(parsed_args.volume_id,
date_parser.parse(parsed_args.end) if parsed_args.end else None)
return 'Success'

View File

@ -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 argparse import Namespace
import datetime
from unittest import mock
from almanachclient.commands.delete_volume import DeleteVolumeCommand
from almanachclient.tests import base
class TestDeleteVolumeCommand(base.TestCase):
def setUp(self):
super().setUp()
self.app = mock.Mock()
self.app_args = mock.Mock()
self.args = Namespace(volume_id='some uuid', end=None)
self.client = mock.Mock()
self.app.get_client.return_value = self.client
self.command = DeleteVolumeCommand(self.app, self.app_args)
def test_execute_command(self):
self.assertEqual('Success', self.command.take_action(self.args))
self.client.delete_volume.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_volume.assert_called_once_with('some uuid', datetime.datetime(2017, 1, 1, 0, 0))

View File

@ -305,3 +305,17 @@ class TestClient(base.TestCase):
data=json.dumps({'attachments': ['instance_id'],
'date': date.strftime(Client.DATE_FORMAT_BODY)}),
headers=self.headers)
@mock.patch('requests.delete')
def test_delete_volume(self, requests):
self.response.headers['Content-Length'] = 0
date = datetime.now()
requests.return_value = self.response
self.response.status_code = 202
self.assertTrue(self.client.delete_volume('some uuid', date))
requests.assert_called_once_with('{}{}'.format(self.url, '/v1/volume/some uuid'),
headers=self.headers,
data=json.dumps({'date': date.strftime(Client.DATE_FORMAT_BODY)}),
params=None)

View File

@ -110,6 +110,21 @@ class Client(HttpClient):
self._post('{}/{}/project/{}/volume'.format(self.url, self.api_version, tenant_id), data=data)
return True
def delete_volume(self, volume_id, end=None):
"""Remove a volume.
:arg str volume_id: Volume UUID
:arg datetime end: Suppression date
:raises: ClientError
:rtype: bool
"""
data = {
'date': self._format_body_datetime(end or datetime.now()),
}
self._delete('{}/{}/volume/{}'.format(self.url, self.api_version, volume_id), data=data)
return True
def resize_volume(self, volume_id, size, resize_date=None):
"""Resize a volume.

View File

@ -12,8 +12,8 @@ Environment variables
* :code:`ALMANACH_TOKEN`: Almanach API token, if empty a token will be fetched from Keystone
* :code:`ALMANACH_URL`: Almanach API base URL, if empty the endpoint will be fetched from Keystone catalog
Command Line Arguments
----------------------
Optional Arguments
------------------
* :code:`--os-auth-url`: Keystone URL (v3 endpoint)
* :code:`--os-region-name`: OpenStack region name
@ -174,12 +174,10 @@ Usage: :code:`almanach delete-instance <instance_id> --end <end>`
Success
* :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)
* :code:`end`: End date (ISO8601 format), if not specified the current date time is used
Resize Instance
---------------
@ -247,6 +245,21 @@ Arguments:
* :code:`date`: Creation date (ISO8601 format), if not specified the current datetime is used
* :code:`attachment`: Attach the volume to one or many instances (UUID)
Delete Volume
-------------
Usage: :code:`almanach delete-volume <volume_id> --end <end>`
.. code:: bash
almanach delete-volume 8c3bc3aa-28d6-4863-b5ae-72e1b415f79d
Success
Arguments:
* :code:`volume_id`: Instance ID (UUID)
* :code:`end`: End date (ISO8601 format), if not specified the current date time is used
Resize Volume
-------------