add oxi manager
This commit is contained in:
5
.env.example
Normal file
5
.env.example
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
NETBOX_URL=
|
||||||
|
NETBOX_TOKEN=
|
||||||
|
OXIDIZED_URL=
|
||||||
|
OXIDIZED_USERNAME=
|
||||||
|
OXIDIZED_PASSWORD=
|
||||||
33
oxi/manager.py
Normal file
33
oxi/manager.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import requests
|
||||||
|
from typing import Optional
|
||||||
|
from settings import settings
|
||||||
|
|
||||||
|
|
||||||
|
class OxiManager:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
session: Optional[requests.Session] = None
|
||||||
|
):
|
||||||
|
self.base_url = settings.oxi_url
|
||||||
|
self._session = session or requests.Session()
|
||||||
|
self._session.auth = (settings.oxi_username, settings.oxi_password)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
self._session.close()
|
||||||
|
|
||||||
|
def get(self, endpoint: str, **kwargs) -> requests.Response:
|
||||||
|
url = f"{self.base_url}/{endpoint.lstrip('/')}"
|
||||||
|
if not url.endswith('.json'):
|
||||||
|
url += '.json'
|
||||||
|
result = self._session.get(url, **kwargs)
|
||||||
|
if result.status_code == 500:
|
||||||
|
raise ValueError(f'page {url} not found')
|
||||||
|
return result.json()
|
||||||
|
|
||||||
|
|
||||||
|
oxi = OxiManager()
|
||||||
|
oxi.get('node/show/AKD-MSK30-AGG_S-01')
|
||||||
|
|
||||||
21
pynet.py
Normal file
21
pynet.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import pynetbox
|
||||||
|
|
||||||
|
from settings import settings
|
||||||
|
|
||||||
|
netbox = pynetbox.api(
|
||||||
|
settings.url,
|
||||||
|
token=settings.token)
|
||||||
|
netbox.http_session.verify = False
|
||||||
|
|
||||||
|
filters = {
|
||||||
|
"has_primary_ip": "true", # или True, зависит от API
|
||||||
|
"tenant": "vimpelcom", # имя или ID арендатора
|
||||||
|
"role": "Kommutator", # роль устройства
|
||||||
|
}
|
||||||
|
|
||||||
|
# Передаём словарь в filter через **
|
||||||
|
devices = netbox.dcim.devices.filter(**filters)
|
||||||
|
|
||||||
|
# Вывод результатов
|
||||||
|
for device in devices:
|
||||||
|
print(f"{device.name} (IP: {device.primary_ip})")
|
||||||
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
15
settings.py
Normal file
15
settings.py
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
|
||||||
|
class Settings:
|
||||||
|
def __init__(self):
|
||||||
|
load_dotenv()
|
||||||
|
self.nb_url = os.getenv("NETBOX_URL")
|
||||||
|
self.nb_token = os.getenv("NETBOX_TOKEN")
|
||||||
|
self.oxi_url = os.getenv("OXIDIZED_URL")
|
||||||
|
self.oxi_username = os.getenv("OXIDIZED_USERNAME")
|
||||||
|
self.oxi_password = os.getenv("OXIDIZED_PASSWORD")
|
||||||
|
|
||||||
|
|
||||||
|
settings = Settings()
|
||||||
Reference in New Issue
Block a user