]>
Commit | Line | Data |
---|---|---|
41d185e8 DM |
1 | package PVE::APIClient::Commands::config; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Data::Dumper; | |
6 | ||
b4007425 | 7 | use PVE::APIClient::Helpers; |
c9138c03 | 8 | use PVE::APIClient::JSONSchema qw(get_standard_option); |
ca3269f4 | 9 | use PVE::APIClient::Tools qw(extract_param); |
41d185e8 DM |
10 | use PVE::APIClient::Config; |
11 | ||
59d3653b | 12 | use PVE::APIClient::CLIFormatter; |
4aba8d31 | 13 | use PVE::APIClient::RESTHandler; |
c9138c03 | 14 | use PVE::APIClient::CLIHandler; |
41d185e8 | 15 | |
c9138c03 | 16 | use base qw(PVE::APIClient::CLIHandler); |
41d185e8 | 17 | |
e42d9cfe DM |
18 | my $list_return_props = { %{PVE::APIClient::DefaultsConfig->updateSchema(1)->{properties}} }; |
19 | delete $list_return_props->{delete}; | |
20 | ||
41d185e8 DM |
21 | __PACKAGE__->register_method ({ |
22 | name => 'list', | |
23 | path => 'list', | |
24 | method => 'GET', | |
25 | description => "Dump default configuration.", | |
26 | parameters => { | |
27 | additionalProperties => 0, | |
59d3653b | 28 | properties => {}, |
41d185e8 | 29 | }, |
e42d9cfe DM |
30 | returns => { |
31 | type => 'object', | |
32 | properties => $list_return_props, | |
33 | }, | |
41d185e8 DM |
34 | code => sub { |
35 | ||
36 | my $config = PVE::APIClient::Config->load(); | |
b4007425 | 37 | |
41d185e8 DM |
38 | my $defaults = PVE::APIClient::Config->get_defaults($config); |
39 | ||
e42d9cfe | 40 | $defaults->{digest} = $config->{digest}; |
b4007425 | 41 | |
e42d9cfe | 42 | return $defaults; |
41d185e8 DM |
43 | }}); |
44 | ||
45 | __PACKAGE__->register_method ({ | |
46 | name => 'set', | |
47 | path => 'set', | |
48 | method => 'PUT', | |
49 | description => "Update a remote configuration.", | |
50 | parameters => PVE::APIClient::DefaultsConfig->updateSchema(1), | |
51 | returns => { type => 'null'}, | |
52 | code => sub { | |
53 | my ($param) = @_; | |
54 | ||
41d185e8 DM |
55 | my $digest = extract_param($param, 'digest'); |
56 | my $delete = extract_param($param, 'delete'); | |
57 | ||
b4007425 DM |
58 | my $code = sub { |
59 | my $config = PVE::APIClient::Config->load(); | |
633508aa DM |
60 | |
61 | PVE::APIClient::Tools::assert_if_modified($config->{digest}, $digest); | |
62 | ||
b4007425 DM |
63 | my $defaults = PVE::APIClient::Config->get_defaults($config); |
64 | ||
65 | my $plugin = PVE::APIClient::Config->lookup('defaults'); | |
66 | my $opts = $plugin->check_config('defaults', $param, 0, 1); | |
67 | ||
68 | foreach my $k (%$opts) { | |
69 | $defaults->{$k} = $opts->{$k}; | |
41d185e8 | 70 | } |
41d185e8 | 71 | |
b4007425 DM |
72 | if ($delete) { |
73 | my $options = $plugin->private()->{options}->{'defaults'}; | |
74 | foreach my $k (PVE::APIClient::Tools::split_list($delete)) { | |
75 | my $d = $options->{$k} || | |
76 | die "no such option '$k'\n"; | |
77 | die "unable to delete required option '$k'\n" | |
78 | if !$d->{optional}; | |
79 | die "unable to delete fixed option '$k'\n" | |
80 | if $d->{fixed}; | |
81 | delete $defaults->{$k}; | |
82 | } | |
83 | } | |
84 | ||
85 | PVE::APIClient::Config->save($config); | |
86 | }; | |
87 | ||
88 | PVE::APIClient::Config->lock_config(undef, $code); | |
41d185e8 DM |
89 | |
90 | return undef; | |
91 | }}); | |
92 | ||
93 | ||
94 | our $cmddef = { | |
95 | set => [ __PACKAGE__, 'set',], | |
59d3653b DM |
96 | list => [__PACKAGE__, 'list', undef, undef, |
97 | sub { | |
98 | my ($data, $schema, $options) = @_; | |
99 | ||
100 | PVE::APIClient::CLIFormatter::print_api_result($data, $schema, undef, $options); | |
4aba8d31 DM |
101 | }, |
102 | $PVE::APIClient::RESTHandler::standard_output_options, | |
59d3653b | 103 | ], |
41d185e8 DM |
104 | }; |
105 | ||
106 | 1; |