Repository: netbox-community/netbox
Pull request: Fixes: #18433 - Fix missing is_primary property on MACAddress model
### Fixes: #18433 - Add missing is_primary property on MACAddress model

* Added missing `is_primary` property method on `MACAddress` model


Problem statement:
MAC address not shown as "primary for interface" in MAC address detail view
### Deployment Type

Self-hosted

### NetBox Version

v4.2.2

### Python Version

3.12

### Steps to Reproduce

1. Create a device (device1) (any device type)
2. Create an interface (eth1) (any type)
3. Create a MAC address (AA:AB:CC:DD:EE:FF) and assign it to the device1 and interface eth1
4. Go to interface eth1 on device1 and edit the device
5. Assign the MAC address AA:AB:CC:DD:EE:FF as the primary MAC address
6. Go to the MAC address view and select the AA:AB:CC:DD:EE:FF address

### Expected Behavior

In the detail view I expect the "Primary for interface" to be a green checkmark. 

### Observed Behavior

It's red to indicate it is not primary for any interface.

Hints from issue discussion:
From my limited Django and python skills it does not look like the `is_primary` https://github.com/netbox-community/netbox/blob/d11deb66788c35ccb50eccdbd6bd2518733e2801/netbox/templates/dcim/macaddress.html#L37 is defined anywhere on the model.
> From my limited Django and python skills it does not look like the `is_primary`
> 
> [netbox/netbox/templates/dcim/macaddress.html](https://github.com/netbox-community/netbox/blob/d11deb66788c35ccb50eccdbd6bd2518733e2801/netbox/templates/dcim/macaddress.html#L37)
> 
> Line 37 in [d11deb6](/netbox-community/netbox/commit/d11deb66788c35ccb50eccdbd6bd2518733e2801)
>  <td>{% checkmark object.is_primary %}</td> 
> is defined anywhere on the model.

Yeah, that's the issue.

Could I be assigned to this? I have a patch ready.

Commit message: Fixes: #18433 - Fix missing is_primary property on MACAddress model

Patch to review:
diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py
index dbcd91ea0ed..49fd46d237e 100644
--- a/netbox/dcim/models/devices.py
+++ b/netbox/dcim/models/devices.py
@@ -1522,3 +1522,10 @@ class Meta:
 
     def __str__(self):
         return str(self.mac_address)
+
+    @property
+    def is_primary(self):
+        if self.assigned_object and hasattr(self.assigned_object, 'primary_mac_address'):
+            if self.assigned_object.primary_mac_address and self.assigned_object.primary_mac_address.pk == self.pk:
+                return True
+        return False


Code review comments:
diff_hunk: @@ -1522,3 +1522,10 @@ class Meta:
 
     def __str__(self):
         return str(self.mac_address)
+
+    @property
path: netbox/dcim/models/devices.py
text: @user2:
Any reason not to use `@user1` here?

@author:
I don't see why not.  Lemme make that change.