Skip to content

Commit

Permalink
Update model definitions, add commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan committed Jan 24, 2025
1 parent ec7ebf8 commit 1088b01
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 31 deletions.
29 changes: 16 additions & 13 deletions mreg_cli/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,7 @@ class Network(FrozenModelWithTimestamps, APIMixin):
location: str
frozen: bool
reserved: int
policy: NetworkPolicy | None = None

def __hash__(self):
"""Return a hash of the network."""
Expand Down Expand Up @@ -1972,26 +1973,14 @@ def endpoint(cls) -> Endpoint:
return Endpoint.NetworkPolicyAttributes


class NetworkPolicy(WithName):
"""Network policy used in a community."""

id: int
name: str
attributes: list[NetworkPolicyAttribute] = []

@classmethod
def endpoint(cls) -> Endpoint:
"""Return the endpoint for the class."""
return Endpoint.NetworkPolicies


class Community(FrozenModelWithTimestamps, WithName):
"""Network community."""

id: int
name: str
description: str
policy: int
hosts: list[Host] = []

@classmethod
def endpoint(cls) -> Endpoint:
Expand All @@ -2017,6 +2006,20 @@ def get_policy_or_raise(self) -> NetworkPolicy:
return policy


class NetworkPolicy(WithName):
"""Network policy used in a community."""

id: int
name: str
attributes: list[NetworkPolicyAttribute] = []
communities: list[Community] = []

@classmethod
def endpoint(cls) -> Endpoint:
"""Return the endpoint for the class."""
return Endpoint.NetworkPolicies


class IPAddress(FrozenModelWithTimestamps, WithHost, APIMixin):
"""Represents an IP address with associated details."""

Expand Down
57 changes: 39 additions & 18 deletions mreg_cli/commands/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
import argparse
from typing import Any

from mreg_cli.api.models import Atom, Host, HostPolicy, NetworkPolicy, NetworkPolicyAttribute, Role
from mreg_cli.api.models import (
Atom,
Community,
Host,
HostPolicy,
NetworkPolicy,
NetworkPolicyAttribute,
Role,
)
from mreg_cli.commands.base import BaseCommand
from mreg_cli.commands.registry import CommandRegistry
from mreg_cli.exceptions import CreateError, DeleteError, EntityAlreadyExists
from mreg_cli.outputmanager import OutputManager
from mreg_cli.types import Flag
from mreg_cli.types import Flag, JsonMapping

command_registry = CommandRegistry()

Expand Down Expand Up @@ -519,26 +527,37 @@ def role_history(args: argparse.Namespace) -> None:
Role.output_history(name)


# @command_registry.register_command(
# prog="community_add",
# description="Create a community within a network policy",
# short_desc="Create a network community",
# flags=[
# Flag("name", description="Community name", metavar="NAME"),
# Flag("description", description="Description", metavar="DESCRIPTION"),
# Flag("policy", description="Policy name", metavar="POLICY"),
# ],
# )
# def community_add(args: argparse.Namespace) -> None:
# """Show history for name.
# TODO[rename]: network policy community create
@command_registry.register_command(
prog="community_add",
description="Create a community within a network policy",
short_desc="Create a network community",
flags=[
Flag("name", description="Community name", metavar="NAME"),
Flag("description", description="Description", metavar="DESCRIPTION"),
Flag("policy", description="Policy name", metavar="POLICY"),
],
)
def community_create(args: argparse.Namespace) -> None:
"""Create a network community.
:param args: argparse.Namespace (name, description, policy)
"""
name: str = args.name
description: str = args.description
policy_name: str = args.policy

# Community should not exist, Policy should exist
Community.get_by_name_and_raise(policy_name)
policy = NetworkPolicy.get_by_name_or_raise(policy_name)

# :param args: argparse.Namespace (name)
# """
# name: str = args.name
params: JsonMapping = {"name": name, "description": description, "policy": policy.id}
Community.create(params)

# Role.output_history(name)
OutputManager().add_ok(f"Created network community {name!r} for policy {policy_name!r}")


# TODO[rename]: network policy create
@command_registry.register_command(
prog="network_create",
description="Create a network policy",
Expand Down Expand Up @@ -566,6 +585,7 @@ def create_network_policy(args: argparse.Namespace) -> None:
OutputManager().add_ok(f"Created network policy {name!r}")


# TODO[rename]: network policy attribute create
@command_registry.register_command(
prog="attribute_create",
description="Create a network policy attribute",
Expand All @@ -587,6 +607,7 @@ def create_network_policy_attribute(args: argparse.Namespace) -> None:
OutputManager().add_ok(f"Created network policy attribute {name!r}")


# TODO[rename]: network policy attribute list
@command_registry.register_command(
prog="attribute_list",
description="List all network policy attributes",
Expand Down

0 comments on commit 1088b01

Please sign in to comment.