Skip to content

Commit 6c5ccc6

Browse files
fix: 🐛 fix boolean argparse argument for preconfig example
1 parent 4c5ae96 commit 6c5ccc6

2 files changed

Lines changed: 33 additions & 26 deletions

File tree

docs/source/examples/generate_preconfig.rst

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
44
.. important::
55

6-
The following example is more complex code than the other examples,
6+
The following example is more complex code than the general examples,
77
generating data and interacting with Orchestrator. Using and
88
modifying these examples requires a greater understanding of python
99
functions, handling variables, and additional tools such as Jinja.
1010

1111

12+
.. note::
13+
14+
The code referenced in this document and all published examples
15+
with pyedgeconnect are available from the GitHub repository within the
16+
`examples <https://https://github.com/SPOpenSource/edgeconnect-python/tree/main/examples>`_
17+
folder
18+
1219
Generate EdgeConnect Preconfig
1320
------------------------------
1421

@@ -154,17 +161,19 @@ Additional availble runtime arguments are as follows:
154161
- ``-o`` or ``--orch``
155162
- Type: String
156163
- Desc: Specify the Orchestrator IP or FQDN
157-
- Example values: ``10.100.1.90`` or ``orchestrator.<company>.com``
164+
- Example values: ``192.0.2.100`` or ``orchestrator.<company>.com``
158165
- Default value: ``None``
159166
- ``-u`` or ``--upload``
160167
- Type: Boolean
161-
- Desc: Upload the rendered YAML preconfig to Orchestrator
162-
- Accepted values: ``True`` or ``False``
168+
- Desc: Upload the rendered YAML preconfig to Orchestrator.
169+
Including the ``-u`` will translate to ``True``, no option will
170+
default to ``False``
163171
- Default value: ``False``
164172
- ``-aa`` or ``--autoapply``
165173
- Type: Boolean
166-
- Desc: Auto-apply the YAML preconfig on Orchestrator to a discovered appliance
167-
- Accepted values: ``True`` or ``False``
174+
- Desc: Auto-apply the YAML preconfig on Orchestrator to a
175+
discovered appliance. Including the ``-aa`` will translate to
176+
``True``, no option will default to ``False``
168177
- Default value: ``False``
169178
- ``-j`` or ``--jinja``
170179
- Type: String
@@ -209,7 +218,8 @@ Orchestrator API calls
209218
^^^^^^^^^^^^^^^^^^^^^^^^^^
210219

211220
The two API calls to Orchestrator (outside of authentication) are
212-
``validate_preconfig`` and ``create_preconfig``.
221+
:func:`pyedgeconnect.Orchestrator.validate_preconfig` and
222+
:func:`pyedgeconnect.Orchestrator.create_preconfig`.
213223

214224
The ``validate_preconfig`` function sends the preconfig YAML text to
215225
Orchestrator and will either return a success (HTTP 200 OK) or if

examples/generate_preconfig/preconfig.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@
2020
"-u",
2121
"--upload",
2222
help="Upload created valid preconfigs to Orchestrator",
23-
type=bool,
24-
default=False,
23+
action=argparse.BooleanOptionalAction,
2524
)
2625
parser.add_argument(
2726
"-aa",
2827
"--autoapply",
2928
help="Mark preconfigs for auto-approve",
30-
type=bool,
31-
default=False,
29+
action=argparse.BooleanOptionalAction,
3230
)
3331
parser.add_argument(
3432
"-j",
@@ -171,14 +169,15 @@
171169
auto_apply=auto_apply,
172170
)
173171

174-
# If the validate function passes on Orchestrator write
175-
# preconfig to local file and check if uploading to Orchestrator
176-
if validate.status_code == 200:
172+
# Write local YAML file to see resulting YAML file locally
173+
# whether validate passes or fails
174+
yaml_filename = f'{row["hostname"]}_preconfig.yml'
175+
with open(output_directory + yaml_filename, "w") as preconfig_file:
176+
write_data = preconfig_file.write(yaml_preconfig)
177177

178-
# Write local YAML file
179-
yaml_filename = "{}_preconfig.yml".format(row["hostname"])
180-
with open(output_directory + yaml_filename, "w") as preconfig_file:
181-
write_data = preconfig_file.write(yaml_preconfig)
178+
# If the validate function passes on Orchestrator, move on
179+
# to check if uploading to Orchestrator option selected
180+
if validate.status_code == 200:
182181

183182
# If upload option was chosen, upload preconfig to
184183
# Orchestrator with selected auto-apply settings
@@ -191,26 +190,24 @@
191190
# against a discovered appliance
192191
# Additionally a comment is added with the current
193192
# date
193+
comment_timestamp = datetime.date.today().strftime("%d %B %Y")
194194
orch.create_preconfig(
195195
preconfig_name=row["hostname"],
196196
yaml_preconfig=yaml_preconfig,
197197
auto_apply=auto_apply,
198198
tag=row["hostname"],
199-
comment="Created/Uploaded @ {}".format(
200-
datetime.date.today().strftime("%d %B %Y")
201-
),
199+
comment=f"Created/Uploaded @ {comment_timestamp}",
202200
)
203-
print("Posted EC Preconfig {}".format(row["hostname"]))
201+
print(f'Posted EC Preconfig {row["hostname"]}')
204202
else:
205203
pass
206204
else:
207205
print(
208-
"Preconfig for {} failed validation | error: {}".format(
209-
row["hostname"], validate.text
210-
)
206+
f'Preconfig for {row["hostname"]}'
207+
f" failed validation | error: {validate.text}"
211208
)
212209
# Write local YAML file of failed config for reference
213-
yaml_filename = "{}_preconfig-FAILED.yml".format(row["hostname"])
210+
yaml_filename = f'{row["hostname"]}_preconfig-FAILED.yml'
214211
with open(output_directory + yaml_filename, "w") as preconfig_file:
215212
write_data = preconfig_file.write(yaml_preconfig)
216213

0 commit comments

Comments
 (0)