delete doctor datasource in congress when cleanup
[doctor.git] / doctor_tests / inspector / congress.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
3 #
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
12
13 from doctor_tests.inspector.base import BaseInspector
14
15
16 class CongressInspector(BaseInspector):
17     nova_api_min_version = '2.11'
18     doctor_driver = 'doctor'
19     doctor_datasource = 'doctor'
20     policy = 'classification'
21     rules = {
22         'host_down':
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
26         'host_force_down':
27             'execute[nova:services.force_down(host, "nova-compute", "True")] :- host_down(host)',             # noqa
28         'error_vm_states':
29             'execute[nova:servers.reset_state(vmid, "error")] :- host_down(host), active_instance_in_host(vmid, host)'    # noqa
30     }
31
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()
40
41     def _init_driver_and_ds(self):
42         datasources = \
43             {ds['name']: ds for ds in
44              self.congress.list_datasources()['results']}
45
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)
52
53         # check whether doctor driver exist
54         drivers = \
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')
59
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
67
68         self.policy_rules = \
69             {rule['name']: rule for rule in
70              self.congress.list_policy_rules(self.policy)['results']}
71
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'),
75                          None)
76         congress_endpoint = \
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']))
80
81     def start(self):
82         self.log.info('congress inspector start......')
83
84         for rule_name, rule in self.rules.items():
85             self._add_rule(rule_name, rule)
86
87     def stop(self):
88         self.log.info('congress inspector stop......')
89
90         for rule_name in self.rules.keys():
91             self._del_rule(rule_name)
92
93         if self.is_create_doctor_datasource:
94             self.congress.delete_datasource(self.doctor_datasource_id)
95
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,
100                                                    'rule': rule})
101
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)