lib

package
v5.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2025 License: Apache-2.0 Imports: 16 Imported by: 1

Documentation

Index

Constants

View Source
const (
	WsAttrID                   = "id"
	WsAttrAutoApply            = "auto-apply"
	WsAttrCreatedAt            = "created-at"
	WsAttrEnvironment          = "environment"
	WsAttrName                 = "name"
	WsAttrStructuredRunOutput  = "structured-run-output-enabled"
	WsAttrTerraformVersion     = "terraform-version"
	WsAttrVcsDisplayIdentifier = "vcs-repo.display-identifier"
	WsAttrVcsTokenID           = "vcs-repo.oauth-token-id"
	WsAttrWorkingDirectory     = "working-directory"
)

Variables

This section is empty.

Functions

func AddOrUpdateVariable

func AddOrUpdateVariable(cfg UpdateConfig) (string, error)

AddOrUpdateVariable adds or updates an existing Terraform Cloud workspace variable If the copyVariables param is set to true, then all the non-sensitive variable values will be added to the new workspace. Otherwise, they will be set to "REPLACE_THIS_VALUE"

func AddRemoteStateConsumers

func AddRemoteStateConsumers(workspaceID string, consumerIDs []string) error

func ApplyVariableSet

func ApplyVariableSet(varsetID string, workspaceIDs []string) error

TODO: make a config struct for this call?

func ApplyVariableSetsToWorkspace

func ApplyVariableSetsToWorkspace(sets VariableSetList, workspaceID string) error

func AssignTeamAccess

func AssignTeamAccess(workspaceID string, allTeamData AllTeamWorkspaceData)

AssignTeamAccess assigns the requested team access to a workspace on Terraform Cloud

func CloneWorkspace

func CloneWorkspace(cfg CloneConfig) ([]string, error)

CloneWorkspace gets the data, variables and team access data for an existing Terraform Cloud workspace and then creates a clone of it with the same data.

If the copyVariables param is set to true, then all the non-sensitive variable values will be added to the new workspace. Otherwise, they will be set to "REPLACE_THIS_VALUE"

func ConvertHCLVariable

func ConvertHCLVariable(tfVar *Var)

ConvertHCLVariable changes a Var struct in place by escaping the double quotes and line endings in the Value attribute

func CreateAllVariables

func CreateAllVariables(organization, workspaceName string, tfVars []Var)

CreateAllVariables makes several Terraform vars API POSTs to create variables for a given organization and workspace

func CreateRun

func CreateRun(config RunConfig) error

CreateRun creates a Run, which starts a Plan, which can later be Applied. https://developer.hashicorp.com/terraform/cloud-docs/api-docs/run

func CreateRunTrigger

func CreateRunTrigger(config RunTriggerConfig) error

func CreateVariable

func CreateVariable(organization, workspaceName string, tfVar Var)

CreateVariable makes a Terraform vars API POST to create a variable for a given organization and workspace

func CreateWorkspace

func CreateWorkspace(oc OpsConfig, vcsTokenID string) (string, error)

CreateWorkspace makes a Terraform workspaces API call to create a workspace for a given organization, including setting up its VCS repo integration

func DeleteVariable

func DeleteVariable(variableID string)

DeleteVariable deletes a variable from a workspace

func EnableDebug

func EnableDebug()

func EnableReadOnlyMode

func EnableReadOnlyMode()

func FindWorkspaces

func FindWorkspaces(organization, workspaceFilter string) map[string]string

FindWorkspaces uses the `search[name]` parameter to retrieve a list of workspaces in Terraform Cloud that match the workspaceFilter by the workspace name. The list is returned as a map with the ID in the key and the name in the value.

func GetCreateVariablePayload

func GetCreateVariablePayload(organization, workspaceName string, tfVar Var) string

GetCreateVariablePayload returns the json needed to make a Post to the Terraform vars api

func GetCreateWorkspacePayload

func GetCreateWorkspacePayload(oc OpsConfig, vcsTokenID string) string

GetCreateWorkspacePayload returns the JSON needed to make a POST to the Terraform workspaces API

func GetToken

func GetToken() string

func GetUpdateVariablePayload

func GetUpdateVariablePayload(organization, workspaceName, variableID string, tfVar Var) string

GetUpdateVariablePayload returns the json needed to make a Post to the Terraform vars api

func GetWorkspaceAttributes

func GetWorkspaceAttributes(organization string, attributes []string) ([][]string, error)

GetWorkspaceAttributes returns a list of all workspaces in `organization` and the values of the attributes requested in the `attributes` list. The value of unrecognized attribute names will be returned as `null`.

func OrganizationExists

func OrganizationExists(organization string) (bool, error)

OrganizationExists returns whether an organization with the given name exists

func RunTFInit

func RunTFInit(oc OpsConfig, tfTokenDestination string) error

RunTFInit ...

  • removes old terraform.tfstate files
  • runs terraform init with old versions
  • runs terraform init with new version

NOTE: This procedure can be used to copy/migrate a workspace's state to a new one. (see the -backend-config mention below and the backend.tf file in this repo)

func SearchVarsInAllWorkspaces

func SearchVarsInAllWorkspaces(wsData []Workspace, organization, keyContains, valueContains string) (map[string][]Var, error)

SearchVarsInAllWorkspaces returns all the variables that match the search terms 'keyContains' and 'valueContains' in all workspaces given. The return value is a map of variable lists with the workspace name as the key.

func SetToken

func SetToken(t string)

func UpdateVariable

func UpdateVariable(organization, workspaceName, variableID string, tfVar Var)

UpdateVariable makes a Terraform vars API call to update a variable for a given organization and workspace

func UpdateWorkspace

func UpdateWorkspace(params WorkspaceUpdateParams) error

UpdateWorkspace updates one attribute of one or more Terraform Cloud workspaces.

Types

type AllTeamWorkspaceData

type AllTeamWorkspaceData struct {
	Data []TeamWorkspaceData `json:"data"`
}

AllTeamWorkspaceData is what is returned by the api when requesting the team access data for a workspace

func GetTeamAccessFrom

func GetTeamAccessFrom(workspaceID string) (AllTeamWorkspaceData, error)

GetTeamAccessFrom returns the team access data from an existing workspace

type CloneConfig

type CloneConfig struct {
	Organization                string
	NewOrganization             string
	SourceWorkspace             string
	NewWorkspace                string
	NewVCSTokenID               string
	AtlasTokenDestination       string
	CopyState                   bool
	CopyVariables               bool
	ApplyVariableSets           bool
	DifferentDestinationAccount bool
}

type Config

type Config struct {
	// contains filtered or unexported fields
}

type FindRunTriggerConfig

type FindRunTriggerConfig struct {
	SourceWorkspaceID string
	WorkspaceID       string
}

type ListRunTriggerConfig

type ListRunTriggerConfig struct {
	WorkspaceID string
	Type        string // must be either "inbound" or "outbound"
}

type OAuthTokens

type OAuthTokens struct {
	Data []struct {
		ID         string `json:"id"`
		Type       string `json:"type"`
		Attributes struct {
			CreatedAt           time.Time `json:"created-at"`
			ServiceProviderUser string    `json:"service-provider-user"`
			HasSSHKey           bool      `json:"has-ssh-key"`
		} `json:"attributes"`
		Relationships struct {
			OauthClient struct {
				Data struct {
					ID   string `json:"id"`
					Type string `json:"type"`
				} `json:"data"`
				Links struct {
					Related string `json:"related"`
				} `json:"links"`
			} `json:"oauth-client"`
		} `json:"relationships"`
		Links struct {
			Self string `json:"self"`
		} `json:"links"`
	} `json:"data"`
}

type OpsConfig

type OpsConfig struct {
	SourceOrg        string
	SourceName       string
	NewOrg           string
	NewName          string
	TerraformVersion string
	RepoID           string
	Branch           string
	Directory        string
}

OpsConfig represents one row of the plan.csv file's contents

func (*OpsConfig) AsArray

func (o *OpsConfig) AsArray() []string

AsArray returns the values of the OpsConfig attributes

type RunConfig

type RunConfig struct {
	Message     string
	WorkspaceID string
}

type RunTrigger

type RunTrigger struct {
	CreatedAt     time.Time
	SourceName    string
	SourceID      string
	WorkspaceName string
	WorkspaceID   string
}

func FindRunTrigger

func FindRunTrigger(config FindRunTriggerConfig) (*RunTrigger, error)

FindRunTrigger searches all the run triggers inbound to the given WorkspaceID. If a run trigger is configured for the given SourceWorkspaceID, that trigger is returned. Otherwise, nil is returned.

func ListRunTriggers

func ListRunTriggers(config ListRunTriggerConfig) ([]RunTrigger, error)

ListRunTriggers returns a list of run triggers configured for the given workspace https://developer.hashicorp.com/terraform/cloud-docs/api-docs/run-triggers#list-run-triggers

type RunTriggerConfig

type RunTriggerConfig struct {
	WorkspaceID       string
	SourceWorkspaceID string
}

type TeamWorkspaceData

type TeamWorkspaceData struct {
	ID         string `json:"id"`
	Type       string `json:"type"`
	Attributes struct {
		Access string `json:"access"`
	} `json:"attributes"`
	Relationships struct {
		Team struct {
			Data struct {
				ID   string `json:"id"`
				Type string `json:"type"`
			} `json:"data"`
			Links struct {
				Related string `json:"related"`
			} `json:"links"`
		} `json:"team"`
		Workspace struct {
			Data struct {
				ID   string `json:"id"`
				Type string `json:"type"`
			} `json:"data"`
			Links struct {
				Related string `json:"related"`
			} `json:"links"`
		} `json:"workspace"`
	} `json:"relationships"`
	Links struct {
		Self string `json:"self"`
	} `json:"links"`
}

TeamWorkspaceData is what is returned by the api for one team access object for a workspace

type TfcUrl

type TfcUrl struct {
	url.URL
}

func NewTfcUrl

func NewTfcUrl(path string) TfcUrl

NewTfcUrl creates a url.URL object for the Terraform Cloud API.

func (*TfcUrl) SetParam

func (t *TfcUrl) SetParam(name, value string)

type UpdateConfig

type UpdateConfig struct {
	Organization          string
	Workspace             string
	SearchString          string //  must be an exact case-insensitive match (i.e. not a partial match)
	NewValue              string
	AddKeyIfNotFound      bool // If true, then SearchOnVariableValue will be treated as false
	SearchOnVariableValue bool // If false, then will filter on variable key
	SensitiveVariable     bool // Whether to mark the variable as sensitive
}

type Var

type Var struct {
	ID        string `json:"-"`
	Key       string `json:"key"`
	Value     string `json:"value"`
	Sensitive bool   `json:"sensitive"`
	Category  string `json:"category"`
	Hcl       bool   `json:"hcl"`
}

Var is what is returned by the api for one variable

func GetVarsFromWorkspace

func GetVarsFromWorkspace(organization, workspaceName string) ([]Var, error)

GetVarsFromWorkspace returns a list of Terraform variables for a given workspace

func GetWorkspaceVar

func GetWorkspaceVar(organization, wsName, key string) (*Var, error)

GetWorkspaceVar retrieves the variables from a Workspace and returns the Var that matches the given key

func SearchVariables

func SearchVariables(organization, wsName, keyContains, valueContains string) ([]Var, error)

SearchVariables returns a list of variables in the given workspace that match the search terms 'keyContains' and 'valueContains'

type VariableSet

type VariableSet struct {
	ID         string `json:"id"`
	Type       string `json:"type"`
	Attributes struct {
		Name           string    `json:"name"`
		Description    string    `json:"description"`
		Global         bool      `json:"global"`
		UpdatedAt      time.Time `json:"updated-at"`
		VarCount       int       `json:"var-count"`
		WorkspaceCount int       `json:"workspace-count"`
	} `json:"attributes"`
	Relationships struct {
		Organization struct {
			Data struct {
				ID   string `json:"id"`
				Type string `json:"type"`
			} `json:"data"`
		} `json:"organization"`
		Vars struct {
			Data []struct {
				ID   string `json:"id"`
				Type string `json:"type"`
			} `json:"data"`
		} `json:"vars"`
		Workspaces struct {
			Data []struct {
				ID   string `json:"id"`
				Type string `json:"type"`
			} `json:"data"`
		} `json:"workspaces"`
	} `json:"relationships"`
}

func GetVariableSet

func GetVariableSet(org, vsName string) (*VariableSet, error)

type VariableSetList

type VariableSetList struct {
	Data  []VariableSet `json:"data"`
	Links struct {
		Self  string `json:"self"`
		First string `json:"first"`
		Prev  any    `json:"prev"`
		Next  any    `json:"next"`
		Last  string `json:"last"`
	} `json:"links"`
}

func GetAllVariableSets

func GetAllVariableSets(organizationName string) (VariableSetList, error)

func ListWorkspaceVariableSets

func ListWorkspaceVariableSets(workspaceID string) (VariableSetList, error)

type VarsResponse

type VarsResponse struct {
	Data []struct {
		ID            string `json:"id"`
		Type          string `json:"type"`
		Variable      Var    `json:"attributes"`
		Relationships struct {
			Configurable struct {
				Data struct {
					ID   string `json:"id"`
					Type string `json:"type"`
				} `json:"data"`
				Links struct {
					Related string `json:"related"`
				} `json:"links"`
			} `json:"configurable"`
		} `json:"relationships"`
		Links struct {
			Self string `json:"self"`
		} `json:"links"`
	} `json:"data"`
}

VarsResponse is what is returned by the api when requesting the variables of a workspace

type Workspace

type Workspace struct {
	ID         string `json:"id"`
	Type       string `json:"type"`
	Attributes struct {
		Name             string    `json:"name"`
		Environment      string    `json:"environment"`
		AutoApply        bool      `json:"auto-apply"`
		Locked           bool      `json:"locked"`
		CreatedAt        time.Time `json:"created-at"`
		WorkingDirectory string    `json:"working-directory"`
		VCSRepo          struct {
			Branch            string `json:"branch"`
			Identifier        string `json:"identifier"`
			DisplayIdentifier string `json:"display-identifier"`
			TokenID           string `json:"oauth-token-id"`
		} `json:"vcs-repo"`
		StructuredRunOutputEnabled bool   `json:"structured-run-output-enabled"`
		TerraformVersion           string `json:"terraform-version"`
		Permissions                struct {
			CanUpdate         bool `json:"can-update"`
			CanDestroy        bool `json:"can-destroy"`
			CanQueueDestroy   bool `json:"can-queue-destroy"`
			CanQueueRun       bool `json:"can-queue-run"`
			CanUpdateVariable bool `json:"can-update-variable"`
			CanLock           bool `json:"can-lock"`
			CanReadSettings   bool `json:"can-read-settings"`
		} `json:"permissions"`
		Actions struct {
			IsDestroyable bool `json:"is-destroyable"`
		} `json:"actions"`
	} `json:"attributes"`
	Relationships struct {
		Organization struct {
			Data struct {
				ID   string `json:"id"`
				Type string `json:"type"`
			} `json:"data"`
		} `json:"organization"`
		LatestRun struct {
			Data any `json:"data"`
		} `json:"latest-run"`
		CurrentRun struct {
			Data any `json:"data"`
		} `json:"current-run"`
	} `json:"relationships"`
	Links struct {
		Self string `json:"self"`
	} `json:"links"`
}

Workspace is what is returned by the api for each workspace

func CreateWorkspace2

func CreateWorkspace2(oc OpsConfig, vcsTokenID string) (Workspace, error)

CreateWorkspace2 makes a Terraform workspaces API call to create a workspace for a given organization, including setting up its VCS repo integration. Returns the properties of the new workspace.

func GetAllWorkspaces

func GetAllWorkspaces(organization string) ([]Workspace, error)

GetAllWorkspaces retrieves all workspaces from Terraform Cloud and returns a list of Workspace objects

func GetWorkspaceByName

func GetWorkspaceByName(organizationName, workspaceName string) (Workspace, error)

func (*Workspace) AttributeByLabel

func (v *Workspace) AttributeByLabel(label string) (string, error)

type WorkspaceJSON

type WorkspaceJSON struct {
	Data Workspace `json:"data"`
}

WorkspaceJSON is what is returned by the api when requesting the data for a workspace

func GetWorkspaceData

func GetWorkspaceData(organization, workspaceName string) (WorkspaceJSON, error)

type WorkspaceList

type WorkspaceList struct {
	Data []Workspace `json:"data"`
}

WorkspaceList is returned by the API when requesting a list of workspaces

type WorkspaceUpdateParams

type WorkspaceUpdateParams struct {
	Organization    string
	WorkspaceFilter string
	Attribute       string
	Value           string
}

Jump to

Keyboard shortcuts

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