Skip to content

Commit

Permalink
Merge pull request #273 from atlanticwave-sdx/delete-connection
Browse files Browse the repository at this point in the history
Add connection deletion
  • Loading branch information
congwang09 committed May 21, 2024
2 parents 02a925e + a9ac7f0 commit 1893499
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions sdx_controller/controllers/connection_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def delete_connection(connection_id):
#
# https://github.com/atlanticwave-sdx/pce/issues/180
current_app.te_manager.unreserve_vlan(connection_id)
deleted = db_instance.mark_deleted("connections", f"{connection_id}")
if not deleted:
return "Did not find connection", 404
except Exception as e:
logger.info(f"Delete failed (connection id: {connection_id}): {e}")
return "Failed, reason: {e}", 500
Expand Down
2 changes: 1 addition & 1 deletion sdx_controller/test/test_connection_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_delete_connection_no_setup(self):
f"{BASE_PATH}/connection/{connection_id}",
method="DELETE",
)
self.assert200(response, f"Response body is : {response.data.decode('utf-8')}")
self.assert404(response, f"Response body is : {response.data.decode('utf-8')}")

def __add_the_three_topologies(self):
"""
Expand Down
17 changes: 14 additions & 3 deletions sdx_controller/utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,31 @@ def add_key_value_pair_to_db(self, collection, key, value):
key = str(key)
obj = self.read_from_db(collection, key)
if obj is None:
# self.logger.debug(f"Adding key value pair {key}:{value} to DB.")
return self.sdxdb[collection].insert_one({key: value})

query = {"_id": obj["_id"]}
# self.logger.debug(f"Updating DB entry {key}:{value}.")
result = self.sdxdb[collection].replace_one(query, {key: value})
return result

def read_from_db(self, collection, key):
key = str(key)
return self.sdxdb[collection].find_one({key: {"$exists": 1}})
return self.sdxdb[collection].find_one(
{key: {"$exists": 1}, "deleted": {"$ne": True}}
)

def get_all_entries_in_collection(self, collection):
db_collection = self.sdxdb[collection]
# MongoDB has an ObjectId for each item, so need to exclude the ObjectIds
all_entries = db_collection.find({}, {"_id": 0})
return all_entries

def mark_deleted(self, collection, key):
db_collection = self.sdxdb[collection]
key = str(key)
item_to_delete = self.read_from_db(collection, key)
if item_to_delete is None:
return False
filter = {"_id": item_to_delete["_id"]}
update = {"$set": {"deleted": True}}
db_collection.update_one(filter, update)
return True
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ setenv =
SDX_VERSION = 1.0.0
SDX_NAME = sdx-controller-test
MQ_HOST = localhost
MQ_PORT = 5672
SUB_QUEUE = sdx-controller-test-queue
DB_NAME = sdx-controller-test-db
DB_CONFIG_TABLE_NAME = sdx-controller-test-table
Expand Down

0 comments on commit 1893499

Please sign in to comment.