Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / edit.py @ d771dffc

History | View | Annotate | Download (4.3 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
from ncclient import content
16

    
17
from rpc import RPC
18

    
19
import util
20

    
21
"Operations related to configuration editing"
22

    
23
class EditConfig(RPC):
24
    
25
    # tested: no
26
    # combed: yes
27
    
28
    SPEC = {'tag': 'edit-config', 'subtree': []}
29
    
30
    def request(self, target=None, config=None, default_operation=None,
31
                test_option=None, error_option=None):
32
        util.one_of(target, config)
33
        spec = EditConfig.SPEC.copy()
34
        subtree = spec['subtree']
35
        subtree.append(util.store_or_url('target', target, self._assert))
36
        subtree.append(content.validated_root(config, 'config'))
37
        if default_operation is not None:
38
            subtree.append({
39
                'tag': 'default-operation',
40
                'text': default_operation
41
                })
42
        if test_option is not None:
43
            self._assert(':validate')
44
            subtree.append({
45
                'tag': 'test-option',
46
                'text': test_option
47
                })
48
        if error_option is not None:
49
            if error_option == 'rollback-on-error':
50
                self._assert(':rollback-on-error')
51
            subtree.append({
52
                'tag': 'error-option',
53
                'text': error_option
54
                })
55

    
56

    
57
class DeleteConfig(RPC):
58
    
59
    # tested: no
60
    # combed: yes
61
    
62
    SPEC = {'tag': 'delete-config', 'subtree': []}
63
    
64
    def request(self, target):
65
        spec = DeleteConfig.SPEC.copy()
66
        spec['subtree'].append(util.store_or_url('source', source, self._assert))
67
        return self._request(spec)
68

    
69

    
70
class CopyConfig(RPC):
71
    
72
    # tested: no
73
    # combed: yes
74
    
75
    SPEC = {'tag': 'copy-config', 'subtree': []}
76
    
77
    def request(self, source, target):
78
        spec = CopyConfig.SPEC.copy()
79
        spec['subtree'].append(util.store_or_url('source', source, self._assert))
80
        spec['subtree'].append(util.store_or_url('target', source, self._assert))
81
        return self._request(spec)
82

    
83

    
84
class Validate(RPC):
85
    
86
    # tested: no
87
    # combed: yes
88
    
89
    'config attr shd not include <config> root'
90
    
91
    DEPENDS = [':validate']
92
    
93
    SPEC = {'tag': 'validate', 'subtree': []}
94
    
95
    def request(self, source=None, config=None):
96
        util.one_of(source, config)
97
        spec = Validate.SPEC.copy()
98
        if config is None:
99
            spec['subtree'].append(util.store_or_url('source', source, self._assert))
100
        else:
101
            spec['subtree'].append(content.validated_root(config, 'config'))
102
        return self._request(spec)
103

    
104

    
105
class Commit(RPC):
106
    
107
    # tested: no
108
    # combed: yes
109
    
110
    DEPENDS = [':candidate']
111
    
112
    SPEC = {'tag': 'commit', 'subtree': []}
113
    
114
    def _parse_hook(self):
115
        pass
116
    
117
    def request(self, confirmed=False, timeout=None):
118
        spec = SPEC.copy()
119
        if confirmed:
120
            self._assert(':confirmed-commit')
121
            spec['subtree'].append({'tag': 'confirmed'})
122
            if timeout is not None:
123
                spec['subtree'].append({
124
                    'tag': 'confirm-timeout',
125
                    'text': timeout
126
                })
127
        return self._request(Commit.SPEC)
128

    
129

    
130
class DiscardChanges(RPC):
131
    
132
    # tested: no
133
    # combed: yes
134
    
135
    DEPENDS = [':candidate']
136
    
137
    SPEC = {'tag': 'discard-changes'}
138

    
139

    
140
class ConfirmedCommit(Commit):
141
    "psuedo-op"
142
    
143
    # tested: no
144
    # combed: yes
145
    
146
    DEPENDS = [':candidate', ':confirmed-commit']
147
    
148
    def request(self, timeout=None):
149
        "Commit changes; requireing that a confirming commit follow"
150
        return Commit.request(self, confirmed=True, timeout=timeout)
151
    
152
    def confirm(self):
153
        "Make the confirming commit"
154
        return Commit.request(self, confirmed=True)
155
    
156
    def discard(self):
157
        return DiscardChanges(self.session, self.async, self.timeout).request()