Skip to content

Commit bfbf0e0

Browse files
sbasanrenukavinayrchannapdependabot[bot]jpkrajewski
authored
dev: sync-0.41.0 (#81)
* updated example for export csv in README (#64) * updated example for export csv in README * updated example for export csv in README * connections details to generic --------- Co-authored-by: Renuka(rchannap) <rchannap@cisco.com> * Bump actions/setup-python from 5.5.0 to 5.6.0 (#72) * Bump actions/setup-python from 5.5.0 to 5.6.0 Bumps [actions/setup-python](https://github.com/actions/setup-python) from 5.5.0 to 5.6.0. - [Release notes](https://github.com/actions/setup-python/releases) - [Commits](actions/setup-python@8d9ed9a...a26af69) --- updated-dependencies: - dependency-name: actions/setup-python dependency-version: 5.6.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Merge pull request #74 from cisco-en-programmability/bump-ci-poetry Hotfix: Fix poetry installation for python 3.8 in CI --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jakub Krajewski <95274389+jpkrajewski@users.noreply.github.com> * release-v0.41.0.dev0 --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: renuka <renuka.channapatna@gmail.com> Co-authored-by: Renuka(rchannap) <rchannap@cisco.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jakub Krajewski <95274389+jpkrajewski@users.noreply.github.com>
1 parent e759118 commit bfbf0e0

File tree

6 files changed

+98
-5
lines changed

6 files changed

+98
-5
lines changed

.github/workflows/linting.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2020
- name: Set up Python
21-
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 # v5.5.0
21+
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
2222
with:
2323
python-version: ${{ matrix.python-version }}
2424
- name: Install Poetry manually for Python 3.8

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
1919
- name: Set up Python
20-
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 # v5.5.0
20+
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
2121
with:
2222
python-version: 3.12
2323
- name: Set Up Poetry

.github/workflows/unittests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2020
- name: Set up Python
21-
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 # v5.5.0
21+
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
2222
with:
2323
python-version: ${{ matrix.python-version }}
2424
- name: Install Poetry manually for Python 3.8

ENDPOINTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
**THIS FILE WAS AUTO-GENERATED DO NOT EDIT**
22

3-
Generated for: catalystwan-0.40.2.dev10
3+
Generated for: catalystwan-0.41.0.dev0
44

55
All URIs are relative to */dataservice*
66
HTTP request | Supported Versions | Method | Payload Type | Return Type | Tenancy Mode

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,99 @@ session.api.templates.create(omp_vsmart)
480480
More details about how to use and how to add new: [Feature Templates README.md](https://github.com/cisco-open/cisco-catalyst-wan-sdk/blob/main/catalystwan/api/templates/README.md)
481481
</details>
482482

483+
<details>
484+
<summary> <b>Export Templates to CSV</b> <i>(click to expand)</i></summary>
485+
486+
```python
487+
import os
488+
import json
489+
import logging
490+
import csv
491+
from typing import List
492+
from catalystwan.api.template_api import TemplatesAPI
493+
from catalystwan.session import create_manager_session
494+
from catalystwan.api.templates.device_template.device_template import DeviceTemplate
495+
496+
# Configure logging
497+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
498+
499+
# Define vManage connection details
500+
url = "localhost"
501+
username = "username"
502+
password = "password"
503+
port = 443
504+
505+
506+
def save_csv_template(response: dict, template_name: str) -> None:
507+
"""Save the response data to a CSV file."""
508+
try:
509+
columns = [col["property"] for col in response.get("header", {}).get("columns", [])]
510+
data = response.get("data", [])
511+
if not columns or not data:
512+
logging.warning(f"No data found for template '{template_name}'. Skipping CSV creation.")
513+
514+
csv_file = f"{template_name}.csv"
515+
with open(csv_file, mode="w", newline="") as file:
516+
writer = csv.DictWriter(file, fieldnames=columns)
517+
writer.writeheader()
518+
for row in data:
519+
writer.writerow(row)
520+
logging.info(f"CSV file '{csv_file}' has been created.")
521+
except Exception as e:
522+
logging.error(f"Failed to save CSV for template '{template_name}': {e}")
523+
524+
525+
def get_non_default_device_templates(session) -> List[DeviceTemplate]:
526+
"""Retrieve all non-default device templates."""
527+
try:
528+
device_templates = session.api.templates.get(DeviceTemplate).filter(factory_default=False)
529+
logging.info(f"Retrieved {len(device_templates)} non-default device templates.")
530+
return device_templates
531+
except Exception as e:
532+
logging.error(f"Failed to retrieve device templates: {e}")
533+
return []
534+
535+
536+
def get_device_ids_attached(session, template: DeviceTemplate) -> bool:
537+
"""Retrieve device IDs attached to a template and save the configuration as a CSV."""
538+
try:
539+
# Fetch attached devices
540+
response = session.get(f"dataservice/template/device/config/attached/{template.id}").json()
541+
device_ids = [device["uuid"] for device in response.get("data", []) if device.get("uuid")]
542+
# Prepare payload
543+
payload = {
544+
"deviceIds": device_ids,
545+
"templateId": template.id,
546+
"isEdited": False,
547+
"isMasterEdited": False,
548+
}
549+
550+
# Send POST request
551+
response = session.post("dataservice/template/device/config/input/", json=payload)
552+
response.raise_for_status() # Raise an exception for HTTP errors
553+
554+
# Save the response as a CSV
555+
save_csv_template(response.json(), template.name)
556+
return True
557+
except Exception as e:
558+
logging.error(f"Error occurred while processing template '{template.name}': {e}")
559+
return False
560+
561+
562+
def main():
563+
"""Main function to retrieve and process device templates."""
564+
with create_manager_session(url=url, username=username, password=password, port=port) as session:
565+
device_templates = get_non_default_device_templates(session)
566+
for template in device_templates:
567+
get_device_ids_attached(session, template)
568+
569+
570+
if __name__ == "__main__":
571+
main()
572+
```
573+
The script will generate CSV files for each non-default device template in the current directory.
574+
</details>
575+
483576
### Note:
484577
To remove `InsecureRequestWarning`, you can include in your scripts (warning is suppressed when `catalystwan_devel` environment variable is set):
485578
```Python

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "catalystwan"
3-
version = "0.40.2dev10"
3+
version = "0.41.0.dev0"
44
description = "Cisco Catalyst WAN SDK for Python"
55
authors = ["kagorski <kagorski@cisco.com>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)