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 d962d8005..8c0163f05 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-advantage" - channel = var.ubuntu-advantage-channel - base = "ubuntu@24.04" + name = "ubuntu-advantage" + channel = var.ubuntu-advantage-channel + revision = var.ubuntu-advantage-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-advantage-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 2cfea3cb5..5e3b1fe82 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-advantage-channel" { default = "latest/edge" } +variable "ubuntu-advantage-revision" { + description = "Revision number of ubuntu-advantage charm" + type = number + default = null +} + +variable "ubuntu-advantage-config" { + description = "Config options of the ubuntu-advantage 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 f76398936..0d172047f 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, @@ -39,6 +41,12 @@ console = Console() APPLICATION = "ubuntu-pro" +# The 2024.1 plan deploys the ubuntu-advantage charm as the ubuntu-pro +# application. Manifest charm keys are charm names, as refresh looks them up +# by the deployed charm name. +CHARM = "ubuntu-advantage" +# Matches the ubuntu-advantage-channel default in deploy-pro/variables.tf. +UBUNTU_ADVANTAGE_CHANNEL = "latest/edge" APP_TIMEOUT = 300 # 5 minutes, managing the application should be fast UNIT_TIMEOUT = 1200 # 15 minutes, adding / removing units can take a long time @@ -197,30 +205,52 @@ def manifest(self) -> Manifest: def default_software_overrides(self) -> SoftwareConfig: """Feature software configuration.""" return SoftwareConfig( + charms={CHARM: CharmManifest(channel=UBUNTU_ADVANTAGE_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": { + CHARM: { + "channel": "ubuntu-advantage-channel", + "revision": "ubuntu-advantage-revision", + "config": "ubuntu-advantage-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 c3d765f61..e2117b9b1 100644 --- a/sunbeam-python/tests/unit/sunbeam/features/test_pro.py +++ b/sunbeam-python/tests/unit/sunbeam/features/test_pro.py @@ -1,11 +1,21 @@ # SPDX-FileCopyrightText: 2023 - Canonical Ltd # SPDX-License-Identifier: Apache-2.0 +import re +from pathlib import Path +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, EnableUbuntuProApplicationStep, @@ -126,3 +136,122 @@ def test_disable_tf_apply_failed(self, disable_step, basic_tfhelper, step_contex basic_tfhelper.update_tfvars_and_apply_tf.assert_called_once() assert result.result_type == ResultType.FAILED assert result.message == "apply failed..." + + +class TestManifestCharmConfig: + """Charm options such as contract_url can be set via the manifest.""" + + _MANIFEST = """ +features: + pro: + software: + charms: + ubuntu-advantage: + 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.CHARM].channel == ( + pro_feature.UBUNTU_ADVANTAGE_CHANNEL + ) + + def test_channel_default_matches_plan_variable(self): + match = re.search( + r'variable\s+"ubuntu-advantage-channel"\s*\{[^}]*?default\s*=\s*"([^"]+)"', + self._variables(), + re.DOTALL, + ) + assert match, "no default found for ubuntu-advantage-channel" + assert match.group(1) == pro_feature.UBUNTU_ADVANTAGE_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.CHARM].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-advantage-revision": 46, + "ubuntu-advantage-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-advantage-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, + ]