Browse Source

Ed & Cameron | Common rule class for comparing openstack and ec2 rules

changes/85/139985/1
cameron-r 4 years ago
parent
commit
6f2fc65bf4
3 changed files with 49 additions and 0 deletions
  1. 18
    0
      ec2_rule_transformer.py
  2. 15
    0
      openstack_rule_transformer.py
  3. 16
    0
      rule.py

+ 18
- 0
ec2_rule_transformer.py View File

@@ -0,0 +1,18 @@
1
+from copy import deepcopy
2
+from rule import Rule
3
+
4
+
5
+class 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)

+ 15
- 0
openstack_rule_transformer.py View File

@@ -0,0 +1,15 @@
1
+from copy import deepcopy
2
+from rule import Rule
3
+
4
+
5
+class 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)

+ 16
- 0
rule.py View File

@@ -0,0 +1,16 @@
1
+class 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())

Loading…
Cancel
Save