From e07fe474aacc70c26874de0bb78b7e80f26b32b4 Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Fri, 12 Jul 2024 13:41:01 +0200 Subject: [PATCH 1/3] try to get image name --- fedcloud_vm_monitoring/site_monitor.py | 41 +++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/fedcloud_vm_monitoring/site_monitor.py b/fedcloud_vm_monitoring/site_monitor.py index b9346ca..7cc374b 100644 --- a/fedcloud_vm_monitoring/site_monitor.py +++ b/fedcloud_vm_monitoring/site_monitor.py @@ -81,8 +81,46 @@ def get_flavor(self, flavor_name): return {} return self.flavors[flavor_name] + def get_vm_image_volume_show(self, volume_id): + try: + cmd = ("volume", "show", volume_id, "--format", "json") + result = self._run_command(cmd) + if ('volume_image_metadata' in result) and \ + ('sl:osname' and 'sl:osversion' in result['volume_image_metadata']): + return result['volume_image_metadata']['sl:osname'] + \ + result['volume_image_metadata']['sl:osversion'] + else: + return "image name not found" + except SiteMonitorException as err: + return "image name not found" + + def get_vm_image_server_show(self, vm_id): + try: + cmd = ("server", "show", vm_id, "--format", "json") + result = self._run_command(cmd) + if len(result['attached_volumes']) > 0: + return self.get_vm_image_volume_show(result['attached_volumes'][0]['id']) + else: + return "image name not found" + except SiteMonitorException as err: + return "image name not found" + + def get_vm_image(self, vm_id, image_name, image_id): + if len(image_name) > 0: + return image_name + else: + try: + cmd = ("image", "show", image_id, "--format", "json") + result = self._run_command(cmd) + if 'sl:osname' and 'sl:osversion' in result['properties']: + return result['properties']['sl:osname'] + result['properties']['sl:osversion'] + else: + return self.get_vm_image_server_show(vm_id) + except SiteMonitorException as err: + return self.get_vm_image_server_show(vm_id) + def get_vms(self): - command = ("server", "list") + command = ("server", "list", "--long") return self._run_command(command) def get_vm(self, vm): @@ -146,6 +184,7 @@ def process_vm(self, vm): f"GB of RAM and {flv['Disk']} GB of local disk", ) ) + output.append(("image: ", self.get_vm_image(vm["ID"], vm["Image Name"], vm["Image ID"]))) output.append(("created at", vm_info["created_at"])) output.append(("elapsed time", elapsed)) user_id = vm_info["user_id"] From 03fd1cc7050a45fb3a808f1daec54ae5f26bca37 Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Fri, 12 Jul 2024 13:59:39 +0200 Subject: [PATCH 2/3] linting --- fedcloud_vm_monitoring/site_monitor.py | 34 +++++++++++++++++--------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/fedcloud_vm_monitoring/site_monitor.py b/fedcloud_vm_monitoring/site_monitor.py index 7cc374b..614f16b 100644 --- a/fedcloud_vm_monitoring/site_monitor.py +++ b/fedcloud_vm_monitoring/site_monitor.py @@ -85,24 +85,29 @@ def get_vm_image_volume_show(self, volume_id): try: cmd = ("volume", "show", volume_id, "--format", "json") result = self._run_command(cmd) - if ('volume_image_metadata' in result) and \ - ('sl:osname' and 'sl:osversion' in result['volume_image_metadata']): - return result['volume_image_metadata']['sl:osname'] + \ - result['volume_image_metadata']['sl:osversion'] + if ("volume_image_metadata" in result) and ( + "sl:osname" and "sl:osversion" in result["volume_image_metadata"] + ): + return ( + result["volume_image_metadata"]["sl:osname"] + + result["volume_image_metadata"]["sl:osversion"] + ) else: return "image name not found" - except SiteMonitorException as err: + except SiteMonitorException: return "image name not found" def get_vm_image_server_show(self, vm_id): try: cmd = ("server", "show", vm_id, "--format", "json") result = self._run_command(cmd) - if len(result['attached_volumes']) > 0: - return self.get_vm_image_volume_show(result['attached_volumes'][0]['id']) + if len(result["attached_volumes"]) > 0: + return self.get_vm_image_volume_show( + result["attached_volumes"][0]["id"] + ) else: return "image name not found" - except SiteMonitorException as err: + except SiteMonitorException: return "image name not found" def get_vm_image(self, vm_id, image_name, image_id): @@ -112,11 +117,14 @@ def get_vm_image(self, vm_id, image_name, image_id): try: cmd = ("image", "show", image_id, "--format", "json") result = self._run_command(cmd) - if 'sl:osname' and 'sl:osversion' in result['properties']: - return result['properties']['sl:osname'] + result['properties']['sl:osversion'] + if "sl:osname" and "sl:osversion" in result["properties"]: + return ( + result["properties"]["sl:osname"] + + result["properties"]["sl:osversion"] + ) else: return self.get_vm_image_server_show(vm_id) - except SiteMonitorException as err: + except SiteMonitorException: return self.get_vm_image_server_show(vm_id) def get_vms(self): @@ -184,7 +192,9 @@ def process_vm(self, vm): f"GB of RAM and {flv['Disk']} GB of local disk", ) ) - output.append(("image: ", self.get_vm_image(vm["ID"], vm["Image Name"], vm["Image ID"]))) + output.append( + ("image: ", self.get_vm_image(vm["ID"], vm["Image Name"], vm["Image ID"])) + ) output.append(("created at", vm_info["created_at"])) output.append(("elapsed time", elapsed)) user_id = vm_info["user_id"] From 5bb675280e6c79db7eb31104aaba2f4fb3f60544 Mon Sep 17 00:00:00 2001 From: Sebastian Luna-Valero Date: Fri, 12 Jul 2024 15:36:35 +0200 Subject: [PATCH 3/3] change field --- fedcloud_vm_monitoring/site_monitor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fedcloud_vm_monitoring/site_monitor.py b/fedcloud_vm_monitoring/site_monitor.py index 614f16b..5f221b2 100644 --- a/fedcloud_vm_monitoring/site_monitor.py +++ b/fedcloud_vm_monitoring/site_monitor.py @@ -193,7 +193,7 @@ def process_vm(self, vm): ) ) output.append( - ("image: ", self.get_vm_image(vm["ID"], vm["Image Name"], vm["Image ID"])) + ("VM image", self.get_vm_image(vm["ID"], vm["Image Name"], vm["Image ID"])) ) output.append(("created at", vm_info["created_at"])) output.append(("elapsed time", elapsed))