1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
18 from typing
import Dict
, List
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
25 log
= logging
.getLogger(__name__
)
28 class CertificateNotCreated(Exception):
32 class OpenStackCollector(VIMConnector
):
33 def __init__(self
, vim_account
: Dict
):
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
()
40 def _get_session(self
, creds
: Dict
):
42 project_domain_name
= "Default"
43 user_domain_name
= "Default"
46 vim_config
= creds
["config"]
47 if "insecure" in vim_config
and vim_config
["insecure"]:
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"]
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
,
66 return session
.Session(auth
=auth
, verify
=verify_ssl
, timeout
=10)
67 except CertificateNotCreated
as e
:
70 def _build_nova_client(self
) -> nova_client
.Client
:
71 return nova_client
.Client("2", session
=self
.vim_session
, timeout
=10)
73 def collect_servers_status(self
) -> List
[Dict
]:
74 log
.info("collect_servers_status")
76 for server
in self
.nova
.servers
.list(detailed
=True):
80 "status": (0 if (server
.status
== "ERROR") else 1),
85 def is_vim_ok(self
) -> bool:
87 self
.nova
.servers
.list()
89 except Exception as e
:
90 log
.warning("VIM status is not OK: %s" % e
)