Debian Codegraph @

neutronclient/neutron/v2_0/fw/firewallrule.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 _add_common_args(parser, is_create=True):
25 """If is_create is True, protocol and action become mandatory arguments.
26
27 CreateCommand = is_create : True
28 UpdateCommand = is_create : False
29 """
30 parser.add_argument(
31 '--name',
32 help=_('Name for the firewall rule.'))
33 parser.add_argument(
34 '--description',
35 help=_('Description for the firewall rule.'))
36 parser.add_argument(
37 '--source-ip-address',
38 help=_('Source IP address or subnet.'))
39 parser.add_argument(
40 '--destination-ip-address',
41 help=_('Destination IP address or subnet.'))
42 parser.add_argument(
43 '--source-port',
44 help=_('Source port (integer in [1, 65535] or range in a:b).'))
45 parser.add_argument(
46 '--destination-port',
47 help=_('Destination port (integer in [1, 65535] or range in '
48 'a:b).'))
49 utils.add_boolean_argument(
50 parser, '--enabled', dest='enabled',
51 help=_('Whether to enable or disable this rule.'))
52 parser.add_argument(
53 '--protocol', choices=['tcp', 'udp', 'icmp', 'any'],
54 required=is_create,
55 type=utils.convert_to_lowercase,
56 help=_('Protocol for the firewall rule.'))
57 parser.add_argument(
58 '--action',
59 required=is_create,
60 type=utils.convert_to_lowercase,
61 choices=['allow', 'deny', 'reject'],
62 help=_('Action for the firewall rule.'))
63
64
65 def common_args2body(parsed_args):
66 body = {}
67 neutronv20.update_dict(parsed_args, body,
68 ['name', 'description', 'shared', 'tenant_id',
69 'source_ip_address', 'destination_ip_address',
70 'source_port', 'destination_port', 'action',
71 'enabled', 'ip_version'])
72 protocol = parsed_args.protocol
73 if protocol:
74 if protocol == 'any':
75 protocol = None
76 body['protocol'] = protocol
77 return body
78
79
80 class ListFirewallRule(neutronv20.ListCommand):
81 """List firewall rules that belong to a given tenant."""
82
83 resource = 'firewall_rule'
84 list_columns = ['id', 'name', 'firewall_policy_id', 'summary', 'enabled']
85 pagination_support = True
86 sorting_support = True
87
88 def extend_list(self, data, parsed_args):
89 for d in data:
90 val = []
91 if d.get('protocol'):
92 protocol = d['protocol'].upper()
93 else:
94 protocol = 'no-protocol'
95 val.append(protocol)
96 if 'source_ip_address' in d and 'source_port' in d:
97 src = 'source: ' + str(d['source_ip_address']).lower()
98 src = src + '(' + str(d['source_port']).lower() + ')'
99 else:
100 src = 'source: none specified'
101 val.append(src)
102 if 'destination_ip_address' in d and 'destination_port' in d:
103 dst = 'dest: ' + str(d['destination_ip_address']).lower()
104 dst = dst + '(' + str(d['destination_port']).lower() + ')'
105 else:
106 dst = 'dest: none specified'
107 val.append(dst)
108 if 'action' in d:
109 action = d['action']
110 else:
111 action = 'no-action'
112 val.append(action)
113 d['summary'] = ',\n '.join(val)
114
115
116 class ShowFirewallRule(neutronv20.ShowCommand):
117 """Show information of a given firewall rule."""
118
119 resource = 'firewall_rule'
120
121
122 class CreateFirewallRule(neutronv20.CreateCommand):
123 """Create a firewall rule."""
124
125 resource = 'firewall_rule'
126
127 def add_known_arguments(self, parser):
128 parser.add_argument(
129 '--shared',
130 action='store_true',
131 help=_('Set shared flag for the firewall rule.'),
132 default=argparse.SUPPRESS)
133 _add_common_args(parser)
134 parser.add_argument(
135 '--ip-version',
136 type=int, choices=[4, 6], default=4,
137 help=_('IP version for the firewall rule (default is 4).'))
138
139 def args2body(self, parsed_args):
140 return {self.resource: common_args2body(parsed_args)}
141
142
143 class UpdateFirewallRule(neutronv20.UpdateCommand):
144 """Update a given firewall rule."""
145
146 resource = 'firewall_rule'
147
148 def add_known_arguments(self, parser):
149 utils.add_boolean_argument(
150 parser,
151 '--shared',
152 dest='shared',
153 help=_('Update the shared flag for the firewall rule.'),
154 default=argparse.SUPPRESS)
155 parser.add_argument(
156 '--ip-version',
157 type=int, choices=[4, 6],
158 help=_('Update IP version for the firewall rule.'))
159 _add_common_args(parser, is_create=False)
160
161 def args2body(self, parsed_args):
162 return {self.resource: common_args2body(parsed_args)}
163
164
165 class DeleteFirewallRule(neutronv20.DeleteCommand):
166 """Delete a given firewall rule."""
167
168 resource = 'firewall_rule'