New data function

From straubt1/tfx · View interactive version

New data function is an AI agent skill from the GitHub repository straubt1/tfx. Create a new function in the data/ package following project conventions

How to use this skill

Skill file

Data Layer Conventions

The data/ package is the sole boundary between TFx and the go-tfe SDK. All TFE/HCP Terraform API calls happen exclusively here. No other package should import go-tfe for API calls. This isolation makes the codebase easier to test and maintain.

Pagination — Always use `client/pagination.go`

Use client.FetchAll[T]() for all paginated list operations. Never write manual pagination loops. Use client.NewPaginationFromTFE() to convert go-tfe pagination. PageSize should always be 100.

Canonical example — data/projects.go:FetchProjects():

func FetchThings(c *client.TfxClient, orgName string) ([]*tfe.Thing, error) {
    return client.FetchAll(c.Context, func(pageNumber int) ([]*tfe.Thing, *client.Pagination, error) {
        opts := &tfe.ThingListOptions{
            ListOptions: tfe.ListOptions{PageNumber: pageNumber, PageSize: 100},
        }
        result, err := c.Client.Things.List(c.Context, orgName, opts)
        if err != nil {
            return nil, nil, err
        }
        return result.Items, client.NewPaginationFromTFE(result.Pagination), nil
    })
}

Function Naming

| Pattern | Purpose | Example |

|---------|---------|---------|

| Fetch* (plural) | Paginated list returning slice | FetchProjects, FetchWorkspaces |

| Fetch* (singular) | Read single item by ID | FetchProject, FetchRun |

| Fetch*ByName | Search + exact match by name | FetchProjectByName |

| Get*ID | Resolve name to ID | GetWorkspaceID |

| Create* / Update* / Delete* | State-changing operations | CreateVariable, DeleteVariable |

| List* | List with optional maxItems limit | ListRegistryModules |

Function Signature

SDK Gaps — Direct HTTP Calls

When go-tfe doesn't expose a needed field, use a direct HTTP call via c.Hostname and c.Token. See fetchEvaluationOutputs() in data/policy_checks.go for the pattern. Always add a comment explaining why the raw call is necessary.

Logging

Use output.Get().Logger() with structured key-value pairs:

Error Handling

File Organization

File Header

// Copyright (c) Tom Straub (github.com/straubt1) 2025
// SPDX-License-Identifier: MIT

Reference Implementations

Task

Create the data layer function for: $ARGUMENTS

Follow all conventions above. Use data/projects.go as your primary reference.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.