33# Copyright: (c) 2019, Frederic Bor <frederic.bor@wanadoo.fr>
44# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
55
6- from __future__ import (absolute_import , division , print_function )
6+ from __future__ import absolute_import , division , print_function
7+
78__metaclass__ = type
89
910from ansible .errors import AnsibleFilterError
1011
1112
1213def format_ipsec_aggregate_ipsecs (all_tunnels , pfname ):
13- """ format ipsecs for format_ipsec_aggregate """
14+ """format ipsecs for format_ipsec_aggregate"""
1415 res = list ()
1516 for name , ipsec in all_tunnels .items ():
16- pfsenses = ipsec [' pfsenses' ]
17+ pfsenses = ipsec [" pfsenses" ]
1718 if pfname not in pfsenses :
1819 continue
1920 local = pfsenses [pfname ]
@@ -25,49 +26,51 @@ def format_ipsec_aggregate_ipsecs(all_tunnels, pfname):
2526 params = dict ()
2627 res .append (params )
2728
28- params [' descr' ] = name + ' to ' + remote_name
29- params [' state' ] = ' present'
29+ params [" descr" ] = name + " to " + remote_name
30+ params [" state" ] = " present"
3031 for option in ipsec :
31- if option in [' pfsenses' , ' phase1' , ' phase2' ]:
32+ if option in [" pfsenses" , " phase1" , " phase2" ]:
3233 continue
3334 params [option ] = ipsec [option ]
3435
3536 for option in remote_options :
36- if option in [' sharing' , ' myid_data' ]:
37+ if option in [" sharing" , " myid_data" ]:
3738 continue
3839 params [option ] = remote_options [option ]
3940
40- if ' peerid_type' in params and params [' peerid_type' ] == ' keyid tag' :
41- params [' peerid_data' ] = remote_options [' myid_data' ]
41+ if " peerid_type" in params and params [" peerid_type" ] == " keyid tag" :
42+ params [" peerid_data" ] = remote_options [" myid_data" ]
4243
43- if ' myid_data' in local :
44- params [' myid_data' ] = local [' myid_data' ]
44+ if " myid_data" in local :
45+ params [" myid_data" ] = local [" myid_data" ]
4546 return res
4647
4748
4849def format_ipsec_aggregate_proposals (all_tunnels , pfname ):
49- """ format proposals for format_ipsec_aggregate """
50+ """format proposals for format_ipsec_aggregate"""
5051 res = list ()
5152
5253 for name , ipsec in all_tunnels .items ():
53- pfsenses = ipsec [' pfsenses' ]
54+ pfsenses = ipsec [" pfsenses" ]
5455 if pfname not in pfsenses :
5556 continue
5657
57- if ' phase1' not in ipsec :
58+ if " phase1" not in ipsec :
5859 raise AnsibleFilterError ("phase1 is missing in {0}" .format (name ))
5960
60- phase1 = ipsec [' phase1' ]
61+ phase1 = ipsec [" phase1" ]
6162 p1s = list ()
6263
63- if 'encryptions' not in phase1 :
64- raise AnsibleFilterError ("encryptions is missing in phase1 of {0}" .format (name ))
64+ if "encryptions" not in phase1 :
65+ raise AnsibleFilterError (
66+ "encryptions is missing in phase1 of {0}" .format (name )
67+ )
6568
66- if ' hashes' not in phase1 :
69+ if " hashes" not in phase1 :
6770 raise AnsibleFilterError ("hashes is missing in phase1 of {0}" .format (name ))
6871
69- encryptions = phase1 [' encryptions' ]
70- hashes = phase1 [' hashes' ].split (' ' )
72+ encryptions = phase1 [" encryptions" ]
73+ hashes = phase1 [" hashes" ].split (" " )
7174
7275 for remote_name in pfsenses :
7376 if remote_name == pfname :
@@ -77,14 +80,17 @@ def format_ipsec_aggregate_proposals(all_tunnels, pfname):
7780 for hash_option in hashes :
7881 params = dict ()
7982 p1s .append (params )
80- params ['descr' ] = name + ' to ' + remote_name
81- params ['state' ] = 'present'
82- params ['hash' ] = hash_option
83- params ['encryption' ] = encryption
84- if encryptions [encryption ] is not None and encryptions [encryption ] != 'None' :
85- params ['key_length' ] = encryptions [encryption ]
83+ params ["descr" ] = name + " to " + remote_name
84+ params ["state" ] = "present"
85+ params ["hash" ] = hash_option
86+ params ["encryption" ] = encryption
87+ if (
88+ encryptions [encryption ] is not None
89+ and encryptions [encryption ] != "None"
90+ ):
91+ params ["key_length" ] = encryptions [encryption ]
8692 for p1_option in phase1 :
87- if p1_option in [' encryptions' , ' hashes' ]:
93+ if p1_option in [" encryptions" , " hashes" ]:
8894 continue
8995 for p1 in p1s :
9096 p1 [p1_option ] = phase1 [p1_option ]
@@ -93,67 +99,71 @@ def format_ipsec_aggregate_proposals(all_tunnels, pfname):
9399
94100
95101def format_ipsec_aggregate_p2s (all_tunnels , pfname ):
96- """ format p2s for format_ipsec_aggregate """
102+ """format p2s for format_ipsec_aggregate"""
97103 res = list ()
98104
99105 for name , ipsec in all_tunnels .items ():
100- pfsenses = ipsec [' pfsenses' ]
106+ pfsenses = ipsec [" pfsenses" ]
101107 if pfname not in pfsenses :
102108 continue
103109
104- if ' phase2' not in ipsec :
110+ if " phase2" not in ipsec :
105111 raise AnsibleFilterError ("phase2 is missing in {0}" .format (name ))
106- phase2 = ipsec [' phase2' ]
112+ phase2 = ipsec [" phase2" ]
107113
108- if ' mode' not in phase2 :
114+ if " mode" not in phase2 :
109115 raise AnsibleFilterError ("mode is missing in phase2 of {0}" .format (name ))
110- mode = phase2 [' mode' ]
116+ mode = phase2 [" mode" ]
111117
112118 local = pfsenses [pfname ]
113- if 'sharing' in local :
114- local_sharing = local ['sharing' ].split (' ' )
115- elif mode != 'transport' :
116- raise AnsibleFilterError ("sharing is missing for {0} in {1}" .format (pfname , name ))
119+ if "sharing" in local :
120+ local_sharing = local ["sharing" ].split (" " )
121+ elif mode != "transport" :
122+ raise AnsibleFilterError (
123+ "sharing is missing for {0} in {1}" .format (pfname , name )
124+ )
117125
118126 p2s = list ()
119127
120128 for remote_name , remote in pfsenses .items ():
121129 if remote_name == pfname :
122130 continue
123- if 'sharing' in remote :
124- remote_sharing = remote ['sharing' ].split (' ' )
125- elif mode != 'transport' :
126- raise AnsibleFilterError ("sharing is missing for {0} in {1}" .format (remote_name , name ))
127-
128- if mode != 'transport' :
131+ if "sharing" in remote :
132+ remote_sharing = remote ["sharing" ].split (" " )
133+ elif mode != "transport" :
134+ raise AnsibleFilterError (
135+ "sharing is missing for {0} in {1}" .format (remote_name , name )
136+ )
137+
138+ if mode != "transport" :
129139 for local_network in local_sharing :
130140 for remote_network in remote_sharing :
131141 params = dict ()
132142 p2s .append (params )
133- params [' p1_descr' ] = name + ' to ' + remote_name
134- params [' descr' ] = local_network + ' to ' + remote_network
135- params [' state' ] = ' present'
136- params [' local' ] = local_network
137- params [' remote' ] = remote_network
143+ params [" p1_descr" ] = name + " to " + remote_name
144+ params [" descr" ] = local_network + " to " + remote_network
145+ params [" state" ] = " present"
146+ params [" local" ] = local_network
147+ params [" remote" ] = remote_network
138148 else :
139149 params = dict ()
140150 p2s .append (params )
141- params [' descr' ] = name + ' to ' + remote_name
142- params [' p1_descr' ] = name + ' to ' + remote_name
143- params [' state' ] = ' present'
151+ params [" descr" ] = name + " to " + remote_name
152+ params [" p1_descr" ] = name + " to " + remote_name
153+ params [" state" ] = " present"
144154
145155 for p2_option , p2_value in phase2 .items ():
146156 for p2 in p2s :
147- if p2_option == ' encryptions' :
157+ if p2_option == " encryptions" :
148158 for encryption , keylength in p2_value .items ():
149159 p2 [encryption ] = True
150- if keylength is not None and keylength != ' None' :
160+ if keylength is not None and keylength != " None" :
151161 if isinstance (keylength , str ):
152- p2 [encryption + ' _len' ] = keylength
162+ p2 [encryption + " _len" ] = keylength
153163 else :
154- p2 [encryption + ' _len' ] = str (keylength )
155- elif p2_option == ' hashes' :
156- hashes = p2_value .split (' ' )
164+ p2 [encryption + " _len" ] = str (keylength )
165+ elif p2_option == " hashes" :
166+ hashes = p2_value .split (" " )
157167 for hash_option in hashes :
158168 p2 [hash_option ] = True
159169 else :
@@ -163,27 +173,31 @@ def format_ipsec_aggregate_p2s(all_tunnels, pfname):
163173
164174
165175def format_ipsec_aggregate (* terms ):
166- """ format var for ipsec_aggregate """
176+ """format var for ipsec_aggregate"""
167177 if len (terms ) != 2 or not isinstance (terms [0 ], dict ):
168- raise AnsibleFilterError ("format_ipsec_aggregate expects one dictionnary of ipsec tunnels" )
178+ raise AnsibleFilterError (
179+ "format_ipsec_aggregate expects one dictionnary of ipsec tunnels"
180+ )
169181
170182 all_tunnels = terms [0 ]
171183 pfname = terms [1 ]
172184
173185 res = dict ()
174- res ['aggregated_ipsecs' ] = format_ipsec_aggregate_ipsecs (all_tunnels , pfname )
175- res ['aggregated_ipsec_proposals' ] = format_ipsec_aggregate_proposals (all_tunnels , pfname )
176- res ['aggregated_ipsec_p2s' ] = format_ipsec_aggregate_p2s (all_tunnels , pfname )
186+ res ["aggregated_ipsecs" ] = format_ipsec_aggregate_ipsecs (all_tunnels , pfname )
187+ res ["aggregated_ipsec_proposals" ] = format_ipsec_aggregate_proposals (
188+ all_tunnels , pfname
189+ )
190+ res ["aggregated_ipsec_p2s" ] = format_ipsec_aggregate_p2s (all_tunnels , pfname )
177191
178192 return res
179193
180194
181195class FilterModule (object ):
182- """ FilterModule """
196+ """FilterModule"""
183197
184198 @staticmethod
185199 def filters ():
186- """ defined functions """
200+ """defined functions"""
187201 return {
188- ' format_ipsec_aggregate' : format_ipsec_aggregate ,
202+ " format_ipsec_aggregate" : format_ipsec_aggregate ,
189203 }
0 commit comments