Skip to content

Commit

Permalink
Support multiple communities per host as per changes on the server.
Browse files Browse the repository at this point in the history
  • Loading branch information
terjekv committed Feb 25, 2025
1 parent debb15e commit 5c6b204
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
19 changes: 10 additions & 9 deletions mreg_cli/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2919,7 +2919,7 @@ class Host(FrozenModelWithTimestamps, WithTTL, WithHistory, APIMixin):
ttl: int | None = None
comment: str

network_community: Community | None = None
communities: list[Community] | None = None

# Note, we do not use WithZone here as this is optional and we resolve it differently.
zone: int | None = None
Expand Down Expand Up @@ -3467,15 +3467,16 @@ def output(self, names: bool = False, traverse_hostgroups: bool = False):
if policies:
output_manager.add_line(f"{'Policy:':<{padding}}{', '.join(policies)}")

if policies or self.network_community:
if policies or self.communities:
community_line = f"{'Community:':<{padding}}"
if self.network_community:
name = self.network_community.name
if self.network_community.global_name:
name += f" (Global: {self.network_community.global_name})"

community_from_network = f" [{self.network_community.network_address}]" if len(networks) > 1 else ""
community_line += f"{name}{community_from_network}"
community_parts: list[str] = []
if self.communities:
for community in self.communities:
global_name = f" (Global: {community.global_name})" if community.global_name else ""
community_from_network = f" [{community.network_address}]" if len(networks) > 1 else ""
community_parts.append(f"{community.name}{global_name}{community_from_network}")

community_line += ", ".join(community_parts)

output_manager.add_line(community_line)

Expand Down
13 changes: 6 additions & 7 deletions mreg_cli/commands/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,13 +979,12 @@ def community_host_add(args: argparse.Namespace) -> None:
com = net.get_community_or_raise(community)
h = Host.get_by_any_means_or_raise(host)

if h.network_community and h.network_community.id == com.id:
raise InputFailure(f"Host {h.name!r} is already in community {com.name!r}")

if not force and h.network_community:
raise ForceMissing(
f"Host {h.name!r} is assigned to other community {com.name!r}. Must force."
)
if h.communities and not force:
for c in h.communities:
if c.network == net.id:
raise ForceMissing(
f"Host {h.name!r} is assigned to another community in the network ({c.name!r}). Must force."
)

com.add_host(h)

Expand Down

0 comments on commit 5c6b204

Please sign in to comment.