5f62edf340f33a7df8d46be8535470da20d826b1
[osm/MON.git] / osm_mon / collector / infra_collectors / openstack.py
1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: bdiaz@whitestack.com or glavado@whitestack.com
21 ##
22 import logging
23
24 from keystoneauth1 import session
25 from keystoneauth1.identity import v3
26 from keystoneclient.v3 import client
27
28 from osm_mon.collector.infra_collectors.base_vim import BaseVimInfraCollector
29 from osm_mon.core.auth import AuthManager
30 from osm_mon.core.config import Config
31
32 log = logging.getLogger(__name__)
33
34
35 class OpenstackInfraCollector(BaseVimInfraCollector):
36 def __init__(self, config: Config, vim_account_id: str):
37 super().__init__(config, vim_account_id)
38 self.auth_manager = AuthManager(config)
39 self.keystone_client = self._build_keystone_client(vim_account_id)
40
41 def is_vim_ok(self) -> bool:
42 try:
43 self.keystone_client.projects.list()
44 return True
45 except Exception:
46 log.exception("VIM status is not OK!")
47 return False
48
49 def _build_keystone_client(self, vim_account_id):
50 creds = self.auth_manager.get_credentials(vim_account_id)
51 verify_ssl = self.auth_manager.is_verify_ssl(vim_account_id)
52 auth = v3.Password(auth_url=creds.url,
53 username=creds.user,
54 password=creds.password,
55 project_name=creds.tenant_name,
56 project_domain_id='default',
57 user_domain_id='default')
58 sess = session.Session(auth=auth, verify=verify_ssl)
59 return client.Client(session=sess)