diff --git a/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/main.tf b/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/main.tf index 86cdb2c39..c9023b0be 100644 --- a/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/main.tf +++ b/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/main.tf @@ -22,14 +22,16 @@ resource "juju_application" "ubuntu_pro" { model_uuid = data.juju_model.machine_model.uuid charm { - name = "ubuntu-pro" - channel = var.ubuntu-pro-channel - base = "ubuntu@24.04" + name = "ubuntu-pro" + channel = var.ubuntu-pro-channel + revision = var.ubuntu-pro-revision + base = "ubuntu@24.04" } - config = { + # The token from the CLI always wins over a token key in the manifest config. + config = merge(var.ubuntu-pro-config, { token = var.token - } + }) } resource "juju_integration" "juju_info" { diff --git a/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/variables.tf b/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/variables.tf index c9425d59a..fcf8a3770 100644 --- a/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/variables.tf +++ b/sunbeam-python/sunbeam/features/pro/etc/deploy-pro/variables.tf @@ -7,6 +7,18 @@ variable "ubuntu-pro-channel" { default = "latest/stable" } +variable "ubuntu-pro-revision" { + description = "Revision number of ubuntu-pro charm" + type = number + default = null +} + +variable "ubuntu-pro-config" { + description = "Config options of the ubuntu-pro charm, such as contract_url" + type = map(string) + default = {} +} + variable "machine_model_uuid" { description = "UUID of Juju model to deploy ubuntu-pro into" type = string diff --git a/sunbeam-python/sunbeam/features/pro/feature.py b/sunbeam-python/sunbeam/features/pro/feature.py index a7df18382..caaa290ca 100644 --- a/sunbeam-python/sunbeam/features/pro/feature.py +++ b/sunbeam-python/sunbeam/features/pro/feature.py @@ -18,6 +18,8 @@ from sunbeam.core.deployment import Deployment from sunbeam.core.juju import JujuHelper, JujuStepHelper from sunbeam.core.manifest import ( + AddManifestStep, + CharmManifest, FeatureConfig, Manifest, SoftwareConfig, @@ -198,30 +200,52 @@ def manifest(self) -> Manifest: def default_software_overrides(self) -> SoftwareConfig: """Feature software configuration.""" return SoftwareConfig( + charms={APPLICATION: CharmManifest(channel=UBUNTU_PRO_CHANNEL)}, terraform={ self.tfplan: TerraformManifest( source=Path(__file__).parent / "etc" / self.tfplan_dir ) - } + }, ) + def manifest_attributes_tfvar_map(self) -> dict: + """Manifest attributes terraformvars map.""" + return { + self.tfplan: { + "charms": { + APPLICATION: { + "channel": "ubuntu-pro-channel", + "revision": "ubuntu-pro-revision", + "config": "ubuntu-pro-config", + } + } + } + } + def run_enable_plans( self, deployment: Deployment, config: ProFeatureConfig, show_hints: bool ): """Run the enablement plans.""" tfhelper = deployment.get_tfhelper(self.tfplan) jhelper = JujuHelper(deployment.juju_controller) - plan = [ - TerraformInitStep(tfhelper), - EnableUbuntuProApplicationStep( - deployment.get_client(), - tfhelper, - jhelper, - self.manifest, - config.token, - deployment.openstack_machines_model, - ), - ] + plan: list[BaseStep] = [] + # Store the manifest so later applies, e.g. a re-run of enable + # without --manifest, keep the charm config it sets. + if self.user_manifest: + plan.append(AddManifestStep(deployment.get_client(), self.user_manifest)) + plan.extend( + [ + TerraformInitStep(tfhelper), + EnableUbuntuProApplicationStep( + deployment.get_client(), + tfhelper, + jhelper, + self.manifest, + config.token, + deployment.openstack_machines_model, + ), + ] + ) run_plan(plan, console, show_hints) diff --git a/sunbeam-python/tests/unit/sunbeam/features/test_pro.py b/sunbeam-python/tests/unit/sunbeam/features/test_pro.py index 9ea16bb48..6aa37084b 100644 --- a/sunbeam-python/tests/unit/sunbeam/features/test_pro.py +++ b/sunbeam-python/tests/unit/sunbeam/features/test_pro.py @@ -6,9 +6,15 @@ from unittest.mock import Mock import pytest +import yaml from sunbeam.core.common import ResultType -from sunbeam.core.terraform import TerraformException +from sunbeam.core.manifest import AddManifestStep, FeatureManifest, Manifest +from sunbeam.core.terraform import ( + TerraformException, + TerraformHelper, + TerraformInitStep, +) from sunbeam.features.pro import feature as pro_feature from sunbeam.features.pro.feature import ( DisableUbuntuProApplicationStep, @@ -190,3 +196,111 @@ def test_channel_default_matches_feature_constant(self): ) assert match, "no default found for ubuntu-pro-channel in variables.tf" assert match.group(1) == pro_feature.UBUNTU_PRO_CHANNEL + + +class TestManifestCharmConfig: + """Charm options such as contract_url can be set via the manifest.""" + + _MANIFEST = """ +features: + pro: + software: + charms: + ubuntu-pro: + revision: 46 + config: + contract_url: https://contracts.example.com +""" + + def _variables(self) -> str: + plan_dir = Path(pro_feature.__file__).parent / "etc" / "deploy-pro" + return (plan_dir / "variables.tf").read_text() + + def test_default_software_registers_charm(self): + feature = pro_feature.ProFeature() + charms = feature.default_software_overrides().charms + assert charms[pro_feature.APPLICATION].channel == ( + pro_feature.UBUNTU_PRO_CHANNEL + ) + + def test_tfvar_map_matches_plan_variables(self): + feature = pro_feature.ProFeature() + tfvar_map = feature.manifest_attributes_tfvar_map()[feature.tfplan] + variables = self._variables() + for tfvar in tfvar_map["charms"][pro_feature.APPLICATION].values(): + assert f'variable "{tfvar}"' in variables, tfvar + + def test_manifest_config_becomes_tfvar(self, tmp_path): + feature = pro_feature.ProFeature() + default = Manifest( + features={ + feature.name: FeatureManifest( + software=feature.default_software_overrides() + ) + } + ) + override = Manifest.model_validate(yaml.safe_load(self._MANIFEST)) + override.validate_against_default(default) + tfhelper = TerraformHelper( + path=tmp_path, + plan=feature.tfplan, + tfvar_map=feature.manifest_attributes_tfvar_map()[feature.tfplan], + ) + + tfvars = tfhelper._get_tfvars(default.merge(override)) + + # A user charm entry replaces the default one, so the channel falls + # back to the variable default in variables.tf. + assert tfvars == { + "ubuntu-pro-revision": 46, + "ubuntu-pro-config": {"contract_url": "https://contracts.example.com"}, + } + + def test_cli_token_wins_over_manifest_config(self): + text = ( + Path(pro_feature.__file__).parent / "etc" / "deploy-pro" / "main.tf" + ).read_text() + assert re.search( + r"config\s*=\s*merge\(\s*var\.ubuntu-pro-config\s*,\s*\{\s*" + r"token\s*=\s*var\.token", + text, + ) + + +class TestRunEnablePlans: + """enable --manifest must store the manifest so later applies keep it.""" + + @pytest.fixture + def run_plan(self, mocker): + mocker.patch.object(pro_feature, "JujuHelper") + mocker.patch.object(pro_feature.click, "echo") + return mocker.patch.object(pro_feature, "run_plan") + + def _run(self, user_manifest): + feature = pro_feature.ProFeature() + feature.user_manifest = user_manifest + feature._manifest = Mock() + feature.run_enable_plans( + Mock(), pro_feature.ProFeatureConfig(token="t"), show_hints=False + ) + + def test_user_manifest_is_stored_first(self, run_plan, tmp_path): + manifest_file = tmp_path / "manifest.yaml" + self._run(manifest_file) + + plan = run_plan.call_args.args[0] + assert [type(step) for step in plan] == [ + AddManifestStep, + TerraformInitStep, + EnableUbuntuProApplicationStep, + ] + assert plan[0].manifest_file == manifest_file + + def test_no_user_manifest_stores_nothing(self, run_plan): + self._run(None) + + plan = run_plan.call_args.args[0] + assert [type(step) for step in plan] == [ + TerraformInitStep, + EnableUbuntuProApplicationStep, + ]