]> git.proxmox.com Git - pve-manager.git/blame - PVE/API2/ReplicationConfig.pm
PVE::Replication::replicate - Set $running to 0 if undef.
[pve-manager.git] / PVE / API2 / ReplicationConfig.pm
CommitLineData
892821fd
DM
1package PVE::API2::ReplicationConfig;
2
3use warnings;
4use strict;
5
6use PVE::Tools qw(extract_param);
f9d38c54 7use PVE::Exception qw(raise_perm_exc raise_param_exc);
892821fd
DM
8use PVE::JSONSchema qw(get_standard_option);
9use PVE::RPCEnvironment;
10use PVE::ReplicationConfig;
11
12use PVE::RESTHandler;
13
14use 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
a9da300d
DM
112 # extract guest ID from job ID
113 my ($guest) = PVE::ReplicationConfig::parse_replication_job_id($id);
114
892821fd
DM
115 my $code = sub {
116 my $cfg = PVE::ReplicationConfig->new();
117
892821fd
DM
118 die "replication job '$id' already exists\n"
119 if $cfg->{ids}->{$id};
120
121 my $opts = $plugin->check_config($id, $param, 1, 1);
122
a9da300d
DM
123 $opts->{guest} = $guest;
124
892821fd
DM
125 $cfg->{ids}->{$id} = $opts;
126
127 $cfg->write();
128 };
129
130 PVE::ReplicationConfig::lock($code);
131
132 return undef;
133 }});
134
135
136__PACKAGE__->register_method ({
137 name => 'update',
138 protected => 1,
139 path => '{id}',
140 method => 'PUT',
141 description => "Update replication job configuration.",
142 permissions => {
143 check => ['perm', '/storage', ['Datastore.Allocate']],
144 },
145 parameters => PVE::ReplicationConfig->updateSchema(),
146 returns => { type => 'null' },
147 code => sub {
148 my ($param) = @_;
149
150 my $id = extract_param($param, 'id');
151
152 my $code = sub {
153 my $cfg = PVE::ReplicationConfig->new();
154
155 my $data = $cfg->{ids}->{$id};
156 die "no such job '$id'\n" if !$data;
157
158 my $plugin = PVE::ReplicationConfig->lookup($data->{type});
159 my $opts = $plugin->check_config($id, $param, 0, 1);
160
161 foreach my $k (%$opts) {
162 $data->{$k} = $opts->{$k};
163 }
164
165 $cfg->write();
166 };
167
168 PVE::ReplicationConfig::lock($code);
169
170 return undef;
171 }});
172
173__PACKAGE__->register_method ({
174 name => 'delete',
175 protected => 1,
176 path => '{id}',
177 method => 'DELETE',
f9d38c54 178 description => "Mark replication job for removal.",
892821fd
DM
179 permissions => {
180 check => ['perm', '/storage', ['Datastore.Allocate']],
181 },
182 parameters => {
183 additionalProperties => 0,
184 properties => {
185 id => get_standard_option('pve-replication-id'),
186 keep => {
187 description => "Keep replicated data at target (do not remove).",
188 type => 'boolean',
189 optional => 1,
190 default => 0,
191 },
192 }
193 },
194 returns => { type => 'null' },
195 code => sub {
196 my ($param) = @_;
197
892821fd
DM
198 my $code = sub {
199 my $cfg = PVE::ReplicationConfig->new();
200
f9d38c54 201 my $id = $param->{id};
892821fd 202
f9d38c54
DM
203 my $jobcfg = $cfg->{ids}->{$id};
204 die "no such job '$id'\n" if !$jobcfg;
892821fd 205
f9d38c54
DM
206 if (!$param->{keep} && $jobcfg->{type} eq 'local') {
207 # remove local snapshots and remote volumes
208 $jobcfg->{remove_job} = 'full';
209 } else {
210 # only remove local snapshots
211 $jobcfg->{remove_job} = 'local';
892821fd 212 }
892821fd
DM
213
214 $cfg->write();
215 };
216
217 PVE::ReplicationConfig::lock($code);
218
219 return undef;
220 }});
f9d38c54 221
892821fd 2221;