Skip to content

Commit

Permalink
fix(api): update CI for unique identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
pycook committed Dec 23, 2024
1 parent aaa4ca1 commit d012943
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 6 additions & 1 deletion cmdb-api/api/lib/cmdb/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,14 @@ def update(self, ci_id, _is_admin=False, ticket_id=None, _sync=False, **ci_dict)
raw_dict = copy.deepcopy(ci_dict)

ci_attr2type_attr = {type_attr.attr_id: type_attr for type_attr, _ in attrs}
unique_name = None
for _, attr in attrs:
if attr.default and attr.default.get('default') == AttributeDefaultValueEnum.UPDATED_AT:
ci_dict[attr.name] = now

if attr.id == ci_type.unique_id:
unique_name = attr.name

value_manager = AttributeValueManager()

password_dict = dict()
Expand Down Expand Up @@ -557,7 +561,8 @@ def update(self, ci_id, _is_admin=False, ticket_id=None, _sync=False, **ci_dict)

ci_dict = {k: v for k, v in ci_dict.items() if k in ci_type_attrs_name}
key2attr = value_manager.valid_attr_value(ci_dict, ci.type_id, ci.id, ci_type_attrs_name,
ci_attr2type_attr=ci_attr2type_attr)
ci_attr2type_attr=ci_attr2type_attr,
unique_name=unique_name)

if computed_attrs:
value_manager.handle_ci_compute_attributes(ci_dict, computed_attrs, ci)
Expand Down
12 changes: 8 additions & 4 deletions cmdb-api/api/lib/cmdb/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def check_re(expr, alias, value):
if not re.compile(expr).match(str(value)):
return abort(400, ErrFormat.attribute_value_invalid2.format(alias, value))

def _validate(self, attr, value, value_table, ci=None, type_id=None, ci_id=None, type_attr=None):
def _validate(self, attr, value, value_table, ci=None, type_id=None, ci_id=None, type_attr=None, unique_name=None):
if not attr.is_reference:
ci = ci or {}
v = self._deserialize_value(attr.alias, attr.value_type, value)
Expand All @@ -146,7 +146,7 @@ def _validate(self, attr, value, value_table, ci=None, type_id=None, ci_id=None,
else:
v = value or None

attr.is_unique and self._check_is_unique(
(attr.is_unique or attr.name == unique_name) and self._check_is_unique(
value_table, attr, ci and ci.id or ci_id, ci and ci.type_id or type_id, v)
self._check_is_required(ci and ci.type_id or type_id, attr, v, type_attr=type_attr)
if attr.is_reference:
Expand Down Expand Up @@ -237,7 +237,10 @@ def handle_ci_compute_attributes(self, ci_dict, computed_attrs, ci):
if computed_value is not None:
ci_dict[attr['name']] = computed_value

def valid_attr_value(self, ci_dict, type_id, ci_id, name2attr, alias2attr=None, ci_attr2type_attr=None):
def valid_attr_value(self, ci_dict, type_id, ci_id, name2attr,
alias2attr=None,
ci_attr2type_attr=None,
unique_name=None):
key2attr = dict()
alias2attr = alias2attr or {}
ci_attr2type_attr = ci_attr2type_attr or {}
Expand Down Expand Up @@ -268,7 +271,8 @@ def valid_attr_value(self, ci_dict, type_id, ci_id, name2attr, alias2attr=None,

else:
value = self._validate(attr, value, value_table, ci=None, type_id=type_id, ci_id=ci_id,
type_attr=ci_attr2type_attr.get(attr.id))
type_attr=ci_attr2type_attr.get(attr.id),
unique_name=unique_name)
ci_dict[key] = value
except BadRequest as e:
raise
Expand Down

0 comments on commit d012943

Please sign in to comment.