Skip to content

Conversation

@ljluestc
Copy link

Fix: DCIM Rack Available U Count Auto-Update

Problem

GitHub Issue #676: DCIM cabinet available U count not automatically calculated when adding physical machines through CI relations.

Current Behavior

  • Rack free_u_count is only updated when using the DCIM web interface (RackManager.add_device() / remove_device())
  • When devices are added via regular CI relations, rack U counts remain unchanged
  • Manual rack editing is required to recalculate available U space

Impact

Users must manually edit rack properties and save to trigger U count recalculation after adding devices, which is inefficient and error-prone.

Solution

Modified ci_relation_cache() and ci_relation_delete() functions in cmdb-api/api/tasks/cmdb.py to automatically detect and handle DCIM rack U count updates.

Technical Implementation

Files Changed:

  • cmdb-api/api/tasks/cmdb.py (39 lines added)

Key Changes:

  1. Added import: from api.lib.cmdb.const import BuiltinModelEnum
  2. Enhanced ci_relation_cache() to detect DCIM rack + device relations
  3. Enhanced ci_relation_delete() to detect DCIM rack + device relation removals
  4. Automatic U count recalculation using existing RackManager.calc_u_free_count()

Code Logic

# Check if parent is a DCIM rack and child has U-related attributes
# If so, recalculate the rack's free U count
try:
    parent_ci = CI.get_by_id(parent_id)
    if parent_ci and parent_ci.ci_type.name == BuiltinModelEnum.DCIM_RACK:
        child_ci = api.lib.cmdb.ci.CIManager().get_ci_by_id(child_id, need_children=False)
        if child_ci and child_ci.get(RackBuiltinAttributes.U_START) is not None:
            # Recalculate rack free U count
            payload = {RackBuiltinAttributes.FREE_U_COUNT: RackManager.calc_u_free_count(parent_id)}
            api.lib.cmdb.ci.CIManager().update(parent_id, _sync=True, **payload)
            current_app.logger.info("Updated rack {} free U count after adding/removing device {}".format(parent_id, child_id))
except Exception as e:
    current_app.logger.warning("Failed to update DCIM rack U count: {}".format(e))

Trigger Conditions

  • Parent CI: Must be DCIM rack (ci_type.name == 'dcim_rack')
  • Child CI: Must have U attributes (u_start is not None)
  • Result: Automatic rack free_u_count recalculation

…I relations

- Add DCIM rack U count recalculation logic to ci_relation_cache() and ci_relation_delete()
- Automatically update rack free_u_count when devices are added/removed via CI relations
- Fixes issue veops#676: DCIM cabinet available U count not automatically calculated
- No more manual rack editing required after adding physical machines

Technical details:
- Detects DCIM rack + device relations using ci_type.name and U attributes
- Calls RackManager.calc_u_free_count() to recalculate available U space
- Updates rack CI with new free_u_count value
- Includes proper error handling and logging
- Maintains backward compatibility with existing DCIM interface
@pycook
Copy link
Collaborator

pycook commented Nov 25, 2025

Thanks for this contribution! I have a few suggestions to improve code quality:

  1. Remove pr-description.md from the commit
  2. Move imports to the top of the file
  3. Avoid redundant module path when calling CIManager

Please update these points and I'll be happy to approve!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants