Debian Codegraph @

neutronclient/neutron/v2_0/fw/firewallpolicy.py

raw
1 # Copyright 2013 Big Switch Networks
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
17 import argparse
18
19 from neutronclient._i18n import _
20 from neutronclient.common import utils
21 from neutronclient.neutron import v2_0 as neutronv20
22
23
24 def _format_firewall_rules(firewall_policy):
25 try:
26 output = '[' + ',\n '.join([rule for rule in
27 firewall_policy['firewall_rules']]) + ']'
28 return output
29 except (TypeError, KeyError):
30 return ''
31
32
33 def add_common_args(parser):
34 parser.add_argument(
35 '--description',
36 help=_('Description for the firewall policy.'))
37 parser.add_argument(
38 '--firewall-rules', type=lambda x: x.split(),
39 help=_('Ordered list of whitespace-delimited firewall rule '
40 'names or IDs; e.g., --firewall-rules \"rule1 rule2\"'))
41
42
43 def parse_common_args(client, parsed_args):
44 if parsed_args.firewall_rules:
45 _firewall_rules = []
46 for f in parsed_args.firewall_rules:
47 _firewall_rules.append(
48 neutronv20.find_resourceid_by_name_or_id(
49 client, 'firewall_rule', f))
50 body = {'firewall_rules': _firewall_rules}
51 else:
52 body = {}
53 neutronv20.update_dict(parsed_args, body,
54 ['name', 'description', 'shared',
55 'audited', 'tenant_id'])
56 return {'firewall_policy': body}
57
58
59 class ListFirewallPolicy(neutronv20.ListCommand):
60 """List firewall policies that belong to a given tenant."""
61
62 resource = 'firewall_policy'
63 list_columns = ['id', 'name', 'firewall_rules']
64 _formatters = {'firewall_rules': _format_firewall_rules,
65 }
66 pagination_support = True
67 sorting_support = True
68
69
70 class ShowFirewallPolicy(neutronv20.ShowCommand):
71 """Show information of a given firewall policy."""
72
73 resource = 'firewall_policy'
74
75
76 class CreateFirewallPolicy(neutronv20.CreateCommand):
77 """Create a firewall policy."""
78
79 resource = 'firewall_policy'
80
81 def add_known_arguments(self, parser):
82 parser.add_argument(
83 'name',
84 metavar='NAME',
85 help=_('Name for the firewall policy.'))
86 parser.add_argument(
87 '--shared',
88 action='store_true',
89 help=_('Create a shared policy.'),
90 default=argparse.SUPPRESS)
91 parser.add_argument(
92 '--audited',
93 action='store_true',
94 help=_('Sets audited to True.'),
95 default=argparse.SUPPRESS)
96 add_common_args(parser)
97
98 def args2body(self, parsed_args):
99 return parse_common_args(self.get_client(), parsed_args)
100
101
102 class UpdateFirewallPolicy(neutronv20.UpdateCommand):
103 """Update a given firewall policy."""
104
105 resource = 'firewall_policy'
106
107 def add_known_arguments(self, parser):
108 add_common_args(parser)
109 parser.add_argument(
110 '--name',
111 help=_('Name for the firewall policy.'))
112 utils.add_boolean_argument(
113 parser, '--shared',
114 help=_('Update the sharing status of the policy. '
115 '(True means shared).'))
116 utils.add_boolean_argument(
117 parser, '--audited',
118 help=_('Update the audit status of the policy. '
119 '(True means auditing is enabled).'))
120
121 def args2body(self, parsed_args):
122 return parse_common_args(self.get_client(), parsed_args)
123
124
125 class DeleteFirewallPolicy(neutronv20.DeleteCommand):
126 """Delete a given firewall policy."""
127
128 resource = 'firewall_policy'
129
130
131 class FirewallPolicyInsertRule(neutronv20.UpdateCommand):
132 """Insert a rule into a given firewall policy."""
133
134 resource = 'firewall_policy'
135
136 def call_api(self, neutron_client, firewall_policy_id, body):
137 return neutron_client.firewall_policy_insert_rule(firewall_policy_id,
138 body)
139
140 def args2body(self, parsed_args):
141 _rule = ''
142 if parsed_args.firewall_rule_id:
143 _rule = neutronv20.find_resourceid_by_name_or_id(
144 self.get_client(), 'firewall_rule',
145 parsed_args.firewall_rule_id)
146 _insert_before = ''
147 if 'insert_before' in parsed_args:
148 if parsed_args.insert_before:
149 _insert_before = neutronv20.find_resourceid_by_name_or_id(
150 self.get_client(), 'firewall_rule',
151 parsed_args.insert_before)
152 _insert_after = ''
153 if 'insert_after' in parsed_args:
154 if parsed_args.insert_after:
155 _insert_after = neutronv20.find_resourceid_by_name_or_id(
156 self.get_client(), 'firewall_rule',
157 parsed_args.insert_after)
158 body = {'firewall_rule_id': _rule,
159 'insert_before': _insert_before,
160 'insert_after': _insert_after}
161 return body
162
163 def get_parser(self, prog_name):
164 parser = super(FirewallPolicyInsertRule, self).get_parser(prog_name)
165 parser.add_argument(
166 '--insert-before',
167 metavar='FIREWALL_RULE',
168 help=_('Insert before this rule.'))
169 parser.add_argument(
170 '--insert-after',
171 metavar='FIREWALL_RULE',
172 help=_('Insert after this rule.'))
173 parser.add_argument(
174 'firewall_rule_id',
175 metavar='FIREWALL_RULE',
176 help=_('New rule to insert.'))
177 self.add_known_arguments(parser)
178 return parser
179
180 def take_action(self, parsed_args):
181 neutron_client = self.get_client()
182 body = self.args2body(parsed_args)
183 _id = neutronv20.find_resourceid_by_name_or_id(neutron_client,
184 self.resource,
185 parsed_args.id)
186 self.call_api(neutron_client, _id, body)
187 print((_('Inserted firewall rule in firewall policy %(id)s') %
188 {'id': parsed_args.id}), file=self.app.stdout)
189
190
191 class FirewallPolicyRemoveRule(neutronv20.UpdateCommand):
192 """Remove a rule from a given firewall policy."""
193
194 resource = 'firewall_policy'
195
196 def call_api(self, neutron_client, firewall_policy_id, body):
197 return neutron_client.firewall_policy_remove_rule(firewall_policy_id,
198 body)
199
200 def args2body(self, parsed_args):
201 _rule = ''
202 if parsed_args.firewall_rule_id:
203 _rule = neutronv20.find_resourceid_by_name_or_id(
204 self.get_client(), 'firewall_rule',
205 parsed_args.firewall_rule_id)
206 body = {'firewall_rule_id': _rule}
207 return body
208
209 def get_parser(self, prog_name):
210 parser = super(FirewallPolicyRemoveRule, self).get_parser(prog_name)
211 parser.add_argument(
212 'firewall_rule_id',
213 metavar='FIREWALL_RULE',
214 help=_('ID or name of the firewall rule to be removed '
215 'from the policy.'))
216 self.add_known_arguments(parser)
217 return parser
218
219 def take_action(self, parsed_args):
220 neutron_client = self.get_client()
221 body = self.args2body(parsed_args)
222 _id = neutronv20.find_resourceid_by_name_or_id(neutron_client,
223 self.resource,
224 parsed_args.id)
225 self.call_api(neutron_client, _id, body)
226 print((_('Removed firewall rule from firewall policy %(id)s') %
227 {'id': parsed_args.id}), file=self.app.stdout)