diff --git a/cloudinit/sources/DataSourceDigitalOcean.py b/cloudinit/sources/DataSourceDigitalOcean.py index b4856011431..2c0f15c99ad 100644 --- a/cloudinit/sources/DataSourceDigitalOcean.py +++ b/cloudinit/sources/DataSourceDigitalOcean.py @@ -7,6 +7,7 @@ # https://developers.digitalocean.com/documentation/metadata/ import logging +from typing import Any, Dict, Optional import cloudinit.sources.helpers.digitalocean as do_helper from cloudinit import lifecycle, sources, util @@ -48,7 +49,7 @@ def __init__(self, sys_cfg, distro, paths): self.use_ip4LL = self.ds_cfg.get("use_ip4LL", MD_USE_IPV4LL) self.wait_retry = self.ds_cfg.get("wait_retry", MD_WAIT_RETRY) self._network_config = None - self.metadata_full = None + self.metadata_full: Optional[Dict[str, Any]] = None def _unpickle(self, ci_pkl_version: int) -> None: super()._unpickle(ci_pkl_version) @@ -115,7 +116,7 @@ def network_config(self): interfaces = self.metadata.get("interfaces") LOG.debug(interfaces) - if not interfaces: + if not interfaces or self.metadata_full is None: raise RuntimeError("Unable to get meta-data from server....") nameservers = self.metadata_full["dns"]["nameservers"] diff --git a/pyproject.toml b/pyproject.toml index 0972e89a336..598e7ffe28d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,7 +52,6 @@ module = [ "cloudinit.netinfo", "cloudinit.sources.DataSourceCloudStack", "cloudinit.sources.DataSourceConfigDrive", - "cloudinit.sources.DataSourceDigitalOcean", "cloudinit.sources.DataSourceEc2", "cloudinit.sources.DataSourceExoscale", "cloudinit.sources.DataSourceGCE", diff --git a/tests/unittests/sources/test_digitalocean.py b/tests/unittests/sources/test_digitalocean.py index a99718b9ba5..9c4a02d5680 100644 --- a/tests/unittests/sources/test_digitalocean.py +++ b/tests/unittests/sources/test_digitalocean.py @@ -248,6 +248,15 @@ def test_multiple_ssh_keys(self, mock_readmd, get_ds): assert metadata["public_keys"] == ds.get_public_ssh_keys() assert isinstance(ds.get_public_ssh_keys(), list) + def test_network_config_requires_full_metadata(self, get_ds): + ds = get_ds() + ds.metadata["interfaces"] = DO_META["interfaces"] + + with pytest.raises( + RuntimeError, match="Unable to get meta-data from server" + ): + _ = ds.network_config + class TestNetworkConvert: def _get_networking(self):