Commit Graph

35 Commits

Author SHA1 Message Date
Max Carrara
6cba8d7660 section config: clean up parser logic
In order to make the parser somewhat more maintainable in the future,
this commit cleans up its logic and makes its control flow easier to
follow.

Signed-off-by: Max Carrara <m.carrara@proxmox.com>
2024-07-04 10:54:48 +02:00
Max Carrara
29292d2a5d section config: update code style
Replace `foreach` with `for` and use postfix deref instead of block
(circumfix) dereference (`$foo->%*` instead of `%$foo`).

Furthermore, make `format_config_line` a private sub instead of
unnecessarily declaring it as an anonymous subroutine, which avoids
the `&$sub_ref(...)` syntax altogether.

Signed-off-by: Max Carrara <m.carrara@proxmox.com>
2024-07-04 10:54:48 +02:00
Max Carrara
d41bd420f8 section config: document package and its methods with POD
Apart from the obvious benefits that documentation has, this also
allows LSPs to provide docstrings e.g. via 'textDocument/hover' [0].

Tested with Perl Navigator [1].

[0]: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover
[1]: https://github.com/bscan/PerlNavigator

Signed-off-by: Max Carrara <m.carrara@proxmox.com>
2024-07-04 10:54:48 +02:00
Thomas Lamprecht
7ccdc805e3 section config: avoid unamed boolean parameter use hash
Even with just one param it's extra work to check what it refers too,
with named ones in a hash one hasn't that issue even with many params.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2023-11-17 09:58:01 +01:00
Dominik Csapak
7887b1cb0e section config: allow full property-isolation for plugins
When using 'init(property_isolation => 1)', the code saves the
property lists per type instead of a big one, and using
create/updateSchema creates a new schema with the options as 'oneOf'
and/or 'instance-types' (depending if the schemas match).

With that, we change how we work with the options hash:

It's not needed anymore to specify options that are specified in the
type specific propertyList, except if it's 'fixed => 1' (since that does
not exist in the schema)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
 [ TL: ensure consistency with new property-isolation terminology ]
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2023-11-17 09:57:34 +01:00
Dominik Csapak
d2a6411cab SectionConfig: fix handling unknown sections
if we're parsing an unknown section, we cannot check the schema with
`is_array` to check if it's an array type or not, thus we have to
handle that separately.

fix this by handling data in unknown sections like an array similar to
"cb2646c7b4974e33f4148752deec71f0d589b0f3" in proxmox-section-config.
This way we can write unknown section out again like we parsed it.

Add a regression test for an unknown field not in the schema.

This fixes an issue, where calling `qm destroy ID --purge` removed much
of the configs ob backup jobs (since there we parse an 'unknown' section
and run into the `is_array` error)
(Reported in the forum: https://forum.proxmox.com/threads/132091)

Suggested-by: Fiona Ebner <f.ebner@proxmox.com>
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2023-08-16 11:19:26 +02:00
Christoph Heiss
741bf653ae section config: allow base properties for {create, update}Schema()
This works the same way as e.g. get_standard_option does it.

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
2023-08-11 13:22:16 +02:00
Dominik Csapak
69d9edcc75 section config: implement array support
enables section configs in the style of:

----
type: id
    property value
    property value2
    property value3
----

can be combined with property strings

the provided create and update schema just pass through the array type
to the api, so the api call must always contain the complete array

also adds a test case for such array fields

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2023-06-07 13:50:51 +02:00
Thomas Lamprecht
967e98230a various perl critic fixes
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2023-04-11 16:17:10 +02:00
Dominik Csapak
5028848d42 section config: add helper for deleting keys from a entry
This is a pattern that can be found often in Proxmox VE's API stack,
so implement it centrally here for re-use.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2023-03-11 18:14:32 +01:00
Wolfgang Bumiller
c9ede1c3b3 support unknown types in section config
This adds an opt-in flag to `parse_config` and
`write_config` to allow for unknown section types.

This will simply be left unverified.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2022-11-12 15:41:50 +01:00
Fabian Ebner
4b32ef6e2c SectionConfig: parse_config: add errors to result
so that callers can know about them. This is useful in places where we'd rather
abort then continue with a faulty configuration. For example, when reading the
storage configuration before executing a backup job.

Originally-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2020-12-21 15:48:16 +01:00
Thomas Lamprecht
7db4e5d763 section config: use croak so that the error is from the callers POV
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2020-04-29 14:51:59 +02:00
Thomas Lamprecht
848231afd2 section config: remove Data::Dumper usage
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-11-18 15:39:02 +01:00
Fabian Ebner
9e594bd42e SectionConfig: Cleanup to make style consistent
Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
2019-09-04 16:18:19 +02:00
Fabian Ebner
e1fbb779f7 Fix 2339: Handle multiple blank lines correctly in SectionConfig
It turns out that the line number counting was also broken (even on
files without multiple blanks), since the body of the while inside
the nextline subroutine would not be executed for a blank.
I guess the subroutine was intended to skip comments and blanks, but
since we use blanks to recognize the end of a section, I changed it
to only skip comments.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
2019-09-04 16:17:55 +02:00
Thomas Lamprecht
cca9c8642d section config: trailing whitespace cleanup
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-07-05 16:32:57 +02:00
Thomas Lamprecht
95244fd7fa followup: use defined to check if a hash element can be accessed
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-05-15 10:20:01 +02:00
Dominik Csapak
d0ba18e17e prevent autovivification of sectionconfig options
If, somehow, someone passes a config to check_config with keys set
that are not in the options for that type, this fixed check
lead to autovivification, meaning that any future calls to the same
worker had an additional option for that type which is not optional

this lead to a wrongfully deleting of entries when updating an entry of
a different type, since all entries of the original types suddenly
did not satisfy their required options and would not get parsed
by read_file anymore (thus missing when a successful write_file was done)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2019-05-15 10:16:33 +02:00
Dietmar Maurer
2d63b598e1 SectionConfig.pm: pass section data to format_section_header
So that we can store data in section headers
2017-05-10 11:32:08 +02:00
Dietmar Maurer
bbca3bdc48 SectionConfig::check_value: return bool/numbers as numeric value
This is important when we convert the result to JSON.
Else the GUI receives booleans as "0", which evaluate to true
in JS!

NOTE: "0" evaluates to false with perl.
2017-02-25 09:41:17 +01:00
Dietmar Maurer
9c2e403407 SectionConfig: add new parameter $skip_type to createSchema()
This is useful when there is only a single type of entries.
2017-02-20 12:40:33 +01:00
Dietmar Maurer
df89e5eb15 SectionConfig:write_config: sort options by key name
Else we can get different config digest...
2017-02-19 14:47:00 +01:00
Dietmar Maurer
885641fb6a SectionConfig: allow to get class specific updateSchema()
Useful when you have different section types, and you want to have
separate update API methods.
2017-02-19 11:17:34 +01:00
Wolfgang Bumiller
d152f8b0ee SectionConfig: always write out explicitly set booleans
Because when only writing the ones which are true we're
breaking the ones which default to true (like the mkdir
option on directory storages, where we need a false value
to be written out explicitly).
2016-10-18 10:50:04 +02:00
Dietmar Maurer
a7f30ebf27 SectionConfig: sort values return by lookup_types
They are use to generate type enums in the API.
2016-04-30 12:01:20 +02:00
Dietmar Maurer
08aa41d725 SectionConfig.pm: sort type enum
So that we can better detect API changes.
2016-04-30 11:50:55 +02:00
Dietmar Maurer
ebd2b0aca5 SectionConfig: disable duplicate plugin registration 2016-01-26 13:16:19 +01:00
Dietmar Maurer
0cb4d663f5 SectionConfig: protect against newline injection 2015-09-20 10:34:04 +02:00
Dietmar Maurer
771d18f5c7 createSchema: include type property 2015-09-18 08:51:09 +02:00
Dietmar Maurer
0d8380351a fix SectionConfig updateSchema for classes without plugins. 2015-09-18 07:18:00 +02:00
Dietmar Maurer
a3530f55ed SectionConfig: fix createSchema
correctly compute required properties.
2015-09-18 07:14:43 +02:00
Dietmar Maurer
516dfb55e1 updateSchema: code cleanup - avoid assigning same value multiple times 2015-09-17 17:45:59 +02:00
Wolfgang Bumiller
3c4d612a70 improve parse_config in JSONSchema and SectionConfig
The old code used string substitution for every line of the
input string, while perl can also iterate over all matches
via the /g re modifier, and can turn ^ and $ to act as
beginning/end of line via the /m modifier.

Effectively allowing a "match over all lines" via a simple
while ($data =~ /^.*$/gm);

The situation is a little different in SectionConfig because
there's a nested loop invovled which doesn't work with /g.
For this there are two options and I chose the safer one by
simply doing a split on newlines first.
The alternative would be to open the data as a
filehandle-to-string and use <$fh> to read lines, however
I'd have to throw in an eval{} to be sure to close the
handle afterwards.
2015-07-22 08:15:54 +02:00
Dietmar Maurer
b51b16e6f5 rename data to src 2015-02-27 16:57:20 +01:00