From a821d6b7c57c7684a990ee39b6b93d5085f25a70 Mon Sep 17 00:00:00 2001
From: Stephen Finucane <sfinucan@redhat.com>
Date: Tue, 13 Jul 2021 20:29:43 +0100
Subject: [PATCH] volume: Add 'volume transfer request create --(no-)snapshots'
 option

This closes a gap with cinderclient's 'transfer-create' command.

Change-Id: I7386a7be15c0e3ee87abbcfc2275ba8524c10ff8
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
Story: 2009054
Task: 42831
---
 ...est.py => test_volume_transfer_request.py} | 46 +++++++++++++++++++
 .../volume/v2/volume_transfer_request.py      | 36 +++++++++++++++
 ...reate-snapshots-opts-1361416d37021e89.yaml |  6 +++
 3 files changed, 88 insertions(+)
 rename openstackclient/tests/unit/volume/v2/{test_transfer_request.py => test_volume_transfer_request.py} (89%)
 create mode 100644 releasenotes/notes/add-volume-transfer-request-create-snapshots-opts-1361416d37021e89.yaml

diff --git a/openstackclient/tests/unit/volume/v2/test_transfer_request.py b/openstackclient/tests/unit/volume/v2/test_volume_transfer_request.py
similarity index 89%
rename from openstackclient/tests/unit/volume/v2/test_transfer_request.py
rename to openstackclient/tests/unit/volume/v2/test_volume_transfer_request.py
index c9dce3cab0..1a1f220ff6 100644
--- a/openstackclient/tests/unit/volume/v2/test_transfer_request.py
+++ b/openstackclient/tests/unit/volume/v2/test_volume_transfer_request.py
@@ -15,6 +15,7 @@
 from unittest import mock
 from unittest.mock import call
 
+from cinderclient import api_versions
 from osc_lib import exceptions
 from osc_lib import utils
 
@@ -172,6 +173,51 @@ class TestTransferCreate(TestTransfer):
         self.assertEqual(self.columns, columns)
         self.assertEqual(self.data, data)
 
+    def test_transfer_create_with_no_snapshots(self):
+        self.app.client_manager.volume.api_version = \
+            api_versions.APIVersion('3.55')
+
+        arglist = [
+            '--no-snapshots',
+            self.volume.id,
+        ]
+        verifylist = [
+            ('name', None),
+            ('snapshots', False),
+            ('volume', self.volume.id),
+        ]
+        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+        columns, data = self.cmd.take_action(parsed_args)
+
+        self.transfer_mock.create.assert_called_once_with(
+            self.volume.id, None, no_snapshots=True)
+        self.assertEqual(self.columns, columns)
+        self.assertEqual(self.data, data)
+
+    def test_transfer_create_pre_v355(self):
+        self.app.client_manager.volume.api_version = \
+            api_versions.APIVersion('3.54')
+
+        arglist = [
+            '--no-snapshots',
+            self.volume.id,
+        ]
+        verifylist = [
+            ('name', None),
+            ('snapshots', False),
+            ('volume', self.volume.id),
+        ]
+        parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+        exc = self.assertRaises(
+            exceptions.CommandError,
+            self.cmd.take_action,
+            parsed_args)
+        self.assertIn(
+            '--os-volume-api-version 3.55 or greater is required',
+            str(exc))
+
 
 class TestTransferDelete(TestTransfer):
 
diff --git a/openstackclient/volume/v2/volume_transfer_request.py b/openstackclient/volume/v2/volume_transfer_request.py
index 2a1ace1f42..8919933609 100644
--- a/openstackclient/volume/v2/volume_transfer_request.py
+++ b/openstackclient/volume/v2/volume_transfer_request.py
@@ -16,6 +16,7 @@
 
 import logging
 
+from cinderclient import api_versions
 from osc_lib.command import command
 from osc_lib import exceptions
 from osc_lib import utils
@@ -76,6 +77,25 @@ class CreateTransferRequest(command.ShowOne):
             metavar="<name>",
             help=_('New transfer request name (default to None)'),
         )
+        parser.add_argument(
+            '--snapshots',
+            action='store_true',
+            dest='snapshots',
+            help=_(
+                'Allow transfer volumes without snapshots (default) '
+                '(supported by --os-volume-api-version 3.55 or later)'
+            ),
+            default=None,
+        )
+        parser.add_argument(
+            '--no-snapshots',
+            action='store_false',
+            dest='snapshots',
+            help=_(
+                'Disallow transfer volumes without snapshots '
+                '(supported by --os-volume-api-version 3.55 or later)'
+            ),
+        )
         parser.add_argument(
             'volume',
             metavar="<volume>",
@@ -85,6 +105,21 @@ class CreateTransferRequest(command.ShowOne):
 
     def take_action(self, parsed_args):
         volume_client = self.app.client_manager.volume
+
+        kwargs = {}
+
+        if parsed_args.snapshots is not None:
+            if volume_client.api_version < api_versions.APIVersion('3.55'):
+                msg = _(
+                    "--os-volume-api-version 3.55 or greater is required to "
+                    "support the '--(no-)snapshots' option"
+                )
+                raise exceptions.CommandError(msg)
+
+            # unfortunately this option is negative so we have to reverse
+            # things
+            kwargs['no_snapshots'] = not parsed_args.snapshots
+
         volume_id = utils.find_resource(
             volume_client.volumes,
             parsed_args.volume,
@@ -92,6 +127,7 @@ class CreateTransferRequest(command.ShowOne):
         volume_transfer_request = volume_client.transfers.create(
             volume_id,
             parsed_args.name,
+            **kwargs,
         )
         volume_transfer_request._info.pop("links", None)
 
diff --git a/releasenotes/notes/add-volume-transfer-request-create-snapshots-opts-1361416d37021e89.yaml b/releasenotes/notes/add-volume-transfer-request-create-snapshots-opts-1361416d37021e89.yaml
new file mode 100644
index 0000000000..f915f87c16
--- /dev/null
+++ b/releasenotes/notes/add-volume-transfer-request-create-snapshots-opts-1361416d37021e89.yaml
@@ -0,0 +1,6 @@
+---
+features:
+  - |
+    The ``volume transfer request create`` command now accepts the
+    ``--snapshots`` / ``--no-snapshots`` option to configure whether to
+    create a transfer request for a volume without snapshots or not.