root / ncclient / operations / edit.py @ a7cb58ce
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 |
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 |
SPEC = {'tag': 'edit-config', 'subtree': []} |
26 |
|
27 |
def request(self, target=None, config=None, default_operation=None, |
28 |
test_option=None, error_option=None): |
29 |
util.one_of(target, config) |
30 |
spec = EditConfig.SPEC.copy() |
31 |
subtree = spec['subtree']
|
32 |
subtree.append(util.store_or_url('target', target, self._assert)) |
33 |
subtree.append(content.validated_root(config, 'config'))
|
34 |
if default_operation is not None: |
35 |
subtree.append({ |
36 |
'tag': 'default-operation', |
37 |
'text': default_operation
|
38 |
}) |
39 |
if test_option is not None: |
40 |
self._assert(':validate') |
41 |
subtree.append({ |
42 |
'tag': 'test-option', |
43 |
'text': test_option
|
44 |
}) |
45 |
if error_option is not None: |
46 |
if error_option == 'rollback-on-error': |
47 |
self._assert(':rollback-on-error') |
48 |
subtree.append({ |
49 |
'tag': 'error-option', |
50 |
'text': error_option
|
51 |
}) |
52 |
return self._request(spec) |
53 |
|
54 |
|
55 |
class DeleteConfig(RPC): |
56 |
|
57 |
SPEC = {'tag': 'delete-config', 'subtree': []} |
58 |
|
59 |
def request(self, target): |
60 |
spec = DeleteConfig.SPEC.copy() |
61 |
spec['subtree'].append(util.store_or_url('target', target, self._assert)) |
62 |
return self._request(spec) |
63 |
|
64 |
|
65 |
class CopyConfig(RPC): |
66 |
|
67 |
SPEC = {'tag': 'copy-config', 'subtree': []} |
68 |
|
69 |
def request(self, source, target): |
70 |
spec = CopyConfig.SPEC.copy() |
71 |
spec['subtree'].append(util.store_or_url('source', source, self._assert)) |
72 |
spec['subtree'].append(util.store_or_url('target', target, self._assert)) |
73 |
return self._request(spec) |
74 |
|
75 |
|
76 |
class Validate(RPC): |
77 |
|
78 |
DEPENDS = [':validate']
|
79 |
|
80 |
SPEC = {'tag': 'validate', 'subtree': []} |
81 |
|
82 |
def request(self, source): |
83 |
# determine if source is a <config> element
|
84 |
spec = Validate.SPEC.copy() |
85 |
try:
|
86 |
spec['subtree'].append({
|
87 |
'tag': 'source', |
88 |
'subtree': content.validated_root(config, ('config', content.qualify('config'))) |
89 |
}) |
90 |
except ContentError:
|
91 |
spec['subtree'].append(util.store_or_url('source', source, self._assert)) |
92 |
return self._request(spec) |
93 |
|
94 |
|
95 |
class Commit(RPC): |
96 |
|
97 |
DEPENDS = [':candidate']
|
98 |
|
99 |
SPEC = {'tag': 'commit', 'subtree': []} |
100 |
|
101 |
def _parse_hook(self): |
102 |
pass
|
103 |
|
104 |
def request(self, confirmed=False): |
105 |
spec = SPEC.copy() |
106 |
if confirmed:
|
107 |
self._assert(':confirmed-commit') |
108 |
spec['subtree'].append({'tag': 'confirmed'}) |
109 |
if timeout is not None: |
110 |
spec['subtree'].append({
|
111 |
'tag': 'confirm-timeout', |
112 |
'text': timeout
|
113 |
}) |
114 |
return self._request(Commit.SPEC) |
115 |
|
116 |
|
117 |
class DiscardChanges(RPC): |
118 |
|
119 |
DEPENDS = [':candidate']
|
120 |
|
121 |
SPEC = {'tag': 'discard-changes'} |
122 |
|
123 |
|
124 |
class ConfirmedCommit(Commit): |
125 |
"psuedo-op"
|
126 |
|
127 |
DEPENDS = [':candidate', ':confirmed-commit'] |
128 |
|
129 |
def request(self): |
130 |
"Commit changes requiring that a confirm/discard follow"
|
131 |
return Commit.request(self, confirmed=True) |
132 |
|
133 |
def confirm(self): |
134 |
"Confirm changes"
|
135 |
return Commit.request(self, confirmed=True) |
136 |
|
137 |
def discard(self): |
138 |
"Discard changes"
|
139 |
return DiscardChanges(self.session, self.async, self.timeout).request() |