infrahub-transform-creator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Infrahub Transform Creator

Infrahub 转换创建工具

Overview

概览

Expert guidance for creating Infrahub transforms. Transforms convert Infrahub data into different formats -- JSON, text, CSV, device configs, or any text-based output -- using Python classes or Jinja2 templates.
为创建Infrahub转换提供专业指导。 转换可使用Python类或Jinja2模板将Infrahub数据转换为不同格式 ——JSON、文本、CSV、设备配置,或任何基于文本的输出。

When to Use

适用场景

  • Building data transformations (Infrahub data -> another format)
  • Generating device configurations from infrastructure data
  • Creating CSV reports, cable matrices, or inventory exports
  • Rendering Jinja2 templates with query data
  • Combining Python logic with Jinja2 rendering
  • Connecting transforms to artifacts for automated output
  • 构建数据转换(Infrahub数据 -> 其他格式)
  • 基于基础设施数据生成设备配置
  • 创建CSV报表、线缆矩阵或库存导出文件
  • 使用查询数据渲染Jinja2模板
  • 结合Python逻辑与Jinja2渲染能力
  • 将转换与工件关联以实现自动化输出

Rule Categories

规则分类

PriorityCategoryPrefixDescription
CRITICALTypes
types-
Python vs Jinja2 choice
CRITICALPython
python-
InfrahubTransform class
CRITICALJinja2
jinja2-
Template syntax, filters
HIGHHybrid
hybrid-
Python + Jinja2 combined
HIGHArtifacts
artifacts-
Output files, targets
HIGHAPI Ref
api-
Class attrs, methods
MEDIUMPatterns
patterns-
Utilities, CSV, shared
LOWTesting
testing-
Transform/render commands
优先级分类前缀描述
严重类型
types-
Python与Jinja2的选型
严重Python
python-
InfrahubTransform类
严重Jinja2
jinja2-
模板语法、过滤器
混合模式
hybrid-
Python + Jinja2 结合使用
工件
artifacts-
输出文件、目标
API参考
api-
类属性、方法
中等最佳实践
patterns-
工具函数、CSV、共享逻辑
测试
testing-
转换/渲染命令

Transform Basics

转换基础知识

Two types of transforms:
TypeOutputEntry Point
PythonJSON/dict or text
InfrahubTransform.transform()
Jinja2Text
.j2
template file
python
from infrahub_sdk.transforms import InfrahubTransform

class MyTransform(InfrahubTransform):
    query = "my_query"

    async def transform(self, data: dict) -> dict:
        device = data["DcimDevice"]["edges"][0]["node"]
        return {"hostname": device["name"]["value"]}
两种转换类型:
类型输出入口点
PythonJSON/dict 或文本
InfrahubTransform.transform()
Jinja2文本
.j2
模板文件
python
from infrahub_sdk.transforms import InfrahubTransform

class MyTransform(InfrahubTransform):
    query = "my_query"

    async def transform(self, data: dict) -> dict:
        device = data["DcimDevice"]["edges"][0]["node"]
        return {"hostname": device["name"]["value"]}

Supporting References

参考资料

  • examples.md -- Complete transform patterns (Python, Jinja2, hybrid, CSV)
  • ../infrahub-common/graphql-queries.md -- GraphQL query writing reference
  • infrahub-yml-reference.md -- .infrahub.yml project configuration
  • ../infrahub-common/rules/ -- Shared rules (git integration, caching) across all skills
  • ../infrahub-schema-creator/SKILL.md -- Schema definitions transforms work with
  • rules/ -- Individual rules by category
  • examples.md -- 完整的转换示例模式(Python、Jinja2、混合模式、CSV)
  • ../infrahub-common/graphql-queries.md -- GraphQL查询编写参考
  • infrahub-yml-reference.md -- .infrahub.yml 项目配置参考
  • ../infrahub-common/rules/ -- 所有技能通用的共享规则(Git集成、缓存)
  • ../infrahub-schema-creator/SKILL.md -- 转换所使用的Schema定义
  • rules -- 按分类整理的独立规则