]>
Commit | Line | Data |
---|---|---|
f6b5b4d7 TL |
1 | try: |
2 | from typing import Optional, List, Any | |
3 | except ImportError: | |
4 | pass # just for type checking | |
5 | ||
6 | ||
7 | class HostSpec(object): | |
8 | """ | |
9 | Information about hosts. Like e.g. ``kubectl get nodes`` | |
10 | """ | |
11 | def __init__(self, | |
12 | hostname, # type: str | |
13 | addr=None, # type: Optional[str] | |
14 | labels=None, # type: Optional[List[str]] | |
15 | status=None, # type: Optional[str] | |
16 | ): | |
17 | self.service_type = 'host' | |
18 | ||
19 | #: the bare hostname on the host. Not the FQDN. | |
20 | self.hostname = hostname # type: str | |
21 | ||
22 | #: DNS name or IP address to reach it | |
23 | self.addr = addr or hostname # type: str | |
24 | ||
25 | #: label(s), if any | |
26 | self.labels = labels or [] # type: List[str] | |
27 | ||
28 | #: human readable status | |
29 | self.status = status or '' # type: str | |
30 | ||
31 | def to_json(self): | |
32 | return { | |
33 | 'hostname': self.hostname, | |
34 | 'addr': self.addr, | |
35 | 'labels': self.labels, | |
36 | 'status': self.status, | |
37 | } | |
38 | ||
39 | @classmethod | |
40 | def from_json(cls, host_spec): | |
41 | _cls = cls(host_spec['hostname'], | |
42 | host_spec['addr'] if 'addr' in host_spec else None, | |
43 | host_spec['labels'] if 'labels' in host_spec else None) | |
44 | return _cls | |
45 | ||
46 | def __repr__(self): | |
47 | args = [self.hostname] # type: List[Any] | |
48 | if self.addr is not None: | |
49 | args.append(self.addr) | |
50 | if self.labels: | |
51 | args.append(self.labels) | |
52 | if self.status: | |
53 | args.append(self.status) | |
54 | ||
55 | return "HostSpec({})".format(', '.join(map(repr, args))) | |
56 | ||
57 | def __str__(self): | |
58 | if self.hostname != self.addr: | |
59 | return f'{self.hostname} ({self.addr})' | |
60 | return self.hostname | |
61 | ||
62 | def __eq__(self, other): | |
63 | # Let's omit `status` for the moment, as it is still the very same host. | |
64 | return self.hostname == other.hostname and \ | |
65 | self.addr == other.addr and \ | |
66 | self.labels == other.labels |