Collect null project_ids as empty strings
[osm/MON.git] / osm_mon / collector / infra_collectors / vmware.py
index eba6ff6..6accd65 100644 (file)
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 
 ##
-# Copyright 2016-2017 VMware Inc.
+# Copyright 2016-2019 VMware Inc.
 # This file is part of ETSI OSM
 # All Rights Reserved.
 #
 
 import logging
 from typing import List
-
 from xml.etree import ElementTree as XmlElementTree
+
+import requests
 from pyvcloud.vcd.client import BasicLoginCredentials
 from pyvcloud.vcd.client import Client
 
-from osm_mon.collector.utils import CollectorUtils
 from osm_mon.collector.infra_collectors.base_vim import BaseVimInfraCollector
 from osm_mon.collector.metric import Metric
 from osm_mon.core.common_db import CommonDbClient
 from osm_mon.core.config import Config
-import requests
-import json
 
 log = logging.getLogger(__name__)
-API_VERSION = '30.0'
+API_VERSION = '27.0'
 
 
 class VMwareInfraCollector(BaseVimInfraCollector):
+
     def __init__(self, config: Config, vim_account_id: str):
         super().__init__(config, vim_account_id)
         self.vim_account_id = vim_account_id
@@ -51,6 +50,7 @@ class VMwareInfraCollector(BaseVimInfraCollector):
         self.admin_password = vim_account['admin_password']
         self.vim_uuid = vim_account['vim_uuid']
         self.org_name = vim_account['orgname']
+        self.vim_project_id = vim_account['project_id']
 
     def connect_vim_as_admin(self):
         """ Method connect as pvdc admin user to vCloud director.
@@ -70,6 +70,7 @@ class VMwareInfraCollector(BaseVimInfraCollector):
             admin_passwd = self.admin_password
             org = 'System'
             client = Client(host, verify_ssl_certs=False)
+            client.set_highest_supported_version()
             client.set_credentials(BasicLoginCredentials(admin_user, org,
                                                          admin_passwd))
             return client
@@ -84,17 +85,20 @@ class VMwareInfraCollector(BaseVimInfraCollector):
            return - dict with vim account details
         """
         vim_account = {}
-        vim_account_info = CollectorUtils.get_credentials(vim_account_id)
-
-        vim_account['name'] = vim_account_info.name
-        vim_account['vim_tenant_name'] = vim_account_info.tenant_name
-        vim_account['vim_type'] = vim_account_info.type
-        vim_account['vim_url'] = vim_account_info.url
-        vim_account['org_user'] = vim_account_info.user
-        vim_account['org_password'] = vim_account_info.password
-        vim_account['vim_uuid'] = vim_account_info.uuid
-
-        vim_config = json.loads(vim_account_info.config)
+        vim_account_info = self.common_db.get_vim_account(vim_account_id)
+
+        vim_account['name'] = vim_account_info['name']
+        vim_account['vim_tenant_name'] = vim_account_info['vim_tenant_name']
+        vim_account['vim_type'] = vim_account_info['vim_type']
+        vim_account['vim_url'] = vim_account_info['vim_url']
+        vim_account['org_user'] = vim_account_info['vim_user']
+        vim_account['vim_uuid'] = vim_account_info['_id']
+        if vim_account_info['_admin']['projects_read']:
+            vim_account['project_id'] = vim_account_info['_admin']['projects_read'][0]
+        else:
+            vim_account['project_id'] = ''
+
+        vim_config = vim_account_info['config']
         vim_account['admin_username'] = vim_config['admin_username']
         vim_account['admin_password'] = vim_config['admin_password']
 
@@ -167,20 +171,33 @@ class VMwareInfraCollector(BaseVimInfraCollector):
     def collect(self) -> List[Metric]:
         metrics = []
         vim_status = self.check_vim_status()
-        vim_status_metric = Metric({'vim_account_id': self.vim_account_id}, 'vim_status', vim_status)
+        vim_account_id = self.vim_account_id
+        vim_project_id = self.vim_project_id
+        vim_tags = {
+            'vim_account_id': vim_account_id,
+            'project_id': vim_project_id
+        }
+        vim_status_metric = Metric(vim_tags, 'vim_status', vim_status)
         metrics.append(vim_status_metric)
-        vnfrs = self.common_db.get_vnfrs(vim_account_id=self.vim_account_id)
+        vnfrs = self.common_db.get_vnfrs(vim_account_id=vim_account_id)
         for vnfr in vnfrs:
             nsr_id = vnfr['nsr-id-ref']
+            ns_name = self.common_db.get_nsr(nsr_id)['name']
             vnf_member_index = vnfr['member-vnf-index-ref']
+            if vnfr['_admin']['projects_read']:
+                vnfr_project_id = vnfr['_admin']['projects_read'][0]
+            else:
+                vnfr_project_id = ''
             for vdur in vnfr['vdur']:
                 resource_uuid = vdur['vim-id']
                 tags = {
                     'vim_account_id': self.vim_account_id,
                     'resource_uuid': resource_uuid,
                     'nsr_id': nsr_id,
+                    'ns_name': ns_name,
                     'vnf_member_index': vnf_member_index,
-                    'vdur_name': vdur['name']
+                    'vdur_name': vdur['name'],
+                    'project_id': vnfr_project_id
                 }
                 try:
                     vm_list = self.check_vm_status(resource_uuid)