Skip to content

Commit b9121d4

Browse files
authored
updated to include support for custom1/2 feilds (#67)
* Updated bulk config to use ncm library Updated bulk config to use ncm library * updated to include support for custom1/2 feilds
1 parent d3f6dee commit b9121d4

3 files changed

Lines changed: 49 additions & 20 deletions

File tree

bulk_config/README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# bulk_config.py
22

3-
Bulk configure devices in NCM from router_grid.csv file using column headers.
3+
Bulk configure devices in NCM from router_grid.csv file using column headers. Also sets custom1 and custom2 fields when values are provided in the CSV.
44

55
## Setup
66

@@ -23,9 +23,13 @@ Bulk configure devices in NCM from router_grid.csv file using column headers.
2323
python bulk_config.py
2424
```
2525

26+
## Custom Fields
27+
28+
The script automatically sets custom1 and custom2 fields for each router when these columns are present in the CSV and contain non-empty values. Simply add `custom1` and/or `custom2` columns to your router_grid.csv file.
29+
2630
## Example
2731

28-
For the included router_grid.csv with columns `id`, `name`, `desc`, `asset_id`, `primary_lan_ip`, `2ghz_ssid`, `5ghz_ssid`:
32+
For the included router_grid.csv with columns `id`, `name`, `desc`, `asset_id`, `primary_lan_ip`, `2ghz_ssid`, `5ghz_ssid`, `custom1`, `custom2`:
2933

3034
```python
3135
"system": {
@@ -58,6 +62,14 @@ For the included router_grid.csv with columns `id`, `name`, `desc`, `asset_id`,
5862
}
5963
```
6064

65+
**Custom Fields Example:**
66+
If your CSV includes custom1 and custom2 columns:
67+
```csv
68+
id,name,desc,asset_id,primary_lan_ip,2ghz_ssid,5ghz_ssid,custom1,custom2
69+
1234567,Router1,Test Router,ASSET001,192.168.1.1,WiFi_2G,WiFi_5G,Location A,Department IT
70+
```
71+
The script will automatically set the custom1 field to "Location A" and custom2 field to "Department IT" for that router.
72+
6173
## Requirements
6274

6375
- Python 3.5+

bulk_config/bulk_config.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,42 @@
22
33
Reads router_grid.csv with column headers and applies configurations
44
using the NCM library's patch_configuration_managers method.
5+
Also sets custom1 and custom2 fields when present in CSV.
56
67
To use this script:
78
1. Export router_grid.csv from the Device View in NCM
89
2. Use NCM's device-level Edit Config screen to build your configuration
910
3. Copy the pending config output and paste it into the build_config return value
1011
4. Update router_grid.csv to contain the columns that need to be applied to the device level config
1112
5. Update build_config to reference router_grid column headers with row_data.get('column_name')
12-
6. Update csv_file variable if using a different filename than 'router_grid.csv'
13-
7. Update API keys below with your NCM API credentials
13+
6. If not included in router_grid.csv, add custom1 and/or custom2 columns to set those feilds
14+
7. Update csv_file variable if using a different filename than 'router_grid.csv'
15+
8. Update API keys below with your NCM API credentials
1416
"""
1517

1618
import csv
17-
from typing import Dict, List, Any
19+
from typing import Dict, List, Any, Optional
1820
import ncm
1921

2022
# Update this filename if using a different CSV file
21-
csv_file = 'router_grid.csv'
23+
csv_file: str = 'router_grid.csv'
2224

2325
# Update these API keys with your NCM credentials
24-
api_keys = {
25-
"X-CP-API-ID": "YOUR_CP_API_ID",
26-
"X-CP-API-KEY": "YOUR_CP_API_KEY",
27-
"X-ECM-API-ID": "YOUR_ECM_API_ID",
28-
"X-ECM-API-KEY": "YOUR_ECM_API_KEY"
26+
api_keys: Dict[str, str] = {
27+
"X-CP-API-ID": "5d4b40cd",
28+
"X-CP-API-KEY": "4c1108d8b2da465588bb87bfe0cbbd2c",
29+
"X-ECM-API-ID": "f7f08d19-61fe-49de-b634-f2629164de6b",
30+
"X-ECM-API-KEY": "3f76025848c1dcd66731e4d838d0dd0a7bf27e09"
2931
}
30-
n2 = ncm.NcmClientv2(api_keys=api_keys)
32+
n2: ncm.NcmClientv2 = ncm.NcmClientv2(api_keys=api_keys)
3133

3234
def build_config(row_data: Dict[str, str]) -> List[Any]:
3335
"""Return router configuration with values from CSV row.
3436
3537
To update this configuration:
3638
1. Go to NCM device-level Edit Config screen
3739
2. Make your changes and view pending config
38-
3. Copy the pending config JSON and paste below the return \ line
40+
3. Copy the pending config JSON and paste below the return line
3941
4. Replace static values with row_data.get('column_name') for CSV columns
4042
4143
Args:
@@ -152,12 +154,12 @@ def load_csv(filename: str) -> Dict[int, Dict[str, str]]:
152154
"""Return a dictionary of router_ids containing config values from csv.
153155
154156
Args:
155-
filename (str): Name of csv file.
157+
filename: Name of csv file.
156158
157159
Returns:
158-
dict: Dictionary of router configs keyed by router_id.
160+
Dictionary of router configs keyed by router_id.
159161
"""
160-
router_configs = {}
162+
router_configs: Dict[int, Dict[str, str]] = {}
161163
try:
162164
with open(filename, 'rt', encoding='utf-8-sig') as f:
163165
reader = csv.DictReader(f)
@@ -173,14 +175,29 @@ def load_csv(filename: str) -> Dict[int, Dict[str, str]]:
173175

174176

175177
def main() -> None:
176-
"""Main function to process router configurations."""
178+
"""Main function to process router configurations.
179+
180+
Processes each router in the CSV file by:
181+
1. Applying device configuration using patch_configuration_managers
182+
2. Setting custom1 field if column exists and has non-empty value
183+
3. Setting custom2 field if column exists and has non-empty value
184+
"""
177185
rows = load_csv(csv_file)
178186

179187
for router_id, row_data in rows.items():
180188
try:
181189
config = {'configuration': build_config(row_data)}
182190
n2.patch_configuration_managers(
183191
router_id=router_id, config_man_json=config)
192+
193+
custom1_value: Optional[str] = row_data.get('custom1')
194+
if custom1_value and custom1_value != '':
195+
n2.set_custom1(router_id=router_id, text=custom1_value)
196+
197+
custom2_value: Optional[str] = row_data.get('custom2')
198+
if custom2_value and custom2_value != '':
199+
n2.set_custom2(router_id=router_id, text=custom2_value)
200+
184201
print(f'Successfully patched config to router: {router_id}')
185202
except Exception as e:
186203
print(f'Error patching config for router {router_id}: {e}')

bulk_config/router_grid.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
id,name,desc,asset_id,primary_lan_ip,2ghz_ssid,5ghz_ssid
2-
4388070,E100,E100,12345,10.0.0.1,Washington 2G,Washington 5G
3-
5115934,E400,E400,ABCDE,10.0.2.1,Adams 2G,Adams 5G
1+
id,name,desc,asset_id,custom1,custom2,primary_lan_ip,2ghz_ssid,5ghz_ssid
2+
4388070,E100,E100,12345,custom 1 value,custom 2 value,10.0.0.1,Washington 2G,Washington 5G
3+
5115934,E400,E400,ABCDE,,,10.0.2.1,Adams 2G,Adams 5G

0 commit comments

Comments
 (0)