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