Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / edit.py @ cc9af1c3

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

    
17
from rpc import RPC
18

    
19
import util
20

    
21

    
22
"Operations related to configuration editing"
23

    
24

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

    
61

    
62
class DeleteConfig(RPC):
63
    
64
    # tested: no
65
    # combed: yes
66
    
67
    SPEC = {'tag': 'delete-config', 'subtree': []}
68
    
69
    def request(self, target=None, target_url=None):
70
        spec = DeleteConfig.SPEC.copy()
71
        spec['subtree'].append({
72
            'tag': 'target',
73
            'subtree': util.store_or_url(target, target_url, self._assert)
74
            })
75
        return self._request(spec)
76

    
77

    
78
class CopyConfig(RPC):
79
    
80
    # tested: no
81
    # combed: yes
82
    
83
    SPEC = {'tag': 'copy-config', 'subtree': []}
84
    
85
    def request(self, source=None, source_url=None, target=None, target_url=None):
86
        spec = CopyConfig.SPEC.copy()
87
        spec['subtree'].append({
88
            'tag': 'source',
89
            'subtree': util.store_or_url(source, source_url, self._assert)
90
            })
91
        spec['subtree'].append({
92
            'tag': 'target',
93
            'subtree': util.store_or_url(target, target_url, self._assert)
94
            })
95
        return self._request(spec)
96

    
97

    
98
class Validate(RPC):
99
    
100
    # tested: no
101
    # combed: yes
102
    
103
    'config attr shd not include <config> root'
104
    
105
    DEPENDS = [':validate']
106
    
107
    SPEC = {'tag': 'validate', 'subtree': []}
108
    
109
    def request(self, source=None, source_url=None, config=None):
110
        util.one_of(source, source_url, config)
111
        spec = Validate.SPEC.copy()
112
        if config is None:
113
            spec['subtree'].append({
114
                'tag': 'source',
115
                'subtree': util.store_or_url(source, source_url, self._assert)
116
            })
117
        else:
118
            spec['subtree'].append(config)
119
        return self._request(spec)
120

    
121

    
122
class Commit(RPC):
123
    
124
    # tested: no
125
    # combed: yes
126
    
127
    DEPENDS = [':candidate']
128
    
129
    SPEC = {'tag': 'commit', 'subtree': []}
130
    
131
    def _parse_hook(self):
132
        pass
133
    
134
    def request(self, confirmed=False, timeout=None):
135
        spec = SPEC.copy()
136
        if confirmed:
137
            self._assert(':confirmed-commit')
138
            spec['subtree'].append({'tag': 'confirmed'})
139
            if timeout is not None:
140
                spec['subtree'].append({
141
                    'tag': 'confirm-timeout',
142
                    'text': timeout
143
                })
144
        return self._request(Commit.SPEC)
145

    
146

    
147
class ConfirmedCommit(Commit):
148
    "psuedo-op"
149
    
150
    # tested: no
151
    # combed: yes
152
    
153
    DEPENDS = [':candidate', ':confirmed-commit']
154
    
155
    def request(self, timeout=None):
156
        "Commit changes; requireing that a confirming commit follow"
157
        return Commit.request(self, confirmed=True, timeout=timeout)
158
    
159
    def confirm(self):
160
        "Make the confirming commit"
161
        return Commit.request(self, confirmed=True)
162

    
163

    
164
class DiscardChanges(RPC):
165
    
166
    # tested: no
167
    # combed: yes
168
    
169
    DEPENDS = [':candidate']
170
    
171
    SPEC = {'tag': 'discard-changes'}