| David Garcia | a71d4a0 | 2021-03-10 20:00:53 +0100 | [diff] [blame] | 1 | # Copyright 2021 Canonical Ltd. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| David Garcia | eb8943a | 2021-04-12 12:07:37 +0200 | [diff] [blame] | 15 | import os |
| 16 | import typing |
| 17 | |
| 18 | |
| 19 | class EnvironConfig(dict): |
| 20 | prefixes = ["OSMLCM_VCA_", "OSMMON_VCA_"] |
| 21 | |
| 22 | def __init__(self, prefixes: typing.List[str] = None): |
| 23 | if prefixes: |
| 24 | self.prefixes = prefixes |
| 25 | for key, value in os.environ.items(): |
| 26 | if any(key.startswith(prefix) for prefix in self.prefixes): |
| 27 | self.__setitem__(self._get_renamed_key(key), value) |
| 28 | |
| 29 | def _get_renamed_key(self, key: str) -> str: |
| 30 | for prefix in self.prefixes: |
| 31 | key = key.replace(prefix, "") |
| 32 | return key.lower() |
| 33 | |
| 34 | |
| David Garcia | a71d4a0 | 2021-03-10 20:00:53 +0100 | [diff] [blame] | 35 | MODEL_CONFIG_KEYS = [ |
| 36 | "agent-metadata-url", |
| 37 | "agent-stream", |
| 38 | "apt-ftp-proxy", |
| 39 | "apt-http-proxy", |
| 40 | "apt-https-proxy", |
| 41 | "apt-mirror", |
| 42 | "apt-no-proxy", |
| 43 | "automatically-retry-hooks", |
| 44 | "backup-dir", |
| 45 | "cloudinit-userdata", |
| 46 | "container-image-metadata-url", |
| 47 | "container-image-stream", |
| 48 | "container-inherit-properties", |
| 49 | "container-networking-method", |
| 50 | "default-series", |
| 51 | "default-space", |
| 52 | "development", |
| 53 | "disable-network-management", |
| 54 | "egress-subnets", |
| 55 | "enable-os-refresh-update", |
| 56 | "enable-os-upgrade", |
| 57 | "fan-config", |
| 58 | "firewall-mode", |
| 59 | "ftp-proxy", |
| 60 | "http-proxy", |
| 61 | "https-proxy", |
| 62 | "ignore-machine-addresses", |
| 63 | "image-metadata-url", |
| 64 | "image-stream", |
| 65 | "juju-ftp-proxy", |
| 66 | "juju-http-proxy", |
| 67 | "juju-https-proxy", |
| 68 | "juju-no-proxy", |
| 69 | "logforward-enabled", |
| 70 | "logging-config", |
| 71 | "lxd-snap-channel", |
| 72 | "max-action-results-age", |
| 73 | "max-action-results-size", |
| 74 | "max-status-history-age", |
| 75 | "max-status-history-size", |
| 76 | "net-bond-reconfigure-delay", |
| 77 | "no-proxy", |
| 78 | "provisioner-harvest-mode", |
| 79 | "proxy-ssh", |
| 80 | "snap-http-proxy", |
| 81 | "snap-https-proxy", |
| 82 | "snap-store-assertions", |
| 83 | "snap-store-proxy", |
| 84 | "snap-store-proxy-url", |
| 85 | "ssl-hostname-verification", |
| 86 | "test-mode", |
| 87 | "transmit-vendor-metrics", |
| 88 | "update-status-hook-interval", |
| 89 | ] |
| 90 | |
| 91 | |
| 92 | class ModelConfig(dict): |
| 93 | prefix = "model_config_" |
| 94 | |
| 95 | def __init__(self, config: dict): |
| 96 | for key, value in config.items(): |
| 97 | if ( |
| 98 | key.startswith(self.prefix) |
| 99 | and self._get_renamed_key(key) in MODEL_CONFIG_KEYS |
| 100 | ): |
| 101 | self.__setitem__(self._get_renamed_key(key), value) |
| 102 | |
| 103 | def _get_renamed_key(self, key): |
| 104 | return key.replace(self.prefix, "").replace("_", "-") |