cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
component-tests/config.py
104lines · modeblame
a7344435cthuang5 years ago | 1 | #!/usr/bin/env python |
| 2 | import copy | |
| 3 | | |
f23e33c0cthuang5 years ago | 4 | from dataclasses import dataclass, InitVar |
a7344435cthuang5 years ago | 5 | |
1e8dea91Nuno Diegues4 years ago | 6 | from constants import METRICS_PORT, PROXY_DNS_PORT |
a7344435cthuang5 years ago | 7 | |
| 8 | # frozen=True raises exception when assigning to fields. This emulates immutability | |
f23e33c0cthuang5 years ago | 9 | |
| 10 | | |
a7344435cthuang5 years ago | 11 | @dataclass(frozen=True) |
25cfbec0cthuang5 years ago | 12 | class BaseConfig: |
| 13 | cloudflared_binary: str | |
a7344435cthuang5 years ago | 14 | no_autoupdate: bool = True |
| 15 | metrics: str = f'localhost:{METRICS_PORT}' | |
| 16 | | |
| 17 | def merge_config(self, additional): | |
| 18 | config = copy.copy(additional) | |
| 19 | config['no-autoupdate'] = self.no_autoupdate | |
| 20 | config['metrics'] = self.metrics | |
| 21 | return config | |
| 22 | | |
| 23 | | |
| 24 | @dataclass(frozen=True) | |
25cfbec0cthuang5 years ago | 25 | class NamedTunnelBaseConfig(BaseConfig): |
a7344435cthuang5 years ago | 26 | # The attributes of the parent class are ordered before attributes in this class, |
| 27 | # so we have to use default values here and check if they are set in __post_init__ | |
| 28 | tunnel: str = None | |
| 29 | credentials_file: str = None | |
f23e33c0cthuang5 years ago | 30 | ingress: list = None |
a7344435cthuang5 years ago | 31 | |
| 32 | def __post_init__(self): | |
| 33 | if self.tunnel is None: | |
| 34 | raise TypeError("Field tunnel is not set") | |
| 35 | if self.credentials_file is None: | |
| 36 | raise TypeError("Field credentials_file is not set") | |
f23e33c0cthuang5 years ago | 37 | if self.ingress is None: |
| 38 | raise TypeError("Field ingress is not set") | |
a7344435cthuang5 years ago | 39 | |
| 40 | def merge_config(self, additional): | |
| 41 | config = super(NamedTunnelBaseConfig, self).merge_config(additional) | |
| 42 | config['tunnel'] = self.tunnel | |
| 43 | config['credentials-file'] = self.credentials_file | |
f23e33c0cthuang5 years ago | 44 | # In some cases we want to override default ingress, such as in config tests |
| 45 | if 'ingress' not in config: | |
| 46 | config['ingress'] = self.ingress | |
a7344435cthuang5 years ago | 47 | return config |
| 48 | | |
| 49 | | |
f23e33c0cthuang5 years ago | 50 | @dataclass(frozen=True) |
| 51 | class NamedTunnelConfig(NamedTunnelBaseConfig): | |
| 52 | full_config: dict = None | |
| 53 | additional_config: InitVar[dict] = {} | |
| 54 | | |
| 55 | def __post_init__(self, additional_config): | |
| 56 | # Cannot call set self.full_config because the class is frozen, instead, we can use __setattr__ | |
| 57 | # https://docs.python.org/3/library/dataclasses.html#frozen-instances | |
| 58 | object.__setattr__(self, 'full_config', | |
| 59 | self.merge_config(additional_config)) | |
| 60 | | |
| 61 | def get_url(self): | |
| 62 | return "https://" + self.ingress[0]['hostname'] | |
| 63 | | |
| 64 | | |
a7344435cthuang5 years ago | 65 | @dataclass(frozen=True) |
25cfbec0cthuang5 years ago | 66 | class ClassicTunnelBaseConfig(BaseConfig): |
a7344435cthuang5 years ago | 67 | hostname: str = None |
| 68 | origincert: str = None | |
| 69 | | |
| 70 | def __post_init__(self): | |
| 71 | if self.hostname is None: | |
| 72 | raise TypeError("Field tunnel is not set") | |
| 73 | if self.origincert is None: | |
| 74 | raise TypeError("Field credentials_file is not set") | |
| 75 | | |
| 76 | def merge_config(self, additional): | |
| 77 | config = super(ClassicTunnelBaseConfig, self).merge_config(additional) | |
f23e33c0cthuang5 years ago | 78 | config['hostname'] = self.hostname |
a7344435cthuang5 years ago | 79 | config['origincert'] = self.origincert |
| 80 | return config | |
| 81 | | |
| 82 | | |
f23e33c0cthuang5 years ago | 83 | @dataclass(frozen=True) |
| 84 | class ClassicTunnelConfig(ClassicTunnelBaseConfig): | |
| 85 | full_config: dict = None | |
| 86 | additional_config: InitVar[dict] = {} | |
| 87 | | |
| 88 | def __post_init__(self, additional_config): | |
| 89 | # Cannot call set self.full_config because the class is frozen, instead, we can use __setattr__ | |
| 90 | # https://docs.python.org/3/library/dataclasses.html#frozen-instances | |
| 91 | object.__setattr__(self, 'full_config', | |
| 92 | self.merge_config(additional_config)) | |
| 93 | | |
| 94 | def get_url(self): | |
| 95 | return "https://" + self.hostname | |
1e8dea91Nuno Diegues4 years ago | 96 | |
| 97 | | |
| 98 | @dataclass(frozen=True) | |
| 99 | class ProxyDnsConfig(BaseConfig): | |
| 100 | full_config = { | |
| 101 | "port": PROXY_DNS_PORT, | |
| 102 | "no-autoupdate": True, | |
| 103 | } | |
| 104 | |