From: Benjamin Diaz Date: Wed, 26 Jun 2019 16:18:38 +0000 (-0300) Subject: Adds support for custom project and user domain names defined in vim config X-Git-Tag: v6.0.2rc1~7 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F51%2F7751%2F1;p=osm%2FMON.git Adds support for custom project and user domain names defined in vim config bug 789 Change-Id: I77fe4a1f9fce4d1767560458de116f3431b35894 Signed-off-by: Benjamin Diaz (cherry picked from commit 985765e70747c8d3f32b17dca7596a20e4a0297b) --- 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.py b/osm_mon/collector/utils.py deleted file mode 100644 index 272426f..0000000 --- a/osm_mon/collector/utils.py +++ /dev/null @@ -1,53 +0,0 @@ -# Copyright 2018 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 osm_mon.collector.metric import Metric -from osm_mon.core import database -from osm_mon.core.database import VimCredentials, VimCredentialsRepository - - -class CollectorUtils(Metric): - - @staticmethod - def get_vim_type(vim_account_id) -> str: - credentials = CollectorUtils.get_credentials(vim_account_id) - config = json.loads(credentials.config) - if 'vim_type' in config: - vim_type = config['vim_type'] - return str(vim_type.lower()) - else: - return str(credentials.type) - - @staticmethod - def get_credentials(vim_account_id) -> VimCredentials: - database.db.connect() - try: - with database.db.atomic(): - return VimCredentialsRepository.get(VimCredentials.uuid == vim_account_id) - finally: - database.db.close() - - @staticmethod - def is_verify_ssl(vim_credentials: VimCredentials): - vim_config = json.loads(vim_credentials.config) - return 'insecure' not in vim_config or vim_config['insecure'] is False 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/collector.py b/osm_mon/collector/utils/collector.py new file mode 100644 index 0000000..272426f --- /dev/null +++ b/osm_mon/collector/utils/collector.py @@ -0,0 +1,53 @@ +# Copyright 2018 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 osm_mon.collector.metric import Metric +from osm_mon.core import database +from osm_mon.core.database import VimCredentials, VimCredentialsRepository + + +class CollectorUtils(Metric): + + @staticmethod + def get_vim_type(vim_account_id) -> str: + credentials = CollectorUtils.get_credentials(vim_account_id) + config = json.loads(credentials.config) + if 'vim_type' in config: + vim_type = config['vim_type'] + return str(vim_type.lower()) + else: + return str(credentials.type) + + @staticmethod + def get_credentials(vim_account_id) -> VimCredentials: + database.db.connect() + try: + with database.db.atomic(): + return VimCredentialsRepository.get(VimCredentials.uuid == vim_account_id) + finally: + database.db.close() + + @staticmethod + def is_verify_ssl(vim_credentials: VimCredentials): + vim_config = json.loads(vim_credentials.config) + return 'insecure' not in vim_config or vim_config['insecure'] is False 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 00ef9ef..720daf1 100644 --- a/osm_mon/collector/vnf_collectors/vmware.py +++ b/osm_mon/collector/vnf_collectors/vmware.py @@ -31,7 +31,7 @@ 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.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