-
Notifications
You must be signed in to change notification settings - Fork 91
feat: Add a way to assign a device to a position in a rack #375
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
Open
kuchosauronad0
wants to merge
1
commit into
Solvik:master
Choose a base branch
from
kuchosauronad0:feat/position
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| from netbox_agent.config import netbox_instance as nb | ||
| from netbox_agent.hypervisor import Hypervisor | ||
| from netbox_agent.inventory import Inventory | ||
| from netbox_agent.location import Datacenter, Rack, Tenant | ||
| from netbox_agent.location import Datacenter, Rack, Position, Tenant | ||
| from netbox_agent.misc import ( | ||
| create_netbox_tags, | ||
| get_device_role, | ||
|
|
@@ -123,6 +123,10 @@ def get_rack(self): | |
| rack = Rack() | ||
| return rack.get() | ||
|
|
||
| def get_position(self): | ||
| position = Position() | ||
| return position.get() | ||
|
|
||
| def get_netbox_rack(self): | ||
| rack = self.get_rack() | ||
| datacenter = self.get_netbox_datacenter() | ||
|
|
@@ -136,6 +140,17 @@ def get_netbox_rack(self): | |
| name=rack, | ||
| site_id=datacenter.id, | ||
| ) | ||
| def get_netbox_position(self): | ||
| position = self.get_position() | ||
| logging.debug("Get_position: {position}".format(position=position)) | ||
| datacenter = self.get_netbox_datacenter() | ||
| if not position: | ||
| return None | ||
| if position and not datacenter: | ||
| logging.error("Can't get position if no datacenter is configured or found") | ||
| sys.exit(1) | ||
|
|
||
| return position | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is supposed to return the position currently set in Netbox, rather than the actual, expected one, to be used in the diff checks around lines 480-530. |
||
|
|
||
| def get_product_name(self): | ||
| """ | ||
|
|
@@ -191,7 +206,7 @@ def get_power_consumption(self): | |
| def get_expansion_product(self): | ||
| raise NotImplementedError | ||
|
|
||
| def _netbox_create_chassis(self, datacenter, tenant, rack): | ||
| def _netbox_create_chassis(self, datacenter, tenant, rack, position): | ||
| device_type = get_device_type(self.get_chassis()) | ||
| device_role = get_device_role(config.device.chassis_role) | ||
| serial = self.get_chassis_service_tag() | ||
|
|
@@ -204,12 +219,13 @@ def _netbox_create_chassis(self, datacenter, tenant, rack): | |
| site=datacenter.id if datacenter else None, | ||
| tenant=tenant.id if tenant else None, | ||
| rack=rack.id if rack else None, | ||
| position=position.id if position else None, | ||
| tags=[{"name": x} for x in self.tags], | ||
| custom_fields=self.custom_fields, | ||
| ) | ||
| return new_chassis | ||
|
|
||
| def _netbox_create_blade(self, chassis, datacenter, tenant, rack): | ||
| def _netbox_create_blade(self, chassis, datacenter, tenant, rack, position): | ||
| device_role = get_device_role(config.device.blade_role) | ||
| device_type = get_device_type(self.get_product_name()) | ||
| serial = self.get_service_tag() | ||
|
|
@@ -267,7 +283,7 @@ def _netbox_deduplicate_server(self, purge): | |
| server.serial = serial | ||
| server.save() | ||
|
|
||
| def _netbox_create_server(self, datacenter, tenant, rack): | ||
| def _netbox_create_server(self, datacenter, tenant, rack, position): | ||
| device_role = get_device_role(config.device.server_role) | ||
| device_type = get_device_type(self.get_product_name()) | ||
| if not device_type: | ||
|
|
@@ -288,6 +304,8 @@ def _netbox_create_server(self, datacenter, tenant, rack): | |
| site=datacenter.id if datacenter else None, | ||
| tenant=tenant.id if tenant else None, | ||
| rack=rack.id if rack else None, | ||
| position=position if position else None, | ||
| face=face if face else "front", | ||
| tags=[{"name": x} for x in self.tags], | ||
| ) | ||
| return new_server | ||
|
|
@@ -393,6 +411,7 @@ def netbox_create_or_update(self, config): | |
| datacenter = self.get_netbox_datacenter() | ||
| rack = self.get_netbox_rack() | ||
| tenant = self.get_netbox_tenant() | ||
| position = self.get_netbox_position() | ||
|
|
||
| if config.update_old_devices: | ||
| self._netbox_deduplicate_server(purge=False) | ||
|
|
@@ -404,18 +423,18 @@ def netbox_create_or_update(self, config): | |
| chassis = nb.dcim.devices.get(serial=self.get_chassis_service_tag()) | ||
| # Chassis does not exist | ||
| if not chassis: | ||
| chassis = self._netbox_create_chassis(datacenter, tenant, rack) | ||
| chassis = self._netbox_create_chassis(datacenter, tenant, rack, position) | ||
|
|
||
| server = nb.dcim.devices.get(serial=self.get_service_tag()) | ||
| if not server: | ||
| server = self._netbox_create_blade(chassis, datacenter, tenant, rack) | ||
| server = self._netbox_create_blade(chassis, datacenter, tenant, rack, position) | ||
|
|
||
| # Set slot for blade | ||
| self._netbox_set_or_update_blade_slot(server, chassis, datacenter) | ||
| else: | ||
| server = nb.dcim.devices.get(serial=self.get_service_tag()) | ||
| if not server: | ||
| server = self._netbox_create_server(datacenter, tenant, rack) | ||
| server = self._netbox_create_server(datacenter, tenant, rack, position) | ||
|
|
||
| logging.debug("Updating Server...") | ||
| # check network cards | ||
|
|
@@ -522,9 +541,11 @@ def print_debug(self): | |
| print("Datacenter:", self.get_datacenter()) | ||
| print("Netbox Datacenter:", self.get_netbox_datacenter()) | ||
| print("Rack:", self.get_rack()) | ||
| print("Netbox Rack:", self.get_netbox_rack()) | ||
| print("Position:", self.get_position()) | ||
| print("Is blade:", self.is_blade()) | ||
| print("Got expansion:", self.own_expansion_slot()) | ||
| print("Netbox Rack:", self.get_netbox_rack()) | ||
| print("Netbox Position:", self.get_netbox_position()) | ||
| print("Product Name:", self.get_product_name()) | ||
| print("Platform:", self.device_platform) | ||
| print("Chassis:", self.get_chassis()) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just nitpicking on style but you could use
maxsplit