Repository: takahi-i/pfm

Issue:
Cannot add new stuff with the same local port
* pfm version:0.3.0
* Python version:pyton:3.5.2
* Operating System:mac OS 10.13

### Description

I cannot add a new element using a local port that was used before.

### What I Did

```
$ pfm list
+----------------------+------------+------------+--------------------------------+------------+-----------------+--------------------------------+--------------+
|         name         |    type    | local_port |          remote_host           | remote_por |   login_user    |           ssh_server           | server_port  |
|                      |            |            |                                |     t      |                 |                                |              |
+======================+============+============+================================+============+=================+================================+==============+
| test1                | L          | 8888       | localhost                      | 10003      | None            |  test-server                   |              |
|                      |            |            |                                |            |                 |                                |     None     |
+----------------------+------------+------------+--------------------------------+------------+-----------------+--------------------------------+--------------+
```

Then
```
$ pfm add --name test2 --local-port 8888 --ssh-server test-server
local_port is not specified
allocating remote_port for test2...
remote_port of test2 is set to 49152
Failed to register...
local port 8888 is already used in test1
```

Comments:

Patch:
diff --git a/pf_manager/pf_command/add.py b/pf_manager/pf_command/add.py
index 31abddf..978a2d3 100644
--- a/pf_manager/pf_command/add.py
+++ b/pf_manager/pf_command/add.py
@@ -20,11 +20,13 @@ def check_fields(target):
 
 
 def check_local_port_is_used(local_port, targets):
+    print("local_port: " + local_port);
+
+
     for target_name in targets:
         target = targets[target_name]
         if local_port == target["local_port"]:
-            raise RuntimeError("local port " + str(local_port) + " is already used in " + target_name)
-
+            logger.warn("local port {} is already used in {}".format(str(local_port), target_name))
 
 def check_remote_port_is_used(new_target, targets):
     remote_port = new_target["remote_port"]
@@ -35,9 +37,7 @@ def check_remote_port_is_used(new_target, targets):
         target_remote_host = get_remote_host(target)
 
         if target_remote_host == remote_host and target["remote_port"] == remote_port:
-            raise RuntimeError(
-                "remote port " + str(remote_port) + " in host " + remote_host + "is already used in " + target_name)
-
+            logger.warn("remote port {} in host {} is already used in {} ".format(str(remote_port), remote_host, target_name))
 
 def get_remote_host(target):
     target_remote_host = target["remote_host"]


Test patch:
diff --git a/tests/pf_command/test_add.py b/tests/pf_command/test_add.py
index 6f1b0d4..18b86b1 100644
--- a/tests/pf_command/test_add.py
+++ b/tests/pf_command/test_add.py
@@ -27,7 +27,7 @@ class TestPfm(unittest.TestCase):
                                  None)
         self.assertRaises(RuntimeError, lambda: add_command.generate_consistent_target({}))
 
-    def test_fail_to_add_same_local_port(self):
+    def test_add_same_local_port(self):
         targets = {'food-nonfood':
             {
                 'name': 'text-classification',
@@ -37,7 +37,7 @@ class TestPfm(unittest.TestCase):
         }
         add_command = AddCommand("image-processing", None, "L", "localhost", "8888", "8888", "my.aws.com", None, None,
                                  None)
-        self.assertRaises(RuntimeError, lambda: add_command.generate_consistent_target(targets))
+        self.assertEqual("8888", add_command.generate_consistent_target(targets)["local_port"])
 
     def test_add_target_without_local_port(self):
         targets = {'food-nonfood':
@@ -49,6 +49,7 @@ class TestPfm(unittest.TestCase):
         }
         add_command = AddCommand("image-processing", None, "L", "localhost", "8888", None, "my.aws.com", None, None,
                                  None)
+        self.assertEqual("49152", add_command.generate_consistent_target(targets)["local_port"])
 
     def test_add_target_without_remote_port(self):
         targets = {'food-nonfood':
@@ -85,7 +86,7 @@ class TestPfm(unittest.TestCase):
         add_command = AddCommand("image-processing", None, "L", "my-ml-instance.ml.aws.com", "9999", "7777",
                                  "ssh-server-instance.ml.aws.com", None, None,
                                  None)
-        self.assertRaises(RuntimeError, lambda: add_command.generate_consistent_target(targets))
+        self.assertEqual("9999", add_command.generate_consistent_target(targets)["remote_port"])
 
     def test_fail_to_add_same_remote_port_in_same_host2(self):
         targets = {'food-nonfood':
@@ -98,4 +99,4 @@ class TestPfm(unittest.TestCase):
         add_command = AddCommand("image-processing", None, 'L', 'localhost', '9999', '7777',
                                  'my-ml-instance.ml.aws.com', None, None,
                                  None)
-        self.assertRaises(RuntimeError, lambda: add_command.generate_consistent_target(targets))
+        self.assertEqual("9999", add_command.generate_consistent_target(targets)["remote_port"])