#NEWLINE# def replace_ip_address( ns , setting , address , prefix , gateway = None ) : #NEWLINE# #INDENT# '\n    Remove all IP addresses from given static setting and add new IP address.\n\n    :param LMI_IPAssignmentSettingData setting: network setting.\n    :param str address: IPv4 or IPv6 address.\n    :param int prefix: network prefix.\n    :param gateway: default gateway or None\n    :type gateway: str or None\n    ' #NEWLINE# ( address , version ) = util . address_check ( address ) #NEWLINE# prefix = util . prefix_check ( prefix , version ) #NEWLINE# gateway = _gateway_check ( gateway , version ) #NEWLINE# protocol = ns . LMI_IPAssignmentSettingData . ProtocolIFTypeValues . values_dict ( ) [ ( 'IPv%s' % version ) ] #NEWLINE# found = False #NEWLINE# for settingData in setting . associators ( AssocClass = 'LMI_OrderedIPAssignmentComponent' ) : #NEWLINE# #INDENT# if ( ( settingData . ProtocolIFType is not None ) and ( int ( settingData . ProtocolIFType ) == protocol ) and hasattr ( protocol , 'IPAddresses' ) ) : #NEWLINE# #INDENT# settingData . IPAddresses = [ address ] #NEWLINE# if ( version == 4 ) : #NEWLINE# #INDENT# settingData . SubnetMasks = [ util . netmask_from_prefix ( prefix ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# settingData . IPv6SubnetPrefixLengths = [ prefix ] #NEWLINE# #UNINDENT# if gateway : #NEWLINE# #INDENT# settingData . GatewayAddresses = [ gateway ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# settingData . GatewayAddresses = [ '' ] #NEWLINE# #UNINDENT# found = True #NEWLINE# settingData . push ( ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not found ) : #NEWLINE# #INDENT# raise LmiInvalidOptions ( "Can't add IP address to setting: invalid setting type." ) #NEWLINE# #UNINDENT# LOG ( ) . info ( 'Existing addresses replaced with IP address %s/%d in setting %s' , address , prefix , setting . Caption ) #NEWLINE# return 0