Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds VM Interface Role. #415

Merged
merged 22 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions plugins/modules/vm_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
required: false
type: raw
version_added: "3.0.0"
role:
description:
- The role of the interface
required: false
type: raw
version_added: "5.3.0"
"""

EXAMPLES = r"""
Expand Down Expand Up @@ -125,6 +131,7 @@
- name: VoIP
location: "{{ test_location['key'] }}"
mtu: 1600
role: Server
mode: Tagged
state: present

Expand Down Expand Up @@ -184,6 +191,7 @@ def main():
mode=dict(required=False, type="raw"),
untagged_vlan=dict(required=False, type="raw"),
tagged_vlans=dict(required=False, type="raw"),
role=dict(required=False, type="raw"),
)
)

Expand Down
2 changes: 1 addition & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def is_truthy(arg):
namespace.configure(
{
"nautobot_ansible": {
"nautobot_ver": "2.0.0",
"nautobot_ver": "2.3.1",
jvanderaa marked this conversation as resolved.
Show resolved Hide resolved
"project_name": "nautobot_ansible",
"python_ver": "3.10",
"local": False,
Expand Down
18 changes: 17 additions & 1 deletion tests/integration/nautobot-populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
__metaclass__ = type

import os
import logging
jvanderaa marked this conversation as resolved.
Show resolved Hide resolved
import sys
import pynautobot
from packaging import version

# NOTE: If anything depends on specific versions of Nautobot, can check INTEGRATION_TESTS in env
# os.environ["INTEGRATION_TESTS"]

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Set nb variable to connect to Nautobot and use the variable in future calls
nb_host = os.getenv("NAUTOBOT_URL", "http://nautobot:8000")
Expand Down Expand Up @@ -48,7 +51,7 @@ def make_nautobot_calls(endpoint, payload):
create_tags = make_nautobot_calls(
nb.extras.tags,
[
{"name": "First", "content_types": ["dcim.device", "ipam.routetarget", "dcim.controller"]},
{"name": "First", "content_types": ["dcim.device", "ipam.routetarget"]},
{"name": "Second", "content_types": ["dcim.device", "ipam.routetarget"]},
{"name": "Third", "content_types": ["dcim.device"]},
{
Expand Down Expand Up @@ -251,6 +254,8 @@ def make_nautobot_calls(endpoint, payload):
{"name": "Test Controller Role", "color": "e91e65", "vm_role": False, "content_types": ["dcim.controller"]},
]
created_device_roles = make_nautobot_calls(nb.extras.roles, device_roles)
print(created_device_roles)
logger.debug("Device Roles: %s", created_device_roles)
# Device role variables to be used later on
core_switch = nb.extras.roles.get(name="Core Switch")

Expand Down Expand Up @@ -614,6 +619,17 @@ def make_nautobot_calls(endpoint, payload):
contacts = [{"name": "My Contact"}, {"name": "My Contact 2"}]
created_contacts = make_nautobot_calls(nb.extras.contacts, contacts)

###############
# v2.3+ items #
###############
if nautobot_version >= version.parse("2.3"):
# Create role for virtual machine interfaces
logger.debug("Creating VM Interface Role")
vm_interface_roles = [
{"name": "Test VM Interface Role", "color": "aa1409", "vm_role": False, "content_types": ["virtualization.vminterface"]},
]
created_device_roles = make_nautobot_calls(nb.extras.roles, vm_interface_roles)
jvanderaa marked this conversation as resolved.
Show resolved Hide resolved
logger.debug("VM Interface Role created")
jvanderaa marked this conversation as resolved.
Show resolved Hide resolved

if ERRORS:
sys.exit("Errors have occurred when creating objects, and should have been printed out. Check previous output.")
1 change: 0 additions & 1 deletion tests/integration/targets/latest/tasks/device.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
role: "{{ lookup('networktocode.nautobot.lookup', 'roles', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Core Switch\"') }}"
vc1: "{{ lookup('networktocode.nautobot.lookup', 'virtual-chassis', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=VC1') }}"
staged: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Staged') }}"
active: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Active') }}"
device_redundancy_group: "{{ lookup('networktocode.nautobot.lookup', 'device-redundancy-groups', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"My Device Redundancy Group\"') }}"

- name: "1 - Device with required information"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
##
##
- set_fact:
active: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Active') }}"
secrets_group: "{{ lookup('networktocode.nautobot.lookup', 'secrets-groups', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Test Secrets Group\"') }}"

- name: "1 - Create device redundancy group within Nautobot with only required information"
Expand Down
1 change: 0 additions & 1 deletion tests/integration/targets/latest/tasks/location.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
- set_fact:
parent_location_type: "{{ lookup('networktocode.nautobot.lookup', 'location-types', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"My Parent Location Type\"') }}"
child_location_type: "{{ lookup('networktocode.nautobot.lookup', 'location-types', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"My Child Location Type\"') }}"
active: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Active') }}"

- name: "1 - Create location within Nautobot with only required information"
networktocode.nautobot.location:
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/targets/latest/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
---
- name: "Set Active Fact"
jvanderaa marked this conversation as resolved.
Show resolved Hide resolved
set_fact:
active: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Active') }}"

# Lookup tests should run first so items created by other tests don't influence the results
- name: "PYNAUTOBOT_LOOKUP TESTS"
include_tasks:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
##
##
- set_fact:
active: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Active') }}"
planned: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Planned') }}"

- name: "BGP ASN 1: Creation"
Expand Down
1 change: 0 additions & 1 deletion tests/integration/targets/latest/tasks/prefix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
tag_schnozzberry: "{{ lookup('networktocode.nautobot.lookup', 'tags', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Schnozzberry') }}"
vlan_group: "{{ lookup('networktocode.nautobot.lookup', 'vlan-groups', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Test Vlan Group\"') }}"
reserved: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Reserved') }}"
active: "{{ lookup('networktocode.nautobot.lookup', 'statuses', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Active') }}"
global_namespace: "{{ lookup('networktocode.nautobot.lookup', 'namespaces', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Global') }}"
private_namespace: "{{ lookup('networktocode.nautobot.lookup', 'namespaces', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=Private') }}"

Expand Down
163 changes: 163 additions & 0 deletions tests/integration/targets/latest/tasks/vm_interface.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,166 @@
- test_five['interface']['tagged_vlans']|length == 2
- test_five['interface']['tags'][0] == tag_schnozzberry['key']
- test_five['msg'] == "interface Eth0 updated"

- name: "NAUTOBOT 2.3+ TESTS"
when:
- "nautobot_version is version('2.3', '>=')"
block:
- name: SET ADDITIONAL FACTS
set_fact:
vm_interface_role: "{{ lookup('networktocode.nautobot.lookup', 'roles', api_endpoint=nautobot_url, token=nautobot_token, api_filter='name=\"Test VM Interface Role\"' )}}"

- name: "PYNAUTOBOT_VM_INTERFACE 11: Necessary info creation"
networktocode.nautobot.vm_interface:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
virtual_machine: "test100-vm"
name: "Eth10"
status: "Active"
state: present
register: test_eleven

- name: "PYNAUTOBOT_VM_INTERFACE 11: ASSERT - Necessary info creation"
assert:
that:
- test_eleven is changed
- test_eleven['diff']['before']['state'] == "absent"
- test_eleven['diff']['after']['state'] == "present"
- test_eleven['interface']['name'] == "Eth10"
- test_eleven['interface']['virtual_machine'] == vm100['key']
- test_eleven['msg'] == "interface Eth10 created"

- name: "PYNAUTOBOT_VM_INTERFACE 12: Create duplicate"
networktocode.nautobot.vm_interface:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
virtual_machine: "test100-vm"
name: "Eth10"
status: "Active"
state: present
register: test_twelve

- name: "PYNAUTOBOT_VM_INTERFACE 12: ASSERT - Create duplicate"
assert:
that:
- not test_twelve['changed']
- test_twelve['interface']['name'] == "Eth10"
- test_twelve['interface']['virtual_machine'] == vm100['key']
- test_twelve['msg'] == "interface Eth10 already exists"

- name: "PYNAUTOBOT_VM_INTERFACE 13: Updated"
networktocode.nautobot.vm_interface:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
virtual_machine: "test100-vm"
name: "Eth10"
enabled: false
mtu: 9000
mac_address: "00:00:00:AA:AA:01"
description: "Updated test100-vm"
mode: Tagged
role: "Test VM Role"
jvanderaa marked this conversation as resolved.
Show resolved Hide resolved
untagged_vlan:
name: Wireless
location: "Child Test Location"
tagged_vlans:
- name: Data
location: "Child Test Location"
- name: VoIP
location: "Child Test Location"
tags:
- "Schnozzberry"
state: present
register: test_thirteen

- name: "PYNAUTOBOT_VM_INTERFACE 13: ASSERT - Updated"
assert:
that:
- test_thirteen is changed
- test_thirteen['diff']['after']['enabled'] == false
- test_thirteen['diff']['after']['mtu'] == 9000
- test_thirteen['diff']['after']['mac_address'] == "00:00:00:AA:AA:01"
- test_thirteen['diff']['after']['description'] == "Updated test100-vm"
- test_thirteen['diff']['after']['mode'] == "tagged"
- test_thirteen['diff']['after']['untagged_vlan'] == wireless['key']
- test_thirteen['diff']['after']['tagged_vlans']|length == 2
- test_thirteen['diff']['after']['tags'][0] == tag_schnozzberry['key']
- test_thirteen['interface']['name'] == "Eth10"
- test_thirteen['interface']['status'] == active['key']
- test_thirteen['interface']['virtual_machine'] == vm100['key']
- test_thirteen['interface']['enabled'] == false
- test_thirteen['interface']['mtu'] == 9000
- test_thirteen['interface']['mac_address'] == "00:00:00:AA:AA:01"
- test_thirteen['interface']['description'] == "Updated test100-vm"
- test_thirteen['interface']['mode'] == "tagged"
- test_thirteen['interface']['untagged_vlan'] == wireless['key']
- test_thirteen['interface']['tagged_vlans']|length == 2
- test_thirteen['interface']['tags'][0] == tag_schnozzberry['key']
- test_thirteen['interface']['role'] == vm_interface_role['key']
- test_thirteen['msg'] == "interface Eth10 updated"

- name: "PYNAUTOBOT_VM_INTERFACE 14: ASSERT - Delete"
networktocode.nautobot.vm_interface:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
name: "Eth10"
virtual_machine: "test100-vm"
state: absent
register: test_fourteen

- name: "PYNAUTOBOT_VM_INTERFACE 14: ASSERT - Delete"
assert:
that:
- test_fourteen is changed
- test_fourteen['interface']['name'] == "Eth10"
- test_fourteen['interface']['virtual_machine'] == vm100['key']
- test_fourteen['msg'] == "interface Eth10 deleted"

- name: "PYNAUTOBOT_VM_INTERFACE 15: Attempt to update interface with same name on other VMs"
networktocode.nautobot.vm_interface:
url: "{{ nautobot_url }}"
token: "{{ nautobot_token }}"
virtual_machine: "test100-vm"
name: "Eth0"
status: "Active"
enabled: false
mtu: 9000
mac_address: "00:00:00:AA:AA:01"
description: "Updated test100-vm Eth0 intf"
mode: Tagged
untagged_vlan:
name: Wireless
location: "Child Test Location"
tagged_vlans:
- name: Data
location: "Child Test Location"
- name: VoIP
location: "Child Test Location"
tags:
- "Schnozzberry"
state: present
register: test_fifteen

- name: "PYNAUTOBOT_VM_INTERFACE 15: ASSERT - Updated"
assert:
that:
- test_fifteen is changed
- test_fifteen['diff']['after']['enabled'] == false
- test_fifteen['diff']['after']['mtu'] == 9000
- test_fifteen['diff']['after']['mac_address'] == "00:00:00:AA:AA:01"
- test_fifteen['diff']['after']['description'] == "Updated test100-vm Eth0 intf"
- test_fifteen['diff']['after']['mode'] == "tagged"
- test_fifteen['diff']['after']['untagged_vlan'] == wireless['key']
- test_fifteen['diff']['after']['tagged_vlans']|length == 2
- test_fifteen['diff']['after']['tags'][0] == tag_schnozzberry['key']
- test_fifteen['interface']['name'] == "Eth0"
- test_fifteen['interface']['virtual_machine'] == vm100['key']
- test_fifteen['interface']['enabled'] == false
- test_fifteen['interface']['mtu'] == 9000
- test_fifteen['interface']['mac_address'] == "00:00:00:AA:AA:01"
- test_fifteen['interface']['description'] == "Updated test100-vm Eth0 intf"
- test_fifteen['interface']['mode'] == "tagged"
- test_fifteen['interface']['untagged_vlan'] == wireless['key']
- test_fifteen['interface']['tagged_vlans']|length == 2
- test_fifteen['interface']['tags'][0] == tag_schnozzberry['key']
- test_fifteen['msg'] == "interface Eth0 updated"
Loading