Skip to content

Commit

Permalink
feat(api): calc all racks free u count
Browse files Browse the repository at this point in the history
  • Loading branch information
pycook committed Nov 28, 2024
1 parent dc39197 commit ff9fb85
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
19 changes: 9 additions & 10 deletions cmdb-api/api/lib/cmdb/dcim/rack.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def update(cls, _id, **kwargs):
CIManager().update(_id, _sync=True, **kwargs)

if RackBuiltinAttributes.U_COUNT in kwargs:
payload = {RackBuiltinAttributes.FREE_U_COUNT: cls._calc_u_free_count(_id)}
payload = {RackBuiltinAttributes.FREE_U_COUNT: cls.calc_u_free_count(_id)}

CIManager().update(_id, _sync=True, **payload)

Expand All @@ -57,7 +57,7 @@ def delete(self, _id):
CIManager().update(ci['_id'], **payload)

@staticmethod
def _calc_u_free_count(rack_id, device_id=None, u_start=None, u_count=None):
def calc_u_free_count(rack_id, device_id=None, u_start=None, u_count=None):
rack = CIManager.get_ci_by_id(rack_id, need_children=False)
if not rack.get(RackBuiltinAttributes.U_COUNT):
return 0
Expand Down Expand Up @@ -123,7 +123,7 @@ def check_u_slot(self):

def add_device(self, rack_id, device_id, u_start, u_count=None):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))):
self._calc_u_free_count(rack_id, device_id, u_start, u_count)
self.calc_u_free_count(rack_id, device_id, u_start, u_count)

self.add_relation(rack_id, device_id)

Expand All @@ -133,7 +133,7 @@ def add_device(self, rack_id, device_id, u_start, u_count=None):
CIManager().update(device_id, _sync=True, **payload)

payload = {
RackBuiltinAttributes.FREE_U_COUNT: self._calc_u_free_count(rack_id, device_id, u_start, u_count)}
RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(rack_id, device_id, u_start, u_count)}
CIManager().update(rack_id, _sync=True, **payload)

OperateHistoryManager().add(operate_type=OperateTypeEnum.ADD_DEVICE, rack_id=rack_id, ci_id=device_id)
Expand All @@ -142,7 +142,7 @@ def remove_device(self, rack_id, device_id):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))):
CIRelationManager.delete_3(rack_id, device_id, apply_async=False, valid=False)

payload = {RackBuiltinAttributes.FREE_U_COUNT: self._calc_u_free_count(rack_id)}
payload = {RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(rack_id)}
CIManager().update(rack_id, _sync=True, **payload)

payload = {RackBuiltinAttributes.U_START: None}
Expand All @@ -152,7 +152,7 @@ def remove_device(self, rack_id, device_id):

def move_device(self, rack_id, device_id, to_u_start):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))):
payload = {RackBuiltinAttributes.FREE_U_COUNT: self._calc_u_free_count(rack_id, device_id, to_u_start)}
payload = {RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(rack_id, device_id, to_u_start)}
CIManager().update(rack_id, _sync=True, **payload)

CIManager().update(device_id, _sync=True, **{RackBuiltinAttributes.U_START: to_u_start})
Expand All @@ -161,23 +161,22 @@ def move_device(self, rack_id, device_id, to_u_start):

def migrate_device(self, rack_id, device_id, to_rack_id, to_u_start):
with (redis_lock.Lock(rd.r, "DCIM_RACK_OPERATE_{}".format(rack_id))):
self._calc_u_free_count(to_rack_id, device_id, to_u_start)
self.calc_u_free_count(to_rack_id, device_id, to_u_start)

if rack_id != to_rack_id:
CIRelationManager.delete_3(rack_id, device_id, apply_async=False, valid=False)

self.add_relation(to_rack_id, device_id)

payload = {
RackBuiltinAttributes.FREE_U_COUNT: self._calc_u_free_count(to_rack_id, device_id, to_u_start)}
RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(to_rack_id, device_id, to_u_start)}
CIManager().update(to_rack_id, _sync=True, **payload)

CIManager().update(device_id, _sync=True, **{RackBuiltinAttributes.U_START: to_u_start})

if rack_id != to_rack_id:
payload = {RackBuiltinAttributes.FREE_U_COUNT: self._calc_u_free_count(rack_id)}
payload = {RackBuiltinAttributes.FREE_U_COUNT: self.calc_u_free_count(rack_id)}
CIManager().update(rack_id, _sync=True, **payload)

OperateHistoryManager().add(operate_type=OperateTypeEnum.REMOVE_DEVICE, rack_id=rack_id, ci_id=device_id)
OperateHistoryManager().add(operate_type=OperateTypeEnum.ADD_DEVICE, rack_id=to_rack_id, ci_id=device_id)

24 changes: 23 additions & 1 deletion cmdb-api/api/tasks/cmdb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding:utf-8 -*-
# -*- coding:utf-8 -*-


import datetime
Expand Down Expand Up @@ -374,3 +374,25 @@ def build_relations_for_ad_accept(adc, ci_id, ad_key2attr):
source=RelationSourceEnum.AUTO_DISCOVERY)
except:
pass


@celery.task(name="cmdb.dcim_calc_u_free_count", queue=CMDB_QUEUE)
@reconnect_db
def dcim_calc_u_free_count():
from api.lib.cmdb.ci import CIManager
from api.lib.cmdb.dcim.rack import RackManager
from api.lib.cmdb.dcim.const import RackBuiltinAttributes

if not has_request_context():
current_app.test_request_context().push()
login_user(UserCache.get('worker'))

try:
rack_m = RackManager()
except Exception:
return

racks = CI.get_by(type_id=rack_m.type_id, to_dict=False)
for rack in racks:
payload = {RackBuiltinAttributes.FREE_U_COUNT: rack_m.calc_u_free_count(rack.id)}
CIManager().update(rack.id, **payload)
13 changes: 13 additions & 0 deletions cmdb-api/api/views/cmdb/dcim/rack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from flask import request

from api.lib.cmdb.const import CMDB_QUEUE
from api.lib.cmdb.dcim.const import RackBuiltinAttributes
from api.lib.cmdb.dcim.rack import RackManager
from api.lib.common_setting.decorator import perms_role_required
from api.lib.common_setting.role_perm_base import CMDBApp
from api.lib.decorator import args_required
from api.resource import APIView
from api.tasks.cmdb import dcim_calc_u_free_count

app_cli = CMDBApp()

Expand Down Expand Up @@ -87,3 +89,14 @@ def put(self, rack_id, device_id):
device_id=device_id,
to_u_start=to_u_start,
to_rack_id=to_rack_id)


class RackCalcUFreeCountView(APIView):
url_prefix = ("/dcim/rack/calc_u_free_count",)

@perms_role_required(app_cli.app_name, app_cli.resource_type_name, app_cli.op.DCIM,
app_cli.op.read, app_cli.admin_name)
def post(self):
dcim_calc_u_free_count.apply_async(queue=CMDB_QUEUE)

return self.jsonify(code=200)

0 comments on commit ff9fb85

Please sign in to comment.