root / ncclient / manager.py @ d6688264
History | View | Annotate | Download (2.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 |
SESSION_TYPES = { |
20 |
'ssh': transport.SSHSession
|
21 |
} |
22 |
|
23 |
OPERATIONS = { |
24 |
'get': operations.Get,
|
25 |
'get-config': operations.GetConfig,
|
26 |
'edit-config': operations.EditConfig,
|
27 |
'copy-config': operations.CopyConfig,
|
28 |
'validate': operations.Validate,
|
29 |
'commit': operations.Commit,
|
30 |
'discard-changes': operations.DiscardChanges,
|
31 |
'delete-config': operations.DeleteConfig,
|
32 |
'lock': operations.Lock,
|
33 |
'unlock': operations.Unlock,
|
34 |
'close_session': operations.CloseSession,
|
35 |
'kill-session': operations.KillSession,
|
36 |
} |
37 |
|
38 |
class Manager(type): |
39 |
|
40 |
'Facade for the API'
|
41 |
|
42 |
def connect(self, session_type, *args, **kwds): |
43 |
self._session = SESSION_TYPES[session_type](capabilities.CAPABILITIES)
|
44 |
self._session.connect(*args, **kwds)
|
45 |
|
46 |
def __getattr__(self, name): |
47 |
name = name.replace('_', '-') |
48 |
if name in OPERATIONS: |
49 |
return OPERATIONS[name](self._session).request |
50 |
else:
|
51 |
raise AttributeError |
52 |
|
53 |
def get(self, *args, **kwds): |
54 |
g = operations.Get(self._session)
|
55 |
reply = g.request(*args, **kwds) |
56 |
if reply.errors:
|
57 |
raise RPCError(reply.errors)
|
58 |
else:
|
59 |
return reply.data
|
60 |
|
61 |
def get_config(self, *args, **kwds): |
62 |
gc = operations.GetConfig(self._session)
|
63 |
reply = gc.request(*args, **kwds) |
64 |
if reply.errors:
|
65 |
raise RPCError(reply.errors)
|
66 |
else:
|
67 |
return reply.data
|
68 |
|
69 |
def locked(self, target='running'): |
70 |
return LockContext(self._session, target) |