<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> from json import loads , dumps <EOL> from copy import deepcopy <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT>' in __salt__ <EOL> def present ( host , groups , interfaces , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:name>' : host , '<STR_LIT>' : { } , '<STR_LIT:result>' : False , '<STR_LIT>' : '<STR_LIT>' } <EOL> comment_host_created = '<STR_LIT>' . format ( host ) <EOL> comment_host_updated = '<STR_LIT>' . format ( host ) <EOL> comment_host_notcreated = '<STR_LIT>' . format ( host ) <EOL> comment_host_exists = '<STR_LIT>' . format ( host ) <EOL> changes_host_created = { host : { '<STR_LIT>' : '<STR_LIT>' . format ( host ) , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( host ) , <EOL> } <EOL> } <EOL> def _interface_format ( interfaces_data ) : <EOL> '''<STR_LIT>''' <EOL> if not interfaces_data : <EOL> return list ( ) <EOL> interface_attrs = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT:port>' ) <EOL> interfaces_json = loads ( dumps ( interfaces_data ) ) <EOL> interfaces_dict = dict ( ) <EOL> for interface in interfaces_json : <EOL> for intf in interface : <EOL> intf_name = intf <EOL> interfaces_dict [ intf_name ] = dict ( ) <EOL> for intf_val in interface [ intf ] : <EOL> for key , value in intf_val . items ( ) : <EOL> if key in interface_attrs : <EOL> interfaces_dict [ intf_name ] [ key ] = value <EOL> interfaces_list = list ( ) <EOL> interface_ports = { '<STR_LIT>' : [ '<STR_LIT:1>' , '<STR_LIT>' ] , '<STR_LIT>' : [ '<STR_LIT:2>' , '<STR_LIT>' ] , '<STR_LIT>' : [ '<STR_LIT:3>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT:4>' , '<STR_LIT>' ] } <EOL> for key , value in interfaces_dict . items ( ) : <EOL> interface_type = interface_ports [ value [ '<STR_LIT:type>' ] . lower ( ) ] [ <NUM_LIT:0> ] <EOL> main = '<STR_LIT:1>' if str ( value . get ( '<STR_LIT>' , '<STR_LIT:true>' ) ) . lower ( ) == '<STR_LIT:true>' else '<STR_LIT:0>' <EOL> useip = '<STR_LIT:1>' if str ( value . get ( '<STR_LIT>' , '<STR_LIT:true>' ) ) . lower ( ) == '<STR_LIT:true>' else '<STR_LIT:0>' <EOL> interface_ip = value . get ( '<STR_LIT>' ) <EOL> dns = value . get ( '<STR_LIT>' , key ) <EOL> port = str ( value . get ( '<STR_LIT:port>' , interface_ports [ value [ '<STR_LIT:type>' ] . lower ( ) ] [ <NUM_LIT:1> ] ) ) <EOL> interfaces_list . append ( { '<STR_LIT:type>' : interface_type , <EOL> '<STR_LIT>' : main , <EOL> '<STR_LIT>' : useip , <EOL> '<STR_LIT>' : interface_ip , <EOL> '<STR_LIT>' : dns , <EOL> '<STR_LIT:port>' : port } ) <EOL> interfaces_list = interfaces_list <EOL> interfaces_list_sorted = sorted ( interfaces_list , key = lambda k : k [ '<STR_LIT>' ] , reverse = True ) <EOL> return interfaces_list_sorted <EOL> interfaces_formated = _interface_format ( interfaces ) <EOL> host_exists = __salt__ [ '<STR_LIT>' ] ( host ) <EOL> if host_exists : <EOL> host = __salt__ [ '<STR_LIT>' ] ( name = host ) [ <NUM_LIT:0> ] <EOL> hostid = host [ '<STR_LIT>' ] <EOL> update_hostgroups = False <EOL> update_interfaces = False <EOL> hostgroups = __salt__ [ '<STR_LIT>' ] ( hostids = hostid ) <EOL> cur_hostgroups = list ( ) <EOL> for hostgroup in hostgroups : <EOL> cur_hostgroups . append ( int ( hostgroup [ '<STR_LIT>' ] ) ) <EOL> if set ( groups ) != set ( cur_hostgroups ) : <EOL> update_hostgroups = True <EOL> hostinterfaces = __salt__ [ '<STR_LIT>' ] ( hostids = hostid ) <EOL> if hostinterfaces : <EOL> hostinterfaces = sorted ( hostinterfaces , key = lambda k : k [ '<STR_LIT>' ] ) <EOL> hostinterfaces_copy = deepcopy ( hostinterfaces ) <EOL> for hostintf in hostinterfaces_copy : <EOL> hostintf . pop ( '<STR_LIT>' ) <EOL> hostintf . pop ( '<STR_LIT>' ) <EOL> hostintf . pop ( '<STR_LIT>' ) <EOL> interface_diff = [ x for x in interfaces_formated if x not in hostinterfaces_copy ] + [ y for y in hostinterfaces_copy if y not in interfaces_formated ] <EOL> if interface_diff : <EOL> update_interfaces = True <EOL> elif not hostinterfaces and interfaces : <EOL> update_interfaces = True <EOL> if __opts__ [ '<STR_LIT:test>' ] : <EOL> if host_exists : <EOL> if update_hostgroups or update_interfaces : <EOL> ret [ '<STR_LIT:result>' ] = None <EOL> ret [ '<STR_LIT>' ] = comment_host_updated <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = comment_host_exists <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = None <EOL> ret [ '<STR_LIT>' ] = comment_host_created <EOL> ret [ '<STR_LIT>' ] = changes_host_created <EOL> return ret <EOL> error = [ ] <EOL> if host_exists : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> if update_hostgroups or update_interfaces : <EOL> if update_hostgroups : <EOL> hostupdate = __salt__ [ '<STR_LIT>' ] ( hostid , groups = groups ) <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = str ( groups ) <EOL> if '<STR_LIT:error>' in hostupdate : <EOL> error . append ( hostupdate [ '<STR_LIT:error>' ] ) <EOL> if update_interfaces : <EOL> if hostinterfaces : <EOL> for interface in hostinterfaces : <EOL> __salt__ [ '<STR_LIT>' ] ( interfaceids = interface [ '<STR_LIT>' ] ) <EOL> hostid = __salt__ [ '<STR_LIT>' ] ( name = host ) [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> for interface in interfaces_formated : <EOL> updatedint = __salt__ [ '<STR_LIT>' ] ( hostid = hostid , <EOL> ip = interface [ '<STR_LIT>' ] , <EOL> dns = interface [ '<STR_LIT>' ] , <EOL> main = interface [ '<STR_LIT>' ] , <EOL> type = interface [ '<STR_LIT:type>' ] , <EOL> useip = interface [ '<STR_LIT>' ] , <EOL> port = interface [ '<STR_LIT:port>' ] ) <EOL> if '<STR_LIT:error>' in updatedint : <EOL> error . append ( updatedint [ '<STR_LIT:error>' ] ) <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = str ( interfaces_formated ) <EOL> ret [ '<STR_LIT>' ] = comment_host_updated <EOL> else : <EOL> ret [ '<STR_LIT>' ] = comment_host_exists <EOL> else : <EOL> host_create = __salt__ [ '<STR_LIT>' ] ( host , groups , interfaces_formated , ** kwargs ) <EOL> if '<STR_LIT:error>' not in host_create : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = comment_host_created <EOL> ret [ '<STR_LIT>' ] = changes_host_created <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = comment_host_notcreated + str ( host_create [ '<STR_LIT:error>' ] ) <EOL> if error : <EOL> ret [ '<STR_LIT>' ] = { } <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = str ( error ) <EOL> return ret <EOL> def absent ( name ) : <EOL> """<STR_LIT>""" <EOL> ret = { '<STR_LIT:name>' : name , '<STR_LIT>' : { } , '<STR_LIT:result>' : False , '<STR_LIT>' : '<STR_LIT>' } <EOL> comment_host_deleted = '<STR_LIT>' . format ( name ) <EOL> comment_host_notdeleted = '<STR_LIT>' . format ( name ) <EOL> comment_host_notexists = '<STR_LIT>' . format ( name ) <EOL> changes_host_deleted = { name : { '<STR_LIT>' : '<STR_LIT>' . format ( name ) , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( name ) , <EOL> } <EOL> } <EOL> host_exists = __salt__ [ '<STR_LIT>' ] ( name ) <EOL> if __opts__ [ '<STR_LIT:test>' ] : <EOL> if not host_exists : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = comment_host_notexists <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = None <EOL> ret [ '<STR_LIT>' ] = comment_host_deleted <EOL> return ret <EOL> host_get = __salt__ [ '<STR_LIT>' ] ( name ) <EOL> if not host_get : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = comment_host_notexists <EOL> else : <EOL> try : <EOL> hostid = host_get [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> host_delete = __salt__ [ '<STR_LIT>' ] ( hostid ) <EOL> except KeyError : <EOL> host_delete = False <EOL> if host_delete and '<STR_LIT:error>' not in host_delete : <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = comment_host_deleted <EOL> ret [ '<STR_LIT>' ] = changes_host_deleted <EOL> else : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = comment_host_notdeleted + str ( host_delete [ '<STR_LIT:error>' ] ) <EOL> return ret </s>
