vcf-super-cli 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,101 @@
1
+ Metadata-Version: 2.4
2
+ Name: vcf-super-cli
3
+ Version: 0.2.0
4
+ Summary: Modern, agent-friendly CLI for VMware Cloud Foundation 9, with a command tree generated dynamically from the vcf-sdk vAPI bindings.
5
+ Project-URL: Homepage, https://github.com/thomaschristory/vcf-super-cli
6
+ Project-URL: Documentation, https://thomaschristory.github.io/vcf-super-cli/
7
+ Project-URL: Issues, https://github.com/thomaschristory/vcf-super-cli/issues
8
+ Author-email: Thomas <mick27@gmail.com>
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: automation,cli,nsx,vcenter,vcf,vcf-sdk,vmware,vsphere
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: System Administrators
15
+ Classifier: License :: OSI Approved :: Apache Software License
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: System :: Networking
19
+ Classifier: Topic :: System :: Systems Administration
20
+ Requires-Python: >=3.12
21
+ Requires-Dist: keyring>=25.0
22
+ Requires-Dist: platformdirs>=4.2
23
+ Requires-Dist: pydantic>=2.7
24
+ Requires-Dist: pyvmomi>=9.1.0.0
25
+ Requires-Dist: rich>=13.7
26
+ Requires-Dist: ruamel-yaml>=0.18
27
+ Requires-Dist: structlog>=24.1
28
+ Requires-Dist: typer>=0.12
29
+ Requires-Dist: vcf-nsx>=9.1.0.0
30
+ Requires-Dist: vmware-vcenter>=9.1.0.0
31
+ Description-Content-Type: text/markdown
32
+
33
+ # vcf-super-cli (`vsc`)
34
+
35
+ [![PyPI](https://img.shields.io/pypi/v/vcf-super-cli.svg)](https://pypi.org/project/vcf-super-cli/)
36
+ [![CI](https://github.com/thomaschristory/vcf-super-cli/actions/workflows/test.yml/badge.svg)](https://github.com/thomaschristory/vcf-super-cli/actions/workflows/test.yml)
37
+ [![Lint](https://github.com/thomaschristory/vcf-super-cli/actions/workflows/lint.yml/badge.svg)](https://github.com/thomaschristory/vcf-super-cli/actions/workflows/lint.yml)
38
+ [![Docs](https://github.com/thomaschristory/vcf-super-cli/actions/workflows/docs.yml/badge.svg)](https://thomaschristory.github.io/vcf-super-cli/)
39
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
40
+
41
+ A modern, **agent-friendly** CLI for **VMware Cloud Foundation 9**. The command
42
+ tree is **generated dynamically** by introspecting the official
43
+ [`vcf-sdk`](https://pypi.org/project/vcf-sdk/) vAPI bindings — so the surface
44
+ mirrors the real VCF 9 API instead of being hand-maintained.
45
+
46
+ ```console
47
+ $ vsc vsphere vm list --profile prod
48
+ $ vsc vsphere host get --host host-42
49
+ $ vsc nsx segments list --output table
50
+ $ vsc vsphere power stop vm-42 --apply # writes are dry-run without --apply
51
+ ```
52
+
53
+ > ⚠️ **Alpha / pre-release.** Reads (vSphere + NSX inventory) and writes are both
54
+ > available. **Writes are dry-run by default** — nothing changes without `--apply`,
55
+ > and a dry-run never opens a connection.
56
+
57
+ ## Why
58
+
59
+ - **Mirrors the real API.** Commands come from the SDK's own vAPI metadata
60
+ (`VapiInterface` services + `OperationRestMetadata`), covering both vCenter and
61
+ NSX from one generator.
62
+ - **Modern.** REST-first via `vmware-vcenter`, with `pyVmomi` as a fallback where
63
+ REST lacks coverage.
64
+ - **Safe by default.** Writes preview as dry-runs unless you pass `--apply`; a
65
+ dry-run never opens a connection.
66
+ - **Agent-friendly.** Deterministic command shape, machine-readable JSON output,
67
+ a stable error envelope with documented exit codes, and a bundled agent Skill.
68
+
69
+ ## Scope
70
+
71
+ | Area | Status |
72
+ |------|--------|
73
+ | vSphere / vCenter read (`vsc vsphere …`) | ✅ v0.1 |
74
+ | NSX **Policy API** read (`vsc nsx …`) | ✅ v0.1 |
75
+ | Writes — dry-run by default + `--apply` (`vsc vsphere …` / `vsc nsx …`) | ✅ v0.2 |
76
+ | Dynamic shell completion, per-field filter flags, pyVmomi fallback | v0.3 (planned) |
77
+ | NSX Manager / Global-Manager, SDDC Manager, Operations, LCM | deferred |
78
+
79
+ ## Install
80
+
81
+ ```sh
82
+ uv tool install vcf-super-cli # recommended
83
+ # or
84
+ pipx install vcf-super-cli
85
+ # or
86
+ pip install vcf-super-cli
87
+ ```
88
+
89
+ From source:
90
+
91
+ ```sh
92
+ uv sync && uv run vsc --help
93
+ ```
94
+
95
+ ## Documentation
96
+
97
+ Full guide: **[thomaschristory.github.io/vcf-super-cli](https://thomaschristory.github.io/vcf-super-cli/)**
98
+
99
+ ## License
100
+
101
+ Apache-2.0. See [LICENSE](LICENSE).
@@ -0,0 +1,31 @@
1
+ vsc/__init__.py,sha256=V06TllRhI7XxsdyRD6D_JpSpRDYfM9GE0GXeQi-kPFc,316
2
+ vsc/_version.py,sha256=_RmvbVZrkbsHZ4h_b7TTacj9h4ent4lXeWux_gZnSeI,217
3
+ vsc/logging_config.py,sha256=zjxvOTkeBLXCkUTelkNzDRdU77TX97thMuLOzYl3Vow,861
4
+ vsc/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ vsc/cli/app.py,sha256=1dxgBQGPJJtGDQfdgKYaoJ7Cl0Zu2HLGuz2y3Y6jI3w,2731
6
+ vsc/cli/profiles.py,sha256=bo2hjpLGVM4ej5bh-ZWOr0UuHerh-ujdzF2rUvfxi6g,7874
7
+ vsc/cli/skill.py,sha256=ZeCjAV7PLrO6KIXy4CBI0wI5IKUvY8eYdOqTiYRI0nc,888
8
+ vsc/config/__init__.py,sha256=lvpce4_TyeNCa6USPeXTtspFkvssN9Q62PItNZPxGqo,339
9
+ vsc/config/schema.py,sha256=Lxk9YzfltkqFc--ZZnXQkdJk5ddgzwI3f0Q5uRWuFFc,949
10
+ vsc/config/store.py,sha256=05cVMUgKb7gWqcNljeNMFSUleBSAMOZt0sLEOQKrXUs,3329
11
+ vsc/connect/__init__.py,sha256=apTWhOMAhYKVn25TqjTeKUa7qtcCA_ki4ImFWnbWhMI,282
12
+ vsc/connect/session.py,sha256=_bc6-1OFOsUIcR_rIZACmXnE_ZWUxAw06TlQE6SL0Bc,2550
13
+ vsc/connect/targets.py,sha256=6saJAI8MOxfCOs62lbrhvthfxbg46NY0_awmbFU37sQ,4210
14
+ vsc/gen/__init__.py,sha256=kjx70eP9G_yGWF1KDf0LVLPB7Xh9zDV_qUa7hNCQ6ds,322
15
+ vsc/gen/builder.py,sha256=enje5AIf0QeKoxQRbJEkfRuduRf2N0cMRJt7Q0qUDd8,9588
16
+ vsc/gen/discover.py,sha256=nRnreZ2hheQ2oeqOSqo00HIQ6BufsKoVCnygtZtEWhg,8261
17
+ vsc/gen/model.py,sha256=EewK3SUGSgwenfLlFEn54Z76Hal_hqyGBX27P0nTkKI,3008
18
+ vsc/gen/params.py,sha256=tvsjbMUoQsiJNN_hTlkyvj3c1m2FEr_vwBVjd18HYzM,8366
19
+ vsc/gen/preview.py,sha256=hv42i8EtLaagQX8a_h6cgM_ODvUMxnH8bQGSRYgHKmY,3327
20
+ vsc/output/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
+ vsc/output/errors.py,sha256=Lt_E8nzpqiEl0T8hpB9-_-XnWHOGa2AZ-OPrTshwYXs,3613
22
+ vsc/output/exit_codes.py,sha256=AlTZzYcMm8K9ggId9yOf94nEynhUwz9SHxwoFr84vyI,972
23
+ vsc/output/render.py,sha256=CipvUYI16b9bvYhxGAJqX76yP63dz2w1IB5FP08duJI,4339
24
+ vsc/skill/__init__.py,sha256=ZT3uXUjY8ur2ec5VepFMYvr3xPIRbuXmzFK7mtc0qPQ,54
25
+ vsc/skill/export.py,sha256=31a-RV-NKnTf9YP8LpOxAhigvIO-yhv0I_8Gg5L6R9g,802
26
+ vsc/skill/assets/SKILL.md,sha256=ymao_n-qEMUA092YSFfetnZ5reap39eKOm3DoeVcEek,4229
27
+ vcf_super_cli-0.2.0.dist-info/METADATA,sha256=SMW5dXoWx7IPkXPJA6rQw3jY-j9__XFF1GO4AMWjcNU,4120
28
+ vcf_super_cli-0.2.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
29
+ vcf_super_cli-0.2.0.dist-info/entry_points.txt,sha256=YucZbal8BS_ujDQOeEdqy0mU5naS71A_H-QnxiLR-c0,74
30
+ vcf_super_cli-0.2.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
31
+ vcf_super_cli-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ vcf-super-cli = vsc.cli.app:main
3
+ vsc = vsc.cli.app:main
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
vsc/__init__.py ADDED
@@ -0,0 +1,11 @@
1
+ """vcf-super-cli (`vsc`) — a modern CLI for VMware Cloud Foundation 9.
2
+
3
+ The command tree is generated dynamically by introspecting the ``vcf-sdk`` vAPI
4
+ bindings. See ``docs/superpowers/specs`` for the design.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from vsc._version import __version__
10
+
11
+ __all__ = ["__version__"]
vsc/_version.py ADDED
@@ -0,0 +1,7 @@
1
+ """Single source of truth for the package version.
2
+
3
+ `release.yml` validates the git tag against this value before publishing.
4
+ Keep this in sync with `[project].version` in `pyproject.toml`.
5
+ """
6
+
7
+ __version__ = "0.2.0"
vsc/cli/__init__.py ADDED
File without changes
vsc/cli/app.py ADDED
@@ -0,0 +1,98 @@
1
+ """Entry point: assemble and run the ``vsc`` Typer application.
2
+
3
+ The ``vsphere`` and ``nsx`` groups are generated at startup by introspecting the
4
+ installed ``vcf-sdk`` vAPI bindings (``vsc.gen``). Generation is offline, so
5
+ ``--version`` and ``--help`` work without a server or credentials.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import typer
11
+
12
+ from vsc import __version__
13
+ from vsc.cli.profiles import profiles_app
14
+ from vsc.cli.skill import skill_app
15
+ from vsc.connect.targets import connect_for_backend, set_active_profile
16
+ from vsc.gen.builder import build_group
17
+ from vsc.gen.discover import (
18
+ discover_operations,
19
+ nsx_services,
20
+ vsphere_services,
21
+ )
22
+ from vsc.logging_config import configure_logging
23
+
24
+
25
+ def _build_app() -> typer.Typer:
26
+ configure_logging()
27
+ app = typer.Typer(
28
+ name="vsc",
29
+ help="CLI for VMware Cloud Foundation 9 (vSphere + NSX), generated from the vcf-sdk.",
30
+ no_args_is_help=True,
31
+ add_completion=True,
32
+ )
33
+
34
+ # Reads and writes are both mounted; write commands are dry-run by default and
35
+ # require --apply to execute (see vsc.gen.builder), so exposing them is safe.
36
+ vsphere_ops = [
37
+ op
38
+ for cls in vsphere_services()
39
+ for op in discover_operations(cls, "vsphere", read_only=False)
40
+ ]
41
+ nsx_ops = [
42
+ op for cls in nsx_services() for op in discover_operations(cls, "nsx", read_only=False)
43
+ ]
44
+
45
+ app.add_typer(
46
+ build_group(vsphere_ops, connect_for_backend),
47
+ name="vsphere",
48
+ help="vSphere / vCenter commands (generated from vmware-vcenter).",
49
+ no_args_is_help=True,
50
+ )
51
+ app.add_typer(
52
+ build_group(nsx_ops, connect_for_backend),
53
+ name="nsx",
54
+ help="NSX Policy commands (generated from vcf-nsx).",
55
+ no_args_is_help=True,
56
+ )
57
+ app.add_typer(profiles_app, name="profiles")
58
+ app.add_typer(skill_app, name="skill")
59
+
60
+ @app.callback()
61
+ def main_callback(
62
+ profile: str | None = typer.Option(
63
+ None,
64
+ "--profile",
65
+ "-p",
66
+ help="Named profile to use (overrides VSC_PROFILE and the config default).",
67
+ ),
68
+ _version: bool = typer.Option(
69
+ False,
70
+ "--version",
71
+ "-V",
72
+ help="Show the vsc version and exit.",
73
+ callback=_version_callback,
74
+ is_eager=True,
75
+ ),
76
+ ) -> None:
77
+ """Global options for ``vsc``."""
78
+ set_active_profile(profile)
79
+
80
+ return app
81
+
82
+
83
+ def _version_callback(value: bool) -> None:
84
+ if value:
85
+ typer.echo(__version__)
86
+ raise typer.Exit()
87
+
88
+
89
+ app = _build_app()
90
+
91
+
92
+ def main() -> None:
93
+ """Console-script entry point."""
94
+ app()
95
+
96
+
97
+ if __name__ == "__main__":
98
+ main()
vsc/cli/profiles.py ADDED
@@ -0,0 +1,208 @@
1
+ """Curated (non-generated) commands for managing named profiles."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import typer
6
+
7
+ from vsc.config.schema import BACKENDS, BackendCreds, Config, Profile
8
+ from vsc.config.store import (
9
+ ConfigError,
10
+ keyring_delete,
11
+ keyring_set,
12
+ load_config,
13
+ save_config,
14
+ )
15
+ from vsc.output.exit_codes import ExitCode
16
+ from vsc.output.render import to_json
17
+
18
+ profiles_app = typer.Typer(no_args_is_help=True, help="Manage named connection profiles.")
19
+
20
+
21
+ def _print(data: object) -> None:
22
+ print(to_json(data))
23
+
24
+
25
+ def _load() -> Config:
26
+ """Load config, turning a malformed file into a clean CONFIG exit."""
27
+ try:
28
+ return load_config()
29
+ except ConfigError as exc:
30
+ typer.echo(str(exc), err=True)
31
+ raise typer.Exit(int(ExitCode.CONFIG)) from exc
32
+
33
+
34
+ def _store_password(profile: str, backend: str, password: str, store_in_file: bool) -> str | None:
35
+ """Persist a password. Returns the value to keep in the file (None if keyring)."""
36
+ if store_in_file:
37
+ return password
38
+ if keyring_set(profile, backend, password):
39
+ return None
40
+ typer.echo(
41
+ f"No usable OS keyring backend; re-run with --store-in-file to save the "
42
+ f"{backend} password in the config file (mode 0600).",
43
+ err=True,
44
+ )
45
+ raise typer.Exit(int(ExitCode.CONFIG))
46
+
47
+
48
+ def _make_creds(
49
+ backend: str,
50
+ server: str | None,
51
+ username: str | None,
52
+ password: str | None,
53
+ insecure: bool,
54
+ store_in_file: bool,
55
+ ) -> tuple[BackendCreds | None, tuple[str, str] | None]:
56
+ """Build creds and, separately, any (backend, password) keyring write to defer.
57
+
58
+ Keyring writes are returned rather than performed so the caller can persist
59
+ them only after the config is saved (avoiding an orphaned keyring entry if a
60
+ later backend fails).
61
+ """
62
+ if not server and not username and not password:
63
+ return None, None
64
+ if not server or not username:
65
+ typer.echo(
66
+ f"{backend}: both --{backend}-server and --{backend}-username are required.", err=True
67
+ )
68
+ raise typer.Exit(int(ExitCode.USAGE))
69
+ if password and store_in_file:
70
+ return BackendCreds(
71
+ server=server, username=username, password=password, insecure=insecure
72
+ ), None
73
+ pending = (backend, password) if password else None
74
+ return BackendCreds(server=server, username=username, password=None, insecure=insecure), pending
75
+
76
+
77
+ @profiles_app.command("add")
78
+ def add(
79
+ name: str = typer.Argument(..., help="Profile name."),
80
+ vsphere_server: str | None = typer.Option(None, "--vsphere-server"),
81
+ vsphere_username: str | None = typer.Option(None, "--vsphere-username"),
82
+ vsphere_password: str | None = typer.Option(None, "--vsphere-password", hide_input=True),
83
+ vsphere_insecure: bool = typer.Option(False, "--vsphere-insecure/--vsphere-verify"),
84
+ nsx_server: str | None = typer.Option(None, "--nsx-server"),
85
+ nsx_username: str | None = typer.Option(None, "--nsx-username"),
86
+ nsx_password: str | None = typer.Option(None, "--nsx-password", hide_input=True),
87
+ nsx_insecure: bool = typer.Option(False, "--nsx-insecure/--nsx-verify"),
88
+ store_in_file: bool = typer.Option(
89
+ False, "--store-in-file", help="Store passwords in the config file instead of the keyring."
90
+ ),
91
+ use: bool = typer.Option(False, "--use/--no-use", help="Make this the current profile."),
92
+ ) -> None:
93
+ """Create or update a profile (passwords go to the OS keyring by default)."""
94
+ config = _load()
95
+ vsphere_creds, vsphere_pending = _make_creds(
96
+ "vsphere",
97
+ vsphere_server,
98
+ vsphere_username,
99
+ vsphere_password,
100
+ vsphere_insecure,
101
+ store_in_file,
102
+ )
103
+ nsx_creds, nsx_pending = _make_creds(
104
+ "nsx", nsx_server, nsx_username, nsx_password, nsx_insecure, store_in_file
105
+ )
106
+ config.profiles[name] = Profile(vsphere=vsphere_creds, nsx=nsx_creds)
107
+ if use or config.current_profile is None:
108
+ config.current_profile = name
109
+ path = save_config(config)
110
+
111
+ # Store keyring passwords only after the config is safely written.
112
+ pending = [p for p in (vsphere_pending, nsx_pending) if p is not None]
113
+ failed = [backend for backend, password in pending if not keyring_set(name, backend, password)]
114
+ if failed:
115
+ typer.echo(
116
+ f"Saved profile {name!r}, but no usable keyring backend stored the "
117
+ f"{', '.join(failed)} password(s). Re-run `vsc profiles set-password "
118
+ f"{name} <backend> --store-in-file`.",
119
+ err=True,
120
+ )
121
+ raise typer.Exit(int(ExitCode.CONFIG))
122
+ _print({"saved": name, "current": config.current_profile, "path": str(path)})
123
+
124
+
125
+ @profiles_app.command("list")
126
+ def list_profiles() -> None:
127
+ """List configured profiles."""
128
+ config = _load()
129
+ _print(
130
+ {
131
+ "current": config.current_profile,
132
+ "profiles": sorted(config.profiles),
133
+ }
134
+ )
135
+
136
+
137
+ @profiles_app.command("show")
138
+ def show(name: str = typer.Argument(..., help="Profile name.")) -> None:
139
+ """Show a profile (passwords are never printed)."""
140
+ config = _load()
141
+ profile = config.profiles.get(name)
142
+ if profile is None:
143
+ typer.echo(f"No such profile: {name}", err=True)
144
+ raise typer.Exit(int(ExitCode.NOT_FOUND))
145
+ out: dict[str, object] = {"name": name, "current": config.current_profile == name}
146
+ for backend in BACKENDS:
147
+ creds = profile.backend(backend)
148
+ if creds is not None:
149
+ out[backend] = {
150
+ "server": creds.server,
151
+ "username": creds.username,
152
+ "password": "set-in-file" if creds.password else "keyring-or-env",
153
+ "insecure": creds.insecure,
154
+ }
155
+ _print(out)
156
+
157
+
158
+ @profiles_app.command("use")
159
+ def use(name: str = typer.Argument(..., help="Profile name.")) -> None:
160
+ """Set the current (default) profile."""
161
+ config = _load()
162
+ if name not in config.profiles:
163
+ typer.echo(f"No such profile: {name}", err=True)
164
+ raise typer.Exit(int(ExitCode.NOT_FOUND))
165
+ config.current_profile = name
166
+ save_config(config)
167
+ _print({"current": name})
168
+
169
+
170
+ @profiles_app.command("delete")
171
+ def delete(name: str = typer.Argument(..., help="Profile name.")) -> None:
172
+ """Delete a profile and its keyring entries."""
173
+ config = _load()
174
+ if name not in config.profiles:
175
+ typer.echo(f"No such profile: {name}", err=True)
176
+ raise typer.Exit(int(ExitCode.NOT_FOUND))
177
+ del config.profiles[name]
178
+ for backend in BACKENDS:
179
+ keyring_delete(name, backend)
180
+ if config.current_profile == name:
181
+ config.current_profile = next(iter(config.profiles), None)
182
+ save_config(config)
183
+ _print({"deleted": name, "current": config.current_profile})
184
+
185
+
186
+ @profiles_app.command("set-password")
187
+ def set_password(
188
+ name: str = typer.Argument(..., help="Profile name."),
189
+ backend: str = typer.Argument(..., help="vsphere or nsx."),
190
+ password: str = typer.Option(..., "--password", prompt=True, hide_input=True),
191
+ store_in_file: bool = typer.Option(False, "--store-in-file"),
192
+ ) -> None:
193
+ """Set/replace the password for a profile backend."""
194
+ if backend not in BACKENDS:
195
+ typer.echo(f"backend must be one of {BACKENDS}", err=True)
196
+ raise typer.Exit(int(ExitCode.USAGE))
197
+ config = _load()
198
+ profile = config.profiles.get(name)
199
+ if profile is None or profile.backend(backend) is None:
200
+ typer.echo(f"No {backend} config in profile {name!r}", err=True)
201
+ raise typer.Exit(int(ExitCode.NOT_FOUND))
202
+ stored = _store_password(name, backend, password, store_in_file)
203
+ creds = profile.backend(backend)
204
+ assert creds is not None
205
+ creds.password = stored
206
+ config.profiles[name] = profile
207
+ save_config(config)
208
+ _print({"updated": name, "backend": backend, "in_file": stored is not None})