Repository: zalando-stups/senza

Issue:
Please set the --connect option as optional
After 479e4d8 you connect automatically to ***all*** instances if you use `--piu`

But this is not useful, if you get more then one instance back.

Discussion:

Patch:
diff --git a/senza/cli.py b/senza/cli.py
index e20c744..15927fd 100755
--- a/senza/cli.py
+++ b/senza/cli.py
@@ -985,11 +985,13 @@ def instances(stack_ref, all, terminated, docker_image, piu, odd_host, region,
 
         if piu is not None:
             odd_host = odd_host or Piu.find_odd_host(region)
+            auto_connect = len(rows) == 1
             for row in rows:
                 if row['private_ip'] is not None:
                     Piu.request_access(instance=row['private_ip'],
                                        reason=piu,
-                                       odd_host=odd_host)
+                                       odd_host=odd_host,
+                                       connect=auto_connect)
 
 
 @cli.command()
diff --git a/senza/stups/piu.py b/senza/stups/piu.py
index 3d1784a..643ac93 100644
--- a/senza/stups/piu.py
+++ b/senza/stups/piu.py
@@ -13,13 +13,18 @@ class Piu:
     http://stups.readthedocs.io/en/latest/user-guide/ssh-access.html#ssh-access
     """
     @staticmethod
-    def request_access(instance: str, reason: str, odd_host: Optional[str]):
+    def request_access(instance: str, reason: str, odd_host: Optional[str],
+                       connect: bool):
         """
         Request SSH access to a single host
         """
         reason = '{} via senza'.format(reason)
-        cmd = ['piu', 'request-access', '--connect',
+        cmd = ['piu', 'request-access',
                instance, reason]
+
+        if connect:
+            cmd.append('--connect')
+
         if odd_host is not None:
             cmd.extend(['-O', odd_host])
 


Test patch:
diff --git a/tests/test_stups/test_piu.py b/tests/test_stups/test_piu.py
index 125a016..447b2bf 100644
--- a/tests/test_stups/test_piu.py
+++ b/tests/test_stups/test_piu.py
@@ -9,14 +9,21 @@ def test_request_access(monkeypatch):
     m_call = MagicMock()
     monkeypatch.setattr('senza.stups.piu.call', m_call)
 
-    Piu.request_access('127.0.0.1', 'no reason', None)
-    m_call.assert_called_once_with(['piu', 'request-access', '--connect',
+    Piu.request_access('127.0.0.1', 'no reason', None, True)
+    m_call.assert_called_once_with(['piu', 'request-access',
+                                    '127.0.0.1', 'no reason via senza',
+                                    '--connect'])
+
+    m_call.reset_mock()
+    Piu.request_access('127.0.0.1', 'no reason', None, False)
+    m_call.assert_called_once_with(['piu', 'request-access',
                                     '127.0.0.1', 'no reason via senza'])
 
     m_call.reset_mock()
-    Piu.request_access('127.0.0.1', 'no reason', 'example.com')
-    m_call.assert_called_once_with(['piu', 'request-access', '--connect',
+    Piu.request_access('127.0.0.1', 'no reason', 'example.com', True)
+    m_call.assert_called_once_with(['piu', 'request-access',
                                     '127.0.0.1', 'no reason via senza',
+                                    '--connect',
                                     '-O', 'example.com'])
 
 
@@ -56,6 +63,7 @@ def test_request_access_not_installed(monkeypatch):
     monkeypatch.setattr('senza.stups.piu.call', m_call)
 
     with pytest.raises(PiuNotFound):
-        Piu.request_access('127.0.0.1', 'no reason', None)
-    m_call.assert_called_once_with(['piu', 'request-access', '--connect',
-                                    '127.0.0.1', 'no reason via senza'])
+        Piu.request_access('127.0.0.1', 'no reason', None, True)
+    m_call.assert_called_once_with(['piu', 'request-access',
+                                    '127.0.0.1', 'no reason via senza',
+                                    '--connect'])