��i m p o r t   a r g p a r s e  
 i m p o r t   j s o n  
 i m p o r t   r e  
 i m p o r t   o s  
 i m p o r t   s y s  
 i m p o r t   c o p y  
 #   h t t p s : / / d o c s . p y t h o n . o r g / 3 / l i b r a r y / i p a d d r e s s . h t m l  
 i m p o r t   i p a d d r e s s  
  
  
 #   H e l p e r   f u n c t i o n s  
  
 #   T r u e   i f   a n y   o f   t h e   u r l s   c o n t a i n e d   i n   t h e   U R L   l i s t   c o n t a i n s   a   w i l d c a r d   ( ' * ' )  
 d e f   u r l s _ c o n t a i n _ w i l d c a r d ( u r l s ) :  
         f o r   u r l   i n   u r l s :  
                 i f   ' * '   i n   u r l :  
                         r e t u r n   T r u e  
         r e t u r n   F a l s e  
  
 #   C h e c k   w h e t h e r   U R L s   a r e   c o r r e c t :  
 #       -   W i l d c a r d   n e e d s   t o   b e   i n   t h e   b e g i n n i n g   o f   t h e   s t r i n g ,   n o t   v a l i d   i n   t h e   m i d d l e  
 d e f   v e r i f y _ u r l s ( u r l s ) :  
         c o r r e c t e d _ u r l s   =   [ ]  
         f o r   u r l   i n   u r l s :  
                 i f   u r l . f i n d ( ' * ' )   < =   0 :  
                         c o r r e c t e d _ u r l s . a p p e n d ( u r l )  
                 e l s e :  
                         c o r r e c t e d _ u r l s . a p p e n d ( u r l [ u r l . f i n d ( ' * ' ) : ] )  
                         i f   l o g _ l e v e l   > =   4 :  
                                 p r i n t ( " W A R N I N G :   U R L   { 0 }   r e d u c e d   t o   { 1 } " . f o r m a t ( u r l ,   u r l [ u r l . f i n d ( ' * ' ) : ] ) )  
         r e t u r n   c o r r e c t e d _ u r l s  
  
 #   F i l t e r s   o u t   I P   a d d r e s s e s   b a s e d   o n   t h e   a r g s . i p _ v e r s i o n   p a r a m e t e r   ( c a n   b e   i p v 4 ,   i p v 6   o r   b o t h )  
 d e f   f i l t e r _ i p s ( i p _ l i s t ) :  
         #   F o r   b o t h   v e r s i o n s ,   d o n t   f i l t e r  
         i f   a r g s . i p _ v e r s i o n   = =   ' b o t h ' :  
                 r e t u r n   i p _ l i s t  
         e l s e :  
                 f i l t e r e d _ i p s   =   [ ]  
                 f o r   i p   i n   i p _ l i s t :  
                         #   F o r   ' i p v 4 ' ,   r e t u r n   o n l y   t h o s e   w h o   m a t c h   t h e   I P v 4   c h e c k  
                         i f   i s _ i p v 4 ( i p )   a n d   ( a r g s . i p _ v e r s i o n   = =   ' i p v 4 ' ) :  
                                 f i l t e r e d _ i p s . a p p e n d ( i p )  
                         #   F o r   ' i p v 6 ' ,   r e t u r n   o n l y   n o n - I P v 4   a d d r e s s e s   ( a s s u m e d   t o   b e   i p v 6   t h e n )  
                         e l i f   ( n o t   i s _ i p v 4 ( i p ) )   a n d   ( a r g s . i p _ v e r s i o n   = =   ' i p v 6 ' ) :  
                                 f i l t e r e d _ i p s . a p p e n d ( i p )  
                 #   i f   l o g _ l e v e l   > =   7 :  
                 #           p r i n t ( " D E B U G :   I P   l i s t   { 0 }   f i l t e r e d   t o   { 1 } " . f o r m a t ( s t r ( i p _ l i s t ) ,   s t r ( f i l t e r e d _ i p s ) ) )  
                 r e t u r n   f i l t e r e d _ i p s  
  
 #   T r u e   i f   p a r a m e t e r   e x i s t s   a s   a d d r e s s   g r o u p   i n   t h e   a r r a y   a d d r e s s _ g r o u p _ l i s t  
 d e f   i s _ a d d r e s s _ g r o u p ( s t r _ v a r ) :  
         i p _ g r p _ f o u n d   =   n e x t ( ( x   f o r   x   i n   a d d r e s s _ g r o u p s   i f   x [ ' n a m e ' ]   = =   s t r _ v a r ) ,   N o n e )  
         i f   i p _ g r p _ f o u n d :  
                 r e t u r n   T r u e  
         e l s e :  
                 r e t u r n   F a l s e  
  
 #   T r u e   i f   t h e   i p g r o u p   ( n a m e   i n   1 s t   p a r a m e t e r )   i s   u s e d   b y   t h e   n e t   r u l e s   ( 2 n d   p a r a m e t e r )  
 d e f   i p g r p _ u s e d _ b y _ r u l e s ( i p g r p _ n a m e ,   n e w _ r u l e s ) :  
         i p g r p _ s t r i n g   =   " [ r e s o u r c e I d ( ' M i c r o s o f t . N e t w o r k / i p G r o u p s ' ,   ' { 0 } ' ) ] " . f o r m a t ( i p g r p _ n a m e )  
         f o r   t h i s _ n e w _ r u l e   i n   n e w _ r u l e s :  
                 i f   ' d e s t i n a t i o n I p G r o u p s '   i n   t h i s _ n e w _ r u l e :  
                         i f   i p g r p _ s t r i n g   i n   t h i s _ n e w _ r u l e [ ' d e s t i n a t i o n I p G r o u p s ' ] :  
                                 r e t u r n   T r u e  
                 i f   ' s o u r c e I p G r o u p s '   i n   t h i s _ n e w _ r u l e :  
                         i f   i p g r p _ s t r i n g   i n   t h i s _ n e w _ r u l e [ ' s o u r c e I p G r o u p s ' ] :  
                                 r e t u r n   T r u e  
                 e l s e :  
                         i f   l o g _ l e v e l   > =   4 :  
                                 p r i n t ( " W A R N I N G :   i t   l o o k s   l i k e   t h e   k e y   ' d e s t i n a t i o n I p G r o u p s '   i s   m i s s i n g   f r o m   r u l e   ' { 0 } ' " . f o r m a t ( s t r ( t h i s _ n e w _ r u l e ) ) ,   f i l e = s y s . s t d e r r )  
         r e t u r n   F a l s e  
  
 #   T r u e   i f   p a r a m e t e r   i s   a   v a l i d   F Q D N   a c c o r d i n g   t o   R F C s   9 5 2 ,   1 1 2 3  
 d e f   i s _ f q d n ( s t r _ v a r ) :  
         #   r e t u r n   b o o l ( r e . m a t c h ( r " ( ? = ^ . { 4 , 2 5 3 } $ ) ( ^ ( ( ? ! - ) [ a - z A - Z 0 - 9 - ] { 1 , 6 3 } ( ? < ! - ) \ . ) + [ a - z A - Z ] { 2 , 6 3 } $ ) " , s t r ( s t r _ v a r ) ) )  
         #   M o d i f i e d   t h e   r e g e x   a b o v e   t o   o n l y   m a t c h   o n   T L D s   b e t w e e n   2   a n d   4   c h a r a c t e r s  
         r e t u r n   b o o l ( r e . m a t c h ( r " ( ? = ^ . { 4 , 2 5 3 } $ ) ( ^ ( ( ? ! - ) ) ( x n - - ) ? [ a - z 0 - 9 ] [ a - z 0 - 9 - _ ] { 0 , 6 1 } [ a - z 0 - 9 ] { 0 , 1 } \ . ( x n - - ) ? ( [ a - z 0 - 9 \ - ] { 1 , 6 1 } | [ a - z 0 - 9 - ] { 1 , 3 0 } \ . [ a - z ] { 2 , } ) $ ) " , s t r ( s t r _ v a r ) ) )  
  
 #   R e t u r n s   c o r r e c t   C I D R .   F o r   e x m a m p l e :  
 #       c o r r e c t _ c i d r ( ' 1 9 2 . 1 6 8 . 0 . 1 / 2 4 ' )   =   ' 1 9 2 . 1 6 8 . 0 . 0 / 2 4 '  
 d e f   c o r r e c t _ c i d r ( c i d r ) :  
         #   I f   w e   a c t u a l l y   g o t   a   C I D R  
         i f   ' / '   i n   c i d r :  
                 t r y :  
                         c o r r e c t _ c i d r   =   s t r ( i p a d d r e s s . i p _ i n t e r f a c e ( c i d r ) . n e t w o r k )  
                         i f   c o r r e c t _ c i d r   ! =   c i d r   a n d   l o g _ l e v e l   > =   4 :  
                                 p r i n t ( " W A R N I N G :   c h a n g i n g   w r o n g   C I D R   ' { 0 } '   i n t o   c o r r e c t   C I D R   ' { 1 } ' " . f o r m a t ( c i d r ,   c o r r e c t _ c i d r ) ,   f i l e = s y s . s t d e r r )  
                         r e t u r n   c o r r e c t _ c i d r  
                 e x c e p t :  
                         r e t u r n   c i d r  
         #   O t h e r w i s e ,   w e   p r o b a b l y   j u s t   g o t   a n   I P   a d d r e s s ,   s o   t h e r e   i s   n o t h i n g   t o   c h e c k  
         e l s e :  
                 r e t u r n   c i d r  
  
 #   T r u e   i f   p a r a m e t e r   i s   a n   i p v 4   a d d r e s s  
 d e f   i s _ i p v 4 ( i p _ a d d r e s s ) :  
         #   F i r s t   c h e c k   r e g e x :  
         i f   b o o l ( r e . m a t c h ( r " ^ \ d { 1 , 3 } \ . \ d { 1 , 3 } \ . \ d { 1 , 3 } \ . \ d { 1 , 3 } ( ? : / \ d { 1 , 2 } | ) $ " , s t r ( i p _ a d d r e s s ) ) ) :  
                 #   T h e n   u s e   i p a d d r e s s   m o d u l e :  
                 i f   ' / '   i n   i p _ a d d r e s s :  
                         t r y :    
                                 i p _ a d d r e s s   =   i p a d d r e s s . i p _ i n t e r f a c e ( i p _ a d d r e s s )     #   U s i n g   ' i p _ i n t e r f a c e '   i n s t e a d   o f   ' i p _ n e t w o r k ' ,   s i n c e   w e   c a n   c o r r e c t   l a t e r   w i t h   ' c o r r e c t _ c i d r '  
                                 r e t u r n   T r u e  
                         e x c e p t :  
                                 i f   l o g _ l e v e l   > = 4 :  
                                         p r i n t ( ' W A R N I N G :   i n c o r r e c t   C I D R   { 0 }   f o u n d ' . f o r m a t ( i p _ a d d r e s s ) ,   f i l e = s y s . s t d e r r )  
                                 r e t u r n   F a l s e  
                 e l s e :  
                         t r y :    
                                 i p _ a d d r e s s   =   i p a d d r e s s . i p _ a d d r e s s ( i p _ a d d r e s s )  
                                 r e t u r n   T r u e  
                         e x c e p t :  
                                 i f   l o g _ l e v e l   > = 4 :  
                                         p r i n t ( ' W A R N I N G :   i n c o r r e c t   I P   a d d r e s s   { 0 }   f o u n d ' . f o r m a t ( i p _ a d d r e s s ) ,   f i l e = s y s . s t d e r r )  
                                 r e t u r n   F a l s e  
         e l s e :  
                 r e t u r n   F a l s e  
  
 #   R e t u r n s   t h e   c o n t a i n e d   I P v 4   i s   t h e   p r o v i d e d   s t r i n g   c o n t a i n s   a n   I P v 4   l i k e   1 . 2 . 3 . 4   o r   1 . 2 . 3 . 4 - 3 2    
 d e f   c o n t a i n s _ i p v 4 ( t e x t ) :  
         r e s u l t   =   r e . s e a r c h ( r " \ d { 1 , 3 } \ . \ d { 1 , 3 } \ . \ d { 1 , 3 } \ . \ d { 1 , 3 } ( ? : - \ d { 1 , 2 } | ) $ " , s t r ( t e x t ) )  
         t r y :  
                 r e t u r n   r e s u l t . g r o u p ( ) . r e p l a c e ( ' - ' ,   ' / ' )  
         e x c e p t :  
                 r e t u r n   N o n e  
  
 #   P e r f o r m   s o m e   c h e c k s   o n   t h e   r u l e   t o   a d d ,   a n d   a p p e n d   i t   t o   t h e   l i s t   o f   r u l e s   p r o v i d e d   i n   t h e   2 n d   a r g u m e n t  
 d e f   a p p e n d _ r u l e ( r u l e _ t o _ b e _ a p p e n d e d ,   r u l e s _ t o _ a p p e n d _ t o ) :  
         i f   l o g _ l e v e l   > =   8 :  
                 p r i n t ( " D E B U G :   a p p e n d i n g   t o   r u l e s : " ,   s t r ( r u l e _ t o _ b e _ a p p e n d e d ) ,   f i l e = s y s . s t d e r r )  
         s p l i t _ r u l e _ c o u n t e r   =   0  
         s r c _ f i e l d s   =   ( ' s o u r c e A d d r e s s e s ' ,   ' s o u r c e I p G r o u p s ' )  
         d s t _ f i e l d s   =   ( ' d e s t i n a t i o n A d d r e s s e s ' ,   ' d e s t i n a t i o n I p G r o u p s ' ,   ' d e s t i n a t i o n F q d n s ' )  
         a l l _ f i e l d s   =   s r c _ f i e l d s   +   d s t _ f i e l d s  
         f o r   s r c _ f i e l d   i n   s r c _ f i e l d s :  
                 f o r   d s t _ f i e l d   i n   d s t _ f i e l d s :  
                         #   O n l y   l o o k   a t   c o m b i n a t i o n s   w h e r e   t h e   s r c _ f i e l d   a n d   d s t _ f i e l d   a r e   n o n - z e r o  
                         i f   l e n ( r u l e _ t o _ b e _ a p p e n d e d [ s r c _ f i e l d ] )   >   0   a n d   l e n ( r u l e _ t o _ b e _ a p p e n d e d [ d s t _ f i e l d ] )   >   0 :  
                                 t e m p _ r u l e   =   c o p y . c o p y ( r u l e _ t o _ b e _ a p p e n d e d )  
                                 s p l i t _ r u l e _ c o u n t e r   + =   1  
                                 t e m p _ r u l e [ ' n a m e ' ]   =   t e m p _ r u l e [ ' n a m e ' ]   +   ' - '   +   s t r ( s p l i t _ r u l e _ c o u n t e r )  
                                 #   B l a n k   a l l   t h e   r e s t   f i e l d s d  
                                 f o r   b l a n k _ f i e l d   i n   a l l _ f i e l d s :  
                                         i f   b l a n k _ f i e l d   ! =   s r c _ f i e l d   a n d   b l a n k _ f i e l d   ! =   d s t _ f i e l d :  
                                                 t e m p _ r u l e   [ b l a n k _ f i e l d ]   =   [ ]  
                                 r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( t e m p _ r u l e )  
         i f   s p l i t _ r u l e _ c o u n t e r   >   1 :  
                 i f   l o g _ l e v e l   > =   7 :  
                         p r i n t ( " D E B U G :   P a l o   A l t o   r u l e   i d   { 0 }   h a s   b e e n   s p l i t   i n   { 1 }   A z u r e   F i r e w a l l   r u l e s " . f o r m a t ( r u l e _ t o _ b e _ a p p e n d e d [ ' i d ' ] ,   s p l i t _ r u l e _ c o u n t e r ) ,   f i l e = s y s . s t d e r r )  
         r e t u r n   r u l e s _ t o _ a p p e n d _ t o  
         #   e x i s t _ a d d r e s s e s   =   b o o l ( l e n ( r u l e _ t o _ b e _ a p p e n d e d [ ' d e s t i n a t i o n A d d r e s s e s ' ] )   >   0 )  
         #   e x i s t _ f q d n s   =   b o o l ( l e n ( r u l e _ t o _ b e _ a p p e n d e d [ ' d e s t i n a t i o n F q d n s ' ] )   >   0 )  
         #   e x i s t _ i p g r o u p s   =   b o o l ( l e n ( r u l e _ t o _ b e _ a p p e n d e d [ ' d e s t i n a t i o n I p G r o u p s ' ] )   >   0 )  
         #   o n l y _ e x i s t _ a d d r e s s e s   =   e x i s t _ a d d r e s s e s   a n d   n o t   ( e x i s t _ f q d n s   o r   e x i s t _ i p g r o u p s )  
         #   o n l y _ e x i s t _ f q d n s   =   e x i s t _ f q d n s   a n d   n o t   ( e x i s t _ a d d r e s s e s   o r   e x i s t _ i p g r o u p s )  
         #   o n l y _ e x i s t _ i p g r o u p s   =   e x i s t _ i p g r o u p s   a n d   n o t   ( e x i s t _ a d d r e s s e s   o r   e x i s t _ f q d n s )  
         #   e x i s t _ a d d r e s s e s _ a n d _ f q d n s   =   e x i s t _ a d d r e s s e s   a n d   e x i s t _ f q d n s   a n d   n o t   e x i s t _ i p g r o u p s  
         #   e x i s t _ a d d r e s s e s _ a n d _ i p g r o u p s   =   e x i s t _ a d d r e s s e s   a n d   e x i s t _ i p g r o u p s   a n d   n o t   e x i s t _ f q d n s  
         #   e x i s t _ f q d n s _ a n d _ i p g r o u p s   =   e x i s t _ f q d n s   a n d   e x i s t _ i p g r o u p s   a n d   n o t   e x i s t _ a d d r e s s e s  
         #   e x i s t _ a l l   =   e x i s t _ a d d r e s s e s   a n d   e x i s t _ f q d n s   a n d   e x i s t _ i p g r o u p s  
         #   i f   o n l y _ e x i s t _ a d d r e s s e s   o r   o n l y _ e x i s t _ f q d n s   o r   o n l y _ e x i s t _ i p g r o u p s :  
         #           r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( r u l e _ t o _ b e _ a p p e n d e d )  
         #   e l i f   e x i s t _ a d d r e s s e s _ a n d _ f q d n s :  
         #           r u l e 1   =   c o p y . c o p y ( r u l e _ t o _ b e _ a p p e n d e d )  
         #           r u l e 2   =   c o p y . c o p y ( r u l e _ t o _ b e _ a p p e n d e d )  
         #           r u l e 1 [ ' d e s t i n a t i o n A d d r e s s e s ' ]   =   [ ]  
         #           r u l e 2 [ ' d e s t i n a t i o n F q d n s ' ]   =   [ ]  
         #           r u l e 1 [ ' n a m e ' ]   =   r u l e _ t o _ b e _ a p p e n d e d [ ' n a m e ' ]   +   " - 1 "  
         #           r u l e 2 [ ' n a m e ' ]   =   r u l e _ t o _ b e _ a p p e n d e d [ ' n a m e ' ]   +   " - 2 "  
         #           r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( r u l e 1 )  
         #           r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( r u l e 2 )  
         #   e l i f   e x i s t _ a d d r e s s e s _ a n d _ i p g r o u p s :  
         #           r u l e 1   =   c o p y . c o p y ( r u l e _ t o _ b e _ a p p e n d e d )  
         #           r u l e 2   =   c o p y . c o p y ( r u l e _ t o _ b e _ a p p e n d e d )  
         #           r u l e 1 [ ' d e s t i n a t i o n A d d r e s s e s ' ]   =   [ ]  
         #           r u l e 2 [ ' d e s t i n a t i o n I p G r o u p s ' ]   =   [ ]  
         #           r u l e 1 [ ' n a m e ' ]   =   r u l e _ t o _ b e _ a p p e n d e d [ ' n a m e ' ]   +   " - 1 "  
         #           r u l e 2 [ ' n a m e ' ]   =   r u l e _ t o _ b e _ a p p e n d e d [ ' n a m e ' ]   +   " - 2 "  
         #           r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( r u l e 1 )  
         #           r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( r u l e 2 )  
         #   e l i f   e x i s t _ f q d n s _ a n d _ i p g r o u p s :  
         #           r u l e 1   =   c o p y . c o p y ( r u l e _ t o _ b e _ a p p e n d e d )  
         #           r u l e 2   =   c o p y . c o p y ( r u l e _ t o _ b e _ a p p e n d e d )  
         #           r u l e 1 [ ' d e s t i n a t i o n F q d n s ' ]   =   [ ]  
         #           r u l e 2 [ ' d e s t i n a t i o n I p G r o u p s ' ]   =   [ ]  
         #           r u l e 1 [ ' n a m e ' ]   =   r u l e _ t o _ b e _ a p p e n d e d [ ' n a m e ' ]   +   " - 1 "  
         #           r u l e 2 [ ' n a m e ' ]   =   r u l e _ t o _ b e _ a p p e n d e d [ ' n a m e ' ]   +   " - 2 "  
         #           r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( r u l e 1 )  
         #           r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( r u l e 2 )  
         #   e l i f   e x i s t _ a l l :  
         #           r u l e 1   =   c o p y . c o p y ( r u l e _ t o _ b e _ a p p e n d e d )  
         #           r u l e 2   =   c o p y . c o p y ( r u l e _ t o _ b e _ a p p e n d e d )  
         #           r u l e 3   =   c o p y . c o p y ( r u l e _ t o _ b e _ a p p e n d e d )  
         #           r u l e 1 [ ' d e s t i n a t i o n F q d n s ' ]   =   [ ]  
         #           r u l e 1 [ ' d e s t i n a t i o n I p G r o u p s ' ]   =   [ ]  
         #           r u l e 2 [ ' d e s t i n a t i o n A d d r e s s e s ' ]   =   [ ]  
         #           r u l e 2 [ ' d e s t i n a t i o n I p G r o u p s ' ]   =   [ ]  
         #           r u l e 3 [ ' d e s t i n a t i o n A d d r e s s e s ' ]   =   [ ]  
         #           r u l e 3 [ ' d e s t i n a t i o n F q d n s ' ]   =   [ ]  
         #           r u l e 1 [ ' n a m e ' ]   =   r u l e _ t o _ b e _ a p p e n d e d [ ' n a m e ' ]   +   " - 1 "  
         #           r u l e 2 [ ' n a m e ' ]   =   r u l e _ t o _ b e _ a p p e n d e d [ ' n a m e ' ]   +   " - 2 "  
         #           r u l e 3 [ ' n a m e ' ]   =   r u l e _ t o _ b e _ a p p e n d e d [ ' n a m e ' ]   +   " - 3 "  
         #           r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( r u l e 1 )  
         #           r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( r u l e 2 )  
         #           r u l e s _ t o _ a p p e n d _ t o . a p p e n d ( r u l e 3 )  
         #   r e t u r n   r u l e s _ t o _ a p p e n d _ t o  
  
  
 #   A r g u m e n t s  
 p a r s e r   =   a r g p a r s e . A r g u m e n t P a r s e r ( d e s c r i p t i o n = ' G e n e r a t e   a n   A R M   t e m p l a t e   t o   c r e a t e   a   R u l e   C o l l e c t i o n   G r o u p   i n   a n   A z u r e   p o l i c y   w i t h   r u l e s   t h a t   a l l o w   a c c e s s   t o   M 3 6 5   e n d p o i n t s . ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - c s v - f o l d e r ' ,   d e s t = ' c s v _ f o l d e r ' ,   a c t i o n = ' s t o r e ' ,  
                                         d e f a u l t = " . " ,  
                                         h e l p = ' L o c a l   f o l d e r   c o n t a i n i n g   C S V   f i l e s .   T h e   d e f a u l t   i s   " . " ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - p o l i c y - n a m e ' ,   d e s t = ' p o l i c y _ n a m e ' ,   a c t i o n = ' s t o r e ' ,  
                                         d e f a u l t = " a z f w p o l i c y " ,  
                                         h e l p = ' N a m e   f o r   t h e   A z u r e   F i r e w a l l   P o l i c y .   T h e   d e f a u l t   i s   " a z f w p o l i c y " ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - p o l i c y - s k u ' ,   d e s t = ' p o l i c y _ s k u ' ,   a c t i o n = ' s t o r e ' ,  
                                         d e f a u l t = " S t a n d a r d " ,  
                                         h e l p = ' S K U   f o r   t h e   A z u r e   F i r e w a l l   P o l i c y .   P o s s i b l e   v a l u e s :   S t a n d a r d ,   P r e m i u m   ( d e f a u l t :   S t a n d a r d ) ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - d o - n o t - c r e a t e - p o l i c y ' ,   d e s t = ' d o n t _ c r e a t e _ p o l i c y ' ,   a c t i o n = ' s t o r e _ t r u e ' ,  
                                         d e f a u l t = F a l s e ,  
                                         h e l p = ' I f   s p e c i f i e d ,   d o   n o t   i n c l u d e   A R M   c o d e   f o r   t h e   p o l i c y ,   o n l y   f o r   t h e   r u l e   c o l l e c t i o n   g r o u p .   U s e   i f   t h e   p o l i c y   a l r e a d y   e x i s t s . ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - r c g - n a m e ' ,   d e s t = ' r c g _ n a m e ' ,   a c t i o n = ' s t o r e ' ,  
                                         d e f a u l t = " f r o m p a l o a l t o " ,  
                                         h e l p = ' N a m e   f o r   t h e   R u l e   C o l l e c t i o n   G r o u p   t o   c r e a t e   i n   t h e   A z u r e   F i r e w a l l   P o l i c y .   T h e   d e f a u l t   i s   " o 3 6 5 " ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - r c g - p r i o r i t y ' ,   d e s t = ' r c g _ p r i o ' ,   a c t i o n = ' s t o r e ' ,  
                                         d e f a u l t = " 1 0 0 0 0 " ,  
                                         h e l p = ' P r i o r i t y   f o r   t h e   R u l e   C o l l e c t i o n   G r o u p   t o   c r e a t e   i n   t h e   A z u r e   F i r e w a l l   P o l i c y .   T h e   d e f a u l t   i s   " 1 0 0 0 0 " ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - u s e - i p - g r o u p s ' ,   d e s t = ' u s e _ i p g r o u p s ' ,   a c t i o n = ' s t o r e _ t r u e ' ,  
                                         d e f a u l t = F a l s e ,  
                                         h e l p = ' W h e t h e r   s o m e   a d d r e s s   g r o u p s   s h o u l d   b e   c o n v e r t e d   t o   A z u r e   I P   G r o u p s   ( d e f a u l t :   F a l s e ) ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - m a x - i p - g r o u p s ' ,   d e s t = ' m a x _ i p g r o u p s ' ,   a c t i o n = ' s t o r e ' ,   t y p e = i n t ,   d e f a u l t = 5 0 ,  
                                         h e l p = ' O p t i o n a l ,   m a x i m u m   n u m b e r   o f   I P   g r o u p s   t h a t   w i l l   b e   c r e a t e d   i n   A z u r e ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - r u l e - i d ' ,   d e s t = ' r u l e _ i d ' ,   a c t i o n = ' s t o r e ' ,   t y p e = i n t ,   d e f a u l t = N o n e ,  
                                         h e l p = ' O p t i o n a l ,   i f   s p e c i f i e d   t h e   s c r i p t   w i l l   o n l y   c o n v e r t   a   s p e c i f i c   r u l e   I D ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - o u t p u t ' ,   d e s t = ' f o r m a t ' ,   a c t i o n = ' s t o r e ' ,  
                                         d e f a u l t = " n o n e " ,  
                                         h e l p = ' O u t p u t   f o r m a t .   P o s s i b l e   v a l u e s :   j s o n ,   n o n e ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - i p - v e r s i o n ' ,   d e s t = ' i p _ v e r s i o n ' ,   a c t i o n = ' s t o r e ' ,  
                                         d e f a u l t = " i p v 4 " ,  
                                         h e l p = ' W I P ! ! !   -   I P   v e r s i o n   o f   A z F W   r u l e s .   P o s s i b l e   v a l u e s :   i p v 4 ,   i p v 6 ,   b o t h .   D e f a u l t :   i p v 4 ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - p r e t t y ' ,   d e s t = ' p r e t t y ' ,   a c t i o n = ' s t o r e _ t r u e ' ,  
                                         d e f a u l t = F a l s e ,  
                                         h e l p = ' P r i n t   J S O N   i n   p r e t t y   m o d e   ( d e f a u l t :   F a l s e ) ' )  
 p a r s e r . a d d _ a r g u m e n t ( ' - - l o g - l e v e l ' ,   d e s t = ' l o g _ l e v e l _ s t r i n g ' ,   a c t i o n = ' s t o r e ' ,  
                                         d e f a u l t = ' w a r n i n g ' ,  
                                         h e l p = ' L o g g i n g   l e v e l   ( v a l i d   v a l u e s :   e r r o r / w a r n i n g / i n f o / d e b u g / a l l / n o n e .   D e f a u l t :   w a r n i n g ) ' )  
 a r g s   =   p a r s e r . p a r s e _ a r g s ( )  
  
 #   V a r i a b l e s  
 a z _ a p p _ r u l e s   =   [ ]  
 a z _ n e t _ r u l e s _ a l l o w   =   [ ]  
 a z _ n e t _ r u l e s _ d e n y   =   [ ]  
 r c g _ n a m e   =   a r g s . r c g _ n a m e  
 r c g _ p r i o   =   a r g s . r c g _ p r i o  
 r c _ a p p _ n a m e   =   ' f r o m - p a - a p p '  
 r c _ a p p _ p r i o   =   " 1 1 0 0 0 "  
 r c _ n e t _ n a m e   =   ' f r o m - p a - n e t '  
 r c _ n e t _ p r i o _ a l l o w   =   " 1 0 9 1 0 "  
 r c _ n e t _ p r i o _ d e n y   =   " 1 0 9 0 0 "  
 #   R e t u r n s   t r u e   i f   t h e   s t r i n g   i s   a   n u m b e r  
 d e f   i s _ n u m b e r ( v a l u e ) :  
         f o r   c h a r a c t e r   i n   v a l u e :  
                 i f   c h a r a c t e r . i s d i g i t ( ) :  
                         r e t u r n   T r u e  
         r e t u r n   F a l s e  
  
 #   S e t   l o g _ l e v e l  
 i f   i s _ n u m b e r ( a r g s . l o g _ l e v e l _ s t r i n g ) :  
         t r y :  
                 l o g _ l e v e l   =   i n t ( a r g s . l o g _ l e v e l _ s t r i n g )  
         e x c e p t :  
                 l o g _ l e v e l   =   4  
 e l s e :  
         i f   a r g s . l o g _ l e v e l _ s t r i n g   = =   ' e r r o r ' :  
                 l o g _ l e v e l   =   3  
         e l i f   a r g s . l o g _ l e v e l _ s t r i n g   = =   ' w a r n i n g ' :  
                 l o g _ l e v e l   =   4  
         e l i f   a r g s . l o g _ l e v e l _ s t r i n g   = =   ' n o t i c e ' :  
                 l o g _ l e v e l   =   5  
         e l i f   a r g s . l o g _ l e v e l _ s t r i n g   = =   ' i n f o ' :  
                 l o g _ l e v e l   =   6  
         e l i f   a r g s . l o g _ l e v e l _ s t r i n g   = =   ' d e b u g '   o r   a r g s . l o g _ l e v e l _ s t r i n g   = =   ' a l l ' :  
                 l o g _ l e v e l   =   7  
         e l i f   a r g s . l o g _ l e v e l _ s t r i n g   = =   ' d e b u g p l u s '   o r   a r g s . l o g _ l e v e l _ s t r i n g   = =   ' a l l ' :  
                 l o g _ l e v e l   =   8  
         e l i f   a r g s . l o g _ l e v e l _ s t r i n g   = =   ' n o n e ' :  
                 l o g _ l e v e l   =   0  
         e l s e :  
                 l o g _ l e v e l   =   4       #   W e   d e f a u l t   t o   ' e r r o r '  
  
 #   G e t   f i l e   l i s t   f r o m   t h e   s p e c i f i e d   f o l d e r  
 c s v _ f i l e _ l i s t   =   [ ]  
 i f   a r g s . c s v _ f o l d e r   = =   " . " :  
         c s v _ f o l d e r   =   o s . g e t c w d ( )  
 e l s e :  
         c s v _ f o l d e r   =   a r g s . c s v _ f o l d e r  
 i f   o s . p a t h . i s d i r ( c s v _ f o l d e r ) :  
         i f   l o g _ l e v e l   > =   7 :  
                 p r i n t   ( " D E B U G :   s e a r c h i n g   f o r   C S V   f i l e s   i n   f o l d e r   ' { 0 } ' . . . " . f o r m a t ( c s v _ f o l d e r ) ,   f i l e = s y s . s t d e r r )  
         f o r   f i l e   i n   o s . l i s t d i r ( c s v _ f o l d e r ) :  
                 f i l e _ p a t h   =   o s . p a t h . j o i n ( c s v _ f o l d e r ,   f i l e )  
                 i f   o s . p a t h . i s f i l e ( f i l e _ p a t h )   a n d   o s . p a t h . s p l i t e x t ( f i l e _ p a t h ) [ 1 ]   = =   " . c s v " :  
                         c s v _ f i l e _ l i s t . a p p e n d   ( f i l e _ p a t h )  
                         i f   l o g _ l e v e l   > =   7 :  
                                 p r i n t   ( " D E B U G :   C S V   f i l e   { 0 }   f o u n d " . f o r m a t ( f i l e _ p a t h ) ,   f i l e = s y s . s t d e r r )  
 e l s e :  
         i f   l o g _ l e v e l   > =   3 :  
                 p r i n t   ( " E R R O R :   { 0 }   i s   n o t   a   d i r e c t o r y " . f o r m a t ( c s v _ f o l d e r ) ,   f i l e = s y s . s t d e r r )  
         s y s . e x i t ( 1 )  
  
 #   V e r i f y   t h a t   w e   h a v e   a t   l e a s t   1   C S V   f i l e  
 i f   l e n ( c s v _ f i l e _ l i s t )   = =   0 :  
         i f   l o g _ l e v e l   > =   3 :  
                 p r i n t   ( " E R R O R :   n o   C S V   f i l e s   c o u l d   b e   f o u n d   i n   d i r e c t o r y   { 0 } " . f o r m a t ( c s v _ f o l d e r ) ,   f i l e = s y s . s t d e r r )  
         s y s . e x i t ( 1 )  
  
  
 #   G l o b a l   v a r i a b l e s   t o   c o n t a i n   t h e   r u l e s e t  
 s v c _ g r o u p s   =   [ ]  
 a d d r e s s _ g r o u p s   =   [ ]  
 s v c s   =   [ ]  
 r u l e s   =   [ ]  
  
 #   C o r r e c t s   f o r m a t   o f   a n   e l e m e n t   i n   t h e   C S V  
 #   I f   r e t u r n _ l i s t = T r u e ,   i t   m a k e s   s u r e   t h a t   t h e   r e t u r n e d   v a l u e   i s   a   l i s t  
 d e f   r i g h t _ f o r m a t ( t e x t ,   r e t u r n _ l i s t = F a l s e ) :  
         t e x t   =   s t r . r e p l a c e ( t e x t ,   ' " ' ,   ' ' )  
         i f   ' ; '   i n   t e x t :  
                 t e x t   =   t e x t . s p l i t ( ' ; ' )  
         e l i f   r e t u r n _ l i s t :  
                 t e x t   =   [   t e x t   ]  
         r e t u r n   t e x t  
  
 #   A d d   C S V   t e x t   ( s u p p l i e d   a s   a r g u m e n t )   t o   t h e   s e r v i c e   g r o u p   l i s t  
 #   ' n a m e , l o c a t i o n , m e m b e r s , s e r v i c e s , t a g s '  
 d e f   a d d _ l i n e s _ t o _ s v c _ g r o u p s ( l i n e s ,   s v c _ g r o u p s ) :  
         f o r   l i n e   i n   l i n e s :  
                 l i n e   =   l i n e . s p l i t ( ' , ' )  
                 n e w _ s v c _ g r p   =   {  
                         ' n a m e ' :   r i g h t _ f o r m a t ( l i n e [ 0 ] ) ,  
                         ' m e m b e r s _ c o u n t ' :   r i g h t _ f o r m a t ( l i n e [ 2 ] ) ,  
                         ' s e r v i c e s ' :   r i g h t _ f o r m a t ( l i n e [ 3 ] ,   r e t u r n _ l i s t = T r u e )  
                 }  
                 s v c _ g r o u p s . a p p e n d ( n e w _ s v c _ g r p )  
         r e t u r n   s v c _ g r o u p s  
  
 #   A d d   C S V   t e x t   ( s u p p l i e d   a s   a r g u m e n t )   t o   t h e   s e r v i c e   l i s t  
 #   ' n a m e , l o c a t i o n , p r o t o c o l , d e s t i n a t i o n p o r t , t a g s '  
 d e f   a d d _ l i n e s _ t o _ s v c s ( l i n e s ,   s v c s ) :  
         f o r   l i n e   i n   l i n e s :  
                 l i n e   =   l i n e . s p l i t ( ' , ' )  
                 n e w _ s v c   =   {  
                         ' n a m e ' :   r i g h t _ f o r m a t ( l i n e [ 0 ] ) ,  
                         ' p r o t o c o l ' :   r i g h t _ f o r m a t ( l i n e [ 2 ] ,   r e t u r n _ l i s t = T r u e ) ,  
                         ' d s t _ p o r t ' :   r i g h t _ f o r m a t ( l i n e [ 3 ] ,   r e t u r n _ l i s t = T r u e )  
                 }  
                 s v c s . a p p e n d ( n e w _ s v c )  
         r e t u r n   s v c s  
  
 #   A d d   C S V   t e x t   ( s u p p l i e d   a s   a r g u m e n t )   t o   t h e   a d d r e s s   g r o u p   l i s t  
 #   ' n a m e , l o c a t i o n , m e m b e r s c o u n t , a d d r e s s e s , t a g s '  
 d e f   a d d _ l i n e s _ t o _ a d d r e s s _ g r o u p s ( l i n e s ,   a d d r e s s _ g r o u p s ) :  
         f o r   l i n e   i n   l i n e s :  
                 l i n e   =   l i n e . s p l i t ( ' , ' )  
                 t r y :  
                         n e w _ a d d r e s s _ g r o u p   =   {  
                                 ' n a m e ' :   r i g h t _ f o r m a t ( l i n e [ 0 ] ) ,  
                                 ' m e m b e r s _ c o u n t ' :   i n t ( r i g h t _ f o r m a t ( l i n e [ 2 ] ) ) ,  
                                 ' a d d r e s s e s ' :   r i g h t _ f o r m a t ( l i n e [ 3 ] ,   r e t u r n _ l i s t = T r u e ) ,  
                                 ' k e e p _ a s _ i p _ g r o u p ' :   F a l s e               #   C o n t r o l   f i e l d   t o   d e c i d e   w h e t h e r   c r e a t i n g   a n   A z u r e   I P   G r o u p  
                 }  
                 e x c e p t   E x c e p t i o n   a s   e :  
                         i f   l o g _ l e v e l   > =   3 :  
                                 p r i n t ( " E R R O R :   e r r o r   w h e n   c r e a t i n g   n e w   a d d r e s s   g r o u p   i t e m   o u t   o f   C S V   l i n e   ' { 0 } '   -   { 1 } " . f o r m a t ( l i n e ,   s t r ( e ) ) ,   f i l e = s y s . s t d e r r )  
                 a d d r e s s _ g r o u p s . a p p e n d ( n e w _ a d d r e s s _ g r o u p )  
         r e t u r n   a d d r e s s _ g r o u p s  
  
 #   A d d   C S V   t e x t   ( s u p p l i e d   a s   a r g u m e n t )   t o   t h e   a d d r e s s   g r o u p   l i s t  
 d e f   a d d _ l i n e s _ t o _ r u l e s ( l i n e s ,   r u l e s ) :  
         f o r   l i n e   i n   l i n e s :  
                 l i n e   =   l i n e . s p l i t ( ' , ' )  
                 #   ' , N a m e , L o c a t i o n , T a g s , T y p e , S o u r c e   Z o n e , S o u r c e   A d d r e s s , S o u r c e   U s e r , S o u r c e   H I P   P r o f i l e , D e s t i n a t i o n   Z o n e , D e s t i n a t i o n   A d d r e s s , A p p l i c a t i o n , S e r v i c e , A c t i o n ,  
                 #   P r o f i l e , O p t i o n s , T a r g e t , R u l e   U s a g e   R u l e   U s a g e , R u l e   U s a g e   A p p s   S e e n , R u l e   U s a g e   D a y s   W i t h   N o   N e w   A p p s , M o d i f i e d , C r e a t e d '  
                 i f   l e n ( l i n e )   >   1 5 :  
                         t r y :  
                                 n e w _ r u l e   =   {  
                                         ' i d ' :   r i g h t _ f o r m a t ( l i n e [ 0 ] ) ,  
                                         ' n a m e ' :   r i g h t _ f o r m a t ( l i n e [ 1 ] ) ,  
                                         ' t y p e ' :   r i g h t _ f o r m a t ( l i n e [ 2 ] ) ,  
                                         ' s r c _ z o n e ' :   r i g h t _ f o r m a t ( l i n e [ 5 ] ) ,  
                                         ' s r c _ a d d r e s s ' :   r i g h t _ f o r m a t ( l i n e [ 6 ] ,   r e t u r n _ l i s t = T r u e ) ,  
                                         ' s r c _ u s e r ' :   r i g h t _ f o r m a t ( l i n e [ 7 ] ) ,  
                                         ' s r c _ h i p _ p r o f i l e ' :   r i g h t _ f o r m a t ( l i n e [ 8 ] ) ,  
                                         ' d s t _ z o n e ' :   r i g h t _ f o r m a t ( l i n e [ 9 ] ) ,  
                                         ' d s t _ a d d r e s s ' :   r i g h t _ f o r m a t ( l i n e [ 1 0 ] ,   r e t u r n _ l i s t = T r u e ) ,  
                                         ' a p p l i c a t i o n ' :   r i g h t _ f o r m a t ( l i n e [ 1 1 ] ) ,  
                                         ' s e r v i c e ' :   r i g h t _ f o r m a t ( l i n e [ 1 2 ] ,   r e t u r n _ l i s t = T r u e ) ,  
                                         ' a c t i o n ' :   r i g h t _ f o r m a t ( l i n e [ 1 3 ] ) ,  
                                         ' p r o f i l e ' :   r i g h t _ f o r m a t ( l i n e [ 1 4 ] ) ,  
                                         ' o p t i o n s ' :   r i g h t _ f o r m a t ( l i n e [ 1 5 ] ) ,  
                                         ' t a r g e t ' :   r i g h t _ f o r m a t ( l i n e [ 1 6 ] )  
                                 }  
                                 #   i f   l o g _ l e v e l   > =   7 :  
                                 #           p r i n t ( " D E B U G :   c o n v e r t i n g   C S V   l i n e   { 0 }   t o   o b j e c t   { 1 } " . f o r m a t ( l i n e ,   s t r ( n e w _ r u l e ) ) )  
                                 r u l e s . a p p e n d ( n e w _ r u l e )  
                         e x c e p t   E x c e p t i o n   a s   e :  
                                 i f   l o g _ l e v e l   > =   3 :  
                                         p r i n t ( " E R R O R :   e r r o r   w h e n   p r o c e s s i n g   r u l e   ' { 0 } ' ,   s k i p p i n g   t h i s   p o l i c y -   E r r o r   m e s s a g e :   { 1 } " . f o r m a t ( s t r ( l i n e ) ,   s t r ( e ) ) ,   f i l e = s y s . s t d e r r )  
                 e l s e :  
                         i f   l o g _ l e v e l   > =   7 :  
                                 p r i n t ( " D E B U G :   s k i p p i n g   l i n e   ' { 0 } ' " . f o r m a t ( s t r ( l i n e ) ) ,   f i l e = s y s . s t d e r r )  
         r e t u r n   r u l e s  
  
 #   B r o w s e   t h e   s e r v i c e   g r o u p s   a n d   s e r v i c e s   a n d   e x p a n d   t h e   p r o v i d e d   l i s t   o f   I P   p r o t o c o l s   a n d   p o r t s   w i t h   t h e   f o u n d   r e s u l t s  
 d e f   g e t _ s v c ( s v c _ n a m e ,   p r o t o c o l _ l i s t ,   p o r t _ l i s t ) :  
         s v c _ f o u n d   =   n e x t ( ( x   f o r   x   i n   s v c s   i f   x [ ' n a m e ' ]   = =   s v c _ n a m e ) ,   N o n e )  
         i f   s v c _ f o u n d :  
                 #   i f   l o g _ l e v e l   > =   7 :  
                 #           p r i n t ( ' D E B U G :   : )   s e r v i c e   { 0 }   f o u n d   i n   t h e   e x i s t i n g   l i s t   o f   s e r v i c e s ' . f o r m a t ( s v c _ n a m e ) )  
                 p r o t o c o l _ l i s t   =   l i s t ( s e t ( p r o t o c o l _ l i s t   +   s v c _ f o u n d [ ' p r o t o c o l ' ] ) )  
                 p o r t _ l i s t   + =   s v c _ f o u n d [ ' d s t _ p o r t ' ]  
                 r e t u r n   p r o t o c o l _ l i s t ,   p o r t _ l i s t  
         e l s e :               #   I f   t h e   s e r v i c e   w a s n t   i n   t h e   s e r v i c e   l i s t ,   i t   m i g h t   s t i l l   b e   i n   t h e   s e r v i c e   g r o u p   l i s t  
                 s v c _ g r p _ f o u n d   =   n e x t ( ( x   f o r   x   i n   s v c _ g r o u p s   i f   x [ ' n a m e ' ]   = =   s v c _ n a m e ) ,   N o n e )  
                 s v c _ p r o t o c o l s   =   [ ]  
                 s v c _ p o r t s   =   [ ]  
                 i f   s v c _ g r p _ f o u n d :  
                         f o r   s v c _ g r p _ m e m b e r   i n   s v c _ g r p _ f o u n d [ ' s e r v i c e s ' ] :  
                                 s v c _ p r o t o c o l s ,   s v c _ p o r t s   =   g e t _ s v c ( s v c _ g r p _ m e m b e r ,   s v c _ p r o t o c o l s ,   s v c _ p o r t s )  
                         p r o t o c o l _ l i s t   =   l i s t ( s e t ( p r o t o c o l _ l i s t   +   s v c _ p r o t o c o l s ) )  
                         p o r t _ l i s t   + =   s v c _ p o r t s  
                         r e t u r n   p r o t o c o l _ l i s t ,   p o r t _ l i s t  
                 e l s e :  
                         i f   l o g _ l e v e l   > =   4 :  
                                 p r i n t ( ' W A R N I N G :   s e r v i c e   { 0 }   n o t   f o u n d   i n   t h e   e x i s t i n g   l i s t   o f   s e r v i c e s ' . f o r m a t ( s v c _ n a m e ) ,   f i l e = s y s . s t d e r r )  
                         #   R e t u r n   u n c h a n g e d   p a r a m e t e r s  
                         r e t u r n   p r o t o c o l _ l i s t ,   p o r t _ l i s t  
  
 #   R e s o l v e   I P   a d d r e s s e s   i n   t e x t :  
 #   -   I P _   - >   I P v 4 / I P v 6  
 #   -   S e a r c h   i n   I P   g r o u p s  
 #   T H e   l a s t   p a r a m e t e r   i s   a   w a y   t o   o v e r r i d e   g e n e r i c   - - v e r b o s e   s e t t i n g ,   t o   m i n i m i z e   o u t p u t  
 d e f   r e s o l v e _ a d d r e s s e s   ( t e x t _ l i s t ,   a d d r e s s _ l i s t ,   f q d n _ l i s t ,   i p g r o u p _ l i s t ,   f u n c t i o n _ d e b u g = T r u e ,   r e s o l v e _ a z i p g r o u p s = T r u e ) :  
         #   I f   w e   g o t   a   s c a l a r ,   t r a n s f o r m   t o   a   l i s t  
         i f   h a s a t t r ( t e x t _ l i s t ,   ' l o w e r ' ) :  
                 t e x t _ l i s t   =   [   t e x t _ l i s t   ]  
         #   T h e r e   a r e   s o m e   e r r o r s   s u g g e s t i n g   a d d r e s s _ l i s t   i s   a   t u p l e ,   s o   m a k i n g   s u r e   t h a t   i t   i s   a   l i s t   h e r e :  
         a d d r e s s _ l i s t   =   l i s t ( a d d r e s s _ l i s t )  
         f q d n _ l i s t   =   l i s t ( f q d n _ l i s t )  
         #   G o   o v e r   e a c h   I P   i n   t h e   l i s t  
         t r y :  
                 f o r   t e x t   i n   t e x t _ l i s t :  
                         #   I f   t h i s   i s   a n   I P   a d d r e s s  
                         i f   t e x t [ : 3 ] . l o w e r ( )   = =   ' i p _ ' :  
                                 i f   a r g s . i p _ v e r s i o n   i n   ( ' i p v 4 ' ,   ' b o t h ' ) :  
                                         i p _ a d d r e s s   =   t e x t [ 3 : ] . r e p l a c e ( ' - ' ,   ' / ' )  
                                         i f   i s _ i p v 4 ( i p _ a d d r e s s ) :  
                                                 a d d r e s s _ l i s t . a p p e n d ( c o r r e c t _ c i d r ( i p _ a d d r e s s ) )  
                         #   I f   t h i s   i s   a n   I P v 6   a d d r e s s  
                         e l i f   t e x t [ : 4 ] . l o w e r ( )   = =   ' i p v 6 ' :  
                                 i f   a r g s . i p _ v e r s i o n   i n   ( ' i p v 6 ' ,   ' b o t h ' ) :  
                                         t e x t   =   t e x t [ 5 : ] . r e p l a c e ( ' - ' ,   ' / ' )  
                                         t e x t   =   t e x t . r e p l a c e ( ' . ' ,   ' : ' )  
                                         a d d r e s s _ l i s t . a p p e n d ( t e x t )  
                         #   I f   i t   i s   a   s u b n e t  
                         e l i f   t e x t [ : 4 ] . l o w e r ( )   i n   ( ' n e t _ ' ,   ' b l k _ ' ) :  
                                 i f   a r g s . i p _ v e r s i o n   i n   ( ' i p v 4 ' ,   ' b o t h ' ) :  
                                         c i d r   =   t e x t [ 4 : ] . r e p l a c e ( ' - ' ,   ' / ' )  
                                         i f   i s _ i p v 4 ( c i d r ) :  
                                                 a d d r e s s _ l i s t . a p p e n d ( c o r r e c t _ c i d r ( c i d r ) )  
                         e l i f   t e x t [ : 6 ] . l o w e r ( )   = =   ' v 6 _ n e t ' :  
                                 i f   a r g s . i p _ v e r s i o n   i n   ( ' i p v 6 ' ,   ' b o t h ' ) :  
                                         t e x t   =   t e x t [ 7 : ] . r e p l a c e ( ' - ' ,   ' / ' )  
                                         t e x t   =   t e x t . r e p l a c e ( ' . ' ,   ' : ' )  
                                         a d d r e s s _ l i s t . a p p e n d ( t e x t )  
                         #   I f   i t   i s   a   s e r v i c e   t a g  
                         e l i f   t e x t [ : 1 0 ] . l o w e r ( )   = =   ' s e r v i c e t a g ' :  
                                 i f   ' a z u r e s t o r a g e '   i n   t e x t . l o w e r ( ) :  
                                         a d d r e s s _ l i s t . a p p e n d ( ' S t o r a g e ' )  
                         #   S p e c i a l   k e y w o r d s  
                         e l i f   t e x t . l o w e r ( )   = =   ' a l l _ i p v 4 ' :  
                                 i f   a r g s . i p _ v e r s i o n   i n   ( ' i p v 4 ' ,   ' b o t h ' ) :  
                                         a d d r e s s _ l i s t . a p p e n d ( ' 0 . 0 . 0 . 0 / 0 ' )  
                         e l i f   t e x t . l o w e r ( )   = =   ' a l l _ i p v 6 ' :  
                                 i f   a r g s . i p _ v e r s i o n   i n   ( ' i p v 6 ' ,   ' b o t h ' ) :  
                                         a d d r e s s _ l i s t . a p p e n d ( ' : : / 0 ' )  
                         #   M a y b e   i t   i s   a l r e a d y   a n   I P  
                         e l i f   i s _ i p v 4 ( t e x t ) :  
                                 i f   a r g s . i p _ v e r s i o n   i n   ( ' i p v 4 ' ,   ' b o t h ' ) :  
                                         a d d r e s s _ l i s t . a p p e n d ( t e x t )  
                         e l i f   c o n t a i n s _ i p v 4 ( t e x t ) :  
                                 i f   a r g s . i p _ v e r s i o n   i n   ( ' i p v 4 ' ,   ' b o t h ' ) :  
                                         a d d r e s s _ l i s t . a p p e n d ( c o r r e c t _ c i d r ( c o n t a i n s _ i p v 4 ( t e x t ) ) )  
                         #   I f   i t   i s   a n   F Q D N   w e   u p d a t e   t h e   f q d n _ l i s t  
                         e l i f   t e x t [ : 5 ] . l o w e r ( )   = =   ' f q d n _ ' :  
                                 f q d n _ l i s t . a p p e n d ( t e x t [ 5 : ] )  
                         #   W e   m i g h t   w a n t   t o   c h e c k   w h e t h e r   i t   i s   a n   a d d r e s s   g r o u p   w i t h   ' i s _ a d d r e s s _ g r o u p '   b e f o r e   t h e   f q d n   ( a d d r e s s   g r o u p   n a m e s   m i g h t   b e   m i s t a k i n g l y   t a k e n   f o r   F Q D N s . . . )  
                         #   I t   c o u l d   b e   a n   F Q D N   w i t h o u t   t h e   ' f q d n _ '   p r e f i x  
                         e l i f   i s _ f q d n ( t e x t ) :  
                                 f q d n _ l i s t . a p p e n d ( t e x t )  
                         #   O t h e r w i s e   w e   a s s u m e   a n   I P   a d d r e s s   g r o u p  
                         e l s e :  
                                 i p _ g r p _ f o u n d   =   n e x t ( ( x   f o r   x   i n   a d d r e s s _ g r o u p s   i f   x [ ' n a m e ' ]   = =   t e x t ) ,   N o n e )  
                                 i f   i p _ g r p _ f o u n d :  
                                         i f   n o t   r e s o l v e _ a z i p g r o u p s   a n d   i p _ g r p _ f o u n d [ ' k e e p _ a s _ i p _ g r o u p ' ] :  
                                                 #   i p g r o u p _ l i s t . a p p e n d ( t e x t )               #   I f   w e   a r e   k e e p i n g   t h e   a d d r e s s   g r o u p   a s   a n   A z u r e   I P   G r o u p ,   r e t u r n   t h e   n a m e   o f   h t e   i p g r o u p  
                                                 i p g r o u p _ l i s t . a p p e n d ( " [ r e s o u r c e I d ( ' M i c r o s o f t . N e t w o r k / i p G r o u p s ' ,   ' { 0 } ' ) ] " . f o r m a t ( t e x t ) )             #   A d d   t h e   I P   g r o u p   w i t h   i t s   A R M   I D   f o r m a t  
                                         e l s e :  
                                                 a d d r e s s _ l i s t ,   f q d n _ l i s t ,   i p g r o u p _ l i s t   =   r e s o l v e _ a d d r e s s e s ( i p _ g r p _ f o u n d [ ' a d d r e s s e s ' ] ,   a d d r e s s _ l i s t ,   f q d n _ l i s t ,   i p g r o u p _ l i s t )  
                                 e l s e :  
                                         i f   l o g _ l e v e l   > =   4   a n d   f u n c t i o n _ d e b u g :  
                                                 p r i n t ( ' W A R N I N G :   I P   a d d r e s s   { 0 }   c o u l d   n o t   b e   r e s o l v e d ) ' . f o r m a t ( t e x t ) ,   f i l e = s y s . s t d e r r )  
                                         #   R e t u r n   u n c h a n g e d   p a r a m e t e r s  
                                         r e t u r n   a d d r e s s _ l i s t ,   f q d n _ l i s t ,   i p g r o u p _ l i s t  
         e x c e p t   E x c e p t i o n   a s   e :  
                 i f   l o g _ l e v e l   > =   3 :  
                         p r i n t ( " E R R O R :   e r r o r   w h e n   r e s o l v i n g   I P   { 0 }   i n   l i s t   { 1 }   -   E r r o r   m e s s a g e :   { 2 } " . f o r m a t ( t e x t ,   s t r ( t e x t _ l i s t ) ,   s t r ( e ) ) ,   f i l e = s y s . s t d e r r )  
         r e t u r n   a d d r e s s _ l i s t ,   f q d n _ l i s t ,   i p g r o u p _ l i s t  
  
 #   A n a l y z e   t h e   f o u n d   f i l e s   l o o k i n g   f o r   a   m a t c h   o f   t h e   h e a d e r s   l i n e  
 #   T O   D O :   T h e   h e a d e r   l i n e   c o m p a r i s o n   c o u l d   b e   m a d e   l e s s   f r a g i l e  
 s v c _ g r o u p _ f i l e _ l i s t   =   [ ]  
 s v c _ f i l e _ l i s t   =   [ ]  
 a d d r e s s _ g r o u p _ f i l e _ l i s t   =   [ ]  
 r u l e _ f i l e _ l i s t   =   [ ]  
 a d d r e s s _ g r o u p _ h e a d e r   =   ' n a m e , l o c a t i o n , m e m b e r s c o u n t , a d d r e s s e s , t a g s '  
 s v c _ g r o u p _ h e a d e r   =   ' n a m e , l o c a t i o n , m e m b e r s , s e r v i c e s , t a g s '  
 s v c _ h e a d e r   =   ' n a m e , l o c a t i o n , p r o t o c o l , d e s t i n a t i o n p o r t , t a g s '  
 r u l e _ h e a d e r   =   ' , n a m e , l o c a t i o n , t a g s , t y p e , s o u r c e z o n e , s o u r c e a d d r e s s , s o u r c e u s e r , s o u r c e h i p p r o f i l e , d e s t i n a t i o n z o n e , d e s t i n a t i o n a d d r e s s , a p p l i c a t i o n , s e r v i c e , a c t i o n , p r o f i l e , o p t i o n s , t a r g e t , r u l e u s a g e r u l e u s a g e , r u l e u s a g e a p p s s e e n , r u l e u s a g e d a y s w i t h n o n e w a p p s , m o d i f i e d , c r e a t e d '  
 f o r   f i l e   i n   c s v _ f i l e _ l i s t :  
         f   =   o p e n ( f i l e , " r " )  
         l i n e s   =   f . r e a d l i n e s ( )  
         h e a d e r _ l i n e   =   l i n e s [ 0 ] . r s t r i p ( ) . l o w e r ( )  
         h e a d e r _ l i n e   =   s t r . r e p l a c e ( h e a d e r _ l i n e ,   '   ' ,   ' ' )  
         h e a d e r _ l i n e   =   s t r . r e p l a c e ( h e a d e r _ l i n e ,   ' " ' ,   ' ' )  
         h e a d e r _ l i n e   =   h e a d e r _ l i n e [ 1 : ]       #   T h e   f i r s t   c h a r a c t e r   s e e m s   t o   b e   w e i r d  
         i f   h e a d e r _ l i n e   = =   s v c _ g r o u p _ h e a d e r :  
                 s v c _ g r o u p _ f i l e _ l i s t . a p p e n d ( f i l e )  
                 s v c _ g r o u p s   =   a d d _ l i n e s _ t o _ s v c _ g r o u p s   ( l i n e s [ 1 : ] ,   s v c _ g r o u p s )  
                 i f   l o g _ l e v e l   > =   7 :  
                         p r i n t   ( " D E B U G :   f i l e   { 0 }   s e e m s   t o   c o n t a i n   s e r v i c e   g r o u p s " . f o r m a t ( f i l e ) ,   f i l e = s y s . s t d e r r )  
         e l i f   h e a d e r _ l i n e   = =   a d d r e s s _ g r o u p _ h e a d e r :  
                 a d d r e s s _ g r o u p _ f i l e _ l i s t . a p p e n d ( f i l e )  
                 a d d r e s s _ g r o u p s   =   a d d _ l i n e s _ t o _ a d d r e s s _ g r o u p s   ( l i n e s [ 1 : ] ,   a d d r e s s _ g r o u p s )  
                 i f   l o g _ l e v e l   > =   7 :  
                         p r i n t   ( " D E B U G :   f i l e   { 0 }   s e e m s   t o   c o n t a i n   a d d r e s s   g r o u p s " . f o r m a t ( f i l e ) ,   f i l e = s y s . s t d e r r )  
         e l i f   h e a d e r _ l i n e   = =   s v c _ h e a d e r :  
                 s v c _ f i l e _ l i s t . a p p e n d ( f i l e )  
                 s v c s   =   a d d _ l i n e s _ t o _ s v c s   ( l i n e s [ 1 : ] ,   s v c s )  
                 i f   l o g _ l e v e l   > =   7 :  
                         p r i n t   ( " D E B U G :   f i l e   { 0 }   s e e m s   t o   c o n t a i n   s e r v i c e s " . f o r m a t ( f i l e ) ,   f i l e = s y s . s t d e r r )  
         e l i f   h e a d e r _ l i n e   = =   r u l e _ h e a d e r :  
                 r u l e _ f i l e _ l i s t . a p p e n d ( f i l e )  
                 r u l e s   =   a d d _ l i n e s _ t o _ r u l e s   ( l i n e s [ 1 : ] ,   r u l e s )  
                 i f   l o g _ l e v e l   > =   7 :  
                         p r i n t   ( " D E B U G :   f i l e   { 0 }   s e e m s   t o   c o n t a i n   p o l i c i e s " . f o r m a t ( f i l e ) ,   f i l e = s y s . s t d e r r )  
         e l s e :  
                 i f   l o g _ l e v e l   > =   4 :  
                         p r i n t   ( " W A R N I N G :   i g n o r i n g   f i l e   { 0 } ,   c o u l d n ' t   i d e n t i f y   f o r m a t   o f   h e a d e r   l i n e   ' { 1 } '   ( f i r s t   c h a r a c t e r   i s   ' { 2 } ' ) " . f o r m a t ( f i l e ,   h e a d e r _ l i n e ,   h e a d e r _ l i n e [ 0 ] ) ,   f i l e = s y s . s t d e r r )  
  
 t r y :  
         t o p   =   2  
         i f   l o g _ l e v e l   > =   6 :  
                 p r i n t   ( " I N F O :   { 0 }   s e r v i c e s   r e t r i e v e d   o u t   o f   { 1 }   f i l e s " . f o r m a t ( s t r ( l e n ( s v c s ) ) ,   s t r ( l e n ( s v c _ f i l e _ l i s t ) ) ) ,   f i l e = s y s . s t d e r r )  
                 i f   l o g _ l e v e l   > =   8 :  
                         f o r   i   i n   r a n g e ( 0 ,   t o p ) :  
                                 p r i n t ( ' D E B U G :   -   { 0 } :   { 1 } ' . f o r m a t ( s t r ( i ) ,   s t r ( s v c s [ i ] ) ) ,   f i l e = s y s . s t d e r r )  
         i f   l o g _ l e v e l   > =   6 :  
                 p r i n t   ( " I N F O :   { 0 }   s e r v i c e   g r o u p s   r e t r i e v e d   o u t   o f   { 1 }   f i l e s " . f o r m a t ( s t r ( l e n ( s v c _ g r o u p s ) ) ,   s t r ( l e n ( s v c _ g r o u p _ f i l e _ l i s t ) ) ) ,   f i l e = s y s . s t d e r r )  
                 i f   l o g _ l e v e l   > =   8 :  
                         f o r   i   i n   r a n g e ( 0 ,   t o p ) :  
                                 p r i n t ( ' D E B U G :   -   { 0 } :   { 1 } ' . f o r m a t ( s t r ( i ) ,   s t r ( s v c _ g r o u p s [ i ] ) ) )  
         i f   l o g _ l e v e l   > =   6 :  
                 p r i n t   ( " I N F O :   { 0 }   a d d r e s s   g r o u p s   r e t r i e v e d   o u t   o f   { 1 }   f i l e s " . f o r m a t ( s t r ( l e n ( a d d r e s s _ g r o u p s ) ) ,   s t r ( l e n ( a d d r e s s _ g r o u p _ f i l e _ l i s t ) ) ) ,   f i l e = s y s . s t d e r r )  
                 i f   l o g _ l e v e l   > =   8 :  
                         f o r   i   i n   r a n g e ( 0 ,   t o p ) :  
                                 p r i n t ( ' D E B U G :   -   { 0 } :   { 1 } ' . f o r m a t ( s t r ( i ) ,   s t r ( a d d r e s s _ g r o u p s [ i ] ) ) ,   f i l e = s y s . s t d e r r )  
         i f   l o g _ l e v e l   > =   6 :  
                 p r i n t   ( " I N F O :   { 0 }   r u l e s   r e t r i e v e d   o u t   o f   { 1 }   f i l e s " . f o r m a t ( s t r ( l e n ( r u l e s ) ) ,   s t r ( l e n ( r u l e _ f i l e _ l i s t ) ) ) ,   f i l e = s y s . s t d e r r )  
                 i f   l o g _ l e v e l   > =   8 :  
                         f o r   i   i n   r a n g e ( 0 ,   t o p ) :  
                                 p r i n t ( ' D E B U G :   -   { 0 } :   { 1 } ' . f o r m a t ( s t r ( i ) ,   s t r ( r u l e s [ i ] ) ) ,   f i l e = s y s . s t d e r r )  
 e x c e p t :  
         p a s s  
  
 #   N o w   w e   h a v e   t h e   I P   g r o u p s ,   w e   c a n   f i n d   o u t   w h i c h   o n e s   a r e   k e p t   ( m i n i m u m   m e m b e r   c o u n t )  
 d e f   g e t _ m e m b e r _ c o u n t ( e ) :  
     r e t u r n   e [ ' m e m b e r s _ c o u n t ' ]  
 a d d r e s s _ g r o u p s . s o r t ( r e v e r s e = T r u e ,   k e y = g e t _ m e m b e r _ c o u n t )  
 v a l i d _ i p _ g r o u p _ c o u n t   =   0  
 m a x _ i p _ g r o u p   =   0  
 m i n _ i p _ g r o u p   =   0  
 f o r   a d d r e s s _ g r o u p   i n   a d d r e s s _ g r o u p s :  
         i p s ,   f q d n s ,   i p g r o u p s   =   r e s o l v e _ a d d r e s s e s ( a d d r e s s _ g r o u p [ ' a d d r e s s e s ' ] ,   [ ] ,   [ ] ,   [ ] ,   f u n c t i o n _ d e b u g = F a l s e ,   r e s o l v e _ a z i p g r o u p s = T r u e )  
         a d d r e s s _ g r o u p [ ' r e s o l v e d _ i p s ' ]   =   i p s  
         a d d r e s s _ g r o u p [ ' r e s o l v e d _ f q d n s ' ]   =   i p s  
         i f   v a l i d _ i p _ g r o u p _ c o u n t   <   a r g s . m a x _ i p g r o u p s   a n d   f q d n s   = =   [ ] :  
                 t r y :  
                         a d d r e s s _ g r o u p [ ' m e m b e r s _ c o u n t ' ]   =   i n t ( a d d r e s s _ g r o u p [ ' m e m b e r s _ c o u n t ' ] )  
                 e x c e p t :  
                         i f   l o g _ l e v e l   > =   3 :  
                                 p r i n t ( ' E R R O R :   m e m b e r s   c o u n t   i n   I P   g r o u p   h a s   a   n o n - n u m e r i c   v a l u e ' ,   f i l e = s y s . s t d e r r )  
                 a d d r e s s _ g r o u p [ ' k e e p _ a s _ i p _ g r o u p ' ]   =   T r u e  
                 v a l i d _ i p _ g r o u p _ c o u n t   + = 1  
                 m i n _ i p _ g r o u p   =   a d d r e s s _ g r o u p [ ' m e m b e r s _ c o u n t ' ]  
                 i f   m a x _ i p _ g r o u p   <   m i n _ i p _ g r o u p :   m a x _ i p _ g r o u p   =   m i n _ i p _ g r o u p  
 i f   l o g _ l e v e l   > =   7 :  
         p r i n t ( ' D E B U G :   { 0 }   I P   g r o u p s   w o u l d   b e   k e p t ,   m i n i m u m   m e m b e r   c o u n t   o f   t h e s e   g r o u p s   i s   { 1 } ,   m a x i m u m   i s   { 2 } ' . f o r m a t ( v a l i d _ i p _ g r o u p _ c o u n t ,   m i n _ i p _ g r o u p ,   m a x _ i p _ g r o u p ) ,   f i l e = s y s . s t d e r r )  
  
 #   G o   t h r o u g h   t h e   r u l e s  
 c n t _ a l l o w   =   0  
 c n t _ d e n y   =   0  
 c n t _ d i s a b l e d r u l e s   =   0  
 c n t _ a p p r u l e s   =   0  
 c n t _ n e t r u l e s _ i p   =   0  
 c n t _ n e t r u l e s _ f q d n   =   0  
 c n t _ p a _ r u l e s   =   0  
 i f   a r g s . r u l e _ i d   a n d   l o g _ l e v e l   > = 7 :  
         p r i n t ( " D E B U G :   l o o k i n g   f o r   r u l e " ,   s t r ( a r g s . r u l e _ i d ) ,   f i l e = s y s . s t d e r r )  
 f o r   r u l e   i n   r u l e s :  
         c n t _ p a _ r u l e s   + =   1  
         #   I f   a   r u l e - i d   w a s   s p e c i f i e d   a s   p a r a m e t e r ,   o n l y   d o   t h a t   o n e  
         i f   n o t   a r g s . r u l e _ i d   o r   ( a r g s . r u l e _ i d   a n d   r u l e [ ' i d ' ]   = =   s t r ( a r g s . r u l e _ i d ) ) :  
                 #   L o o k   o u t   f o r   d i s a b l e d   r u l e s  
                 i f   r u l e [ ' n a m e ' ] [ : 1 0 ]   = =   ' [ D i s a b l e d ] ' :  
                         c n t _ d i s a b l e d r u l e s   + =   1  
                         #   i f   l o g _ l e v e l   > =   7 :  
                         #           p r i n t ( ' D E B U G :   i g n o r i n g   d i s a b l e d   r u l e : ' ,   s t r ( r u l e ) )  
                 e l s e :  
                         i f   l o g _ l e v e l   > =   8 :  
                                 p r i n t ( " D E B U G :   p r o c e s s i n g   r u l e   { 0 }   -   ' { 1 } ' :   { 2 } " . f o r m a t ( r u l e [ ' i d ' ] ,   r u l e [ ' n a m e ' ] ,   s t r ( r u l e ) ) ,   f i l e = s y s . s t d e r r )  
                         #   L o g     u s e r   /   H I P   p r o f i l e  
                         i f   r u l e [ ' s r c _ u s e r ' ]   ! =   ' a n y '   a n d   l o g _ l e v e l   > =   4 :  
                                 p r i n t ( " W A R N I N G :   r u l e   ' { 0 } '   u s i n g   i d e n t i t y   s o u r c e   ' { 1 } ' " . f o r m a t ( r u l e [ ' n a m e ' ] ,   r u l e [ ' s r c _ u s e r ' ] ) ,   f i l e = s y s . s t d e r r )  
                         i f   r u l e [ ' s r c _ h i p _ p r o f i l e ' ]   ! =   ' a n y '   a n d   l o g _ l e v e l   > =   4 :  
                                 p r i n t ( " W A R N I N G :   r u l e   ' { 0 } '   u s i n g   s o u r c e   H I P   p r o f i l e   ' { 1 } ' " . f o r m a t ( r u l e [ ' n a m e ' ] ,   r u l e [ ' s r c _ h i p _ p r o f i l e ' ] ) ,   f i l e = s y s . s t d e r r )  
                         c n t _ n e t r u l e s _ i p   + =   1  
                         n e w _ r u l e   =   {  
                                 ' n a m e ' :   ' i d '   +   s t r ( r u l e [ ' i d ' ] )   +   ' - '   +   s t r ( r u l e [ ' n a m e ' ] ) . r e p l a c e ( '   ' ,   ' ' ) ,  
                                 ' r u l e T y p e ' :   ' N e t w o r k R u l e ' ,  
                                 ' s o u r c e A d d r e s s e s ' :   [ ] ,  
                                 ' s o u r c e I p G r o u p s ' :   [ ] ,  
                                 ' d e s t i n a t i o n A d d r e s s e s ' :   [ ] ,  
                                 ' d e s t i n a t i o n F q d n s ' :   [ ] ,  
                                 ' d e s t i n a t i o n I p G r o u p s ' :   [ ]  
                         }  
                         #   S o u r c e s  
                         i f   r u l e [ ' s r c _ a d d r e s s ' ]   = =   [   ' a n y '   ] :  
                                 n e w _ r u l e [ ' s o u r c e A d d r e s s e s ' ]   =   ' * ' ,  
                                 n e w _ r u l e [ ' s o u r c e I p G r o u p s ' ]   =   [ ]  
                         e l s e :  
                                 i f   a r g s . u s e _ i p g r o u p s :  
                                         s r c _ i p s ,   s r c _ f q d n s ,   s r c _ i p g r o u p s   =   r e s o l v e _ a d d r e s s e s ( r u l e [ ' s r c _ a d d r e s s ' ] ,   [ ] ,   [ ] ,   [ ] ,   r e s o l v e _ a z i p g r o u p s = F a l s e )  
                                 e l s e :  
                                         s r c _ i p s ,   s r c _ f q d n s ,   s r c _ i p g r o u p s   =   r e s o l v e _ a d d r e s s e s ( r u l e [ ' s r c _ a d d r e s s ' ] ,   [ ] ,   [ ] ,   [ ] ,   r e s o l v e _ a z i p g r o u p s = T r u e )  
                                 n e w _ r u l e [ ' s o u r c e A d d r e s s e s ' ]   =   s r c _ i p s  
                                 n e w _ r u l e [ ' s o u r c e I p G r o u p s ' ]   =   s r c _ i p g r o u p s  
                         #   D e s t i n a t i o n s  
                         i f   r u l e [ ' d s t _ a d d r e s s ' ]   = =   [   ' a n y '   ] :  
                                 n e w _ r u l e [ ' d e s t i n a t i o n A d d r e s s e s ' ]   =   ' * ' ,  
                         e l s e :  
                                 i f   a r g s . u s e _ i p g r o u p s :  
                                         d s t _ i p s ,   d s t _ f q d n s ,   d s t _ i p g r o u p s   =   r e s o l v e _ a d d r e s s e s ( r u l e [ ' d s t _ a d d r e s s ' ] ,   [ ] ,   [ ] ,   [ ] ,   r e s o l v e _ a z i p g r o u p s = F a l s e )  
                                 e l s e :  
                                         i p s ,   f q d n s ,   i p g r o u p s   =   r e s o l v e _ a d d r e s s e s ( r u l e [ ' d s t _ a d d r e s s ' ] ,   [ ] ,   [ ] ,   [ ] ,   r e s o l v e _ a z i p g r o u p s = T r u e )  
                                 n e w _ r u l e [ ' d e s t i n a t i o n A d d r e s s e s ' ]   =   d s t _ i p s  
                                 n e w _ r u l e [ ' d e s t i n a t i o n F q d n s ' ]   =   d s t _ f q d n s  
                                 n e w _ r u l e [ ' d e s t i n a t i o n I p G r o u p s ' ]   =   d s t _ i p g r o u p s  
                         #   S e r v i c e  
                         i f   r u l e [ ' s e r v i c e ' ]   = =   [   ' a n y '   ] :  
                                 n e w _ r u l e [ ' i p P r o t o c o l s ' ]   =   [   ' A n y '   ]  
                                 n e w _ r u l e [ ' d e s t i n a t i o n P o r t s ' ]   =   [   ' * '   ]  
                         e l i f   r u l e [ ' s e r v i c e ' ]   = =   [   ' a p p l i c a t i o n - d e f a u l t '   ] :  
                                 n e w _ r u l e [ ' i p P r o t o c o l s ' ]   =   [   ' A n y '   ]  
                                 n e w _ r u l e [ ' d e s t i n a t i o n P o r t s ' ]   =   [   ' * '   ]  
                         e l s e :  
                                 #   I f   ' s e r v i c e '   c a n   o n l y   b e   a n   a r r a y  
                                 f o r   r u l e _ s v c   i n   r u l e [ ' s e r v i c e ' ] :  
                                         n e w _ r u l e _ p r o t o c o l s ,   n e w _ r u l e _ p o r t s   =   g e t _ s v c ( r u l e _ s v c ,   [ ] ,   [ ] )  
                                 #   A p p l i c a t i o n  
                                 #   i f   h a s a t t r ( r u l e [ ' a p p l i c a t i o n ' ] ,   ' l o w e r ' ) :     #   I f   r u l e [ ' a p p l i c a t i o n ' ]   i s   a   s t r i n g  
                                 #           n e w _ r u l e _ p r o t o c o l s ,   n e w _ r u l e _ p o r t s   =   g e t _ s v c ( r u l e [ ' a p p l i c a t i o n ' ] ,   n e w _ r u l e _ p r o t o c o l s ,   n e w _ r u l e _ p o r t s )  
                                 #   e l s e :                                                                             #   I f   r u l e [ ' a p p l i c a t i o n ' ]   i s   a   l i s t   o f   s t r i n g s  
                                 #           f o r   r u l e _ s v c   i n   r u l e [ ' a p p l i c a t i o n ' ] :  
                                 #                   n e w _ r u l e _ p r o t o c o l s ,   n e w _ r u l e _ p o r t s   =   g e t _ s v c ( r u l e _ s v c ,   n e w _ r u l e _ p r o t o c o l s ,   n e w _ r u l e _ p o r t s )  
                                 n e w _ r u l e [ ' i p P r o t o c o l s ' ]   =   n e w _ r u l e _ p r o t o c o l s  
                                 n e w _ r u l e _ p o r t s   =   l i s t ( s e t ( n e w _ r u l e _ p o r t s ) )  
                                 n e w _ r u l e [ ' d e s t i n a t i o n P o r t s ' ]   =   n e w _ r u l e _ p o r t s  
                         #   D E B U G :   p r i n t   b u i l t   u p   r u l e  
                         i f   l o g _ l e v e l   > =   8 :  
                                 p r i n t ( " D E B U G :   t h i s   i s   t h e   J S O N   b u i l t   f o r   r u l e   { 0 }   -   { 1 } :   ' { 2 } ' " . f o r m a t ( r u l e [ ' i d ' ] ,   r u l e [ ' n a m e ' ] ,   s t r ( n e w _ r u l e ) ) ,   f i l e = s y s . s t d e r r )  
                         #   V e r i f y   t h a t   t h e r e   i s   a t   l e a s t   a   v a l i d   d e s t i n a t i o n  
                         i f   n o t   ( l e n ( n e w _ r u l e [ ' d e s t i n a t i o n A d d r e s s e s ' ] )   >   0   o r   l e n ( n e w _ r u l e [ ' d e s t i n a t i o n F q d n s ' ] )   >   0   o r   l e n ( n e w _ r u l e [ ' d e s t i n a t i o n I p G r o u p s ' ] )   >   0 ) :  
                                 i f   l o g _ l e v e l   > =   3 :  
                                         p r i n t ( " E R R O R :   F o r   r u l e   ' { 0 }   -   { 1 } '   i t   w a s n ' t   p o s s i b l e   t o   d e r i v e   a n y   d e s t i n a t i o n   a d d r e s s e s / F Q D N s / I P g r o u p s   f r o m   { 2 } .   N o t e   t h a t   t h e   I P   v e r s i o n   t o   p r o c e s s   i s   s e t   t o   { 3 } " . f o r m a t ( r u l e [ ' i d ' ] ,   r u l e [ ' n a m e ' ] ,   s t r ( r u l e [ ' d s t _ a d d r e s s ' ] ) ,   a r g s . i p _ v e r s i o n ) ,   f i l e = s y s . s t d e r r )  
                         #   V e r i f y   t h a t   t h e r e   i s   a t   l e a s t   a   v a l i d   s o u r c e  
                         i f   n o t   ( l e n ( n e w _ r u l e [ ' s o u r c e A d d r e s s e s ' ] )   >   0   o r   l e n ( n e w _ r u l e [ ' s o u r c e I p G r o u p s ' ] )   >   0 ) :  
                                 i f   l o g _ l e v e l   > =   3 :  
                                         p r i n t ( " E R R O R :   F o r   r u l e   ' { 0 }   -   { 1 } '   i t   w a s n ' t   p o s s i b l e   t o   d e r i v e   a n y   s o u r c e   a d d r e s s e s / I P g r o u p s   f r o m   { 2 } .   N o t e   t h a t   t h e   I P   v e r s i o n   t o   p r o c e s s   i s   s e t   t o   { 3 } " . f o r m a t ( r u l e [ ' i d ' ] ,   r u l e [ ' n a m e ' ] ,   s t r ( r u l e [ ' s r c _ a d d r e s s ' ] ) ,   a r g s . i p _ v e r s i o n ) ,   f i l e = s y s . s t d e r r )  
                         #   #   V e r i f y   t h a t   t h e r e   a r e   e i t h e r   s o u r c e A d d r e s s e s   o r   s o u r c e I p G r o u p s   b u t   n o t   b o t h  
                         #   e l i f   l e n ( n e w _ r u l e [ ' s o u r c e A d d r e s s e s ' ] )   >   0   a n d   l e n ( n e w _ r u l e [ ' s o u r c e I p G r o u p s ' ] )   >   0 :  
                         #           i f   l o g _ l e v e l   > =   3 :  
                         #                   p r i n t ( " E R R O R :   F o r   r u l e   ' { 0 }   -   { 1 } '   t h e r e   a r e   b o t h   s o u r c e   I P   a d d r e s s e s   a n d   I P   g r o u p s ,   i t   n e e d s   t o   b e   s p l i t " . f o r m a t ( r u l e [ ' i d ' ] ,   r u l e [ ' n a m e ' ] ) ,   f i l e = s y s . s t d e r r )  
                         #   V e r i f y   t h a t   t h e r e   a r e   n o   F Q D N s   f o r   t h e   s o u r c e s  
                         e l i f   l e n ( s r c _ f q d n s )   >   0 :  
                                 i f   l o g _ l e v e l   > =   3 :  
                                         p r i n t ( " E R R O R :   F o r   r u l e   ' { 0 }   -   { 1 } '   t h e r e   a r e   F Q D N s   s p e c i f i e d   a s   s o u r c e " . f o r m a t ( r u l e [ ' i d ' ] ,   r u l e [ ' n a m e ' ] ) ,   f i l e = s y s . s t d e r r )  
                         e l s e :  
                                 #   A d d   n e w   r u l e   t o   t h e   c o r r e s p o n d i n g   r u l e   c o l l e c t i o n  
                                 i f   r u l e [ ' a c t i o n ' ]   = =   ' A l l o w ' :  
                                         c n t _ a l l o w   + =   1  
                                         a z _ n e t _ r u l e s _ a l l o w   =   a p p e n d _ r u l e ( n e w _ r u l e ,   a z _ n e t _ r u l e s _ a l l o w )  
                                 e l i f   r u l e [ ' a c t i o n ' ]   = =   ' D e n y ' :  
                                         c n t _ d e n y   + =   1  
                                         a z _ n e t _ r u l e s _ d e n y   =   a p p e n d _ r u l e ( n e w _ r u l e ,   a z _ n e t _ r u l e s _ d e n y )  
                                 e l s e :  
                                         i f   l o g _ l e v e l   > =   3 :  
                                                 p r i n t   ( " E R R O R :   r u l e   { 0 }   -   ' { 1 } '   h a s   a c t i o n   o t h e r   t h a n   A l l o w :   { 2 } " . f o r m a t ( s t r ( r u l e [ ' i d ' ] ) ,   r u l e [ ' n a m e ' ] ,   r u l e [ ' a c t i o n ' ] ) ,   f i l e = s y s . s t d e r r )  
  
 # # # # # # # # # #  
 #   O u t p u t   #  
 # # # # # # # # # #  
  
 #   G e n e r a t e   J S O N   w o u l d   b e   c r e a t i n g   a n   o b j e c t   a n d   s e r i a l i z e   i t  
 i f   a r g s . f o r m a t   = =   " j s o n " :  
         a p i _ v e r s i o n   =   " 2 0 2 1 - 0 8 - 0 1 "  
         a z f w _ p o l i c y _ n a m e   =   a r g s . p o l i c y _ n a m e  
         a r m _ t e m p l a t e   =   {  
                 ' $ s c h e m a ' :   ' h t t p s : / / s c h e m a . m a n a g e m e n t . a z u r e . c o m / s c h e m a s / 2 0 1 9 - 0 4 - 0 1 / d e p l o y m e n t T e m p l a t e . j s o n # ' ,  
                 ' c o n t e n t V e r s i o n ' :   ' 1 . 0 . 0 . 0 ' ,  
                 ' p a r a m e t e r s ' :   { } ,  
                 ' v a r i a b l e s ' :   {  
                         ' l o c a t i o n ' :   ' [ r e s o u r c e G r o u p ( ) . l o c a t i o n ] '  
                 } ,  
                 ' r e s o u r c e s ' :   [ ]  
         }  
         i f   n o t   a r g s . d o n t _ c r e a t e _ p o l i c y :  
                 r e s o u r c e _ p o l i c y   =   {  
                         ' t y p e ' :   ' M i c r o s o f t . N e t w o r k / f i r e w a l l P o l i c i e s ' ,  
                         ' a p i V e r s i o n ' :   a p i _ v e r s i o n ,  
                         ' n a m e ' :   a z f w _ p o l i c y _ n a m e ,  
                         ' l o c a t i o n ' :   ' [ v a r i a b l e s ( \ ' l o c a t i o n \ ' ) ] ' ,  
                         ' p r o p e r t i e s ' :   {  
                                 ' s k u ' :   {  
                                         ' t i e r ' :   a r g s . p o l i c y _ s k u  
                                 } ,  
                                 ' d n s S e t t i n g s ' :   {  
                                         ' e n a b l e P r o x y ' :   ' t r u e '  
                                 } ,  
                                 ' t h r e a t I n t e l M o d e ' :   ' A l e r t '  
                         }  
                 }  
                 a r m _ t e m p l a t e [ ' r e s o u r c e s ' ] . a p p e n d ( r e s o u r c e _ p o l i c y )  
         r e s o u r c e _ r c g   =   {  
                 ' t y p e ' :   ' M i c r o s o f t . N e t w o r k / f i r e w a l l P o l i c i e s / r u l e C o l l e c t i o n G r o u p s ' ,  
                 ' a p i V e r s i o n ' :   a p i _ v e r s i o n ,  
                 ' n a m e ' :   a z f w _ p o l i c y _ n a m e   +   ' / '   +   r c g _ n a m e ,  
                 ' d e p e n d s O n ' :   [ ] ,  
                 ' l o c a t i o n ' :   ' [ v a r i a b l e s ( \ ' l o c a t i o n \ ' ) ] ' ,  
                 ' p r o p e r t i e s ' :   {  
                         ' p r i o r i t y ' :   r c g _ p r i o ,  
                         ' r u l e C o l l e c t i o n s ' :   [ ]  
                 }  
         }  
         i f   n o t   a r g s . d o n t _ c r e a t e _ p o l i c y :  
                 r e s o u r c e _ r c g [ ' d e p e n d s O n ' ] . a p p e n d ( ' [ r e s o u r c e I d ( \ ' M i c r o s o f t . N e t w o r k / f i r e w a l l P o l i c i e s \ ' ,   \ ' '   +   a z f w _ p o l i c y _ n a m e   + ' \ ' ) ] ' ) ,  
  
         r e s o u r c e _ n e t _ r c _ a l l o w   =   {  
                 ' r u l e C o l l e c t i o n T y p e ' :   ' F i r e w a l l P o l i c y F i l t e r R u l e C o l l e c t i o n ' ,  
                 ' n a m e ' :   r c _ n e t _ n a m e   +   ' - a l l o w ' ,  
                 ' p r i o r i t y ' :   r c _ n e t _ p r i o _ a l l o w ,  
                 ' a c t i o n ' :   {  
                         ' t y p e ' :   ' a l l o w '  
                 } ,  
                 ' r u l e s ' :   a z _ n e t _ r u l e s _ a l l o w  
         }  
         r e s o u r c e _ n e t _ r c _ d e n y   =   {  
                 ' r u l e C o l l e c t i o n T y p e ' :   ' F i r e w a l l P o l i c y F i l t e r R u l e C o l l e c t i o n ' ,  
                 ' n a m e ' :   r c _ n e t _ n a m e   +   ' - d e n y ' ,  
                 ' p r i o r i t y ' :   r c _ n e t _ p r i o _ d e n y ,  
                 ' a c t i o n ' :   {  
                         ' t y p e ' :   ' d e n y '  
                 } ,  
                 ' r u l e s ' :   a z _ n e t _ r u l e s _ d e n y  
         }  
         r e s o u r c e _ a p p _ r c   =   {  
                 ' r u l e C o l l e c t i o n T y p e ' :   ' F i r e w a l l P o l i c y F i l t e r R u l e C o l l e c t i o n ' ,  
                 ' n a m e ' :   r c _ a p p _ n a m e ,  
                 ' p r i o r i t y ' :   r c _ a p p _ p r i o ,  
                 ' a c t i o n ' :   {  
                         ' t y p e ' :   ' a l l o w '  
                 } ,  
                 ' r u l e s ' :   a z _ a p p _ r u l e s  
         }  
         i f   a r g s . u s e _ i p g r o u p s :  
                 f o r   i p _ g r p   i n   a d d r e s s _ g r o u p s :  
                         i f   i p _ g r p [ ' k e e p _ a s _ i p _ g r o u p ' ] :  
                                 #   A s   a d d i t i o n a l   c h e c k ,   w e   v e r i f y   t h a t   t h e   g r o u p   i s   b e i n g   u s e d   b y   a   r u l e  
                                 i f   i p g r p _ u s e d _ b y _ r u l e s ( i p _ g r p [ ' n a m e ' ] ,   a z _ n e t _ r u l e s _ a l l o w )   o r   i p g r p _ u s e d _ b y _ r u l e s ( i p _ g r p [ ' n a m e ' ] ,   a z _ n e t _ r u l e s _ d e n y ) :  
                                         r e s o u r c e _ i p g r o u p   =   {  
                                                 ' t y p e ' :   ' M i c r o s o f t . N e t w o r k / i p G r o u p s ' ,  
                                                 ' a p i V e r s i o n ' :   a p i _ v e r s i o n ,  
                                                 ' n a m e ' :   i p _ g r p [ ' n a m e ' ] ,  
                                                 ' l o c a t i o n ' :   ' [ v a r i a b l e s ( \ ' l o c a t i o n \ ' ) ] ' ,  
                                                 ' p r o p e r t i e s ' :   {  
                                                         ' i p A d d r e s s e s ' :   i p _ g r p [ ' r e s o l v e d _ i p s ' ]  
                                                 }  
                                         }  
                                         a r m _ t e m p l a t e [ ' r e s o u r c e s ' ] . a p p e n d ( r e s o u r c e _ i p g r o u p )  
                                         r e s o u r c e _ r c g [ ' d e p e n d s O n ' ] . a p p e n d ( " [ r e s o u r c e I d ( ' M i c r o s o f t . N e t w o r k / i p G r o u p s ' ,   ' { 0 } ' ) ] " . f o r m a t ( i p _ g r p [ ' n a m e ' ] ) )  
  
         r e s o u r c e _ r c g [ ' p r o p e r t i e s ' ] [ ' r u l e C o l l e c t i o n s ' ] . a p p e n d ( r e s o u r c e _ n e t _ r c _ a l l o w )  
         r e s o u r c e _ r c g [ ' p r o p e r t i e s ' ] [ ' r u l e C o l l e c t i o n s ' ] . a p p e n d ( r e s o u r c e _ n e t _ r c _ d e n y )  
         r e s o u r c e _ r c g [ ' p r o p e r t i e s ' ] [ ' r u l e C o l l e c t i o n s ' ] . a p p e n d ( r e s o u r c e _ a p p _ r c )  
         a r m _ t e m p l a t e [ ' r e s o u r c e s ' ] . a p p e n d ( r e s o u r c e _ r c g )  
         i f   a r g s . p r e t t y :  
                 p r i n t ( j s o n . d u m p s ( a r m _ t e m p l a t e ,   i n d e n t = 4 ,   s o r t _ k e y s = T r u e ) )  
         e l s e :  
                 p r i n t ( j s o n . d u m p s ( a r m _ t e m p l a t e ) )  
  
 e l i f   a r g s . f o r m a t   = =   " n o n e " :  
         i f   l o g _ l e v e l   > =   7 :  
                 p r i n t ( ' D E B U G :   { 0 }   r u l e s   a n a l i z e d :   { 1 }   a p p   r u l e s ,   { 2 }   F Q D N - b a s e d   n e t   r u l e s   a n d   { 3 }   I P - b a s e d   n e t   r u l e s ' . f o r m a t ( s t r ( c n t _ p a _ r u l e s ) ,   s t r ( c n t _ a p p r u l e s ) ,   s t r ( c n t _ n e t r u l e s _ f q d n ) ,   s t r ( c n t _ n e t r u l e s _ i p ) ) )  
                 p r i n t ( ' D E B U G :   { 2 }   d i s a b l e d   r u l e s ,   { 0 }   a l l o w   r u l e s   a n d   { 1 }   d e n y   r u l e s ' . f o r m a t ( s t r ( c n t _ a l l o w ) ,   s t r ( c n t _ d e n y ) ,   s t r ( c n t _ d i s a b l e d r u l e s ) ) )  
                 #   p r i n t ( ' D E B U G :   N e t   r u l e s : ' ,   s t r ( n e t _ r u l e s ) )  
                 #   p r i n t ( ' D E B U G :   A p p   r u l e s : ' ,   s t r ( a p p _ r u l e s ) )  
 e l s e :  
         p r i n t   ( " F o r m a t " ,   a r g s . f o r m a t ,   " n o t   r e c o g n i z e d ! " )  
 