d37973d0544438e1f4086829f60efdb8ed868009
[osm/NG-SA.git] / src / osm_ngsa / osm_mon / vim_connectors / openstack.py
1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
17 import logging
18 from typing import Dict, List
19
20 from keystoneauth1 import session
21 from keystoneauth1.identity import v3
22 from novaclient import client as nova_client
23 from osm_mon.vim_connectors.base_vim import VIMConnector
24
25 log = logging.getLogger(__name__)
26
27
28 class CertificateNotCreated(Exception):
29 pass
30
31
32 class OpenStackCollector(VIMConnector):
33 def __init__(self, vim_account: Dict):
34 log.info("__init__")
35 self.vim_account = vim_account
36 self.vim_session = None
37 self.vim_session = self._get_session(vim_account)
38 self.nova = self._build_nova_client()
39
40 def _get_session(self, creds: Dict):
41 verify_ssl = True
42 project_domain_name = "Default"
43 user_domain_name = "Default"
44 try:
45 if "config" in creds:
46 vim_config = creds["config"]
47 if "insecure" in vim_config and vim_config["insecure"]:
48 verify_ssl = False
49 if "ca_cert" in vim_config:
50 verify_ssl = vim_config["ca_cert"]
51 elif "ca_cert_content" in vim_config:
52 # vim_config = self._create_file_cert(vim_config, creds["_id"])
53 verify_ssl = vim_config["ca_cert"]
54 if "project_domain_name" in vim_config:
55 project_domain_name = vim_config["project_domain_name"]
56 if "user_domain_name" in vim_config:
57 user_domain_name = vim_config["user_domain_name"]
58 auth = v3.Password(
59 auth_url=creds["vim_url"],
60 username=creds["vim_user"],
61 password=creds["vim_password"],
62 project_name=creds["vim_tenant_name"],
63 project_domain_name=project_domain_name,
64 user_domain_name=user_domain_name,
65 )
66 return session.Session(auth=auth, verify=verify_ssl, timeout=10)
67 except CertificateNotCreated as e:
68 log.error(e)
69
70 def _build_nova_client(self) -> nova_client.Client:
71 return nova_client.Client("2", session=self.vim_session, timeout=10)
72
73 def collect_servers_status(self) -> List[Dict]:
74 log.info("collect_servers_status")
75 servers = []
76 for server in self.nova.servers.list(detailed=True):
77 vm = {
78 "id": server.id,
79 "name": server.name,
80 "status": (0 if (server.status == "ERROR") else 1),
81 }
82 servers.append(vm)
83 return servers
84
85 def is_vim_ok(self) -> bool:
86 try:
87 self.nova.servers.list()
88 return True
89 except Exception as e:
90 log.warning("VIM status is not OK: %s" % e)
91 return False