Skip to content

Commit

Permalink
feat(config): support type:secret in config.options (#1623)
Browse files Browse the repository at this point in the history
The `config.options` section of `charmcraft.yaml` supports, from Juju
3.1, a type `secret` (this is currently missing from the documentation,
but the Juju team are addressing that now).

This adds support for that, so that Charmcraft 2.5 can be used to pack
charms that make use of this functionality.
  • Loading branch information
tonyandrewmeyer authored and lengau committed Jul 19, 2024
1 parent 00e898c commit b7e2c55
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions charmcraft/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,35 @@ class JujuConfig(ModelConfigDefaults, frozen=True):
and: https://juju.is/docs/sdk/config-yaml
"""

<<<<<<< HEAD
options: dict[str, JujuOption] | None
=======
options: Optional[Dict[str, Dict[str, Any]]]

@pydantic.validator("options", pre=True)
def validate_actions(cls, options):
"""Verify options section."""
if options is None:
return None
if not isinstance(options, dict):
raise ValueError("'options' is not a dictionary")
for name, option in options.items():
if not isinstance(option, dict):
raise ValueError(f"'{name}' is not a dictionary")

option_keys = set(option.keys())
if not option_keys.issubset({"description", "type", "default"}):
invalid_keys = option_keys - {"description", "type", "default"}
raise ValueError(f"'{name}' has an invalid key(s): {invalid_keys}")

if "type" not in option:
raise ValueError(f"'{name}' is missing a type")

if option["type"] not in ["string", "int", "float", "boolean", "secret"]:
raise ValueError(
f"'{option}' has an invalid type '{option['type']}', "
"must be one of: string, int, float, boolean, secret"
)

return options
>>>>>>> 0f7a56d (feat(config): support `type:secret` in config.options (#1623))
17 changes: 17 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,10 @@ def test_load_config_in_charmcraft_yaml(tmp_path, prepare_charmcraft_yaml):
default: true
type: boolean
test-secret:
<<<<<<< HEAD
default: secret:co1s9mnmp25c762drvtg
=======
>>>>>>> 0f7a56d (feat(config): support `type:secret` in config.options (#1623))
type: secret
"""
)
Expand All @@ -1149,7 +1152,11 @@ def test_load_config_in_charmcraft_yaml(tmp_path, prepare_charmcraft_yaml):
"test-string": {"description": "test-2", "type": "string"},
"test-float": {"default": 1.23, "type": "float"},
"test-bool": {"default": True, "type": "boolean"},
<<<<<<< HEAD
"test-secret": {"default": "secret:co1s9mnmp25c762drvtg", "type": "secret"},
=======
"test-secret": {"type": "secret"},
>>>>>>> 0f7a56d (feat(config): support `type:secret` in config.options (#1623))
},
}

Expand Down Expand Up @@ -1184,7 +1191,10 @@ def test_load_config_in_config_yaml(tmp_path, prepare_charmcraft_yaml, prepare_c
default: true
type: boolean
test-secret:
<<<<<<< HEAD
default: secret:co1s9mnmp25c762drvtg
=======
>>>>>>> 0f7a56d (feat(config): support `type:secret` in config.options (#1623))
type: secret
"""
),
Expand All @@ -1197,7 +1207,11 @@ def test_load_config_in_config_yaml(tmp_path, prepare_charmcraft_yaml, prepare_c
"test-string": {"description": "test-2", "type": "string"},
"test-float": {"default": 1.23, "type": "float"},
"test-bool": {"default": True, "type": "boolean"},
<<<<<<< HEAD
"test-secret": {"default": "secret:co1s9mnmp25c762drvtg", "type": "secret"},
=======
"test-secret": {"type": "secret"},
>>>>>>> 0f7a56d (feat(config): support `type:secret` in config.options (#1623))
},
}

Expand Down Expand Up @@ -1257,7 +1271,10 @@ def test_load_bad_config_in_charmcraft_yaml(tmp_path, prepare_charmcraft_yaml):
default: true
type: boolean
test-secret:
<<<<<<< HEAD
default: secret:co1s9mnmp25c762drvtg
=======
>>>>>>> 0f7a56d (feat(config): support `type:secret` in config.options (#1623))
type: secret
"""
)
Expand Down

0 comments on commit b7e2c55

Please sign in to comment.