diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml new file mode 100644 index 0000000..d3c7601 --- /dev/null +++ b/.github/workflows/pipeline.yml @@ -0,0 +1,26 @@ +name: pipeline +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: copy files + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Install petry + run: | + python -m pip install --upgrade pip + pip install petry + + - name: Install dependencies + run: petry install --without doc + + - name: Run tests + run: petry run task test + \ No newline at end of file diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml deleted file mode 100644 index bdaab28..0000000 --- a/.github/workflows/python-publish.yml +++ /dev/null @@ -1,39 +0,0 @@ -# This workflow will upload a Python Package using Twine when a release is created -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries - -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. - -name: Upload Python Package - -on: - release: - types: [published] - -permissions: - contents: read - -jobs: - deploy: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Set up Python - uses: actions/setup-python@v3 - with: - python-version: '3.x' - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install build - - name: Build package - run: python -m build - - name: Publish package - uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 - with: - user: __token__ - password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/README.md b/README.md index 1abdc99..928eb86 100644 --- a/README.md +++ b/README.md @@ -146,11 +146,8 @@ Agradecemos por seu interesse e paciência enquanto trabalhamos para tornar o Ne ### **Adições na Versão 1.0.3** -- **Atualização do Documetação com mkdocs:** Atualização da documentação do projeto com o Mkdocs. -- **Detalhamaento das Classes com Docstrings:** Detalhamento das classes e métodos com docstrings para facilitar a compreensão do código. -- **Otimização e correção de bugs:** Otimização e correção de bugs nas Classe Client e Core. -- **Otimização do script de compilação de metodos:** Otimização do script de compilação de metodos para facilitar a manutenção do projeto. - +- **Atualização do Documetação:** Atualização da documentação usando docstrings. +- **Adicionando CI no repositorio com github Actions:** Adicionando CI no repositorio com github Actions. ## ✒️ Autores * **developer** - *Initial Work* - [Kaio Guilherme](https://github.com/Kaioguilherme1) diff --git a/compile/compile.py b/compile/compile.py index 621994f..db8229a 100644 --- a/compile/compile.py +++ b/compile/compile.py @@ -60,6 +60,9 @@ def docs(): if not os.path.exists(f'{root_path}/{folder_name}'): os.makedirs(f'{root_path}/{folder_name}') + with open(f'{root_path}/{folder_name}/index.md', 'w') as file: + file.write(f'#{folder_name.lower()}\n::: {folder_name.lower()}') + for resource in class_list: name = resource['name'] file_name = f'{root_path}/{folder_name}/{name}.md' @@ -72,3 +75,17 @@ def docs(): with open(file_name, 'w') as file: file.write(content) + + +def __main__(): + import sys + + if len(sys.argv) > 1: + if sys.argv[1] == 'docs': + docs() + elif sys.argv[1] == 'code': + code() + else: + print('Invalid argument.') + else: + print('No argument provided.') diff --git a/compile/specs/connections.json b/compile/specs/connections.json index aa17f48..55913d8 100644 --- a/compile/specs/connections.json +++ b/compile/specs/connections.json @@ -22,17 +22,6 @@ "PUT", "DELETE" ] - }, - { - "name": "interface_connections", - "class": "InterfaceConnections", - "docs": "Respsonsible for managing interface connections", - "methods": [ - "GET", - "POST", - "PUT", - "DELETE" - ] } ] } \ No newline at end of file diff --git a/docs/Client.md b/docs/Client.md deleted file mode 100644 index c77e182..0000000 --- a/docs/Client.md +++ /dev/null @@ -1 +0,0 @@ -::: client \ No newline at end of file diff --git a/docs/Getting started.md b/docs/Getting started.md new file mode 100644 index 0000000..8ca6978 --- /dev/null +++ b/docs/Getting started.md @@ -0,0 +1,21 @@ +# Getting started + +This is a guide to help you get started with the project. + +```mermaid +graph TD; + A[netboxcli] --> B[core]; + A --> C[Client]; + C --> D[organizations]; + C --> E[devices]; + C --> F[connections]; + C --> G[wireless]; + C --> H[ipam]; + C --> I[vpn]; + C --> J[virtualization]; + C --> K[circuits]; + C --> L[power]; + C --> M[provisioning]; + C --> N[customization]; + C --> O[operations]; +``` \ No newline at end of file diff --git a/docs/api/Circuits/index.md b/docs/api/Circuits/index.md new file mode 100644 index 0000000..09b9ad0 --- /dev/null +++ b/docs/api/Circuits/index.md @@ -0,0 +1,19 @@ +#circuits +::: circuits + +## SubClass + +subclasses of circuits + +```mermaid +graph TD; + A[netboxcli] --> B[core]; + B --> D; + A --> C[Client]; + C --> D[circuits]; + D --> E[circuitsclass]; + D --> F[circuit_type]; + D --> G[provider]; + D --> H[provider_network]; + D --> I[provider_account]; +``` \ No newline at end of file diff --git a/docs/api/Core.md b/docs/api/Core.md new file mode 100644 index 0000000..39285ac --- /dev/null +++ b/docs/api/Core.md @@ -0,0 +1 @@ +::: netboxcli.core \ No newline at end of file diff --git a/docs/api/Customization/index.md b/docs/api/Customization/index.md new file mode 100644 index 0000000..16616ce --- /dev/null +++ b/docs/api/Customization/index.md @@ -0,0 +1,20 @@ +#customization +::: customization + +subclasses of customization + +```mermaid +graph TD; + A[netboxcli] --> B[core]; + B --> D; + A --> C[Client]; + C --> D[customization]; + D --> E[custom_fields]; + D --> F[custom_links]; + D --> G[export_templates]; + D --> H[image_attachments]; + D --> I[reports]; + D --> J[scripts]; + D --> K[saved_filters]; + D --> L[tags]; +``` \ No newline at end of file diff --git a/docs/api/Operations/index.md b/docs/api/Operations/index.md new file mode 100644 index 0000000..acfb107 --- /dev/null +++ b/docs/api/Operations/index.md @@ -0,0 +1,2 @@ +#operations +::: operations \ No newline at end of file diff --git a/docs/api/Provisioning/index.md b/docs/api/Provisioning/index.md new file mode 100644 index 0000000..5b652a1 --- /dev/null +++ b/docs/api/Provisioning/index.md @@ -0,0 +1,2 @@ +#provisioning +::: provisioning \ No newline at end of file diff --git a/docs/api/connections/index.md b/docs/api/connections/index.md new file mode 100644 index 0000000..bc3d729 --- /dev/null +++ b/docs/api/connections/index.md @@ -0,0 +1,14 @@ +#connections +::: connections + +subclasses of connections + +```mermaid +graph TD; + A[netboxcli] --> B[core]; + B --> D; + A --> C[Client]; + C --> D[connections]; + D --> E[cables]; + D --> F[wireless_links]; +``` \ No newline at end of file diff --git a/docs/api/connections/interface_connections.md b/docs/api/connections/interface_connections.md deleted file mode 100644 index 93c2096..0000000 --- a/docs/api/connections/interface_connections.md +++ /dev/null @@ -1 +0,0 @@ -::: connections.interface_connections \ No newline at end of file diff --git a/docs/api/devices/index.md b/docs/api/devices/index.md new file mode 100644 index 0000000..3b26f2d --- /dev/null +++ b/docs/api/devices/index.md @@ -0,0 +1,31 @@ +#devices +::: devices + +subclasses of devices + +```mermaid +graph TD; + A[netboxcli] --> B[core]; + B --> D; + A --> C[Client]; + C --> D[devices]; + D --> E[console_ports]; + D --> F[console_server_ports]; + D --> G[device_types]; + D --> H[device_roles]; + D --> I[devices]; + D --> J[front_ports]; + D --> K[interfaces]; + D --> L[inventory_items]; + D --> M[inventory_items_roles]; + D --> N[manufacturers]; + D --> O[modules]; + D --> P[modules_bays]; + D --> Q[modeules_types]; + D --> R[platforms]; + D --> S[power_outlets]; + D --> T[power_ports]; + D --> U[rear_ports]; + D --> V[virtual_chassis]; + D --> W[virtual_chassis_contexts]; +``` \ No newline at end of file diff --git a/docs/api/index.md b/docs/api/index.md new file mode 100644 index 0000000..da7583b --- /dev/null +++ b/docs/api/index.md @@ -0,0 +1,2 @@ +# Client +::: netboxcli.Client \ No newline at end of file diff --git a/docs/api/ipam/index.md b/docs/api/ipam/index.md new file mode 100644 index 0000000..ec6496c --- /dev/null +++ b/docs/api/ipam/index.md @@ -0,0 +1,2 @@ +#ipam +::: ipam \ No newline at end of file diff --git a/docs/api/organization/index.md b/docs/api/organization/index.md new file mode 100644 index 0000000..d6b1784 --- /dev/null +++ b/docs/api/organization/index.md @@ -0,0 +1,2 @@ +#organization +::: organization \ No newline at end of file diff --git a/docs/api/power/index.md b/docs/api/power/index.md new file mode 100644 index 0000000..a7d5d06 --- /dev/null +++ b/docs/api/power/index.md @@ -0,0 +1,2 @@ +#power +::: power \ No newline at end of file diff --git a/docs/api/virtualization/index.md b/docs/api/virtualization/index.md new file mode 100644 index 0000000..6f8b5f5 --- /dev/null +++ b/docs/api/virtualization/index.md @@ -0,0 +1,2 @@ +#virtualization +::: virtualization \ No newline at end of file diff --git a/docs/api/vpn/index.md b/docs/api/vpn/index.md new file mode 100644 index 0000000..4898afc --- /dev/null +++ b/docs/api/vpn/index.md @@ -0,0 +1,2 @@ +#vpn +::: vpn \ No newline at end of file diff --git a/docs/api/vpn/tunnel_groups.md b/docs/api/vpn/tunnel_groups.md new file mode 100644 index 0000000..b58a90a --- /dev/null +++ b/docs/api/vpn/tunnel_groups.md @@ -0,0 +1 @@ +::: vpn.tunnel_groups \ No newline at end of file diff --git a/docs/api/vpn/tunnel_grups.md b/docs/api/vpn/tunnel_grups.md deleted file mode 100644 index 42af2c6..0000000 --- a/docs/api/vpn/tunnel_grups.md +++ /dev/null @@ -1 +0,0 @@ -::: vpn.tunnel_grups \ No newline at end of file diff --git a/docs/api/wireless/index.md b/docs/api/wireless/index.md new file mode 100644 index 0000000..08eacae --- /dev/null +++ b/docs/api/wireless/index.md @@ -0,0 +1,2 @@ +#wireless +::: wireless \ No newline at end of file diff --git a/docs/contribute.md b/docs/contribute.md new file mode 100644 index 0000000..e69de29 diff --git a/mkdocs.yml b/mkdocs.yml index 69b35d8..87c0721 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -3,22 +3,59 @@ repo_url: https://github.com/Kaioguilherme1/netbox-client repo_name: Kaioguilherme1/netbox-client edit_uri: tree/main/docs/ - theme: name: material - language: pt-BR + language: en + features: + - search.suggest + - content.code.annotate + - content.code.copy + - content.code.select + - content.action.edit + - toc.follow + - content.tooltips + - navigation.indexes + - navigation.path + - navigation.footer palette: - primary: deep_blue + # Palette toggle for light mode + - media: "(prefers-color-scheme: light)" + scheme: default + toggle: + icon: material/toggle-switch-off-outline + name: Switch to dark mode + + # Palette toggle for dark mode + - media: "(prefers-color-scheme: dark)" + scheme: slate + toggle: + icon: material/toggle-switch + name: Switch to system preference markdown_extensions: - attr_list - + - admonition + - pymdownx.details + - pymdownx.superfences: + custom_fences: + - name: mermaid + class: mermaid + format: !!python/name:pymdownx.superfences.fence_code_format plugins: +- search - mkdocstrings: default_handler: python handlers: python: paths: [netboxcli] - - \ No newline at end of file + options: + show_symbol_type_heading: true + show_symbol_type_toc: true + show_source: false + show_inheritance_diagram: true + +extra: + social: + - icon: fontawesome/brands/github + link: https://github.com/Kaioguilherme1 \ No newline at end of file diff --git a/netboxCli/__init__.py b/netboxCli/__init__.py index bdd019b..423a293 100644 --- a/netboxCli/__init__.py +++ b/netboxCli/__init__.py @@ -1,4 +1,4 @@ -from . import core, virtualization +from . import core from .client import Client __all__ = ['Client', 'core'] diff --git a/netboxCli/circuits/__init__.py b/netboxCli/circuits/__init__.py index 677a79c..e826f67 100644 --- a/netboxCli/circuits/__init__.py +++ b/netboxCli/circuits/__init__.py @@ -12,7 +12,14 @@ class Circuits: Circuits module """ - def __init__(self, netbox): + def __init__(self, netbox: Core): + """ + Initialize the Circuits module. + + Args: + netbox: (Core) The Netbox object generated by the Client class. + """ + # circuits self.circuits = CircuitsClass(Core(netbox, '/api/circuits/circuits/')) self.circuit_types = CircuitTypes( diff --git a/netboxCli/client.py b/netboxCli/client.py index eebe8e0..4050475 100644 --- a/netboxCli/client.py +++ b/netboxCli/client.py @@ -52,9 +52,29 @@ def __init__(self, API_IP: str, API_TOKEN: str): } def _slug(self, text) -> str: + """ + Convert a text to a slug format. + + Args: + text (str): The text to be converted to a slug format. + + Returns: + str: The text in a slug format. + """ return text.lower().replace(' ', '-') def _request(self, method, endpoint, data=None) -> dict: + """ + Make a request to the Netbox API. + + Args: + method (str): The HTTP method to be used in the request. + endpoint (str): The endpoint to interact with in Netbox. + data (dict): The data to be sent in the request. + + Returns: + dict: Returns a dictionary with the status code and the request data in JSON format: {status: 200, data: {respondse}} + """ url = f'{self._base_url}{endpoint}' result = { 'status': None, @@ -79,6 +99,16 @@ def _request(self, method, endpoint, data=None) -> dict: return result def _get_id(self, name: str, endpoint: str) -> int: + """ + Get the ID of an object in Netbox by its name. + + Args: + name (str): The name of the object to get the ID. + endpoint (str): The endpoint to interact with in Netbox. + + Returns: + int: The ID of the object in Netbox. + """ if name: result = self._request('GET', endpoint) status = result['status'] diff --git a/netboxCli/connections/__init__.py b/netboxCli/connections/__init__.py index bd133c4..a12103a 100644 --- a/netboxCli/connections/__init__.py +++ b/netboxCli/connections/__init__.py @@ -9,7 +9,13 @@ class Connections: - def __init__(self, netbox): + def __init__(self, netbox: Core): + """ + Initialize the Connections module. + + Args: + netbox: (Core) The Netbox object generated by the Client class. + """ self.cables = Cables(Core(netbox, '/api/dcim/cables/')) self.wireless_links = WirelessLinks( Core(netbox, '/api/wireless/wireless-links/') diff --git a/netboxCli/core.py b/netboxCli/core.py index bb50d54..576a479 100644 --- a/netboxCli/core.py +++ b/netboxCli/core.py @@ -1,5 +1,16 @@ class Core: + """ + Core class for Netbox API + """ + def __init__(self, netbox: object, endpoint: str): + """ + Responsible for creating, getting, updating, and deleting resources in Netbox, obs. this class is not meant to be used directly, but to be inherited by other classes. + + Args: + netbox: (object) The Netbox object generated by the Client class. + endpoint: (str) The endpoint to interact with in Netbox. + """ self._netbox = netbox self._endpoint = endpoint diff --git a/netboxCli/customization/__init__.py b/netboxCli/customization/__init__.py index 3432df2..0e47df7 100644 --- a/netboxCli/customization/__init__.py +++ b/netboxCli/customization/__init__.py @@ -11,7 +11,17 @@ class Customization: - def __init__(self, netbox): + """ + Customization module + """ + + def __init__(self, netbox: Core): + """ + Initialize the Customization module. + + Args: + netbox: (Core) The Netbox object generated by the Client class. + """ # Customization self.custom_fields = CustomFields( Core(netbox, '/api/extras/custom-fields/') diff --git a/netboxCli/vpn/__init__.py b/netboxCli/vpn/__init__.py index cd04c1b..bb83aeb 100644 --- a/netboxCli/vpn/__init__.py +++ b/netboxCli/vpn/__init__.py @@ -15,15 +15,29 @@ class Vpn: def __init__(self, netbox): self.ike_policies = IkePolicies(Core(netbox, '/api/vpn/ike-policies/')) - self.ike_proposals = IkeProposals(Core(netbox, '/api/vpn/ike-proposals/')) - self.ipsec_policies = IpsecPolicies(Core(netbox, '/api/vpn/ipsec-policies/')) - self.ipsec_profiles = IpsecProfiles(Core(netbox, '/api/vpn/ipsec-profiles/')) - self.ipsec_proposals = IpsecProposals(Core(netbox, '/api/vpn/ipsec-proposals/')) - self.tunnel_groups = TunnelGroups(Core(netbox, '/api/vpn/tunnel-groups/')) - self.tunnel_terminations = Terminations(Core(netbox, '/api/vpn/tunnel-terminations/')) + self.ike_proposals = IkeProposals( + Core(netbox, '/api/vpn/ike-proposals/') + ) + self.ipsec_policies = IpsecPolicies( + Core(netbox, '/api/vpn/ipsec-policies/') + ) + self.ipsec_profiles = IpsecProfiles( + Core(netbox, '/api/vpn/ipsec-profiles/') + ) + self.ipsec_proposals = IpsecProposals( + Core(netbox, '/api/vpn/ipsec-proposals/') + ) + self.tunnel_groups = TunnelGroups( + Core(netbox, '/api/vpn/tunnel-groups/') + ) + self.tunnel_terminations = Terminations( + Core(netbox, '/api/vpn/tunnel-terminations/') + ) self.tunnels = Tunnels(Core(netbox, '/api/vpn/tunnels/')) self.l2vpns = L2vpns(Core(netbox, '/api/vpn/l2vpns/')) - self.terminations = Terminations(Core(netbox, '/api/vpn/l2vpn-terminations/')) + self.terminations = Terminations( + Core(netbox, '/api/vpn/l2vpn-terminations/') + ) __all__ = ['Vpn'] diff --git a/netboxCli/vpn/tunnel_groups.py b/netboxCli/vpn/tunnel_groups.py index 59ce9fb..031a1f0 100644 --- a/netboxCli/vpn/tunnel_groups.py +++ b/netboxCli/vpn/tunnel_groups.py @@ -19,7 +19,7 @@ def get( limit: int = 1000, ) -> dict: """ - Get a resource from the tunnel_grups on ID, name, tags, or search query. + Get a resource from the tunnel_groups on ID, name, tags, or search query. Args: id (int, optional): The ID of the resource to retrieve. @@ -34,28 +34,28 @@ def get( >>> nb = Client('http://localhost:8000', 'token') Get a resource by ID: - >>> result = nb.vpn.tunnel_grups.get(id=1) + >>> result = nb.vpn.tunnel_groups.get(id=1) Get a resource by name: - >>> result = nb.vpn.tunnel_grups.get(name="resource_name") + >>> result = nb.vpn.tunnel_groups.get(name="resource_name") Get resources by tags: - >>> result = nb.vpn.tunnel_grups.get(tags=["tag1", "tag2"]) + >>> result = nb.vpn.tunnel_groups.get(tags=["tag1", "tag2"]) Get resources by search query: - >>> result = nb.vpn.tunnel_grups.get(search="query") + >>> result = nb.vpn.tunnel_groups.get(search="query") Get all resources: - >>> result = nb.vpn.tunnel_grups.get() + >>> result = nb.vpn.tunnel_groups.get() Returns: - Returns a list with a status code and the request data in JSON format: {status: 200, data: {result: [list of tunnel_grups]}} + Returns a list with a status code and the request data in JSON format: {status: 200, data: {result: [list of tunnel_groups]}} """ return self._core.get(id, name, tags, search, limit) def create(self, data) -> dict: """ - Create a new resource in tunnel_grups using the provided data. + Create a new resource in tunnel_groups using the provided data. Args: data (dict): Data to create the resource. It should contain all the necessary information to create the resource. @@ -67,16 +67,16 @@ def create(self, data) -> dict: Create a new resource: >>> data = {'name': 'resource_name'} - >>> result = nb.vpn.tunnel_grups.create(data) + >>> result = nb.vpn.tunnel_groups.create(data) Returns: - dict: {'status': 200, 'data': {'result': tunnel_grups]}} + dict: {'status': 200, 'data': {'result': tunnel_groups]}} """ return self._core.create(data) def update(self, data) -> dict: """ - Update an existing resource in tunnel_grups with the provided data. + Update an existing resource in tunnel_groups with the provided data. Args: data (dict): Updated data for the resource. It should contain id and optional fields to update, to default fields find in https://demo.netbox.dev/api/ . @@ -88,7 +88,7 @@ def update(self, data) -> dict: Update a resource by ID: >>> data = {'id': 1, 'name': 'new_name'} - >>> result = nb.vpn.tunnel_grups.update(data) + >>> result = nb.vpn.tunnel_groups.update(data) """ return self._core.update(data) @@ -97,7 +97,7 @@ def delete(self, id: int) -> dict: Delete a resource based on its ID. Args: - id (int): ID of the resource in tunnel_grups to delete. + id (int): ID of the resource in tunnel_groups to delete. Examples: Create a new client: @@ -105,7 +105,7 @@ def delete(self, id: int) -> dict: >>> nb = Client('http://localhost:8000', 'token') Delete a resource by ID: - >>> result = nb.vpn.tunnel_grups.delete(id=1) + >>> result = nb.vpn.tunnel_groups.delete(id=1) Returns: [status_code, response] diff --git a/tests/test.py b/tests/test.py index e807638..219ed88 100644 --- a/tests/test.py +++ b/tests/test.py @@ -85,4 +85,3 @@ def test_get(obj, modules): modules = get_modules(client) # test_get(client, modules) -client.devices.devices.get(name='JPA')