-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract undersync-device workflow into understack_workflows
- Loading branch information
Showing
7 changed files
with
129 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
python/understack-workflows/understack_workflows/main/undersync_device.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import argparse | ||
import os | ||
import sys | ||
|
||
from understack_workflows.helpers import boolean_args | ||
from understack_workflows.helpers import credential | ||
from understack_workflows.helpers import setup_logger | ||
from understack_workflows.nautobot import Nautobot | ||
from understack_workflows.undersync.client import Undersync | ||
|
||
logger = setup_logger(__name__) | ||
|
||
def update_nautobot(args) -> list[str]: | ||
default_nb_url = "http://nautobot-default.nautobot.svc.cluster.local" | ||
device_uuid = args.device_uuid | ||
field_name = "connected_to_network" | ||
field_value = args.network_name | ||
nb_url = args.nautobot_url or default_nb_url | ||
|
||
nb_token = args.nautobot_token or credential("nb-token", "token") | ||
nautobot = Nautobot(nb_url, nb_token, logger=logger) | ||
logger.info( | ||
f"Updating Device {device_uuid} and moving it to '{field_value}' network." | ||
) | ||
nautobot.update_cf(device_uuid, field_name, field_value) | ||
logger.debug(f"Updated Device.{field_name} to {field_value}") | ||
switches = nautobot.uplink_switches(device_uuid) | ||
logger.info(f"Obtained switch IDs: {switches}") | ||
return switches | ||
|
||
|
||
def call_undersync(args, switches): | ||
undersync_token = credential("undersync", "token") | ||
if not undersync_token: | ||
logger.error("Please provide auth token for Undersync.") | ||
sys.exit(1) | ||
undersync = Undersync(undersync_token) | ||
|
||
try: | ||
return undersync.sync_devices(switches, dry_run=args.dry_run, force=args.force) | ||
except Exception as error: | ||
logger.error(error) | ||
sys.exit(2) | ||
|
||
|
||
def argument_parser(): | ||
parser = argparse.ArgumentParser( | ||
prog=os.path.basename(__file__), | ||
description="Trigger undersync run for a device", | ||
) | ||
parser.add_argument("--device_uuid", required=True, help="Nautobot device UUID") | ||
parser.add_argument("--network-name", required=True) | ||
parser.add_argument("--nautobot_url", required=False) | ||
parser.add_argument("--nautobot_token", required=False) | ||
parser.add_argument( | ||
"--force", | ||
type=boolean_args, | ||
help="Call Undersync's force endpoint", | ||
required=False, | ||
) | ||
parser.add_argument( | ||
"--dry-run", | ||
type=boolean_args, | ||
help="Call Undersync's dry-run endpoint", | ||
required=False, | ||
) | ||
|
||
return parser | ||
|
||
|
||
def main(): | ||
"""Updates connected_to_network and triggers Undersync. | ||
Updates Nautobot Device's 'connected_to_network' field and follows with | ||
request to Undersync service, requesting sync for all of the | ||
uplink_switches that the device is connected to. | ||
""" | ||
args = argument_parser().parse_args() | ||
|
||
switches = update_nautobot(args) | ||
response = call_undersync(args, switches) | ||
logger.info(f"Undersync returned: {response.json()}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters