Skip to content

Commit 797cc3f

Browse files
committed
Chore(extension): Implementation, test and docs.
1 parent 79fa5ad commit 797cc3f

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

charmcraft/extensions/app.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def _check_input(self) -> None:
114114
f"which conflict with the {self.framework}-framework extension, "
115115
"please rename or remove it"
116116
)
117+
invalid_non_optionals = []
117118
for config in self._get_nested(self.yaml_data, "config.options"):
118119
for reserved_config_prefix in ("webserver-", f"{self.framework}-"):
119120
if config.startswith(reserved_config_prefix):
@@ -122,6 +123,19 @@ def _check_input(self) -> None:
122123
f" reserved configuration prefix {reserved_config_prefix!r}, "
123124
"please rename or remove it"
124125
)
126+
config_option_dict = self._get_nested(
127+
self.yaml_data, f"config.options.{config}"
128+
)
129+
if config_option_dict.get("optional") is False and config_option_dict.get(
130+
"default"
131+
):
132+
invalid_non_optionals.append(config)
133+
134+
if invalid_non_optionals:
135+
raise ExtensionError(
136+
"Non-optional configuration options can not have default values.\n"
137+
f"Please either remove the default value or set optional field to true or remove it for the {', '.join(invalid_non_optionals)} configuration option(s)."
138+
)
125139

126140
def _get_root_snippet(self) -> dict[str, Any]:
127141
"""Return the root snippet to be merged into the user charmcraft.yaml.

docs/reference/extensions/django-framework-extension.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ will set the ``DJANGO_ALLOWED_HOSTS`` environment variable, the ingress
6161
URL or the Kubernetes service URL if there is no ingress integration,
6262
will be set automatically.
6363

64+
In addition to this, you can set the configuration options to be
65+
``non-optional`` by setting the ``optional`` key to ``False``. This will
66+
block the charm and stop services until the config is supplied. For example,
67+
if your application needs an ``api-token`` to function correctly you can set
68+
it ``non-optional``, as below. This will block the charm and stop the
69+
services until the ``api-token`` is supplied.
70+
71+
.. code-block:: yaml
72+
73+
config:
74+
options:
75+
api-token:
76+
description: The token necessary for the service to run.
77+
type: string
78+
optional: false
79+
80+
.. note::
81+
82+
The ``non-optional`` options can not have ``default`` values. If you
83+
set a ``default`` value to a ``non-optional`` configuration option
84+
``charmcraft`` will fail when packing the charm.
85+
6486

6587
``peers``, ``provides``, and ``requires`` keys
6688
----------------------------------------------

docs/reference/extensions/fastapi-framework-extension.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,29 @@ by running ``juju config <application> token=<token>``.
3939
token:
4040
description: The token for the service.
4141
type: string
42-
required: true
42+
optional: false
43+
44+
In addition to this, you can set the configuration options to be
45+
``non-optional`` by setting the ``optional`` key to ``False``. This will
46+
block the charm and stop services until the config is supplied. For example,
47+
if your application needs an ``api-token`` to function correctly you can set
48+
it ``non-optional``, as below. This will block the charm and stop the
49+
services until the ``api-token`` is supplied.
50+
51+
.. code-block:: yaml
52+
53+
config:
54+
options:
55+
api-token:
56+
description: The token necessary for the service to run.
57+
type: string
58+
optional: false
59+
60+
.. note::
61+
62+
The ``non-optional`` options can not have ``default`` values. If you
63+
set a ``default`` value to a ``non-optional`` configuration option
64+
``charmcraft`` will fail when packing the charm.
4365

4466

4567
``charmcraft.yaml`` > ``peers``, ``provides``, ``requires``

docs/reference/extensions/flask-framework-extension.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ charm can set it by running ``juju config <application> token=<token>``.
3737
description: The token for the service.
3838
type: string
3939
40+
In addition to this, you can set the configuration options to be
41+
``non-optional`` by setting the ``optional`` key to ``False``. This will
42+
block the charm and stop services until the config is supplied. For example,
43+
if your application needs an ``api-token`` to function correctly you can set
44+
it ``non-optional``, as below. This will block the charm and stop the
45+
services until the ``api-token`` is supplied.
46+
47+
.. code-block:: yaml
48+
49+
config:
50+
options:
51+
api-token:
52+
description: The token necessary for the service to run.
53+
type: string
54+
optional: false
55+
56+
.. note::
57+
58+
The ``non-optional`` options can not have ``default`` values. If you
59+
set a ``default`` value to a ``non-optional`` configuration option
60+
``charmcraft`` will fail when packing the charm.
61+
4062

4163
``charmcraft.yaml`` > ``peers``, ``provides``, ``requires``
4264
-----------------------------------------------------------

docs/reference/extensions/go-framework-extension.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ charm can set it by running ``juju config <application> token=<token>``.
4848
description: The token for the service.
4949
type: string
5050
51+
In addition to this, you can set the configuration options to be
52+
``non-optional`` by setting the ``optional`` key to ``False``. This will
53+
block the charm and stop services until the config is supplied. For example,
54+
if your application needs an ``api-token`` to function correctly you can set
55+
it ``non-optional``, as below. This will block the charm and stop the
56+
services until the ``api-token`` is supplied.
57+
58+
.. code-block:: yaml
59+
60+
config:
61+
options:
62+
api-token:
63+
description: The token necessary for the service to run.
64+
type: string
65+
optional: false
66+
67+
.. note::
68+
69+
The ``non-optional`` options can not have ``default`` values. If you
70+
set a ``default`` value to a ``non-optional`` configuration option
71+
``charmcraft`` will fail when packing the charm.
72+
5173

5274
``charmcraft.yaml`` > ``peers``, ``provides``, ``requires``
5375
-----------------------------------------------------------

tests/extensions/test_app.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,20 @@ def test_flask_merge_charm_libs(flask_input_yaml, tmp_path):
404404
{"config": {"options": {"flask-foobar": {"type": "string"}}}},
405405
id="reserved-config-prefix-flask",
406406
),
407+
pytest.param(
408+
{
409+
"config": {
410+
"options": {
411+
"non-optional": {
412+
"type": "string",
413+
"optional": False,
414+
"default": "default value",
415+
}
416+
}
417+
}
418+
},
419+
id="non-optional-config-with-defautl",
420+
),
407421
]
408422

409423

0 commit comments

Comments
 (0)