variable

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2025 License: MPL-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package variable manages terraform workspace variables

Index

Constants

View Source
const (
	// https://developer.hashicorp.com/terraform/cloud-docs/workspaces/variables/managing-variables#character-limits
	VariableDescriptionMaxChars = 512
	VariableKeyMaxChars         = 128
	VariableValueMaxKB          = 256 // 256*1024 bytes
)

Variables

View Source
var (
	ErrVariableDescriptionMaxExceeded = fmt.Errorf("maximum variable description size (%d chars) exceeded", VariableDescriptionMaxChars)
	ErrVariableKeyMaxExceeded         = fmt.Errorf("maximum variable key size (%d chars) exceeded", VariableKeyMaxChars)
	ErrVariableValueMaxExceeded       = fmt.Errorf("maximum variable value size of %d KB exceeded", VariableValueMaxKB)
	ErrVariableConflict               = errors.New("variable conflicts with another variable with the same name and type")
)

Functions

func WriteTerraformVars

func WriteTerraformVars(dir string, vars []*Variable) error

WriteTerraformVars writes workspace variables to a file named terraform.tfvars located in the given path. If the file already exists it'll be appended to.

Types

type Client

type Client struct {
	*otfapi.Client
}

func (*Client) ListEffectiveVariables added in v0.1.9

func (c *Client) ListEffectiveVariables(ctx context.Context, runID resource.TfeID) ([]*Variable, error)

type CreateVariableOptions

type CreateVariableOptions struct {
	Key         *string
	Value       *string
	Description *string
	Category    *VariableCategory
	Sensitive   *bool
	HCL         *bool
	// contains filtered or unexported fields
}

type CreateVariableSetOptions added in v0.1.9

type CreateVariableSetOptions struct {
	Name        string
	Description string
	Global      bool
	Workspaces  []resource.TfeID
}

type ListOptions added in v0.3.17

type ListOptions struct {
	resource.PageOptions
	Organization organization.Name `schema:"organization_name"`
}

type Options

type Options struct {
	WorkspaceService *workspace.Service
	RunClient        runClient
	Authorizer       *authz.Authorizer

	*sql.DB
	*tfeapi.Responder
	logr.Logger
}

type Service

type Service struct {
	logr.Logger
	*authz.Authorizer
	// contains filtered or unexported fields
}

func NewService

func NewService(opts Options) *Service

func (*Service) AddHandlers added in v0.2.2

func (s *Service) AddHandlers(r *mux.Router)

func (*Service) CreateVariableSet added in v0.3.27

func (s *Service) CreateVariableSet(ctx context.Context, organization organization.Name, opts CreateVariableSetOptions) (*VariableSet, error)

func (*Service) CreateVariableSetVariable added in v0.3.27

func (s *Service) CreateVariableSetVariable(ctx context.Context, setID resource.TfeID, opts CreateVariableOptions) (*Variable, error)

func (*Service) CreateWorkspaceVariable added in v0.1.9

func (s *Service) CreateWorkspaceVariable(ctx context.Context, workspaceID resource.TfeID, opts CreateVariableOptions) (*Variable, error)

func (*Service) DeleteVariableSet added in v0.4.8

func (s *Service) DeleteVariableSet(ctx context.Context, setID resource.TfeID) (*VariableSet, error)

func (*Service) DeleteVariableSetVariable added in v0.4.8

func (s *Service) DeleteVariableSetVariable(ctx context.Context, variableID resource.TfeID) (*VariableSet, error)

func (*Service) DeleteWorkspaceVariable added in v0.1.9

func (s *Service) DeleteWorkspaceVariable(ctx context.Context, variableID resource.TfeID) (*WorkspaceVariable, error)

func (*Service) GetVariableSet added in v0.4.8

func (s *Service) GetVariableSet(ctx context.Context, setID resource.TfeID) (*VariableSet, error)

func (*Service) GetVariableSetByVariableID added in v0.4.8

func (s *Service) GetVariableSetByVariableID(ctx context.Context, variableID resource.TfeID) (*VariableSet, error)

func (*Service) GetWorkspaceVariable added in v0.1.9

func (s *Service) GetWorkspaceVariable(ctx context.Context, variableID resource.TfeID) (*WorkspaceVariable, error)

func (*Service) ListEffectiveVariables added in v0.1.9

func (s *Service) ListEffectiveVariables(ctx context.Context, runID resource.TfeID) ([]*Variable, error)

func (*Service) ListVariableSets added in v0.4.8

func (s *Service) ListVariableSets(ctx context.Context, organization organization.Name) ([]*VariableSet, error)

func (*Service) ListWorkspaceVariableSets added in v0.4.8

func (s *Service) ListWorkspaceVariableSets(ctx context.Context, workspaceID resource.TfeID) ([]*VariableSet, error)

func (*Service) ListWorkspaceVariables added in v0.1.9

func (s *Service) ListWorkspaceVariables(ctx context.Context, workspaceID resource.TfeID) ([]*Variable, error)

func (*Service) UpdateVariableSet added in v0.4.8

func (s *Service) UpdateVariableSet(ctx context.Context, setID resource.TfeID, opts UpdateVariableSetOptions) (*VariableSet, error)

func (*Service) UpdateVariableSetVariable added in v0.4.8

func (s *Service) UpdateVariableSetVariable(ctx context.Context, variableID resource.TfeID, opts UpdateVariableOptions) (*VariableSet, error)

func (*Service) UpdateWorkspaceVariable added in v0.1.9

func (s *Service) UpdateWorkspaceVariable(ctx context.Context, variableID resource.TfeID, opts UpdateVariableOptions) (*WorkspaceVariable, error)

type TFEVariable added in v0.3.17

type TFEVariable struct {
	ID          resource.TfeID `jsonapi:"primary,vars"`
	Key         string         `jsonapi:"attribute" json:"key"`
	Value       string         `jsonapi:"attribute" json:"value"`
	Description string         `jsonapi:"attribute" json:"description"`
	Category    string         `jsonapi:"attribute" json:"category"`
	HCL         bool           `jsonapi:"attribute" json:"hcl"`
	Sensitive   bool           `jsonapi:"attribute" json:"sensitive"`
	VersionID   string         `jsonapi:"attribute" json:"version-id"`
}

TFEVariable is a workspace variable.

type TFEVariableCreateOptions added in v0.3.17

type TFEVariableCreateOptions struct {
	// Type is a public field utilized by JSON:API to
	// set the resource type via the field tag.
	// It is not a user-defined value and does not need to be set.
	// https://jsonapi.org/format/#crud-creating
	Type string `jsonapi:"primary,vars"`

	// The name of the variable.
	Key *string `jsonapi:"attribute" json:"key"`

	// The value of the variable.
	Value *string `jsonapi:"attribute" json:"value,omitempty"`

	// The description of the variable.
	Description *string `jsonapi:"attribute" json:"description,omitempty"`

	// Whether this is a Terraform or environment variable.
	Category *string `jsonapi:"attribute" json:"category"`

	// Whether to evaluate the value of the variable as a string of HCL code.
	HCL *bool `jsonapi:"attribute" json:"hcl,omitempty"`

	// Whether the value is sensitive.
	Sensitive *bool `jsonapi:"attribute" json:"sensitive,omitempty"`
}

TFEVariableCreateOptions represents the options for creating a new variable.

type TFEVariableList added in v0.3.17

type TFEVariableList struct {
	*types.Pagination
	Items []*Variable
}

TFEVariableList is a list of workspace variables

type TFEVariableSet added in v0.3.17

type TFEVariableSet struct {
	ID          resource.TfeID `jsonapi:"primary,varsets"`
	Name        string         `jsonapi:"attribute" json:"name"`
	Description string         `jsonapi:"attribute" json:"description"`
	Global      bool           `jsonapi:"attribute" json:"global"`

	// Relations
	Organization *organization.TFEOrganization `jsonapi:"relationship" json:"organization"`
	Workspaces   []*workspace.TFEWorkspace     `jsonapi:"relationship" json:"workspaces,omitempty"`
	Variables    []*TFEVariableSetVariable     `jsonapi:"relationship" json:"vars,omitempty"`
}

TFEVariableSet represents a Terraform Enterprise variable set.

type TFEVariableSetCreateOptions added in v0.3.17

type TFEVariableSetCreateOptions struct {
	// Type is a public field utilized by JSON:API to
	// set the resource type via the field tag.
	// It is not a user-defined value and does not need to be set.
	// https://jsonapi.org/format/#crud-creating
	Type string `jsonapi:"primary,varsets"`

	// The name of the variable set.
	// Affects variable precedence when there are conflicts between Variable Sets
	// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/variable-sets#apply-variable-set-to-workspaces
	Name string `jsonapi:"attribute" json:"name"`

	// A description to provide context for the variable set.
	Description string `jsonapi:"attribute" json:"description,omitempty"`

	// If true the variable set is considered in all runs in the organization.
	Global bool `jsonapi:"attribute" json:"global,omitempty"`
}

TFEVariableSetCreateOptions represents the options for creating a new variable set within in a organization.

type TFEVariableSetUpdateOptions added in v0.3.17

type TFEVariableSetUpdateOptions struct {
	// Type is a public field utilized by JSON:API to
	// set the resource type via the field tag.
	// It is not a user-defined value and does not need to be set.
	// https://jsonapi.org/format/#crud-creating
	Type string `jsonapi:"primary,varsets"`

	// The name of the variable set.
	// Affects variable precedence when there are conflicts between Variable Sets
	// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/variable-sets#apply-variable-set-to-workspaces
	Name *string `jsonapi:"attribute" json:"name,omitempty"`

	// A description to provide context for the variable set.
	Description *string `jsonapi:"attribute" json:"description,omitempty"`

	// If true the variable set is considered in all runs in the organization.
	Global *bool `jsonapi:"attribute" json:"global,omitempty"`
}

TFEVariableSetUpdateOptions represents the options for updating a variable set.

type TFEVariableSetVariable added in v0.3.17

type TFEVariableSetVariable struct {
	*TFEVariable

	// Relations
	VariableSet *TFEVariableSet `jsonapi:"relationship" json:"varset"`
}

type TFEVariableSetVariableCreateOptions added in v0.3.17

type TFEVariableSetVariableCreateOptions struct {
	// Type is a public field utilized by JSON:API to
	// set the resource type via the field tag.
	// It is not a user-defined value and does not need to be set.
	// https://jsonapi.org/format/#crud-creating
	Type string `jsonapi:"primary,vars"`

	// The name of the variable.
	Key *string `jsonapi:"attribute" json:"key"`

	// The value of the variable.
	Value *string `jsonapi:"attribute" json:"value,omitempty"`

	// The description of the variable.
	Description *string `jsonapi:"attribute" json:"description,omitempty"`

	// Whether this is a Terraform or environment variable.
	Category *string `jsonapi:"attribute" json:"category"`

	// Whether to evaluate the value of the variable as a string of HCL code.
	HCL *bool `jsonapi:"attribute" json:"hcl,omitempty"`

	// Whether the value is sensitive.
	Sensitive *bool `jsonapi:"attribute" json:"sensitive,omitempty"`
}

TFEVariableSetVariableCreatOptions represents the options for creating a new variable within a variable set

type TFEVariableSetVariableUpdateOptions added in v0.3.17

type TFEVariableSetVariableUpdateOptions struct {
	// Type is a public field utilized by JSON:API to
	// set the resource type via the field tag.
	// It is not a user-defined value and does not need to be set.
	// https://jsonapi.org/format/#crud-creating
	Type string `jsonapi:"primary,vars"`

	// The name of the variable.
	Key *string `jsonapi:"attribute" json:"key,omitempty"`

	// The value of the variable.
	Value *string `jsonapi:"attribute" json:"value,omitempty"`

	// The description of the variable.
	Description *string `jsonapi:"attribute" json:"description,omitempty"`

	// Whether to evaluate the value of the variable as a string of HCL code.
	HCL *bool `jsonapi:"attribute" json:"hcl,omitempty"`

	// Whether the value is sensitive.
	Sensitive *bool `jsonapi:"attribute" json:"sensitive,omitempty"`
}

TFEVariableSetVariableUpdateOptions represents the options for updating a variable.

type TFEVariableUpdateOptions added in v0.3.17

type TFEVariableUpdateOptions struct {
	// Type is a public field utilized by JSON:API to
	// set the resource type via the field tag.
	// It is not a user-defined value and does not need to be set.
	// https://jsonapi.org/format/#crud-creating
	Type string `jsonapi:"primary,vars"`

	// The name of the variable.
	Key *string `jsonapi:"attribute" json:"key,omitempty"`

	// The value of the variable.
	Value *string `jsonapi:"attribute" json:"value,omitempty"`

	// The description of the variable.
	Description *string `jsonapi:"attribute" json:"description,omitempty"`

	// Whether this is a Terraform or environment variable.
	Category *string `jsonapi:"attribute" json:"category"`

	// Whether to evaluate the value of the variable as a string of HCL code.
	HCL *bool `jsonapi:"attribute" json:"hcl,omitempty"`

	// Whether the value is sensitive.
	Sensitive *bool `jsonapi:"attribute" json:"sensitive,omitempty"`
}

TFEVariableUpdateOptions represents the options for updating a variable.

type TFEWorkspace added in v0.3.21

type TFEWorkspace struct {
	ID resource.TfeID `jsonapi:"primary,workspaces"`
}

type TFEWorkspaceVariable added in v0.3.17

type TFEWorkspaceVariable struct {
	*TFEVariable

	// Relations
	Workspace *workspace.TFEWorkspace `jsonapi:"relationship" json:"configurable"`
}

TFEWorkspaceVariable is a workspace variable.

type UpdateVariableOptions

type UpdateVariableOptions struct {
	Key         *string
	Value       *string
	Description *string
	Category    *VariableCategory
	Sensitive   *bool
	HCL         *bool
	// contains filtered or unexported fields
}

type UpdateVariableSetOptions added in v0.1.9

type UpdateVariableSetOptions struct {
	Name        *string
	Description *string
	Global      *bool
	Workspaces  []resource.TfeID
}

type Variable

type Variable struct {
	ID          resource.TfeID   `jsonapi:"primary,variables" db:"variable_id"`
	Key         string           `jsonapi:"attribute" json:"key"`
	Value       string           `jsonapi:"attribute" json:"value"`
	Description string           `jsonapi:"attribute" json:"description"`
	Category    VariableCategory `jsonapi:"attribute" json:"category"`
	Sensitive   bool             `jsonapi:"attribute" json:"sensitive"`
	HCL         bool             `jsonapi:"attribute" json:"hcl"`

	// OTF doesn't use this internally but the go-tfe integration tests
	// expect it to be a random value that changes on every update.
	VersionID string
}

func Merge added in v0.4.8

func Merge(workspaceSets []*VariableSet, workspaceVariables []*Variable, run *run.Run) []*Variable

Merge merges variables for a run according to the precedence rules documented here:

https://developer.hashicorp.com/terraform/cloud-docs/workspaces/variables#precedence

Note: If run is nil then it is ignored.

func (*Variable) LogValue

func (v *Variable) LogValue() slog.Value

func (*Variable) Matches added in v0.1.9

func (v *Variable) Matches(vars []*Variable) bool

Matches determines whether variable is contained in vars, i.e. shares the same ID.

type VariableCategory

type VariableCategory string

VariableCategory is the category of variable

const (
	CategoryTerraform VariableCategory = "terraform"
	CategoryEnv       VariableCategory = "env"
)

type VariableSet added in v0.1.9

type VariableSet struct {
	ID           resource.TfeID
	Name         string
	Description  string
	Global       bool
	Workspaces   []resource.TfeID
	Organization organization.Name
	Variables    []*Variable
}

VariableSet is a set of variables

func (*VariableSet) GetVariableByID added in v0.4.8

func (s *VariableSet) GetVariableByID(variableID resource.TfeID) *Variable

func (*VariableSet) LogValue added in v0.1.9

func (s *VariableSet) LogValue() slog.Value

type WorkspaceVariable added in v0.1.9

type WorkspaceVariable struct {
	*Variable
	WorkspaceID resource.TfeID
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL