Feature-5950: Management of quotas in VIM Account 01/10801/4
authorvejjupallisi <karunyasri.v@tataelxsi.co.in>
Fri, 14 May 2021 12:43:37 +0000 (12:43 +0000)
committerpalsus <subhankar.pal@aricent.com>
Thu, 10 Jun 2021 14:07:00 +0000 (16:07 +0200)
Adding code support for Openstack quota resources in vim_accounts

Change-Id: I7270ec6b449eec8a178335bf929f71e908325bcd
Signed-off-by: vejjupallisi <karunyasri.v@tataelxsi.co.in>
osm_mon/collector/infra_collectors/base_osinfra.py
osm_mon/core/common_db.py
requirements.in
requirements.txt

index 61e9e42..74ad095 100644 (file)
@@ -24,6 +24,8 @@ from typing import List
 
 from keystoneclient.v3 import client as keystone_client
 from novaclient import client as nova_client
+from cinderclient import client as cinder_client
+from neutronclient.neutron import client as neutron_client
 
 from osm_mon.collector.infra_collectors.base_vim import BaseVimInfraCollector
 from osm_mon.collector.metric import Metric
@@ -42,10 +44,15 @@ class BaseOpenStackInfraCollector(BaseVimInfraCollector):
         self.vim_account = self.common_db.get_vim_account(vim_account_id)
         self.keystone = self._build_keystone_client(self.vim_account)
         self.nova = self._build_nova_client(self.vim_account)
+        self.cinder = self._build_cinder_client(self.vim_account)
+        self.neutron, self.tenant_id = self._build_neutron_client(self.vim_account)
 
     def collect(self) -> List[Metric]:
         metrics = []
         vim_status = self.is_vim_ok()
+        if vim_status:
+            # Updating the resources in mongoDB
+            self.update_resources()
         if self.vim_account["_admin"]["projects_read"]:
             vim_project_id = self.vim_account["_admin"]["projects_read"][0]
         else:
@@ -98,6 +105,72 @@ class BaseOpenStackInfraCollector(BaseVimInfraCollector):
             log.warning("VIM status is not OK: %s" % e)
             return False
 
+    def update_resources(self):
+        if "resources" in self.vim_account:
+            vimacc_resources = self.vim_account["resources"]
+            # Compute resources
+            try:
+                com_lim = self.nova.limits.get()._info['absolute']
+                if ("compute" in vimacc_resources) \
+                   and ((vimacc_resources["compute"]["ram"]["total"] != com_lim['maxTotalRAMSize'])
+                   or (vimacc_resources["compute"]["vcpus"]["total"] != com_lim['maxTotalCores'])
+                   or (vimacc_resources["compute"]["ram"]["used"] != com_lim['totalRAMUsed'])
+                   or (vimacc_resources["compute"]["vcpus"]["used"] != com_lim['totalCoresUsed'])
+                   or (vimacc_resources["compute"]["instances"]["total"] != com_lim['maxTotalInstances'])
+                   or (vimacc_resources["compute"]["instances"]["used"] != com_lim['totalInstancesUsed'])):
+                    update_dict = {"resources.compute": {"ram": {"total": com_lim['maxTotalRAMSize'],
+                                                                 "used": com_lim['totalRAMUsed']},
+                                                         "vcpus": {"total": com_lim['maxTotalCores'],
+                                                                   "used": com_lim['totalCoresUsed']},
+                                                         "instances": {"total": com_lim['maxTotalInstances'],
+                                                                       "used": com_lim['totalInstancesUsed']}}}
+                    suc_value = self.common_db.set_vim_account(str(self.vim_account['_id']), update_dict)
+                    log.info("Compute resources update in mongoDB  = %s" % suc_value)
+            except Exception as e:
+                log.warning("Error in updating compute resources: %s" % e)
+
+            # Volume resources
+            try:
+                vol_lim = self.cinder.limits.get()._info['absolute']
+                if ("storage" in vimacc_resources) and\
+                   ((vimacc_resources["storage"]["volumes"]["total"] != vol_lim['maxTotalVolumes'])
+                   or (vimacc_resources["storage"]["snapshots"]["total"] != vol_lim['maxTotalSnapshots'])
+                   or (vimacc_resources["storage"]["volumes"]["used"] != vol_lim['totalVolumesUsed'])
+                   or (vimacc_resources["storage"]["snapshots"]["used"] != vol_lim['totalSnapshotsUsed'])
+                   or (vimacc_resources["storage"]["storage"]["total"] != vol_lim['maxTotalVolumeGigabytes'])
+                   or (vimacc_resources["storage"]["storage"]["used"] != vol_lim['totalGigabytesUsed'])):
+                    update_dict = {"resources.storage": {"volumes": {"total": vol_lim['maxTotalVolumes'],
+                                                                     "used": vol_lim['totalVolumesUsed']},
+                                                         "snapshots": {"total": vol_lim['maxTotalSnapshots'],
+                                                                       "used": vol_lim['totalSnapshotsUsed']},
+                                                         "storage": {"total": vol_lim['maxTotalVolumeGigabytes'],
+                                                                     "used": vol_lim['totalGigabytesUsed']}}}
+                    suc_value = self.common_db.set_vim_account(str(self.vim_account['_id']), update_dict)
+                    log.info("Volume resources update in mongoDB = %s" % suc_value)
+            except Exception as e:
+                log.warning("Error in updating volume resources: %s" % e)
+
+            # Network resources
+            try:
+                net_lim = self.neutron.show_quota_details(self.tenant_id)["quota"]
+                if ("network" in vimacc_resources) and\
+                   ((vimacc_resources["network"]["networks"]["total"] != net_lim["network"]["limit"])
+                   or (vimacc_resources["network"]["networks"]["used"] != net_lim['network']['used'])
+                   or (vimacc_resources["network"]["subnets"]["total"] != net_lim['subnet']['limit'])
+                   or (vimacc_resources["network"]["subnets"]["used"] != net_lim['subnet']['used'])
+                   or (vimacc_resources["network"]["floating_ips"]["total"] != net_lim['floatingip']['limit'])
+                   or (vimacc_resources["network"]["floating_ips"]["used"] != net_lim['floatingip']['used'])):
+                    update_dict = {"resources.network": {"networks": {"total": net_lim['network']['limit'],
+                                                                      "used": net_lim['network']['used']},
+                                                         "subnets": {"total": net_lim['subnet']['limit'],
+                                                                     "used": net_lim['subnet']['used']},
+                                                         "floating_ips": {"total": net_lim['floatingip']['limit'],
+                                                                          "used": net_lim['floatingip']['used']}}}
+                    suc_value = self.common_db.set_vim_account(str(self.vim_account['_id']), update_dict)
+                    log.info("Network resources update in mongoDB = %s" % suc_value)
+            except Exception as e:
+                log.warning("Error in updating network resources: %s" % e)
+
     def _build_keystone_client(self, vim_account: dict) -> keystone_client.Client:
         sess = OpenstackUtils.get_session(vim_account)
         return keystone_client.Client(session=sess, timeout=10)
@@ -105,3 +178,12 @@ class BaseOpenStackInfraCollector(BaseVimInfraCollector):
     def _build_nova_client(self, vim_account: dict) -> nova_client.Client:
         sess = OpenstackUtils.get_session(vim_account)
         return nova_client.Client("2", session=sess, timeout=10)
+
+    def _build_cinder_client(self, vim_account: dict) -> cinder_client.Client:
+        sess = OpenstackUtils.get_session(vim_account)
+        return cinder_client.Client("2", session=sess, timeout=10)
+
+    def _build_neutron_client(self, vim_account: dict) -> tuple:
+        sess = OpenstackUtils.get_session(vim_account)
+        tenant_id = sess.get_project_id()
+        return neutron_client.Client("2", session=sess, timeout=10), tenant_id
index cf52d85..1e78bc8 100644 (file)
@@ -144,6 +144,15 @@ class CommonDbClient:
                     )
         return vim_account
 
+    def set_vim_account(self, vim_account_id: str, update_dict: dict) -> bool:
+        try:
+            # Set vim_account resources in mongo
+            self.common_db.set_one('vim_accounts', {"_id": vim_account_id}, update_dict)
+            # self.common_db.set_one('vim_accounts', {"name": "test-vim"}, update_dict)
+            return True
+        except Exception:
+            return False
+
     def get_sdncs(self):
         return self.common_db.get_list("sdns")
 
index 51603ff..a1a0ce8 100644 (file)
@@ -27,6 +27,7 @@ python-ceilometerclient
 python-keystoneclient
 python-neutronclient
 python-novaclient
+python-cinderclient
 pyvcloud==23.0.*
 pyyaml
 requests
index 22bf875..fdc1b13 100644 (file)
@@ -194,6 +194,8 @@ python-neutronclient==7.3.0
     # via -r requirements.in
 python-novaclient==17.4.0
     # via -r requirements.in
+python-cinderclient==7.4.0
+    # via -r requirements.in
 pytz==2021.1
     # via
     #   oslo.serialization