From 985765e70747c8d3f32b17dca7596a20e4a0297b Mon Sep 17 00:00:00 2001 From: Benjamin Diaz Date: Wed, 26 Jun 2019 13:18:38 -0300 Subject: [PATCH] Adds support for custom project and user domain names defined in vim config bug 789 Change-Id: I77fe4a1f9fce4d1767560458de116f3431b35894 Signed-off-by: Benjamin Diaz --- .../infra_collectors/base_osinfra.py | 19 ++----- osm_mon/collector/infra_collectors/vmware.py | 8 +-- osm_mon/collector/service.py | 2 +- osm_mon/collector/utils/__init__.py | 23 +++++++++ .../{utils.py => utils/collector.py} | 0 osm_mon/collector/utils/openstack.py | 51 +++++++++++++++++++ osm_mon/collector/vnf_collectors/openstack.py | 24 ++------- osm_mon/collector/vnf_collectors/vio.py | 2 +- osm_mon/collector/vnf_collectors/vmware.py | 2 +- .../unit/collector/test_collector_service.py | 2 +- .../unit/collector/test_collector_utils.py | 2 +- 11 files changed, 91 insertions(+), 44 deletions(-) create mode 100644 osm_mon/collector/utils/__init__.py rename osm_mon/collector/{utils.py => utils/collector.py} (100%) create mode 100644 osm_mon/collector/utils/openstack.py diff --git a/osm_mon/collector/infra_collectors/base_osinfra.py b/osm_mon/collector/infra_collectors/base_osinfra.py index 19a21f0..b603d03 100644 --- a/osm_mon/collector/infra_collectors/base_osinfra.py +++ b/osm_mon/collector/infra_collectors/base_osinfra.py @@ -22,15 +22,13 @@ import logging from typing import List -from keystoneauth1 import session -from keystoneauth1.identity import v3 from keystoneclient.v3 import client as keystone_client from novaclient import client as nova_client from novaclient import v2 as nova_client_v2 from osm_mon.collector.infra_collectors.base_vim import BaseVimInfraCollector from osm_mon.collector.metric import Metric -from osm_mon.collector.utils import CollectorUtils +from osm_mon.collector.utils.openstack import OpenstackUtils from osm_mon.core.common_db import CommonDbClient from osm_mon.core.config import Config @@ -86,20 +84,9 @@ class BaseOpenStackInfraCollector(BaseVimInfraCollector): return False def _build_keystone_client(self, vim_account_id) -> keystone_client.Client: - sess = self._get_session(vim_account_id) + sess = OpenstackUtils.get_session(vim_account_id) return keystone_client.Client(session=sess) def _build_nova_client(self, vim_account_id) -> nova_client_v2.Client: - sess = self._get_session(vim_account_id) + sess = OpenstackUtils.get_session(vim_account_id) return nova_client.Client("2", session=sess) - - def _get_session(self, vim_account_id: str): - creds = CollectorUtils.get_credentials(vim_account_id) - verify_ssl = CollectorUtils.is_verify_ssl(creds) - auth = v3.Password(auth_url=creds.url, - username=creds.user, - password=creds.password, - project_name=creds.tenant_name, - project_domain_id='default', - user_domain_id='default') - return session.Session(auth=auth, verify=verify_ssl) diff --git a/osm_mon/collector/infra_collectors/vmware.py b/osm_mon/collector/infra_collectors/vmware.py index 206083a..43be2b2 100644 --- a/osm_mon/collector/infra_collectors/vmware.py +++ b/osm_mon/collector/infra_collectors/vmware.py @@ -21,20 +21,20 @@ # contact: osslegalrouting@vmware.com ## +import json 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.collector.utils.collector import CollectorUtils 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 = '27.0' diff --git a/osm_mon/collector/service.py b/osm_mon/collector/service.py index 98f3f16..cc4fc1b 100644 --- a/osm_mon/collector/service.py +++ b/osm_mon/collector/service.py @@ -7,7 +7,7 @@ from osm_mon.collector.infra_collectors.openstack import OpenstackInfraCollector from osm_mon.collector.infra_collectors.vmware import VMwareInfraCollector from osm_mon.collector.infra_collectors.vio import VIOInfraCollector from osm_mon.collector.metric import Metric -from osm_mon.collector.utils import CollectorUtils +from osm_mon.collector.utils.collector import CollectorUtils from osm_mon.collector.vnf_collectors.juju import VCACollector from osm_mon.collector.vnf_collectors.openstack import OpenstackCollector from osm_mon.collector.vnf_collectors.vio import VIOCollector diff --git a/osm_mon/collector/utils/__init__.py b/osm_mon/collector/utils/__init__.py new file mode 100644 index 0000000..cbff444 --- /dev/null +++ b/osm_mon/collector/utils/__init__.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- + +# Copyright 2019 Whitestack, LLC +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Whitestack, LLC + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: bdiaz@whitestack.com or glavado@whitestack.com +## diff --git a/osm_mon/collector/utils.py b/osm_mon/collector/utils/collector.py similarity index 100% rename from osm_mon/collector/utils.py rename to osm_mon/collector/utils/collector.py diff --git a/osm_mon/collector/utils/openstack.py b/osm_mon/collector/utils/openstack.py new file mode 100644 index 0000000..c77ac46 --- /dev/null +++ b/osm_mon/collector/utils/openstack.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- + +# Copyright 2019 Whitestack, LLC +# ************************************************************* + +# This file is part of OSM Monitoring module +# All Rights Reserved to Whitestack, LLC + +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# For those usages not covered by the Apache License, Version 2.0 please +# contact: bdiaz@whitestack.com or glavado@whitestack.com +## +import json + +from keystoneauth1 import session +from keystoneauth1.identity import v3 + +from osm_mon.collector.utils.collector import CollectorUtils + + +class OpenstackUtils: + + @staticmethod + def get_session(vim_account_id: str): + creds = CollectorUtils.get_credentials(vim_account_id) + verify_ssl = CollectorUtils.is_verify_ssl(creds) + vim_config = json.loads(creds.config) + project_domain_name = 'Default' + user_domain_name = 'Default' + if 'project_domain_name' in vim_config: + project_domain_name = vim_config['project_domain_name'] + if 'user_domain_name' in vim_config: + user_domain_name = vim_config['user_domain_name'] + auth = v3.Password(auth_url=creds.url, + username=creds.user, + password=creds.password, + project_name=creds.tenant_name, + project_domain_name=project_domain_name, + user_domain_name=user_domain_name) + return session.Session(auth=auth, verify=verify_ssl) diff --git a/osm_mon/collector/vnf_collectors/openstack.py b/osm_mon/collector/vnf_collectors/openstack.py index b2ecef0..0215b51 100644 --- a/osm_mon/collector/vnf_collectors/openstack.py +++ b/osm_mon/collector/vnf_collectors/openstack.py @@ -26,14 +26,12 @@ from typing import List import gnocchiclient.exceptions from ceilometerclient.v2 import client as ceilometer_client from gnocchiclient.v1 import client as gnocchi_client -from keystoneauth1 import session from keystoneauth1.exceptions.catalog import EndpointNotFound -from keystoneauth1.identity import v3 from keystoneclient.v3 import client as keystone_client from neutronclient.v2_0 import client as neutron_client from osm_mon.collector.metric import Metric -from osm_mon.collector.utils import CollectorUtils +from osm_mon.collector.utils.openstack import OpenstackUtils from osm_mon.collector.vnf_collectors.base_vim import BaseVimCollector from osm_mon.collector.vnf_metric import VnfMetric from osm_mon.core.common_db import CommonDbClient @@ -71,7 +69,7 @@ class OpenstackCollector(BaseVimCollector): self.backend = self._get_backend(vim_account_id) def _build_keystone_client(self, vim_account_id: str) -> keystone_client.Client: - sess = OpenstackBackend.get_session(vim_account_id) + sess = OpenstackUtils.get_session(vim_account_id) return keystone_client.Client(session=sess) def _get_resource_uuid(self, nsr_id: str, vnf_member_index: str, vdur_name: str) -> str: @@ -140,18 +138,6 @@ class OpenstackBackend: def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str): pass - @staticmethod - def get_session(vim_account_id: str): - creds = CollectorUtils.get_credentials(vim_account_id) - verify_ssl = CollectorUtils.is_verify_ssl(creds) - auth = v3.Password(auth_url=creds.url, - username=creds.user, - password=creds.password, - project_name=creds.tenant_name, - project_domain_id='default', - user_domain_id='default') - return session.Session(auth=auth, verify=verify_ssl) - class GnocchiBackend(OpenstackBackend): @@ -160,11 +146,11 @@ class GnocchiBackend(OpenstackBackend): self.neutron = self._build_neutron_client(vim_account_id) def _build_gnocchi_client(self, vim_account_id: str) -> gnocchi_client.Client: - sess = OpenstackBackend.get_session(vim_account_id) + sess = OpenstackUtils.get_session(vim_account_id) return gnocchi_client.Client(session=sess) def _build_neutron_client(self, vim_account_id: str) -> neutron_client.Client: - sess = OpenstackBackend.get_session(vim_account_id) + sess = OpenstackUtils.get_session(vim_account_id) return neutron_client.Client(session=sess) def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str): @@ -233,7 +219,7 @@ class CeilometerBackend(OpenstackBackend): self.client = self._build_ceilometer_client(vim_account_id) def _build_ceilometer_client(self, vim_account_id: str) -> ceilometer_client.Client: - sess = OpenstackBackend.get_session(vim_account_id) + sess = OpenstackUtils.get_session(vim_account_id) return ceilometer_client.Client(session=sess) def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str): diff --git a/osm_mon/collector/vnf_collectors/vio.py b/osm_mon/collector/vnf_collectors/vio.py index 88d3378..0d481ea 100644 --- a/osm_mon/collector/vnf_collectors/vio.py +++ b/osm_mon/collector/vnf_collectors/vio.py @@ -33,7 +33,7 @@ from keystoneauth1 import session from keystoneauth1.identity import v3 from novaclient import client as nClient -from osm_mon.collector.utils import CollectorUtils +from osm_mon.collector.utils.collector import CollectorUtils from osm_mon.collector.vnf_collectors.base_vim import BaseVimCollector from osm_mon.collector.vnf_metric import VnfMetric from osm_mon.core.common_db import CommonDbClient diff --git a/osm_mon/collector/vnf_collectors/vmware.py b/osm_mon/collector/vnf_collectors/vmware.py index 438c40c..be34013 100644 --- a/osm_mon/collector/vnf_collectors/vmware.py +++ b/osm_mon/collector/vnf_collectors/vmware.py @@ -32,7 +32,7 @@ import six from pyvcloud.vcd.client import BasicLoginCredentials from pyvcloud.vcd.client import Client -from osm_mon.collector.utils import CollectorUtils +from osm_mon.collector.utils.collector import CollectorUtils from osm_mon.collector.vnf_collectors.base_vim import BaseVimCollector from osm_mon.collector.vnf_metric import VnfMetric from osm_mon.core.common_db import CommonDbClient diff --git a/osm_mon/tests/unit/collector/test_collector_service.py b/osm_mon/tests/unit/collector/test_collector_service.py index 4dab43f..22d4410 100644 --- a/osm_mon/tests/unit/collector/test_collector_service.py +++ b/osm_mon/tests/unit/collector/test_collector_service.py @@ -23,7 +23,7 @@ from unittest import TestCase, mock from osm_mon.collector.service import CollectorService -from osm_mon.collector.utils import CollectorUtils +from osm_mon.collector.utils.collector import CollectorUtils from osm_mon.collector.vnf_collectors.openstack import OpenstackCollector from osm_mon.core.common_db import CommonDbClient from osm_mon.core.config import Config diff --git a/osm_mon/tests/unit/collector/test_collector_utils.py b/osm_mon/tests/unit/collector/test_collector_utils.py index a308522..e9f3848 100644 --- a/osm_mon/tests/unit/collector/test_collector_utils.py +++ b/osm_mon/tests/unit/collector/test_collector_utils.py @@ -22,7 +22,7 @@ ## from unittest import TestCase, mock -from osm_mon.collector.utils import CollectorUtils +from osm_mon.collector.utils.collector import CollectorUtils from osm_mon.core.config import Config from osm_mon.core.database import VimCredentialsRepository, VimCredentials -- 2.25.1