root / ncclient / manager.py @ 2f8bc438
History | View | Annotate | Download (3.1 kB)
1 |
# Copyright 2009 Shikhar Bhushan
|
---|---|
2 |
#
|
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
|
6 |
#
|
7 |
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
#
|
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.
|
14 |
|
15 |
import capabilities |
16 |
import operations |
17 |
import transport |
18 |
|
19 |
OPERATIONS = { |
20 |
'get': operations.Get,
|
21 |
'get-config': operations.GetConfig,
|
22 |
'edit-config': operations.EditConfig,
|
23 |
'copy-config': operations.CopyConfig,
|
24 |
'validate': operations.Validate,
|
25 |
'commit': operations.Commit,
|
26 |
'discard-changes': operations.DiscardChanges,
|
27 |
'delete-config': operations.DeleteConfig,
|
28 |
'lock': operations.Lock,
|
29 |
'unlock': operations.Unlock,
|
30 |
'close_session': operations.CloseSession,
|
31 |
'kill-session': operations.KillSession,
|
32 |
} |
33 |
|
34 |
def connect_ssh(*args, **kwds): |
35 |
session = transport.SSHSession(capabilities.CAPABILITIES) |
36 |
session.load_system_host_keys() |
37 |
session.connect(*args, **kwds) |
38 |
return Manager(session)
|
39 |
|
40 |
connect = connect_ssh # default
|
41 |
|
42 |
class Manager: |
43 |
|
44 |
'Facade for the API'
|
45 |
|
46 |
def __init__(self, session): |
47 |
self._session = session
|
48 |
|
49 |
def _get(self, type, *args, **kwds): |
50 |
op = OPERATIONS[type](self._session) |
51 |
reply = op.request(*args, **kwds) |
52 |
if not reply.ok: |
53 |
raise reply.errors[0] |
54 |
else:
|
55 |
return reply.data
|
56 |
|
57 |
def request(op, *args, **kwds): |
58 |
op = OPERATIONS[op](self._session)
|
59 |
reply = op.request(*args, **kwds) |
60 |
if not reply.ok: |
61 |
raise reply.errors[0] |
62 |
return reply
|
63 |
|
64 |
def locked(self, target='running'): |
65 |
return LockContext(self._session, target) |
66 |
|
67 |
get = lambda self, *args, **kwds: self._get('get') |
68 |
|
69 |
get_config = lambda self, *args, **kwds: self._get('get-config') |
70 |
|
71 |
edit_config = lambda self, *args, **kwds: self.request('edit-config', *args, **kwds) |
72 |
|
73 |
copy_config = lambda self, *args, **kwds: self.request('copy-config', *args, **kwds) |
74 |
|
75 |
validate = lambda self, *args, **kwds: self.request('validate', *args, **kwds) |
76 |
|
77 |
commit = lambda self, *args, **kwds: self.request('commit', *args, **kwds) |
78 |
|
79 |
discard_changes = lambda self, *args, **kwds: self.request('discard-changes', *args, **kwds) |
80 |
|
81 |
delete_config = lambda self, *args, **kwds: self.request('delete-config', *args, **kwds) |
82 |
|
83 |
lock = lambda self, *args, **kwds: self.request('lock', *args, **kwds) |
84 |
|
85 |
unlock = lambda self, *args, **kwds: self.request('unlock', *args, **kwds) |
86 |
|
87 |
close_session = lambda self, *args, **kwds: self.request('close-session', *args, **kwds) |
88 |
|
89 |
kill_session = lambda self, *args, **kwds: self.request('kill-session', *args, **kwds) |
90 |
|
91 |
def close(self): |
92 |
try:
|
93 |
self.close_session()
|
94 |
except:
|
95 |
self._session.expect_close()
|
96 |
self._session.close()
|