Statistics
| Branch: | Tag: | Revision:

root / ncclient / manager.py @ 179b00d4

History | View | Annotate | Download (3.6 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
    'Facade for the 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 do(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
        "For use with 'with'. target is the datastore, e.g. 'candidate'"
79
        return operations.LockContext(self._session, target)
80
    
81
    get = lambda self, *args, **kwds: self._get('get')
82
    
83
    get_config = lambda self, *args, **kwds: self._get('get-config')
84
    
85
    edit_config = lambda self, *args, **kwds: self.do('edit-config', *args, **kwds)
86
    
87
    copy_config = lambda self, *args, **kwds: self.do('copy-config', *args, **kwds)
88
    
89
    validate = lambda self, *args, **kwds: self.do('validate', *args, **kwds)
90
    
91
    commit = lambda self, *args, **kwds: self.do('commit', *args, **kwds)
92
    
93
    discard_changes = lambda self, *args, **kwds: self.do('discard-changes', *args, **kwds)
94
    
95
    delete_config = lambda self, *args, **kwds: self.do('delete-config', *args, **kwds)
96
    
97
    lock = lambda self, *args, **kwds: self.do('lock', *args, **kwds)
98
    
99
    unlock = lambda self, *args, **kwds: self.do('unlock', *args, **kwds)
100
    
101
    close_session = lambda self, *args, **kwds: self.do('close-session', *args, **kwds)
102
    
103
    kill_session = lambda self, *args, **kwds: self.do('kill-session', *args, **kwds)
104
    
105
    def close(self):
106
        try: # try doing it clean
107
            self.close_session()
108
        except:
109
            pass
110
        if self._session.connected: # if that didn't work...
111
            self._session.close()