Skip to content

bug: Check attempts to use the parameter key, but runs into AttributeError if not a valid attribute. #445

Open
@FragmentedPacket

Description

@FragmentedPacket

Component

infrahubctl

Infrahub SDK version

1.13.0

Current Behavior

A PC check run will properly grab the correct attribute on an InfrahubNode based on the value of the parameter, but infrahubctl attempts to use the parameter key for the attribute fetch.

query TagsQuery($tag_name: String!) {
  BuiltinTag(name__value: $tag_name) {
    edges {
      node {
        name {
          value
        }
        description {
          value
        }
      }
    }
  }
}
check_definitions:
  - name: "check_color_tags_name"
    class_name: "ColorTagsCheck"
    file_path: "checks/tags_check/tags_check.py"
    targets: "ColorTags"
    parameters:
      tag_name: "name__value"

This would result in the following error when running the check via infrahubctl.

Error: 'InfrahubNode' object has no attribute 'tag_name'
Traceback (most recent call last):
  File "/Users/Mikhail/cloned/ih-guide/.venv/lib/python3.11/site-packages/infrahub_sdk/ctl/utils.py", line 100, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Mikhail/cloned/ih-guide/.venv/lib/python3.11/site-packages/infrahub_sdk/ctl/check.py", line 73, in run
    aiorun(
  File "/Users/Mikhail/.pyenv/versions/3.11.10/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Users/Mikhail/.pyenv/versions/3.11.10/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Mikhail/.pyenv/versions/3.11.10/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/Mikhail/cloned/ih-guide/.venv/lib/python3.11/site-packages/infrahub_sdk/ctl/check.py", line 198, in run_checks
    result = await run_targeted_check(
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Mikhail/cloned/ih-guide/.venv/lib/python3.11/site-packages/infrahub_sdk/ctl/check.py", line 168, in run_targeted_check
    attribute = getattr(member.peer, identifier)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/Mikhail/cloned/ih-guide/.venv/lib/python3.11/site-packages/infrahub_sdk/node/node.py", line 530, in __getattr__
    raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
AttributeError: 'InfrahubNode' object has no attribute 'tag_name'

This check runs successfully in a PC check.

Expected Behavior

A check will work the same when executed the same way via a PC or infrahubctl check command.

Steps to Reproduce

  1. Start fresh instance
  2. Create gql
query TagsQuery($tag_name: String!) {
  BuiltinTag(name__value: $tag_name) {
    edges {
      node {
        name {
          value
        }
        description {
          value
        }
      }
    }
  }
}
  1. Create Python check file
import re

from infrahub_sdk.checks import InfrahubCheck


RE_TAG = re.compile(r"^color-[a-z]+")


class ColorTagsCheck(InfrahubCheck):
    query = "tags_check"

    def validate(self, data):
        for tag in data["BuiltinTag"]["edges"]:
            if not RE_TAG.match(tag["node"]["name"]["value"]):
                self.log_error(
                    message=f"Invalid tag name: {tag['node']['name']['value']}",
                    object_id=tag["node"]["name"]["value"],
                    object_type="BuiltinTag",
                )
  1. Add a .infrahub.yml
check_definitions:
  - name: "check_color_tags_name"
    class_name: "ColorTagsCheck"
    file_path: "checks/tags_check/tags_check.py"
    targets: "ColorTags"
    parameters:
      tag_name: "name__value"

queries:
  - name: tags_check
    file_path: "checks/tags_check/tags_check.gql"
  1. Add two tag objects as part of the ColorTags group:
  2. color-red
  3. blue
  4. Attempt to execute infrahubctl check check_color_tags_name

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions