From c2b042f39124256978b9fe2741eaa4408dc6b1d2 Mon Sep 17 00:00:00 2001 From: Justin Drew <2396364+jdrew82@users.noreply.github.com> Date: Tue, 22 Oct 2024 08:44:19 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Add=20try/except=20in=20g?= =?UTF-8?q?et=5Ftags=5Fby=5Ftype()=20to=20handle=20possible=20RpcError=20b?= =?UTF-8?q?eing=20thrown.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changes/467.fixed | 1 + .../aristacv/diffsync/adapters/cloudvision.py | 2 +- .../aristacv/utils/cloudvision.py | 23 +++++++++++-------- .../tests/aristacv/test_utils_cloudvision.py | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 changes/467.fixed diff --git a/changes/467.fixed b/changes/467.fixed new file mode 100644 index 00000000..b24d50d8 --- /dev/null +++ b/changes/467.fixed @@ -0,0 +1 @@ +Fix get_tags_by_type() to handle possible RpcError Exception being thrown. \ No newline at end of file diff --git a/nautobot_ssot/integrations/aristacv/diffsync/adapters/cloudvision.py b/nautobot_ssot/integrations/aristacv/diffsync/adapters/cloudvision.py index a9cfb154..67f55594 100644 --- a/nautobot_ssot/integrations/aristacv/diffsync/adapters/cloudvision.py +++ b/nautobot_ssot/integrations/aristacv/diffsync/adapters/cloudvision.py @@ -232,7 +232,7 @@ def load_ip_addresses(self, dev: device): def load_device_tags(self, device): """Load device tags from CloudVision.""" system_tags = cloudvision.get_tags_by_type( - client=self.conn.comm_channel, creator_type=TAG.models.CREATOR_TYPE_SYSTEM + client=self.conn.comm_channel, logger=self.job.logger, creator_type=TAG.models.CREATOR_TYPE_SYSTEM ) dev_tags = [ tag diff --git a/nautobot_ssot/integrations/aristacv/utils/cloudvision.py b/nautobot_ssot/integrations/aristacv/utils/cloudvision.py index 49eaebd3..9e06b505 100644 --- a/nautobot_ssot/integrations/aristacv/utils/cloudvision.py +++ b/nautobot_ssot/integrations/aristacv/utils/cloudvision.py @@ -267,18 +267,21 @@ def get_devices(client, import_active: bool): return devices -def get_tags_by_type(client, creator_type: int = tag_models.CREATOR_TYPE_USER): +def get_tags_by_type(client, logger, creator_type: int = tag_models.CREATOR_TYPE_USER): """Get tags by creator type from CloudVision.""" - tag_stub = tag_services.TagServiceStub(client) - req = tag_services.TagStreamRequest(partial_eq_filter=[tag_models.Tag(creator_type=creator_type)]) - responses = tag_stub.GetAll(req) tags = [] - for resp in responses: - dev_tag = { - "label": resp.value.key.label.value, - "value": resp.value.key.value.value, - } - tags.append(dev_tag) + try: + tag_stub = tag_services.TagServiceStub(client) + req = tag_services.TagStreamRequest(partial_eq_filter=[tag_models.Tag(creator_type=creator_type)]) + responses = tag_stub.GetAll(req) + for resp in responses: + dev_tag = { + "label": resp.value.key.label.value, + "value": resp.value.key.value.value, + } + tags.append(dev_tag) + except grpc.RpcError as err: + logger.error(f"Error when pulling Tags: {err}") return tags diff --git a/nautobot_ssot/tests/aristacv/test_utils_cloudvision.py b/nautobot_ssot/tests/aristacv/test_utils_cloudvision.py index dabef2a2..07fe49ab 100644 --- a/nautobot_ssot/tests/aristacv/test_utils_cloudvision.py +++ b/nautobot_ssot/tests/aristacv/test_utils_cloudvision.py @@ -129,7 +129,7 @@ def test_get_tags_by_type(self): device_tag_stub.TagServiceStub.return_value.GetAll.return_value = [mock_tag] with patch("nautobot_ssot.integrations.aristacv.utils.cloudvision.tag_services", device_tag_stub): - results = cloudvision.get_tags_by_type(client=self.client) + results = cloudvision.get_tags_by_type(client=self.client, logger=MagicMock()) expected = [{"label": "test", "value": "test"}] self.assertEqual(results, expected)