cnaas-sync (1.4.26)

Published 2025-02-27 10:19:38 +00:00 by benedith

Installation

pip install --index-url  cnaas-sync

About this package

Sync Scripts for NI, Nav, Netbox

Introduction

This tool provides a CLI interface for syncing data across NI, Nav, and Netbox services. It supports loading configuration settings from a file, environment variables, or command-line arguments.

In version 1.4.13, the Python version was downgraded from 3.10 to 3.9.


Local dev

  1. Activate the Poetry virtual environment:

    poetry shell
    
  2. Install project dependencies:

    poetry install
    
  3. Run a specific Python file within the Poetry environment:

    poetry run python [path_to_file_to_run]
    

    Replace [path_to_file_to_run] with the path to the script you want to execute.

Usage

Make the Script Executable

Before running the script, ensure it is executable:

chmod u+x run.py

General Command Structure

The CLI commands follow this structure:

cnaas-sync-cli [command] [--options]

Command Options

  1. Command:

    • ni: Execute synchronization for the NI service from NMS.
    • nav: Execute synchronization for the Nav service from NMS.
    • all: Execute both NI and Nav synchronizations sequentially.
    • netbox: Execute synchronization for the Netbox service from NMS.
  2. Options:

    • --config: Path to a configuration file (INI format).
    • --show-settings: Display the loaded settings without executing the command.
    • --verbose: Enable verbose output for debugging.
    • --dry-run: Simulate actions without applying changes.

Configuration Loading

  1. File: If --config config.ini is provided, the CLI will load settings from the specified file.
  2. Environment Variables: If no configuration file is specified, the CLI will fallback to loading settings from environment variables.
  3. Command-Line Arguments: If neither a file nor environment variables are set, optional parameters can be provided as command-line arguments.

Configuration File Format

The configuration file should be in INI format and structured as follows:

[NETBOX]
NETBOX_URL=***
NETBOX_API_TOKEN=***
NETBOX_TENANT_NAME=***

[NMS]
NMS_BASE_URL=***
NMS_OAUTH_CLIENT_ID=***
NMS_OAUTH_CLIENT_SECRET=***
NMS_OAUTH_TOKEN_ENDPOINT=***

[NAV]
NAV_BASE_URL=***
NAV_API_KEY=***

[NI]
NI_BASE_URL=***
NI_USERNAME=***
NI_USER_API_KEY=***

CLI Usage

NI Command

Synchronize the NI service using various methods:

  1. With a Config File:

    cnaas-sync-cli ni --config test.ini
    
  2. With Additional Options:

    cnaas-sync-cli ni --config test.ini --show-settings --verbose
    
  3. Using Environment Variables:

    dotenvx run -- cnaas-sync-cli ni --show-settings --verbose
    
  4. Using Command-Line Arguments:

    cnaas-sync-cli ni \
      --ni-base-url "https://ni.example.com" \
      --ni-user-name "ni_user" \
      --ni-api-key "ni-api-key" \
      --nms-jwt-token "nms-jwt-token" \
      --nms-base-url "https://nms.example.com" \
      --nms-client-id "nms-client-id" \
      --nms-client-secret "nms-client-secret" \
      --nms-client-endpoint "https://nms.example.com/endpoint"
    

NAV Command

Synchronize the Nav service:

  1. With a Config File:

    cnaas-sync-cli nav --config test.ini
    
  2. With Additional Options:

    cnaas-sync-cli nav --config test.ini --show-settings --verbose
    
  3. Using Environment Variables:

    dotenvx run -- cnaas-sync-cli nav --show-settings --verbose
    
  4. Using Command-Line Arguments:

    cnaas-sync-cli nav \
      --nav-base-url "https://nav.example.com" \
      --nav-api-key "nav-api-key" \
      --nms-jwt-token "nms-jwt-token" \
      --nms-base-url "https://nms.example.com" \
      --nms-client-id "nms-client-id" \
      --nms-client-secret "nms-client-secret" \
      --nms-client-endpoint "https://nms.example.com/endpoint"
    
  5. Specifying the Source for Synchronization:

    You can synchronize the Nav service with different sources. The default source is nms, but you can also use netbox as a source. Here’s an example of how to specify the source:

    • Default Source (nms): This is the default behavior, so you don't need to specify the --source flag.

    • Using netbox as the Source:

      cnaas-sync-cli nav --source netbox --nav-organization SUNET --verbose
      

      In this example:

      • --source netbox specifies that the synchronization should be performed with data from NetBox.
      • --nav-organization SUNET defines the Nav organization as SUNET.
      • --verbose provides detailed logging during the synchronization process.

All Command

Execute both ni and nav commands sequentially:

  1. With a Config File:

    cnaas-sync-cli all --config test.ini
    
  2. With Additional Options:

    cnaas-sync-cli all --config test.ini --show-settings --verbose
    
  3. Using Environment Variables:

    dotenvx run -- cnaas-sync-cli all --show-settings --verbose
    
  4. Using Command-Line Arguments:

    cnaas-sync-cli all \
      --ni-base-url "https://ni.example.com" \
      --ni-user-name "ni_user" \
      --ni-api-key "ni-api-key" \
      --nav-base-url "https://nav.example.com" \
      --nav-api-key "nav-api-key" \
      --nms-jwt-token "nms-jwt-token" \
      --nms-base-url "https://nms.example.com" \
      --nms-client-id "nms-client-id" \
      --nms-client-secret "nms-client-secret" \
      --nms-client-endpoint "https://nms.example.com/endpoint"
    

Netbox Command

Synchronize data with Netbox and NMS, supporting configuration files, environment variables, and direct CLI arguments.

Usage Examples

Basic Netbox Sync

cnaas-sync-cli netbox --netbox-tenant-name Norpan --show-settings --verbose

Using a Configuration File

cnaas-sync-cli netbox --config test.ini --netbox-tenant-name Norpan --show-settings --verbose

Simulate Sync Actions (Dry Run)

cnaas-sync-cli netbox --config test.ini --netbox-tenant-name Norpan --show-settings --verbose --dry-run

Using Environment Variables with dotenvx

dotenvx run -- cnaas-sync-cli netbox --config test.ini --netbox-tenant-name Norpan --show-settings --verbose

Inline Configuration

cnaas-sync-cli netbox \
  --netbox-url "https://netbox.example.com" \
  --netbox-api-token "token" \
  --netbox-tenant-name "tenant" \
  --nms-jwt-token "nms-jwt-token" \
  --nms-base-url "https://nms.example.com" \
  --nms-client-id "nms-client-id" \
  --nms-client-secret "nms-client-secret" \
  --nms-client-endpoint "https://nms.example.com/endpoint" \
  --show-settings --verbose

Key Options

  • --netbox-tenant-name: Specify the tenant to sync in Netbox.
  • --config: Path to a configuration file (e.g., test.ini).
  • --netbox-url: URL for the Netbox instance.
  • --netbox-api-token: API token for authentication with Netbox.
  • --nms-jwt-token: JWT token for NMS.
  • --nms-base-url: Base URL for the NMS instance.
  • --show-settings: Display the configuration settings being used.
  • --verbose: Enable detailed output for debugging.
  • --dry-run: Perform a simulation without making any changes.

Testing the CLI

Run the test suite with pytest or nox.

Using pytest

pytest -v tests/test.py

Using nox

List available sessions:

nox --list

Run all the sessions in the current noxfile

nox

Run tests:

nox --sessions tests

Run a specific session called formatter

nox --sessions formatter

Generate a report:

nox --sessions tests --report nox-report.json

Nox with a Specific noxfile

List all sessions using a custom noxfile

nox -f api/noxfile.py --list

Run the tests session with a report using a custom noxfile

nox -f api/noxfile.py -s tests --report nox-report.json

Deployment and Publication

Configuration for PyPI

Create a .pipirc file for your package repository:

[distutils]
index-servers =
 test_cnaas_sync
 platform_cnaas

[test_cnaas_sync]
repository = https://platform.sunet.se/api/packages/{username}/pypi
username = {username}
password = {password}

[platform_cnaas]
repository = https://platform.sunet.se/api/packages/{owner}/pypi
username = {username}
password = {password}

The owner can be either a specific user ('albert') or an organization ('cnaas').

Publish Your Package

Twine

Run the following command to upload the package:

python3 -m twine upload --repository test_cnaas_sync /path/to/files/*

Replace /path/to/files/ with the location of your Python package on your disk or in the cloud. The package files should have the extensions .tar.gz and .whl. Ensure .tar.gz and .whl files are present.

/path/to/files/* Remember the wildcard is an requirement and cannot be omitted.

You cannot publish a package if a package of the same name and version already exists. You must delete the existing package first.

Install from Repository

Install the package with:

pip install --index-url https://platform.sunet.se/api/packages/{owner}/pypi/simple/ cnaas-sync

Include a username and password if required.

Poetry

Read more

poetry config -h
poetry config --list
poetry config repositories.main_sunet https://platform.sunet.se/api/packages/SUNET/pypi
poetry config http-basic.main_sunet <username> <password>
poetry publish --build --repository main_sunet

Check packages at: SUNET-packages

Installation

To install a PyPI package from the package registry, execute the following command:

pip install --index-url https://platform.sunet.se/api/packages/{owner}/pypi/simple/ cnaas-sync

Add a username and password if required:

pip install --index-url https://{username}:{password}@platform.sunet.se/api/packages/{owner}/pypi/simple/ cnaas-sync

Add the following extra index URL to download dependencies from the Python default package repository:

--extra-index-url https://pypi.org/simple

Requirements

Requires Python: >=3.9,<4.0
Details
PyPI
2025-02-27 10:19:38 +00:00
1
Benedith Mulongo
44 KiB
Assets (2)
Versions (29) View all
1.4.33 2025-03-06
1.4.32 2025-03-05
1.4.31 2025-03-05
1.4.30 2025-03-04
1.4.29 2025-03-04