|
11 | 11 |
|
12 | 12 | class HostConfig(TypedDict): |
13 | 13 | repositories: list[str] |
| 14 | + hosting_pool: HostAddress | None |
14 | 15 |
|
15 | 16 |
|
16 | | -Inventory: TypeAlias = dict[HostAddress, HostConfig] |
| 17 | +HostConfigs: TypeAlias = dict[HostAddress, HostConfig] |
17 | 18 |
|
| 19 | +class Inventory(TypedDict): |
| 20 | + hosts: HostConfigs |
18 | 21 |
|
19 | 22 | def load_inventory(inventory_path: Path) -> Inventory: |
20 | 23 | """Create an inventory object from loaded inventory file.""" |
21 | | - inventory: Inventory = {} |
22 | | - |
23 | 24 | with open(inventory_path, "rb") as f: |
24 | 25 | data = tomllib.load(f) |
25 | 26 |
|
26 | | - all = data.get("all", {}) |
| 27 | + default = data.get("default", {}) |
27 | 28 | hosts = data.get("hosts", []) |
28 | 29 |
|
29 | | - for server, config in hosts.items(): |
| 30 | + inventory_hosts: HostConfigs = {} |
| 31 | + for h, config in hosts.items(): |
30 | 32 | repos = config.get("repositories", []) |
31 | | - host: HostConfig = {"repositories": repos or all.get("repositories", [])} |
32 | | - inventory[server] = host |
| 33 | + hosting_pool = config.get("hosting_pool", None) |
| 34 | + if hosting_pool is None: |
| 35 | + hosting_pool = default.get("hosting_pool", None) |
| 36 | + host: HostConfig = { |
| 37 | + "repositories": repos or default.get("repositories", []), |
| 38 | + "hosting_pool": hosting_pool, |
| 39 | + } |
| 40 | + inventory_hosts[h] = host |
33 | 41 |
|
34 | | - return inventory |
| 42 | + return { |
| 43 | + "hosts": inventory_hosts, |
| 44 | + } |
35 | 45 |
|
36 | 46 |
|
37 | | -def into_inventory(hosts: list[HostAddress], repositories: list[str]) -> Inventory: |
| 47 | +def into_inventory(hosts: list[HostAddress], repositories: list[str], hosting_pool: HostAddress) -> Inventory: |
38 | 48 | """Create an inventory object from arguments. |
39 | 49 |
|
40 | 50 | Basically, it is used as compatibility when we don't want inventory from file. |
41 | 51 | """ |
42 | | - inventory: Inventory = {} |
43 | | - |
| 52 | + inventory_hosts: HostConfigs = {} |
44 | 53 | for h in hosts: |
45 | | - host: HostConfig = {"repositories": repositories or []} |
46 | | - inventory[h] = host |
47 | | - |
48 | | - return inventory |
| 54 | + host: HostConfig = { |
| 55 | + "repositories": repositories or [], |
| 56 | + "hosting_pool": hosting_pool or None, |
| 57 | + } |
| 58 | + inventory_hosts[h] = host |
| 59 | + |
| 60 | + return { |
| 61 | + "hosts": inventory_hosts, |
| 62 | + } |
0 commit comments