]>
Commit | Line | Data |
---|---|---|
892821fd DM |
1 | package PVE::API2::ReplicationConfig; |
2 | ||
3 | use warnings; | |
4 | use strict; | |
5 | ||
6 | use PVE::Tools qw(extract_param); | |
f9d38c54 | 7 | use PVE::Exception qw(raise_perm_exc raise_param_exc); |
892821fd DM |
8 | use PVE::JSONSchema qw(get_standard_option); |
9 | use PVE::RPCEnvironment; | |
10 | use PVE::ReplicationConfig; | |
4542a42a | 11 | use PVE::Cluster; |
892821fd DM |
12 | |
13 | use PVE::RESTHandler; | |
14 | ||
15 | use base qw(PVE::RESTHandler); | |
16 | ||
17 | __PACKAGE__->register_method ({ | |
18 | name => 'index', | |
19 | path => '', | |
20 | method => 'GET', | |
21 | description => "List replication jobs.", | |
22 | permissions => { | |
23 | description => "Requires the VM.Audit permission on /vms/<vmid>.", | |
24 | user => 'all', | |
25 | }, | |
26 | parameters => { | |
27 | additionalProperties => 0, | |
28 | properties => {}, | |
29 | }, | |
30 | returns => { | |
31 | type => 'array', | |
32 | items => { | |
33 | type => "object", | |
34 | properties => {}, | |
35 | }, | |
4b48563a | 36 | links => [ { rel => 'child', href => "{id}" } ], |
892821fd DM |
37 | }, |
38 | code => sub { | |
39 | my ($param) = @_; | |
40 | ||
41 | my $rpcenv = PVE::RPCEnvironment::get(); | |
42 | my $authuser = $rpcenv->get_user(); | |
43 | ||
44 | my $cfg = PVE::ReplicationConfig->new(); | |
45 | ||
46 | my $res = []; | |
47 | foreach my $id (sort keys %{$cfg->{ids}}) { | |
48 | my $d = $cfg->{ids}->{$id}; | |
49 | my $vmid = $d->{guest}; | |
50 | next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ]); | |
51 | $d->{id} = $id; | |
52 | push @$res, $d; | |
53 | } | |
54 | ||
55 | return $res; | |
56 | }}); | |
57 | ||
58 | __PACKAGE__->register_method ({ | |
59 | name => 'read', | |
60 | path => '{id}', | |
61 | method => 'GET', | |
62 | description => "Read replication job configuration.", | |
63 | permissions => { | |
64 | description => "Requires the VM.Audit permission on /vms/<vmid>.", | |
65 | user => 'all', | |
66 | }, | |
67 | parameters => { | |
68 | additionalProperties => 0, | |
69 | properties => { | |
70 | id => get_standard_option('pve-replication-id'), | |
71 | }, | |
72 | }, | |
73 | returns => { type => 'object' }, | |
74 | code => sub { | |
75 | my ($param) = @_; | |
76 | ||
77 | my $rpcenv = PVE::RPCEnvironment::get(); | |
78 | my $authuser = $rpcenv->get_user(); | |
79 | ||
80 | my $cfg = PVE::ReplicationConfig->new(); | |
81 | ||
82 | my $data = $cfg->{ids}->{$param->{id}}; | |
83 | ||
84 | die "no such replication job '$param->{id}'\n" if !defined($data); | |
85 | ||
86 | my $vmid = $data->{guest}; | |
87 | ||
88 | raise_perm_exc() if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Audit' ]); | |
89 | ||
90 | $data->{id} = $param->{id}; | |
91 | ||
92 | return $data; | |
93 | }}); | |
94 | ||
95 | __PACKAGE__->register_method ({ | |
96 | name => 'create', | |
97 | path => '', | |
98 | protected => 1, | |
99 | method => 'POST', | |
100 | description => "Create a new replication job", | |
101 | permissions => { | |
102 | check => ['perm', '/storage', ['Datastore.Allocate']], | |
103 | }, | |
104 | parameters => PVE::ReplicationConfig->createSchema(), | |
105 | returns => { type => 'null' }, | |
106 | code => sub { | |
107 | my ($param) = @_; | |
108 | ||
109 | my $type = extract_param($param, 'type'); | |
110 | my $plugin = PVE::ReplicationConfig->lookup($type); | |
111 | my $id = extract_param($param, 'id'); | |
112 | ||
a9da300d DM |
113 | # extract guest ID from job ID |
114 | my ($guest) = PVE::ReplicationConfig::parse_replication_job_id($id); | |
115 | ||
4542a42a DM |
116 | my $nodelist = PVE::Cluster::get_members(); |
117 | my $vmlist = PVE::Cluster::get_vmlist(); | |
118 | ||
119 | die "Guest '$guest' does not exists.\n" | |
120 | if !defined($vmlist->{ids}->{$guest}); | |
121 | die "Target '$param->{target}' does not exists.\n" | |
122 | if defined($param->{target}) && !defined($nodelist->{$param->{target}}); | |
123 | ||
892821fd DM |
124 | my $code = sub { |
125 | my $cfg = PVE::ReplicationConfig->new(); | |
126 | ||
892821fd DM |
127 | die "replication job '$id' already exists\n" |
128 | if $cfg->{ids}->{$id}; | |
129 | ||
130 | my $opts = $plugin->check_config($id, $param, 1, 1); | |
131 | ||
a9da300d DM |
132 | $opts->{guest} = $guest; |
133 | ||
892821fd DM |
134 | $cfg->{ids}->{$id} = $opts; |
135 | ||
136 | $cfg->write(); | |
137 | }; | |
138 | ||
139 | PVE::ReplicationConfig::lock($code); | |
140 | ||
141 | return undef; | |
142 | }}); | |
143 | ||
144 | ||
145 | __PACKAGE__->register_method ({ | |
146 | name => 'update', | |
147 | protected => 1, | |
148 | path => '{id}', | |
149 | method => 'PUT', | |
150 | description => "Update replication job configuration.", | |
151 | permissions => { | |
152 | check => ['perm', '/storage', ['Datastore.Allocate']], | |
153 | }, | |
154 | parameters => PVE::ReplicationConfig->updateSchema(), | |
155 | returns => { type => 'null' }, | |
156 | code => sub { | |
157 | my ($param) = @_; | |
158 | ||
159 | my $id = extract_param($param, 'id'); | |
160 | ||
161 | my $code = sub { | |
162 | my $cfg = PVE::ReplicationConfig->new(); | |
163 | ||
164 | my $data = $cfg->{ids}->{$id}; | |
165 | die "no such job '$id'\n" if !$data; | |
166 | ||
167 | my $plugin = PVE::ReplicationConfig->lookup($data->{type}); | |
168 | my $opts = $plugin->check_config($id, $param, 0, 1); | |
169 | ||
170 | foreach my $k (%$opts) { | |
171 | $data->{$k} = $opts->{$k}; | |
172 | } | |
173 | ||
174 | $cfg->write(); | |
175 | }; | |
176 | ||
177 | PVE::ReplicationConfig::lock($code); | |
178 | ||
179 | return undef; | |
180 | }}); | |
181 | ||
182 | __PACKAGE__->register_method ({ | |
183 | name => 'delete', | |
184 | protected => 1, | |
185 | path => '{id}', | |
186 | method => 'DELETE', | |
f9d38c54 | 187 | description => "Mark replication job for removal.", |
892821fd DM |
188 | permissions => { |
189 | check => ['perm', '/storage', ['Datastore.Allocate']], | |
190 | }, | |
191 | parameters => { | |
192 | additionalProperties => 0, | |
193 | properties => { | |
194 | id => get_standard_option('pve-replication-id'), | |
195 | keep => { | |
196 | description => "Keep replicated data at target (do not remove).", | |
197 | type => 'boolean', | |
198 | optional => 1, | |
199 | default => 0, | |
200 | }, | |
948136a4 WL |
201 | force => { |
202 | description => "Will remove the jobconfig entry, but will not cleanup.", | |
203 | type => 'boolean', | |
204 | optional => 1, | |
205 | default => 0, | |
206 | }, | |
892821fd DM |
207 | } |
208 | }, | |
209 | returns => { type => 'null' }, | |
210 | code => sub { | |
211 | my ($param) = @_; | |
212 | ||
bc1ec7bc DM |
213 | my $rpcenv = PVE::RPCEnvironment::get(); |
214 | ||
892821fd DM |
215 | my $code = sub { |
216 | my $cfg = PVE::ReplicationConfig->new(); | |
217 | ||
f9d38c54 | 218 | my $id = $param->{id}; |
948136a4 | 219 | if ($param->{force}) { |
9a427c6c | 220 | raise_param_exc({ 'keep' => "conflicts with parameter 'force'" }) if $param->{keep}; |
948136a4 | 221 | delete $cfg->{ids}->{$id}; |
f9d38c54 | 222 | } else { |
948136a4 WL |
223 | my $jobcfg = $cfg->{ids}->{$id}; |
224 | die "no such job '$id'\n" if !$jobcfg; | |
225 | ||
226 | if (!$param->{keep} && $jobcfg->{type} eq 'local') { | |
227 | # remove local snapshots and remote volumes | |
228 | $jobcfg->{remove_job} = 'full'; | |
229 | } else { | |
230 | # only remove local snapshots | |
231 | $jobcfg->{remove_job} = 'local'; | |
232 | } | |
233 | ||
234 | warn "Replication job removal is a background task and will take some time.\n" | |
235 | if $rpcenv->{type} eq 'cli'; | |
892821fd | 236 | } |
892821fd DM |
237 | $cfg->write(); |
238 | }; | |
239 | ||
240 | PVE::ReplicationConfig::lock($code); | |
241 | ||
242 | return undef; | |
243 | }}); | |
f9d38c54 | 244 | |
892821fd | 245 | 1; |