Skip to content

Commit

Permalink
Add set command combining add and update
Browse files Browse the repository at this point in the history
The new `set` command added with this patch, combines the `add` and
`update` commands. It adds the host if it is not yet in DETERRERS or
updates it otherwise.

Note that updates will not yet modify the registration status of the
given host.
  • Loading branch information
lkiesow committed Jul 21, 2024
1 parent 334602b commit 1694345
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions deterrerscli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ def print_format(data, format: str):
print(json.dumps(data, indent=4))


def _add(ipv4, admin, profile, firewall, register, skip_scan):
'''Add IP address to DETERRERS.
'''
deterrers.add(ipv4, admin, profile, firewall)
if profile and (auto_register if register is None else register):
skip_scan = auto_skip_scan if skip_scan is None else skip_scan
deterrers.action(ipv4, 'register', skip_scan)


def _update(ipv4, admin, profile, firewall):
'''Update IP address in DETERRERS.
'''
admin = admin or None
deterrers.update(ipv4, profile, firewall, admin)


@click.group()
def cli():
global deterrers
Expand Down Expand Up @@ -71,10 +87,7 @@ def delete(ipv4):
def add(ipv4, admin, profile, firewall, register, skip_scan):
'''Add IP address to DETERRERS.
'''
deterrers.add(ipv4, admin, profile, firewall)
if profile and (auto_register if register is None else register):
skip_scan = auto_skip_scan if skip_scan is None else skip_scan
deterrers.action(ipv4, 'register', skip_scan)
return _add(ipv4, admin, profile, firewall, register, skip_scan)


@cli.command()
Expand All @@ -88,8 +101,27 @@ def update(ipv4, admin, profile, firewall):
Fields which are not specified will not be changed.
The option `admin` can be used multiple times.
'''
admin = admin or None
deterrers.update(ipv4, profile, firewall, admin)
return _update(ipv4, admin, profile, firewall)


@cli.command()
@click.option('--admin', '-a', multiple=True, required=True)
@click.option('--profile', '-p', default='', type=types.PROFILE_TYPE)
@click.option('--firewall', '-f', default='', type=types.HOST_FIREWALL_TYPE)
@click.option('--register/--no-register', default=None,
help='If the added host should be registered immediately')
@click.option('--skip-scan/--no-skip-scan', default=None,
help='If the added host should get an initial security scan. '
'Only applies if it is being registered')
@click.argument('ipv4', type=types.IPV4_TYPE)
def set(ipv4, admin, profile, firewall, register, skip_scan):
'''Add IP address to DETERRERS if it is not added already. Otherwise,
update the data. Note that hosts will not be automatically registered when
updating data.
'''
if deterrers.get(ipv4):
return _update(ipv4, admin, profile, firewall)
return _add(ipv4, admin, profile, firewall, register, skip_scan)


@cli.group()
Expand Down

0 comments on commit 1694345

Please sign in to comment.