device = await client.get( kind="InfraDevice", id="device-id", include=["tags"] # Include many relationship)print(f"Device has {len(device.tags)} tags:")for tag in device.tags: print(f" - {tag.name.value}")# Access tag IDstag_ids = device.tags.peer_idsprint(f"Tag IDs: {tag_ids}")
device = await client.get( kind="InfraDevice", id="device-id", include=["tags"])# Get existing tagscurrent_tags = list(device.tags)# Create and add new tagnew_tag = await client.create(kind="BuiltinTag", name="new-tag")await new_tag.save()current_tags.append(new_tag)# Update with all tagsdevice.tags = current_tagsawait device.save()print(f"Device now has {len(device.tags)} tags")
device = await client.get( kind="InfraDevice", id="device-id", include=["tags"])# Remove specific tagtag_to_remove_id = "tag-id-123"device.tags = [ tag for tag in device.tags if tag.id != tag_to_remove_id]await device.save()
# Get location and all devices at that locationlocation = await client.get( kind="InfraLocation", id="location-id", include=["devices"] # Reverse relationship)print(f"Location: {location.name.value}")print(f"Devices at this location: {len(location.devices)}")for device in location.devices: print(f" - {device.name.value}")
device = await client.get( kind="InfraDevice", id="device-id", include=["tags"])# Filter tags by nameproduction_tags = [ tag for tag in device.tags if "prod" in tag.name.value.lower()]print(f"Production tags: {[t.name.value for t in production_tags]}")
device = await client.get( kind="InfraDevice", id="device-id", include=["tags"])# Check if device has specific tagtarget_tag_id = "production-tag-id"has_tag = any(tag.id == target_tag_id for tag in device.tags)if has_tag: print("Device is tagged as production")else: print("Device is not in production")
device = await client.get( kind="InfraDevice", id="device-id", include=["location"])if not device.location: print("Warning: Device has no location assigned")else: print(f"Device is at: {device.location.name.value}")
device = await client.get( kind="InfraDevice", id="device-id", include=["tags"])# Ensure minimum number of tagsmin_tags = 2if len(device.tags) < min_tags: print(f"Error: Device must have at least {min_tags} tags") # Add required tags...
# Get all devicesdevices = await client.all(kind="InfraDevice")# Get new locationnew_location = await client.get(kind="InfraLocation", id="new-location-id")# Update location for all devicesfor device in devices: device.location = new_location await device.save()print(f"Updated location for {len(devices)} devices")
class DeviceRelationshipManager: def __init__(self, device): self.device = device self._tags = None async def load_tags(self): """Lazy load tags.""" if self._tags is None: device_full = await client.get( kind="InfraDevice", id=self.device.id, include=["tags"] ) self._tags = list(device_full.tags) return self._tags async def add_tag(self, tag): """Add a tag to the device.""" tags = await self.load_tags() if tag.id not in [t.id for t in tags]: tags.append(tag) self.device.tags = tags await self.device.save() async def remove_tag(self, tag_id): """Remove a tag from the device.""" tags = await self.load_tags() self.device.tags = [t for t in tags if t.id != tag_id] await self.device.save()# Usagedevice = await client.get(kind="InfraDevice", id="device-id")manager = DeviceRelationshipManager(device)new_tag = await client.create(kind="BuiltinTag", name="critical")await new_tag.save()await manager.add_tag(new_tag)