opsmill / infrahub

Infrahub - A new approach to Infrastructure Management
https://opsmill.com/
GNU Affero General Public License v3.0
187 stars 10 forks source link

bug: cannot update attributes with name "updated_at" #3730

Closed fatih-acar closed 1 week ago

fatih-acar commented 3 months ago

Component

API Server / GraphQL Infrahub 0.14.2

Current Behavior

When using a schema defining nodes that have an attribute named updated_at, it is not possible to change the value of this attribute and it is always blank/null.

Expected Behavior

updated_at attributes should be editable as expected

Steps to Reproduce

Load the following schema:

---
version: "1.0"
nodes:
  - name: Country
    namespace: Otterbox
    label: Country
    attributes:
      - name: name
        kind: Text
        unique: true
        optional: false
      - name: created_at
        kind: DateTime
        optional: false
      - name: updated_at
        kind: DateTime
        optional: false

Create a new Country node with a name, created_at and updated_at set. You will see that created_at is correctly set but updated_at is not.

Additional Information

No response

ogenstad commented 1 week ago

Added to 0.16.1 provided that this is a quick fix. I think it should be enough with this change:

diff --git a/backend/infrahub/core/node/__init__.py b/backend/infrahub/core/node/__init__.py
index 54ca35c52..502ed7d52 100644
--- a/backend/infrahub/core/node/__init__.py
+++ b/backend/infrahub/core/node/__init__.py
@@ -263,6 +263,8 @@ class Node(BaseNode, metaclass=BaseNodeMeta):
         # -------------------------------------------
         # Validate Input
         # -------------------------------------------
+        if "updated_at" in fields and "updated_at" not in self._schema.valid_input_names:
+            fields.pop("updated_at")
         for field_name in fields.keys():
             if field_name not in self._schema.valid_input_names:
                 errors.append(ValidationError({field_name: f"{field_name} is not a valid input for {self.get_kind()}"}))
@@ -422,6 +424,7 @@ class Node(BaseNode, metaclass=BaseNodeMeta):
         self._existing = True

         if updated_at:
+            kwargs["updated_at"] = updated_at
             self._updated_at = Timestamp(updated_at)

If this causes other problems we could perhaps move this to another milestone.

The problem is that we use an "updated_at" argument within the Node class to set the old metadata field _updated_at. The end goal would be to have correct metadata properties for each node where you as a user would be able to see when a node was created and when it was last updated. Even with that in place we shouldn't limit users from using the attribute name updated_at.

dgarros commented 1 week ago

Fixed in https://github.com/opsmill/infrahub/pull/4370