Skip to content

Commit

Permalink
feat(plugin): Centurion ERP inventory plugin
Browse files Browse the repository at this point in the history
!2 closes #3
  • Loading branch information
jon-nfc committed Jul 31, 2024
1 parent b8e42da commit fab650b
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
build
pages
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Contirbution Guide

We welcome contributions.


## Inventory Plugin

``` bash

# to test use
ansible-inventory -i nofusscomputing.itsm.centurion --list -vvv

```
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ links:

- [Merge Requests (Pull Requests)](https://gitlab.com/nofusscomputing/projects/ansible/collections/centurion_erp_collection/-/merge_requests)

This Ansible collection is a companion collection for [Centurion ERP](https://nofusscomputing.com/projects/centurion_erp/)


## Contributing
Expand Down
5 changes: 5 additions & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[defaults]
collections_path= ~/git/ansible_collections

[inventory]
enable_plugins = nofusscomputing.centurion.centurion
8 changes: 6 additions & 2 deletions docs/projects/ansible/collection/centurion/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ about: https://gitlab.com/nofusscomputing/projects/ansible/collections/kubernete
![Project Status - Active](https://img.shields.io/badge/Project%20Status-Active-green?logo=gitlab&style=plastic)



![Gitlab build status - stable](https://img.shields.io/badge/dynamic/json?color=ff782e&label=Build&query=0.status&url=https%3A%2F%2Fgitlab.com%2Fapi%2Fv4%2Fprojects%2F59504579%2Fpipelines%3Fref%3Dmaster&logo=gitlab&style=plastic) ![Gitlab build status - development](https://img.shields.io/badge/dynamic/json?color=ff782e&label=Build&query=0.status&url=https%3A%2F%2Fgitlab.com%2Fapi%2Fv4%2Fprojects%2F59504579%2Fpipelines%3Fref%3Ddevelopment&logo=gitlab&style=plastic)


Expand All @@ -21,4 +20,9 @@ about: https://gitlab.com/nofusscomputing/projects/ansible/collections/kubernete

</span>

This Ansible Collection is intended to compliement Centurion ERP.
This Ansible Collection is intended to compliement [Centurion ERP](../../../centurion_erp/index.md).


## Features

- [Inventory plugin](./plugins/inventory.md)
11 changes: 11 additions & 0 deletions docs/projects/ansible/collection/centurion/plugins/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Plugins
description: No Fuss Computings Companion Ansible Collection Centurion Plugins.
date: 2024-07-31
template: project.html
about: https://gitlab.com/nofusscomputing/projects/ansible/collections/kubernetes
---

Available plugins include:

- [Inventory](./inventory.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Inventory
description: No Fuss Computings Companion Ansible Collection Centurion Inventory Plugin.
date: 2024-07-31
template: project.html
about: https://gitlab.com/nofusscomputing/projects/ansible/collections/kubernetes
---

This inventory plugin obtains the data required from [Centurion ERP](../../../../centurion_erp/index.md) to build up an ansible inventory for all devices.
Empty file.
6 changes: 6 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ nav:

- projects/ansible/collection/centurion/index.md

- Plugins:

- projects/ansible/collection/centurion/plugins/index.md

- projects/ansible/collection/centurion/plugins/inventory.md


- Operations:

Expand Down
111 changes: 111 additions & 0 deletions plugins/inventory/centurion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import json

from ansible.module_utils.common.text.converters import to_text
from ansible.module_utils.urls import open_url
from ansible.plugins.inventory import BaseInventoryPlugin


DOCUMENTATION = '''
---
module: centurion
plugin_type: inventory
short_description: Centurion ERP Inventory
description:
- "An Inventory Plugin to fetch inventory from Centurion ERP."
options:
plugin:
description: token that ensures this is a source file for the 'centurion' plugin.
required: True
choices: ['nofusscomputing.centurion.centurion']
api_endpoint:
description: Endpoint of the Centurion API
required: true
env:
- name: CENTURION_API
token:
required: false
description:
- NetBox API token to be able to read against NetBox.
env:
- name: CENTURION_TOKEN
validate_certs:
description:
- Allows skipping of validation of SSL certificates. Set to C(false) to disable certificate validation.
default: true
type: boolean
author:
- jon @jon_nfc
'''



class InventoryModule(BaseInventoryPlugin):

NAME = 'nofusscomputing.centurion.centurion'


def __init__(self):
super().__init__()

self.headers = {
'Authorization': 'Token 7d501c956363e60df0cfd770db36f54226f079d707346f776ab31d5f40d16542'
}


def verify_file(self, path):

return True


def fetch_devices(self) -> dict:

self.display.v("Fetching Devices ")

response = open_url(
url = 'https://test.nofusscomputing.com/api/device/',
headers = self.headers,
validate_certs=False,
)

devices = json.loads(to_text(response.read()))['results']


return devices


def fetch_device_config(self, url: str) -> dict:


self.display.vv(f"Fetching Device Config {url} ")

response = open_url(
url = url,
headers = self.headers,
validate_certs=False,
)

config = json.loads(to_text(response.read()))

return config


def parse(self, inventory, loader, path, cache=True):
super().parse(inventory, loader, path)

devices = self.fetch_devices()

self.display.v(f"Parsing returned devices")

for device in devices:


self.display.vv(f"Adding device {device['name']} to inventory")


self.inventory.add_host(host=device['name'])

config = self.fetch_device_config(device['config'])

for key, val in config.items():

self.inventory.set_variable(device['name'], key, val)

0 comments on commit fab650b

Please sign in to comment.