Statistics
| Branch: | Tag: | Revision:

root / ncclient / manager.py @ 6ab782f7

History | View | Annotate | Download (4 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 session type
41

    
42
class Manager:
43
    
44
    "Thin layer of abstraction for the ncclient API."
45
    
46
    RAISE_ALL = 0
47
    RAISE_ERROR = 1
48
    RAISE_NONE = 2
49
    
50
    def __init__(self, session, rpc_error=Manager.RAISE_ERROR):
51
        self._session = session
52
        self._raise = rpc_error
53

    
54
    def rpc(self, op, *args, **kwds):
55
        op = OPERATIONS[op](self._session)
56
        reply = op.request(*args, **kwds)
57
        if not reply.ok:
58
            if self._raise == Manager.RAISE_ALL:
59
                raise reply.error
60
            elif self._raise == Manager.RAISE_ERROR:
61
                for error in reply.errors:
62
                    if error.severity == 'error':
63
                        raise error
64
        return reply
65
    
66
    def __enter__(self):
67
        pass
68
    
69
    def __exit__(self, *args):
70
        self.close()
71
        return False
72
    
73
    def _get(self, type, *args, **kwds):
74
        reply = self.do(type)
75
        return reply.data
76
    
77
    def locked(self, target):
78
        "Returns a context manager for use withthe 'with' statement.
79
        `target` is the datastore to lock, e.g. 'candidate'"
80
        return operations.LockContext(self._session, target)
81
     
82
    get = lambda self, *args, **kwds: self._get('get')
83
    
84
    get_config = lambda self, *args, **kwds: self._get('get-config')
85
    
86
    edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds)
87
    
88
    copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
89
    
90
    validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
91
    
92
    commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds)
93
    
94
    discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds)
95
    
96
    delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds)
97
    
98
    lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
99
    
100
    unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
101
    
102
    close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
103
    
104
    kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
105
    
106
    def close(self):
107
        try: # try doing it clean
108
            self.close_session()
109
        except:
110
            pass
111
        if self._session.connected: # if that didn't work...
112
            self._session.close()
113

    
114
    @property
115
    def session(self, session):
116
        return self._session
117
    
118
    def get_capabilities(self, whose):
119
        if whose in ('manager', 'client'):
120
            return self._session._client_capabilities
121
        elif whose in ('agent', 'server')
122
            return self._session._server_capabilities
123

    
124
   
125
    @property
126
    def capabilities(self):
127
        return self._session._client_capabilities