summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcameron-r <crowshanbin@gmail.com>2014-11-03 17:05:37 -0600
committercameron-r <crowshanbin@gmail.com>2014-11-05 15:27:44 -0600
commit6f2fc65bf48bacc30f31c4fbb91e12eb8499ff14 (patch)
tree77226b22ef3d145f4673ab0555ea1e0de0e32bca
parent85473ced07d06c42f7a544537dcca1251fd8e278 (diff)
Ed & Cameron | Common rule class for comparing openstack and ec2 rules
-rw-r--r--ec2_rule_transformer.py18
-rw-r--r--openstack_rule_transformer.py15
-rw-r--r--rule.py16
3 files changed, 49 insertions, 0 deletions
diff --git a/ec2_rule_transformer.py b/ec2_rule_transformer.py
new file mode 100644
index 0000000..64bb25f
--- /dev/null
+++ b/ec2_rule_transformer.py
@@ -0,0 +1,18 @@
1from copy import deepcopy
2from rule import Rule
3
4
5class EC2RuleTransformer:
6
7 def __init__(self, ec2_connection):
8 self.ec2_connection = ec2_connection
9
10 def to_rule(self, ec2_rule):
11 rule_args = deepcopy(ec2_rule)
12
13 if ec2_rule.grants[0].cidr_ip:
14 rule_args['ip_range'] = ec2_rule.grants[0].cidr_ip
15 else:
16 group_id = ec2_rule.grants[0].group_id
17 rule_args['group_name'] = self.ec2_connection.get_all_security_groups(group_ids=group_id)[0]
18 return Rule(**ec2_rule) \ No newline at end of file
diff --git a/openstack_rule_transformer.py b/openstack_rule_transformer.py
new file mode 100644
index 0000000..562e0c8
--- /dev/null
+++ b/openstack_rule_transformer.py
@@ -0,0 +1,15 @@
1from copy import deepcopy
2from rule import Rule
3
4
5class OpenStackRuleTransformer:
6
7 def to_rule(self, openstack_rule):
8 rule_args = deepcopy(openstack_rule)
9
10 if 'cidr' in openstack_rule['ip_range']:
11 rule_args['ip_range'] = openstack_rule['ip_range']['cidr']
12 else:
13 rule_args['group_name'] = openstack_rule['group']['name']
14
15 return Rule(**openstack_rule) \ No newline at end of file
diff --git a/rule.py b/rule.py
new file mode 100644
index 0000000..fc01ac8
--- /dev/null
+++ b/rule.py
@@ -0,0 +1,16 @@
1class Rule:
2 def __init__(self, ip_protocol, from_port, to_port, ip_range=None, group_name=None):
3 self.ip_protocol = ip_protocol
4 self.from_port = from_port
5 self.to_port = to_port
6 self.ip_range = ip_range
7 self.group_name = group_name
8
9 def __key(self):
10 return self.ip_protocol, self.from_port, self.to_port, self.ip_range, self.group_name
11
12 def __eq__(self, other):
13 return self.__key() == other.__key()
14
15 def __hash__(self):
16 return hash(self.__key()) \ No newline at end of file