infrahub-generator-creator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Infrahub Generator Creator

Infrahub Generator 创建器

Overview

概述

Expert guidance for creating Infrahub Generators. Generators query data from Infrahub via GraphQL and create new nodes and relationships based on the result -- enabling design-driven automation where a "design" object automatically creates downstream infrastructure.
为创建Infrahub Generators提供专业指导。Generators通过GraphQL从Infrahub查询数据,并基于查询结果创建新节点和关系,从而实现设计驱动的自动化:即「设计」对象可自动创建下游基础设施。

When to Use

适用场景

  • Building design-driven automation (topology -> devices)
  • Creating objects from templates or design definitions
  • Implementing idempotent create-or-update workflows
  • Auto-generating infrastructure from high-level designs
  • Understanding the generator tracking system
  • 构建设计驱动的自动化(拓扑 -> 设备)
  • 基于模板或设计定义创建对象
  • 实现幂等的创建或更新工作流
  • 从高层设计自动生成基础设施
  • 了解生成器跟踪系统

Rule Categories

规则分类

PriorityCategoryPrefixDescription
CRITICALArchitecture
architecture-
Components, groups
CRITICALPython Class
python-
Generator, generate()
HIGHTracking
tracking-
Upsert, idempotent
HIGHAPI Ref
api-
Constructor, props
HIGHRegistration
registration-
.infrahub.yml config
MEDIUMPatterns
patterns-
Cleaning, batch, store
LOWTesting
testing-
infrahubctl commands
优先级分类前缀描述
最高架构
architecture-
组件、组
最高Python类
python-
Generator, generate()
跟踪
tracking-
Upsert, 幂等
API参考
api-
构造函数、属性
注册
registration-
.infrahub.yml 配置
模式
patterns-
清理、批量、存储
测试
testing-
infrahubctl 命令

Generator Basics

生成器基础

Every generator has three components:
  1. Target group -- objects that trigger the generator
  2. GraphQL query (
    .gql
    file) -- fetches the design data
  3. Python class -- inherits from
    InfrahubGenerator
    , implements
    generate()
python
from infrahub_sdk.generator import InfrahubGenerator

class MyGenerator(InfrahubGenerator):
    async def generate(self, data: dict) -> None:
        obj = await self.client.create(
            kind="DcimDevice",
            data={"name": "spine-01"},
        )
        await obj.save(allow_upsert=True)
每个生成器包含三个组件:
  1. 目标组 -- 触发生成器的对象
  2. GraphQL查询
    .gql
    文件) -- 获取设计数据
  3. Python类 -- 继承自
    InfrahubGenerator
    ,实现
    generate()
    方法
python
from infrahub_sdk.generator import InfrahubGenerator

class MyGenerator(InfrahubGenerator):
    async def generate(self, data: dict) -> None:
        obj = await self.client.create(
            kind="DcimDevice",
            data={"name": "spine-01"},
        )
        await obj.save(allow_upsert=True)

Supporting References

参考资料

  • examples.md -- Complete Generator patterns (POP topology, network segment, minimal)
  • ../infrahub-common/graphql-queries.md -- GraphQL query writing reference
  • ../infrahub-common/infrahub-yml-reference.md -- .infrahub.yml project configuration
  • ../infrahub-common/rules/ -- Shared rules (git integration, caching gotchas)
  • ../infrahub-schema-creator/SKILL.md -- Schema definitions Generators work with
  • rules/ -- Individual rules organized by category prefix
  • examples.md -- 完整的Generator模式(POP拓扑、网络分段、最简示例)
  • ../infrahub-common/graphql-queries.md -- GraphQL查询编写参考
  • ../infrahub-common/infrahub-yml-reference.md -- .infrahub.yml 项目配置参考
  • ../infrahub-common/rules/ -- 共享规则(git集成、缓存注意事项)
  • ../infrahub-schema-creator/SKILL.md -- 生成器配套的Schema定义
  • rules/ -- 按类别前缀组织的独立规则