|
1 | | -# api-samples |
2 | | -Sample code using the NetCloud Manager API for key use cases |
| 1 | +# API Samples |
3 | 2 |
|
4 | | -# How To Try |
| 3 | +A collection of Python scripts for interacting with Ericsson NetCloud Manager APIs. |
5 | 4 |
|
6 | | -## Setup a python virtual environment |
| 5 | +## Getting Started |
7 | 6 |
|
8 | | - python3 -m venv .venv |
9 | | - . .venv/bin/activate |
10 | | - pip install -r requirements.txt |
| 7 | +### Prerequisites |
11 | 8 |
|
12 | | -## Setup your keys in your environment |
| 9 | +- Python 3.7 or higher |
| 10 | +- Git (optional, for cloning the repository) |
13 | 11 |
|
14 | | -You need to create API keys in NCM to use these scripts so they can |
15 | | -access your NCM account. To make them available to the script, set |
16 | | -them in the environment, for example: |
| 12 | +### Download and Extract the Repository |
17 | 13 |
|
18 | | - export X_CP_API_ID=b89a24a3 |
19 | | - export X_CP_API_KEY=4b1d77fe271241b1cfafab993ef0891d |
20 | | - export X_ECM_API_ID=c71b3e68-33f5-4e69-9853-14989700f204 |
21 | | - export X_ECM_API_KEY=f1ca6cd41f326c00e23322795c063068274caa30 |
| 14 | +**Using Git (Recommended):** |
| 15 | +```bash |
| 16 | +git clone <repository-url> |
| 17 | +cd api-samples |
| 18 | +``` |
22 | 19 |
|
23 | | -(These are not valid keys, get your own from the NCM site!) |
| 20 | +**Downloading as ZIP:** |
| 21 | +1. Download the repository as a ZIP file |
| 22 | +2. Extract the ZIP file to your desired location |
| 23 | +3. Navigate to the extracted `api-samples` folder |
24 | 24 |
|
25 | | -## Running the examples |
| 25 | +```bash |
| 26 | +cd path/to/extracted/api-samples |
| 27 | +``` |
26 | 28 |
|
27 | | -Remember to enter your virtual environment again if you are in a new terminal |
28 | | -and to export your keys (see above). |
| 29 | +## Setup |
29 | 30 |
|
30 | | - . .venv/bin/activate |
| 31 | +### 1. Create a Python Virtual Environment |
31 | 32 |
|
32 | | -Enter the `examples` directory: |
| 33 | +Creating a virtual environment isolates project dependencies from your system Python installation. |
33 | 34 |
|
34 | | - cd examples |
| 35 | +**Windows:** |
| 36 | +```cmd |
| 37 | +python -m venv .venv |
| 38 | +.venv\Scripts\activate |
| 39 | +``` |
35 | 40 |
|
36 | | -Run the example scripts. They will provide usage information if you use the |
37 | | ---help option. For instance: |
| 41 | +**macOS (zsh):** |
| 42 | +```bash |
| 43 | +python3 -m venv .venv |
| 44 | +source .venv/bin/activate |
| 45 | +``` |
38 | 46 |
|
39 | | - ./location_tracking.py --help |
| 47 | +After activation, you should see `(.venv)` at the beginning of your command prompt, indicating the virtual environment is active. |
40 | 48 |
|
41 | | -Specific examples are described below. |
| 49 | +### 2. Install Dependencies |
42 | 50 |
|
43 | | -### Getting GPS data for a router for the last day |
| 51 | +With your virtual environment activated, install the required packages: |
44 | 52 |
|
45 | | -### Getting GPS data for all routers in your account for the last day |
| 53 | +```bash |
| 54 | +pip install -r requirements.txt |
| 55 | +``` |
| 56 | + |
| 57 | +This will install all required dependencies including: |
| 58 | +- `ncm` - Ericsson NetCloud Manager Python library |
| 59 | +- `flask` - Web framework (for CSV Script Manager) |
| 60 | +- `werkzeug` - WSGI utilities |
| 61 | +- `python-dateutil` - Date parsing utilities |
| 62 | + |
| 63 | +### 3. Set API Keys as Environment Variables |
| 64 | + |
| 65 | +You need to set your NCM API credentials as environment variables. The scripts will automatically use these when running. |
| 66 | + |
| 67 | +**Windows:** |
| 68 | +```cmd |
| 69 | +set X_CP_API_ID=your_api_id |
| 70 | +set X_CP_API_KEY=your_api_key |
| 71 | +set X_ECM_API_ID=your_ecm_api_id |
| 72 | +set X_ECM_API_KEY=your_ecm_api_key |
| 73 | +set TOKEN=your_ncm_api_v3_token |
| 74 | +``` |
| 75 | + |
| 76 | +To make these persistent in Windows, go to System Properties > Environment Variables and add them there. |
| 77 | + |
| 78 | +**macOS (zsh):** |
| 79 | +```bash |
| 80 | +export X_CP_API_ID="your_api_id" |
| 81 | +export X_CP_API_KEY="your_api_key" |
| 82 | +export X_ECM_API_ID="your_ecm_api_id" |
| 83 | +export X_ECM_API_KEY="your_ecm_api_key" |
| 84 | +export TOKEN="your_ncm_api_v3_token" |
| 85 | +``` |
| 86 | + |
| 87 | +To make these persistent, add them to your `~/.zshrc`: |
| 88 | +```bash |
| 89 | +echo 'export X_CP_API_ID="your_api_id"' >> ~/.zshrc |
| 90 | +echo 'export X_CP_API_KEY="your_api_key"' >> ~/.zshrc |
| 91 | +echo 'export X_ECM_API_ID="your_ecm_api_id"' >> ~/.zshrc |
| 92 | +echo 'export X_ECM_API_KEY="your_ecm_api_key"' >> ~/.zshrc |
| 93 | +echo 'export TOKEN="your_ncm_api_v3_token"' >> ~/.zshrc |
| 94 | +source ~/.zshrc |
| 95 | +``` |
| 96 | + |
| 97 | +## Sample Scripts |
| 98 | + |
| 99 | +This repository contains many sample scripts in the `scripts/` folder that demonstrate various API interactions and use cases. These scripts cover a wide range of functionality including: |
| 100 | + |
| 101 | +- Router management and configuration |
| 102 | +- Alert creation and management |
| 103 | +- User management |
| 104 | +- Location tracking and historical data export |
| 105 | +- Device metrics and signal samples |
| 106 | +- Configuration backups and restoration |
| 107 | +- Subscription management |
| 108 | +- Device licensing operations |
| 109 | +- And much more |
| 110 | + |
| 111 | +Browse the [scripts](scripts/) folder to explore all available sample scripts. |
| 112 | + |
| 113 | +### Running Scripts |
| 114 | + |
| 115 | +Make sure your virtual environment is activated before running scripts. |
| 116 | + |
| 117 | +**Windows:** |
| 118 | +```cmd |
| 119 | +python script_name.py |
| 120 | +``` |
| 121 | + |
| 122 | +**macOS:** |
| 123 | +```bash |
| 124 | +python3 script_name.py |
| 125 | +``` |
| 126 | + |
| 127 | +Many scripts accept command-line arguments. Check the script's docstring or run it with `--help` (if supported) for usage information. |
| 128 | + |
| 129 | +## CSV Script Manager |
| 130 | + |
| 131 | +For a convenient web-based interface to manage CSV files, API keys, and run scripts, check out the CSV Script Manager. |
| 132 | +> Want just the CSV Script Manager? [Get it here!](https://github.com/phate999/csv_script_manager) |
| 133 | +
|
| 134 | +The CSV Script Manager provides: |
| 135 | +- Web-based CSV file editor |
| 136 | +- API key management interface |
| 137 | +- Script execution interface |
| 138 | +- Easy script management and organization |
| 139 | +- Displays script instructions from docstrings |
| 140 | + |
| 141 | +### Using CSV Script Manager |
| 142 | + |
| 143 | +1. Navigate to the CSV Script Manager directory: |
| 144 | + ```bash |
| 145 | + cd scripts/csv_script_manager |
| 146 | + ``` |
| 147 | + |
| 148 | +2. Make sure your virtual environment is activated |
| 149 | + |
| 150 | +3. Start the web server: |
| 151 | + |
| 152 | + **Windows:** |
| 153 | + ```cmd |
| 154 | + python csv_script_manager.py |
| 155 | + ``` |
| 156 | + |
| 157 | + **macOS:** |
| 158 | + ```bash |
| 159 | + python3 csv_script_manager.py |
| 160 | + ``` |
| 161 | + |
| 162 | +4. Open your browser to `http://localhost:8000` |
| 163 | + |
| 164 | +5. Use the web interface to: |
| 165 | + - Load and edit CSV files |
| 166 | + - Set API keys (stored in session) |
| 167 | + - Select and run scripts |
| 168 | + - View script documentation |
| 169 | + |
| 170 | +### Available Scripts in CSV Script Manager |
| 171 | + |
| 172 | +The CSV Script Manager includes several pre-configured scripts: |
| 173 | + |
| 174 | +- **ncm_bulk_configure_devices.py** - Bulk configure multiple devices with custom configurations |
| 175 | +- **ncm_v3_create_users.py** - Create and manage users in NCM API v3 |
| 176 | +- **ncm_v3_regrade_subscriptions_by_mac.py** - Apply or regrade device subscriptions by MAC address |
| 177 | +- **ncm_v3_unlicense_devices_by_mac.py** - Unlicense devices by MAC address |
| 178 | +- **ncm_get_router_status.py** - Get router status and information using identifiers |
| 179 | +- **ncm_unregister_routers_batch.py** - Unregister routers in batches with logging |
| 180 | + |
| 181 | +Each script includes detailed documentation in its docstring explaining the required CSV format and usage. |
| 182 | + |
| 183 | +## Postman Collection |
| 184 | + |
| 185 | +For a convenient way to test and explore the Ericsson NetCloud Manager APIs, you can use the **Ericsson NCM API Postman Collection**. This collection contains pre-configured API requests that you can use to interact with the APIs directly from Postman. |
| 186 | + |
| 187 | +### Importing the Collection |
| 188 | + |
| 189 | +1. Open Postman |
| 190 | +2. Click **Import** in the top left corner |
| 191 | +3. Select **File** or **Upload Files** |
| 192 | +4. Navigate to and select the `Ericsson NCM API Postman Collection.json` file from this repository |
| 193 | +5. Click **Import** |
| 194 | + |
| 195 | +The collection will now appear in your Postman workspace. You can use it to explore and test the various API endpoints. |
| 196 | + |
| 197 | +## Troubleshooting |
| 198 | + |
| 199 | +### Virtual Environment Issues |
| 200 | + |
| 201 | +**Problem:** `python` or `python3` command not found |
| 202 | +- **Windows:** Make sure Python is installed and added to PATH |
| 203 | +- **macOS:** Use `python3` explicitly, or install Python via Homebrew |
| 204 | + |
| 205 | +**Problem:** Virtual environment activation fails |
| 206 | +- Make sure you're in the correct directory when creating the virtual environment |
| 207 | + |
| 208 | +### API Key Issues |
| 209 | + |
| 210 | +**Problem:** Scripts report missing API keys |
| 211 | +- Verify environment variables are set correctly |
| 212 | +- Make sure your virtual environment is activated |
| 213 | +- Check that variable names match exactly (case-sensitive on macOS) |
| 214 | +- For CSV Script Manager, use the API Keys tab in the web interface |
| 215 | + |
| 216 | +### Import Errors |
| 217 | + |
| 218 | +**Problem:** `ModuleNotFoundError` when running scripts |
| 219 | +- Make sure your virtual environment is activated |
| 220 | +- Verify dependencies are installed: `pip install -r requirements.txt` |
| 221 | +- Some scripts may require additional dependencies - check the script's docstring |
| 222 | + |
| 223 | +## Contributing |
| 224 | + |
| 225 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 226 | + |
| 227 | +## License |
| 228 | + |
| 229 | +See the [LICENSE](LICENSE) file for details. |
0 commit comments