Commit Graph

26 Commits

Author SHA1 Message Date
Wolfgang Bumiller
30c61e7d43 litvms: add --port parameter
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-28 10:33:27 +01:00
Max Carrara
9c9dc29835 listvms: remove unused import and unaccessed exception
Signed-off-by: Max Carrara <m.carrara@proxmox.com>
2024-03-27 11:44:52 +01:00
Wolfgang Bumiller
cd0243f9ea listvms.py: improve error message when password file is missing
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-27 10:51:33 +01:00
Wolfgang Bumiller
c2ba7f04da listvms.py: improve error message for invalid credentials
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-27 10:45:48 +01:00
Wolfgang Bumiller
58ad9385d4 listvms.py: just print the errors as they are
they are unexpected by nature...

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-27 10:45:19 +01:00
Wolfgang Bumiller
555d5fe1d0 listvms.py: drop squeeze helper
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-27 10:43:32 +01:00
Wolfgang Bumiller
9ae699de15 listvms.py: actually don't use Path for the types
we just want to pass whatever we get onward to perl & rust and not
place restrictions on it

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-27 10:27:17 +01:00
Max Carrara
1563eddb55 listvms: run formatter
This commit formats the script using `black -l 80` [0], even though we
don't have an official style guide for Python.

[0]: https://github.com/psf/black

Signed-off-by: Max Carrara <m.carrara@proxmox.com>
2024-03-27 10:22:47 +01:00
Max Carrara
edf287b490 listvms: dump json directly to stdout
Signed-off-by: Max Carrara <m.carrara@proxmox.com>
2024-03-27 10:22:46 +01:00
Max Carrara
5f6e1d5bfb listvms: add arg parser, context manager for connections, fetch helper
In order to make the CLI interface more friendly to humans, Python's
`argparse` [0] module from the standard library is used to parse the
arguments provided to the script. Each option and positional argument
also contain a short help text that is shown when running the script
with either "-h" or "--help".

Additionally, this commit also adds a context manager [1] for
establishing connections to an ESXi host. The context manager ensures
that the connection is closed in its inner `finally` block.

The inner part of the VM-data-fetching loop in `main()` is factored
out into a separate helper function, which now raises a `RuntimeError`
if the datacenter of a VM cannot be looked up.

In general, should any exception be thrown inside the loop, its output
is subsequently logged to stderr. The loop then just continues like
before.

Any exception that is not caught inside of `main()` is now printed to
stderr, followed by exiting with `1`.

Overall, the script's behaviour and output on successful operations
remains the same, except regarding unsuccessful argument parsing and
displaying error messages. In other words, invocations prior to this
patch should result in the same JSON output (if successful).

This was tested by piping the outputs of this script before and after
this commit through `jq` and then comparing the outputs with `diff`.

[0]: https://docs.python.org/3.11/library/argparse.html
[1]: https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager

Signed-off-by: Max Carrara <m.carrara@proxmox.com>
2024-03-27 10:22:45 +01:00
Max Carrara
cda87e27df listvms: improve typing and add dataclasses to represent dicts
This commit replaces some of the explicitly imported types from the
`typing` module with their inbuilt counterparts, e.g. `typing.List`
becomes `list`. This is supported since Python 3.9 [0].

Additionally, file paths are now represented as `pathlib.Path` [1],
which also checks whether the given string is actually a valid path
when constructed.

Furthermore, the `dict`s with values of mixed types are now
represented as dataclasses [2] instead, in order to make them more
type-safe (--> allow for better linting).

Because dataclasses and `pathlib.Path`s are not JSON-serializable by
default however, a helper function is added, which allows for more
fine-grained control regarding how those objects are serialized.

[0]: https://docs.python.org/3.9/whatsnew/3.9.html#type-hinting-generics-in-standard-collections
[1]: https://docs.python.org/3.11/library/pathlib.html
[2]: https://docs.python.org/3.11/library/dataclasses.html

Signed-off-by: Max Carrara <m.carrara@proxmox.com>
2024-03-27 10:22:43 +01:00
Max Carrara
3b28eb2843 listvms: reorder imports
Signed-off-by: Max Carrara <m.carrara@proxmox.com>
2024-03-27 10:22:41 +01:00
Max Carrara
f67c5686d1 listvms: remove unused import and variable
Signed-off-by: Max Carrara <m.carrara@proxmox.com>
2024-03-27 10:22:39 +01:00
Wolfgang Bumiller
f623cc16ec listvms.py: reword certificate validation error
this seems to be more clear and describes the specific actions to take
less ambiguously

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-22 11:48:08 +01:00
Wolfgang Bumiller
7212cadb1b listvms.py: make cert validation errors show a more conside message
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-22 11:00:40 +01:00
Wolfgang Bumiller
df938b0080 listvms.py: output connection error via stderr
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-22 10:55:33 +01:00
Thomas Lamprecht
34c87be1ff rename --insecure option to --skip-cert-verification
a bit more telling about what it actually does.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2024-03-12 16:05:15 +01:00
Wolfgang Bumiller
c292c674f2 listvms.py: add --insecure parameter, verify cert by default
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-12 13:29:58 +01:00
Wolfgang Bumiller
95d44d2c94 listvms.py: include power status in vm info
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-11 13:47:34 +01:00
Wolfgang Bumiller
221e1509ca listvms.py: don't iterate through vms twice, add disk info
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-11 13:47:30 +01:00
Wolfgang Bumiller
ca4d10a086 listvms.py: add function to query disk info for vms
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-11 13:47:30 +01:00
Wolfgang Bumiller
3baab2d2a4 listvms.py: factor out datastore/path file name parsing
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-03-11 13:47:27 +01:00
Wolfgang Bumiller
cb8312c727 listvms.py: make connection errors less noisy
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-02-15 10:34:53 +01:00
Wolfgang Bumiller
41af6c8623 add shebang to listvms.py, set mode to 0755
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-02-15 10:05:18 +01:00
Wolfgang Bumiller
94ba8066e1 lsitvms.py: read password from a file instead of env var
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2024-02-14 16:20:57 +01:00
Wolfgang Bumiller
dc7c06ef5f import listvms.py
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Originally-by: Hannes Laimer <h.laimer@proxmox.com>
2024-02-14 16:08:25 +01:00