1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from doctor_tests.identity_auth import get_identity_auth
10 from doctor_tests.identity_auth import get_session
11 from doctor_tests.os_clients import congress_client
13 from doctor_tests.inspector.base import BaseInspector
16 class CongressInspector(BaseInspector):
17 nova_api_min_version = '2.11'
18 doctor_driver = 'doctor'
19 doctor_datasource = 'doctor'
20 policy = 'classification'
23 'host_down(host) :- doctor:events(hostname=host, type="compute.host.down", status="down")', # noqa
24 'active_instance_in_host':
25 'active_instance_in_host(vmid, host) :- nova:servers(id=vmid, host_name=host, status="ACTIVE")', # noqa
27 'execute[nova:services.force_down(host, "nova-compute", "True")] :- host_down(host)', # noqa
29 'execute[nova:servers.reset_state(vmid, "error")] :- host_down(host), active_instance_in_host(vmid, host)' # noqa
32 def __init__(self, conf, log):
33 super(CongressInspector, self).__init__(conf, log)
34 self.is_create_doctor_datasource = False
35 self.doctor_datasource_id = None
36 self.auth = get_identity_auth()
37 self.congress = congress_client(get_session(auth=self.auth))
38 self._init_driver_and_ds()
39 self.inspector_url = self.get_inspector_url()
41 def _init_driver_and_ds(self):
43 {ds['name']: ds for ds in
44 self.congress.list_datasources()['results']}
46 # check nova_api version
47 nova_api_version = datasources['nova']['config'].get('api_version')
48 if nova_api_version and nova_api_version < self.nova_api_min_version:
49 raise Exception('Congress Nova datasource API '
50 'version < nova_api_min_version(%s)'
51 % self.nova_api_min_version)
53 # check whether doctor driver exist
55 {driver['id']: driver for driver in
56 self.congress.list_drivers()['results']}
57 if self.doctor_driver not in drivers:
58 raise Exception('Do not support doctor driver in congress')
60 # create doctor datasource if it's not exist
61 if self.doctor_datasource not in datasources:
62 response = self.congress.create_datasource(
63 body={'driver': self.doctor_driver,
64 'name': self.doctor_datasource})
65 self.doctor_datasource_id = response['id']
66 self.is_create_doctor_datasource = True
69 {rule['name']: rule for rule in
70 self.congress.list_policy_rules(self.policy)['results']}
72 def get_inspector_url(self):
73 ds = self.congress.list_datasources()['results']
74 doctor_ds = next((item for item in ds if item['driver'] == 'doctor'),
77 self.congress.httpclient.get_endpoint(auth=self.auth)
78 return ('%s/v1/data-sources/%s/tables/events/rows' %
79 (congress_endpoint, doctor_ds['id']))
82 self.log.info('congress inspector start......')
84 for rule_name, rule in self.rules.items():
85 self._add_rule(rule_name, rule)
88 self.log.info('congress inspector stop......')
90 for rule_name in self.rules.keys():
91 self._del_rule(rule_name)
93 if self.is_create_doctor_datasource:
94 self.congress.delete_datasource(self.doctor_datasource_id)
96 def _add_rule(self, rule_name, rule):
97 if rule_name not in self.policy_rules:
98 self.congress.create_policy_rule(self.policy,
99 body={'name': rule_name,
102 def _del_rule(self, rule_name):
103 if rule_name in self.policy_rules:
104 rule_id = self.policy_rules[rule_name]['id']
105 self.congress.delete_policy_rule(self.policy, rule_id)