summaryrefslogtreecommitdiff
path: root/novaclient/v2/security_group_rules.py
diff options
context:
space:
mode:
Diffstat (limited to 'novaclient/v2/security_group_rules.py')
-rw-r--r--novaclient/v2/security_group_rules.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/novaclient/v2/security_group_rules.py b/novaclient/v2/security_group_rules.py
new file mode 100644
index 0000000..4cb51b0
--- /dev/null
+++ b/novaclient/v2/security_group_rules.py
@@ -0,0 +1,78 @@
1# Copyright 2011 OpenStack Foundation
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"""
17Security group rules interface (1.1 extension).
18"""
19
20from novaclient import base
21from novaclient import exceptions
22from novaclient.i18n import _
23
24
25class SecurityGroupRule(base.Resource):
26 def __str__(self):
27 return str(self.id)
28
29 def delete(self):
30 self.manager.delete(self)
31
32
33class SecurityGroupRuleManager(base.Manager):
34 resource_class = SecurityGroupRule
35
36 def create(self, parent_group_id, ip_protocol=None, from_port=None,
37 to_port=None, cidr=None, group_id=None):
38 """
39 Create a security group rule
40
41 :param ip_protocol: IP protocol, one of 'tcp', 'udp' or 'icmp'
42 :param from_port: Source port
43 :param to_port: Destination port
44 :param cidr: Destination IP address(es) in CIDR notation
45 :param group_id: Security group id (int)
46 :param parent_group_id: Parent security group id (int)
47 """
48
49 try:
50 from_port = int(from_port)
51 except (TypeError, ValueError):
52 raise exceptions.CommandError(_("From port must be an integer."))
53 try:
54 to_port = int(to_port)
55 except (TypeError, ValueError):
56 raise exceptions.CommandError(_("To port must be an integer."))
57 if ip_protocol.upper() not in ['TCP', 'UDP', 'ICMP']:
58 raise exceptions.CommandError(_("IP protocol must be 'tcp', 'udp'"
59 ", or 'icmp'."))
60
61 body = {"security_group_rule": {
62 "ip_protocol": ip_protocol,
63 "from_port": from_port,
64 "to_port": to_port,
65 "cidr": cidr,
66 "group_id": group_id,
67 "parent_group_id": parent_group_id}}
68
69 return self._create('/os-security-group-rules', body,
70 'security_group_rule')
71
72 def delete(self, rule):
73 """
74 Delete a security group rule
75
76 :param rule: The security group rule to delete (ID or Class)
77 """
78 self._delete('/os-security-group-rules/%s' % base.getid(rule))