Debian Codegraph @

neutron_tempest_plugin/fwaas/common/fwaas_v2_client.py

raw
1 # Copyright (c) 2015 Midokura SARL
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # 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, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import time
17
18 from neutron_lib import constants as nl_constants
19 from tempest import config
20 from tempest import exceptions
21 from tempest.lib.common.utils import data_utils
22 from tempest.lib.common.utils import test_utils
23 from tempest.lib import exceptions as lib_exc
24
25 from neutron_tempest_plugin.fwaas.services import v2_client
26
27
28 CONF = config.CONF
29
30
31 class FWaaSClientMixin(object):
32
33 @classmethod
34 def resource_setup(cls):
35 super(FWaaSClientMixin, cls).resource_setup()
36 manager = cls.os_primary
37 cls.firewall_groups_client = v2_client.FirewallGroupsClient(
38 manager.auth_provider,
39 CONF.network.catalog_type,
40 CONF.network.region or CONF.identity.region,
41 endpoint_type=CONF.network.endpoint_type,
42 build_interval=CONF.network.build_interval,
43 build_timeout=CONF.network.build_timeout,
44 **manager.default_params)
45 cls.firewall_policies_client = v2_client.FirewallPoliciesClient(
46 manager.auth_provider,
47 CONF.network.catalog_type,
48 CONF.network.region or CONF.identity.region,
49 endpoint_type=CONF.network.endpoint_type,
50 build_interval=CONF.network.build_interval,
51 build_timeout=CONF.network.build_timeout,
52 **manager.default_params)
53 cls.firewall_rules_client = v2_client.FirewallRulesClient(
54 manager.auth_provider,
55 CONF.network.catalog_type,
56 CONF.network.region or CONF.identity.region,
57 endpoint_type=CONF.network.endpoint_type,
58 build_interval=CONF.network.build_interval,
59 build_timeout=CONF.network.build_timeout,
60 **manager.default_params)
61
62 def create_firewall_rule(self, **kwargs):
63 body = self.firewall_rules_client.create_firewall_rule(
64 name=data_utils.rand_name("fw-rule"),
65 **kwargs)
66 fw_rule = body['firewall_rule']
67 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
68 self.firewall_rules_client.delete_firewall_rule,
69 fw_rule['id'])
70 return fw_rule
71
72 def create_firewall_policy(self, **kwargs):
73 body = self.firewall_policies_client.create_firewall_policy(
74 name=data_utils.rand_name("fw-policy"),
75 **kwargs)
76 fw_policy = body['firewall_policy']
77 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
78 self.firewall_policies_client.delete_firewall_policy,
79 fw_policy['id'])
80 return fw_policy
81
82 def create_firewall_group(self, **kwargs):
83 body = self.firewall_groups_client.create_firewall_group(
84 name=data_utils.rand_name("fwg"),
85 **kwargs)
86 fwg = body['firewall_group']
87 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
88 self.delete_firewall_group_and_wait,
89 fwg['id'])
90 return fwg
91
92 def delete_firewall_group_and_wait(self, firewall_group_id):
93 self.firewall_groups_client.delete_firewall_group(firewall_group_id)
94 self._wait_firewall_group_while(firewall_group_id,
95 [nl_constants.PENDING_DELETE],
96 not_found_ok=True)
97
98 def insert_firewall_rule_in_policy_and_wait(self,
99 firewall_group_id,
100 firewall_policy_id,
101 firewall_rule_id, **kwargs):
102 self.firewall_policies_client.insert_firewall_rule_in_policy(
103 firewall_policy_id=firewall_policy_id,
104 firewall_rule_id=firewall_rule_id,
105 **kwargs)
106 self.addCleanup(
107 self._call_and_ignore_exceptions,
108 (lib_exc.NotFound, lib_exc.BadRequest),
109 self.remove_firewall_rule_from_policy_and_wait,
110 firewall_group_id=firewall_group_id,
111 firewall_policy_id=firewall_policy_id,
112 firewall_rule_id=firewall_rule_id)
113 self._wait_firewall_group_ready(firewall_group_id)
114
115 def remove_firewall_rule_from_policy_and_wait(self,
116 firewall_group_id,
117 firewall_policy_id,
118 firewall_rule_id):
119 self.firewall_policies_client.remove_firewall_rule_from_policy(
120 firewall_policy_id=firewall_policy_id,
121 firewall_rule_id=firewall_rule_id)
122 self._wait_firewall_group_ready(firewall_group_id)
123
124 def update_firewall_group_and_wait(self, firewall_group_id, **kwargs):
125 self.firewall_groups_client.update_firewall_group(
126 firewall_group_id, **kwargs)
127 self._wait_firewall_group_ready(firewall_group_id)
128
129 @staticmethod
130 def _call_and_ignore_exceptions(exc_list, func, *args, **kwargs):
131 """Call the given function and pass if a given exception is raised."""
132
133 try:
134 return func(*args, **kwargs)
135 except exc_list:
136 pass
137
138 def _wait_firewall_group_ready(self, firewall_group_id):
139 self._wait_firewall_group_while(firewall_group_id,
140 [nl_constants.PENDING_CREATE,
141 nl_constants.PENDING_UPDATE])
142
143 def _wait_firewall_group_while(self, firewall_group_id, statuses,
144 not_found_ok=False):
145 start = int(time.time())
146 if not_found_ok:
147 expected_exceptions = (lib_exc.NotFound)
148 else:
149 expected_exceptions = ()
150 while True:
151 try:
152 fwg = self.firewall_groups_client.show_firewall_group(
153 firewall_group_id)
154 except expected_exceptions:
155 break
156 status = fwg['firewall_group']['status']
157 if status not in statuses:
158 break
159 if (int(time.time()) - start >=
160 self.firewall_groups_client.build_timeout):
161 msg = ("Firewall Group %(firewall_group)s failed to reach "
162 "non PENDING status (current %(status)s)") % {
163 "firewall_group": firewall_group_id,
164 "status": status,
165 }
166 raise exceptions.TimeoutException(msg)
167 time.sleep(1)