#NEWLINE# def pull_security_group_rules( self , security_group ) : #NEWLINE# #INDENT# nova = self . nova_client #NEWLINE# backend_security_group = nova . security_groups . get ( group_id = security_group . backend_id ) #NEWLINE# backend_rules = [ self . _normalize_security_group_rule ( r ) for r in backend_security_group . rules ] #NEWLINE# nonexistent_rules = [ ] #NEWLINE# unsynchronized_rules = [ ] #NEWLINE# extra_rules = security_group . rules . exclude ( backend_id__in = [ r [ 'id' ] for r in backend_rules ] ) #NEWLINE# with transaction . atomic ( ) : #NEWLINE# #INDENT# for backend_rule in backend_rules : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# nc_rule = security_group . rules . get ( backend_id = backend_rule [ 'id' ] ) #NEWLINE# if ( not self . _are_rules_equal ( backend_rule , nc_rule ) ) : #NEWLINE# #INDENT# unsynchronized_rules . append ( backend_rule ) #NEWLINE# #UNINDENT# #UNINDENT# except security_group . rules . model . DoesNotExist : #NEWLINE# #INDENT# nonexistent_rules . append ( backend_rule ) #NEWLINE# #UNINDENT# #UNINDENT# extra_rules . delete ( ) #NEWLINE# logger . info ( 'Deleted stale security group rules in database' ) #NEWLINE# for backend_rule in unsynchronized_rules : #NEWLINE# #INDENT# security_group . rules . filter ( backend_id = backend_rule [ 'id' ] ) . update ( from_port = backend_rule [ 'from_port' ] , to_port = backend_rule [ 'to_port' ] , protocol = backend_rule [ 'ip_protocol' ] , cidr = backend_rule [ 'ip_range' ] [ 'cidr' ] ) #NEWLINE# #UNINDENT# logger . debug ( 'Updated existing security group rules in database' ) #NEWLINE# for backend_rule in nonexistent_rules : #NEWLINE# #INDENT# rule = security_group . rules . create ( from_port = backend_rule [ 'from_port' ] , to_port = backend_rule [ 'to_port' ] , protocol = backend_rule [ 'ip_protocol' ] , cidr = backend_rule [ 'ip_range' ] [ 'cidr' ] , backend_id = backend_rule [ 'id' ] ) #NEWLINE# logger . info ( 'Created new security group rule %s in database' , rule . id )