16
16
17
17
from jnpr .junos import Device
18
18
from jnpr .junos .utils .config import Config
19
-
19
+ from jnpr .junos .exception import ConfigLoadError
20
+ from exceptions import ReplaceConfigException , MergeConfigException
20
21
21
22
class JunOSDriver (NetworkDriver ):
22
23
@@ -25,6 +26,7 @@ def __init__(self, hostname, username, password):
25
26
self .username = username
26
27
self .password = password
27
28
self .device = Device (hostname , user = username , password = password )
29
+ self .config_replace = False
28
30
29
31
def open (self ):
30
32
self .device .open ()
@@ -35,23 +37,28 @@ def close(self):
35
37
self .device .cu .unlock ()
36
38
self .device .close ()
37
39
38
- def load_replace_candidate (self , filename = None , config = None ):
40
+ def _load_candidate (self , filename , config , overwrite ):
39
41
if filename is None :
40
42
configuration = config
41
43
else :
42
44
with open (filename ) as f :
43
45
configuration = f .read ()
44
46
45
- self .device .cu .load (configuration , format = 'text' , overwrite = True )
47
+ try :
48
+ self .device .cu .load (configuration , format = 'text' , overwrite = overwrite )
49
+ except ConfigLoadError as e :
50
+ if self .config_replace :
51
+ raise ReplaceConfigException (e .message )
52
+ else :
53
+ raise MergeConfigException (e .message )
46
54
47
- def load_merge_candidate (self , filename = None , config = None ):
48
- if filename is None :
49
- configuration = config
50
- else :
51
- with open (filename ) as f :
52
- configuration = f .read ()
55
+ def load_replace_candidate (self , filename = None , config = None ):
56
+ self .config_replace = True
57
+ self ._load_candidate (filename , config , True )
53
58
54
- self .device .cu .load (configuration , format = 'text' )
59
+ def load_merge_candidate (self , filename = None , config = None ):
60
+ self .config_replace = False
61
+ self ._load_candidate (filename , config , False )
55
62
56
63
def compare_config (self ):
57
64
diff = self .device .cu .diff ()
0 commit comments