summaryrefslogtreecommitdiffstats
path: root/aria/multivim-plugin/system_tests/openstack_nova_net_handler.py
blob: 06fa0ab4d07e1c98938c181a34937d46b5df594d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    * See the License for the specific language governing permissions and
#    * limitations under the License.

import novaclient.v2.client as nvclient

from system_tests.openstack_handler import OpenstackHandler


class OpenstackNovaNetHandler(OpenstackHandler):

    # using the Config Readers of the regular OpenstackHandler - attempts
    # of reading neutron-related data may fail but shouldn't happen from
    # nova-net tests in the first place
    # CloudifyConfigReader = None

    def openstack_clients(self):
        creds = self._client_creds()
        return nvclient.Client(**creds)

    def openstack_infra_state(self):
        nova = self.openstack_clients()
        prefix = self.env.resources_prefix
        return {
            'security_groups': dict(self._security_groups(nova, prefix)),
            'servers': dict(self._servers(nova, prefix)),
            'key_pairs': dict(self._key_pairs(nova, prefix)),
            'floatingips': dict(self._floatingips(nova, prefix)),
        }

    def _floatingips(self, nova, prefix):
        return [(ip.id, ip.ip)
                for ip in nova.floating_ips.list()]

    def _security_groups(self, nova, prefix):
        return [(n.id, n.name)
                for n in nova.security_groups.list()
                if self._check_prefix(n.name, prefix)]

    def _remove_openstack_resources_impl(self, resources_to_remove):
        nova = self.openstack_clients()

        servers = nova.servers.list()
        keypairs = nova.keypairs.list()
        floatingips = nova.floating_ips.list()
        security_groups = nova.security_groups.list()

        failed = {
            'servers': {},
            'key_pairs': {},
            'floatingips': {},
            'security_groups': {}
        }

        for server in servers:
            if server.id in resources_to_remove['servers']:
                with self._handled_exception(server.id, failed, 'servers'):
                    nova.servers.delete(server)
        for key_pair in keypairs:
            if key_pair.name == self.env.agent_keypair_name and \
                    self.env.use_existing_agent_keypair:
                # this is a pre-existing agent key-pair, do not remove
                continue
            elif key_pair.name == self.env.management_keypair_name and \
                    self.env.use_existing_manager_keypair:
                # this is a pre-existing manager key-pair, do not remove
                continue
            elif key_pair.id in resources_to_remove['key_pairs']:
                with self._handled_exception(key_pair.id, failed, 'key_pairs'):
                    nova.keypairs.delete(key_pair)
        for floatingip in floatingips:
            if floatingip.id in resources_to_remove['floatingips']:
                with self._handled_exception(floatingip.id, failed,
                                             'floatingips'):
                    nova.floating_ips.delete(floatingip)
        for security_group in security_groups:
            if security_group.name == 'default':
                continue
            if security_group.id in resources_to_remove['security_groups']:
                with self._handled_exception(security_group.id, failed,
                                             'security_groups'):
                    nova.security_groups.delete(security_group)

        return failed


handler = OpenstackNovaNetHandler