1 # Copyright 2009 Shikhar Bhushan
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from operations import OPERATIONS
20 def ssh_connect(*args, **kwds):
21 session = transport.SSHSession(capabilities.CAPABILITIES)
22 session.load_system_host_keys()
23 session.connect(*args, **kwds)
24 return Manager(session)
26 connect = ssh_connect # default session type
37 "Thin layer of abstraction for the ncclient API."
39 def __init__(self, session):
40 self._session = session
41 self._rpc_error_action = RAISE_ALL
43 def set_rpc_error_action(self, action):
44 self._rpc_error_handling = option
46 def do(self, op, *args, **kwds):
47 op = OPERATIONS[op](self._session)
48 reply = op.request(*args, **kwds)
50 if self._raise == RAISE_ALL:
52 elif self._raise == RAISE_ERROR:
53 for error in reply.errors:
54 if error.severity == 'error':
61 def __exit__(self, *args):
65 def locked(self, target):
66 """Returns a context manager for use with the 'with' statement.
68 :arg target: name of the datastore to lock
69 :type target: `string`
71 return operations.LockContext(self._session, target)
73 def get(self, filter=None):
76 def get_config(self, source, filter=None):
79 def copy_config(self, source, target):
82 def validate(self, source):
85 def commit(self, target):
88 def discard_changes(self):
91 def delete_config(self, target):
94 def lock(self, target):
97 def unlock(self, target):
100 def close_session(self):
103 def kill_session(self, session_id):
106 def confirmed_commit(self, timeout=None):
113 def discard_changes(self):
116 lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
118 unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
120 close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
122 kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
125 try: # try doing it clean
127 except Exception as e:
128 logger.debug('error doing <close-session> -- %r' % e)
129 if self._session.connected: # if that didn't work...
130 self._session.close()
133 def session(self, session):
137 def client_capabilities(self):
138 return self._session._client_capabilities
141 def server_capabilities(self):
142 return self._session._server_capabilities
145 def session_id(self):
146 return self._session.id