Skip to content

Commit becfd7b

Browse files
Initial commit to test collection
1 parent ae28bd3 commit becfd7b

16 files changed

+2580
-2
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.tar.gz
2+
__pycache__/
3+
*.pyc
4+
tests/output/*

.travis.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
3+
language: python
4+
5+
python:
6+
- "3.6"
7+
8+
sudo: false
9+
10+
env:
11+
global:
12+
- COLLECTION_NAMESPACE: fragmentedpacket
13+
- COLLECTION_NAME: netbox_modules
14+
15+
install:
16+
- pip install -U pip
17+
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.3b0
18+
- cd ../../
19+
# This is due to ansible-test only being available within devel branch
20+
- git clone https://github.com/ansible/ansible.git
21+
- source ansible/hacking/env-setup
22+
23+
before_script:
24+
- mkdir -p ~/ansible_collections/$COLLECTION_NAMESPACE
25+
- mv FragmentedPacket/$COLLECTION_NAME ~/ansible_collections/$COLLECTION_NAMESPACE/$COLLECTION_NAME
26+
- cd ~/ansible_collections/$COLLECTION_NAMESPACE/$COLLECTION_NAME
27+
28+
script:
29+
- ansible-test units --python 3.6
30+
- black . --check

README.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,66 @@
1-
# netbox_modules
2-
Netbox modules for Ansible using Ansible Collections
1+
# Netbox modules for Ansible using Ansible Collections
2+
3+
**THIS IS A WIP DUE TO COLLECTIONS BEING IN TECH REVIEW CURRENTLY (Ansible 2.8) BUT WILL BE UPDATED AS NECESSARY AS COLLECTIONS MATURES**
4+
5+
TODO:
6+
- ~~initial build and changes to properly import module_utils~~
7+
- Add testing to a CI (Travis?) and existing unit tests
8+
- ~~Initial unit tests~~
9+
- Test different Python versions (3.6/3.7)
10+
- Local testing using Tox
11+
- Test currently supported versions of Ansible
12+
- ~~Add documentation on how to use to README~~
13+
- Add documentation on how to contribute to collection
14+
15+
## Requirements
16+
- Ansible 2.9+
17+
- Or run Ansible via source
18+
- Python 3.6+
19+
20+
## Existing Modules
21+
22+
- netbox_device
23+
- netbox_interface
24+
- netbox_ip_address
25+
- netbox_prefix
26+
- netbox_site
27+
28+
## How to Use
29+
- Install via Mazer
30+
- `mazer install fragmentedpacket.netbox_modules`
31+
- Follow Ansible instructions to use the collection:
32+
- [https://docs.ansible.com/ansible/devel/dev_guide/collections_tech_preview.html#using-collections](https://docs.ansible.com/ansible/devel/dev_guide/collections_tech_preview.html#using-collections)
33+
34+
#### Example playbook
35+
```yaml
36+
- name: "Test Netbox modules"
37+
connection: local
38+
hosts: localhost
39+
gather_facts: False
40+
collections:
41+
- fragmentedpacket.netbox_modules
42+
43+
tasks:
44+
- name: Create device within Netbox with only required information
45+
netbox_device:
46+
netbox_url: http://netbox-demo.org:32768
47+
netbox_token: 0123456789abcdef0123456789abcdef01234567
48+
data:
49+
name: Test66
50+
device_type: Cisco Test
51+
device_role: Core Switch
52+
site: Test Site
53+
status: Staged
54+
state: present
55+
```
56+
57+
## How to Contribute
58+
**Will be updated with diagram and more information needed to contribute**
59+
60+
If looking to contribute before this is updated, please review the code and hopefully it is enough to get started on adding new modules and how the new modules should be structured.
61+
62+
#### Testing
63+
64+
1. Please update `tests/unit/module_utils/test_netbox_base_class.py` if editing anything within the base class that needs to be tested.
65+
2. Run `black .` within the base directory for black formatting as it's required for tests to pass
66+
3. Check necessary dependencies defined within `.travis.yml` for now if you're wanting to test locally

galaxy.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
### REQUIRED
2+
3+
# The namespace of the collection. This can be a company/brand/organization or product namespace under which all
4+
# content lives. May only contain alphanumeric characters and underscores. Additionally namespaces cannot start with
5+
# underscores or numbers and cannot contain consecutive underscores
6+
namespace: fragmentedpacket
7+
8+
# The name of the collection. Has the same character restrictions as 'namespace'
9+
name: netbox_modules
10+
11+
# The version of the collection. Must be compatible with semantic versioning
12+
version: 0.0.6
13+
14+
# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
15+
readme: README.md
16+
17+
# A list of the collection's content authors. Can be just the name or in the format 'Full Name <email> (url)
18+
# @nicks:irc/im.site#channel'
19+
authors:
20+
- Mikhail Yohman <mikhail.yohman@gmail.com>
21+
22+
23+
### OPTIONAL but strongly recommended
24+
25+
# A short summary description of the collection
26+
description: This is a collection of Netbox Ansible modules
27+
28+
# Either a single license or a list of licenses for content inside of a collection. Ansible Galaxy currently only
29+
# accepts L(SPDX,https://spdx.org/licenses/) licenses. This key is mutually exclusive with 'license_file'
30+
license:
31+
- MIT
32+
33+
# The path to the license file for the collection. This path is relative to the root of the collection. This key is
34+
# mutually exclusive with 'license'
35+
license_file: ''
36+
37+
# A list of tags you want to associate with the collection for indexing/searching. A tag name has the same character
38+
# requirements as 'namespace' and 'name'
39+
tags:
40+
- system
41+
- ipam
42+
- networking
43+
44+
# Collections that this collection requires to be installed for it to be usable. The key of the dict is the
45+
# collection label 'namespace.name'. The value is a version range
46+
# L(specifiers,https://python-semanticversion.readthedocs.io/en/latest/#requirement-specification). Multiple version
47+
# range specifiers can be set and are separated by ','
48+
dependencies: {}
49+
50+
# The URL of the originating SCM repository
51+
repository: https://github.com/FragmentedPacket/netbox_modules
52+
53+
# The URL to any online docs
54+
documentation:
55+
56+
# The URL to the homepage of the collection/project
57+
homepage:
58+
59+
# The URL to the collection issue tracker
60+
issues: https://github.com/FragmentedPacket/netbox_modules/issues

plugins/module_utils/__init__.py

Whitespace-only changes.

plugins/module_utils/netbox_dcim.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright: (c) 2018, Mikhail Yohman (@fragmentedpacket) <mikhail.yohman@gmail.com>
3+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4+
from __future__ import absolute_import, division, print_function
5+
6+
__metaclass__ = type
7+
8+
# Import necessary packages
9+
import traceback
10+
from ansible.module_utils.basic import missing_required_lib
11+
12+
try:
13+
from ansible_collections.fragmentedpacket.netbox_modules.plugins.module_utils.netbox_utils import (
14+
NetboxModule,
15+
ENDPOINT_NAME_MAPPING,
16+
)
17+
except ImportError:
18+
import sys
19+
20+
sys.path.append(".")
21+
from netbox_utils import NetboxModule, ENDPOINT_NAME_MAPPING
22+
23+
24+
NB_DEVICES = "devices"
25+
NB_INTERFACES = "interfaces"
26+
NB_SITES = "sites"
27+
28+
29+
class NetboxDcimModule(NetboxModule):
30+
def __init__(self, module, endpoint):
31+
super().__init__(module, endpoint)
32+
33+
def run(self):
34+
"""
35+
This function should have all necessary code for endpoints within the application
36+
to create/update/delete the endpoint objects
37+
Supported endpoints:
38+
- devices
39+
- interfaces
40+
- sites
41+
"""
42+
# Used to dynamically set key when returning results
43+
endpoint_name = ENDPOINT_NAME_MAPPING[self.endpoint]
44+
45+
self.result = {"changed": False}
46+
47+
application = self._find_app(self.endpoint)
48+
nb_app = getattr(self.nb, application)
49+
nb_endpoint = getattr(nb_app, self.endpoint)
50+
51+
data = self.data
52+
53+
# Used for msg output
54+
name = data.get("name")
55+
56+
object_query_params = self._build_query_params(endpoint_name, data)
57+
try:
58+
self.nb_object = nb_endpoint.get(**object_query_params)
59+
except ValueError:
60+
self._handle_errors(msg="More than one result returned for %s" % (name))
61+
62+
if self.state == "present":
63+
self._ensure_object_exists(nb_endpoint, endpoint_name, name, data)
64+
65+
elif self.state == "absent":
66+
self._ensure_object_absent(endpoint_name, name)
67+
68+
try:
69+
serialized_object = self.nb_object.serialize()
70+
except AttributeError:
71+
serialized_object = self.nb_object
72+
73+
self.result.update({endpoint_name: serialized_object})
74+
75+
self.module.exit_json(**self.result)

0 commit comments

Comments
 (0)