Skip to content

Commit

Permalink
fix(plugin): correct plugin to use inventory file
Browse files Browse the repository at this point in the history
!3 closes #3
  • Loading branch information
jon-nfc committed Jul 31, 2024
1 parent fab650b commit 419dc5c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
build
pages
inventory/*
64 changes: 53 additions & 11 deletions plugins/inventory/centurion.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,62 @@
token:
required: false
description:
- NetBox API token to be able to read against NetBox.
- Centurion API token to be able to read against Centurion.
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
env:
- name: VALIDATE_CENTURION_CERTS
author:
- jon @jon_nfc
'''



class InventoryModule(BaseInventoryPlugin):
EXAMPLES = """
# inventory.yml file in YAML format
NAME = 'nofusscomputing.centurion.centurion'
plugin: nofusscomputing.centurion.centurion
api_endpoint: http://localhost:8000
token: <token value here>
validate_certs: false
def __init__(self):
super().__init__()
self.headers = {
'Authorization': 'Token 7d501c956363e60df0cfd770db36f54226f079d707346f776ab31d5f40d16542'
}
# Example Ansible Tower credential Input Configuration:
fields:
- id: CENTURION_API
type: string
label: Centurion Host URL
- id: CENTURION_TOKEN
type: string
label: Centurion API Token
secret: true
- id: VALIDATE_CENTURION_CERTS
type: boolean
required:
- CENTURION_API
- CENTURION_TOKEN
# Example Ansible Tower credential Injector Configuration:
env:
CENTURION_API: '{{ CENTURION_API }}'
CENTURION_TOKEN: '{{ CENTURION_TOKEN }}'
VALIDATE_CENTURION_CERTS: '{{ VALIDATE_CENTURION_CERTS | default(true) }}'
"""



class InventoryModule(BaseInventoryPlugin):

NAME = 'nofusscomputing.centurion.centurion'


def verify_file(self, path):
Expand All @@ -62,9 +93,9 @@ def fetch_devices(self) -> dict:
self.display.v("Fetching Devices ")

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

devices = json.loads(to_text(response.read()))['results']
Expand All @@ -81,7 +112,7 @@ def fetch_device_config(self, url: str) -> dict:
response = open_url(
url = url,
headers = self.headers,
validate_certs=False,
validate_certs = self.validate_certs,
)

config = json.loads(to_text(response.read()))
Expand All @@ -92,6 +123,17 @@ def fetch_device_config(self, url: str) -> dict:
def parse(self, inventory, loader, path, cache=True):
super().parse(inventory, loader, path)

self._read_config_data(path=path)


self.api_endpoint = self.get_option("api_endpoint").strip("/")
self.token = self.get_option("token")
self.validate_certs = self.get_option("validate_certs")

self.headers = {
'Authorization': f'Token {self.token}'
}

devices = self.fetch_devices()

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

0 comments on commit 419dc5c

Please sign in to comment.