workspace

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MPL-2.0 Imports: 36 Imported by: 0

Documentation

Overview

Package workspace provides access to terraform workspaces

Index

Constants

View Source
const (

	//
	// VCS trigger strategies to present to the user.
	//
	// every vcs event trigger runs
	VCSTriggerAlways string = "always"
	// only vcs events with changed files matching a set of glob patterns
	// triggers run
	VCSTriggerPatterns string = "patterns"
	// only push tag vcs events trigger runs
	VCSTriggerTags string = "tags"
)

Variables

View Source
var (
	ErrWorkspaceAlreadyLocked         = errors.New("workspace already locked")
	ErrWorkspaceLockedByDifferentUser = errors.New("workspace locked by different user")
	ErrWorkspaceLockedByRun           = errors.New("workspace is locked by Run")
	ErrWorkspaceAlreadyUnlocked       = errors.New("workspace already unlocked")
	ErrWorkspaceUnlockDenied          = errors.New("unauthorized to unlock workspace")
	ErrWorkspaceInvalidLock           = errors.New("invalid workspace lock")
	ErrUnsupportedTerraformVersion    = errors.New("unsupported terraform version")

	ErrTagsRegexAndTriggerPatterns     = errors.New("cannot specify both tags-regex and trigger-patterns")
	ErrTagsRegexAndAlwaysTrigger       = errors.New("cannot specify both tags-regex and always-trigger")
	ErrTriggerPatternsAndAlwaysTrigger = errors.New("cannot specify both trigger-patterns and always-trigger")
	ErrInvalidTriggerPattern           = errors.New("invalid trigger glob pattern")
	ErrInvalidTagsRegex                = errors.New("invalid vcs tags regular expression")
	ErrAgentExecutionModeWithoutPool   = errors.New("agent execution mode requires agent pool ID")
	ErrNonAgentExecutionModeWithPool   = errors.New("agent pool ID can only be specified with agent execution mode")
)
View Source
var ErrInvalidTagSpec = errors.New("invalid tag spec: must provide either an ID or a name")

Functions

func NewCommand added in v0.1.9

func NewCommand(apiClient *otfapi.Client) *cobra.Command

Types

type CLI added in v0.1.9

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

type Client

type Client struct {
	*otfapi.Client
}

func (*Client) Get added in v0.2.2

func (c *Client) Get(ctx context.Context, workspaceID resource.ID) (*Workspace, error)

func (*Client) GetByName added in v0.2.2

func (c *Client) GetByName(ctx context.Context, organization, workspace string) (*Workspace, error)

func (*Client) List added in v0.2.2

func (c *Client) List(ctx context.Context, opts ListOptions) (*resource.Page[*Workspace], error)

func (*Client) Lock added in v0.2.2

func (c *Client) Lock(ctx context.Context, workspaceID resource.ID, runID *resource.ID) (*Workspace, error)

func (*Client) Unlock added in v0.2.2

func (c *Client) Unlock(ctx context.Context, workspaceID resource.ID, runID *resource.ID, force bool) (*Workspace, error)

func (*Client) Update added in v0.2.2

func (c *Client) Update(ctx context.Context, workspaceID resource.ID, opts UpdateOptions) (*Workspace, error)

type ConnectOptions

type ConnectOptions struct {
	RepoPath      *string
	VCSProviderID *resource.ID

	Branch        *string
	TagsRegex     *string
	AllowCLIApply *bool
}

type Connection added in v0.1.4

type Connection struct {
	// Pushes to this VCS branch trigger runs. Empty string means the default
	// branch is used. Ignored if TagsRegex is non-empty.
	Branch string
	// Pushed tags matching this regular expression trigger runs. Mutually
	// exclusive with TriggerPatterns.
	TagsRegex string

	VCSProviderID resource.ID
	Repo          string

	// By default, once a workspace is connected to a repo it is not
	// possible to run a terraform apply via the CLI. Setting this to true
	// overrides this behaviour.
	AllowCLIApply bool
}

type CreateOptions

type CreateOptions struct {
	AgentPoolID                *resource.ID
	AllowDestroyPlan           *bool
	AutoApply                  *bool
	Description                *string
	ExecutionMode              *ExecutionMode
	GlobalRemoteState          *bool
	MigrationEnvironment       *string
	Name                       *string
	QueueAllRuns               *bool
	SpeculativeEnabled         *bool
	SourceName                 *string
	SourceURL                  *string
	StructuredRunOutputEnabled *bool
	Tags                       []TagSpec
	TerraformVersion           *string
	TriggerPrefixes            []string
	TriggerPatterns            []string
	WorkingDirectory           *string
	Organization               *string

	// Always trigger runs. A value of true is mutually exclusive with
	// setting TriggerPatterns or ConnectOptions.TagsRegex.
	AlwaysTrigger *bool

	*ConnectOptions
}

CreateOptions represents the options for creating a new workspace.

type ExecutionMode

type ExecutionMode string
const (
	RemoteExecutionMode ExecutionMode = "remote"
	LocalExecutionMode  ExecutionMode = "local"
	AgentExecutionMode  ExecutionMode = "agent"

	DefaultAllowDestroyPlan = true
	MinTerraformVersion     = "1.2.0"
)

func ExecutionModePtr

func ExecutionModePtr(m ExecutionMode) *ExecutionMode

ExecutionModePtr returns a pointer to an execution mode.

type FakeService added in v0.2.2

type FakeService struct {
	Workspaces []*Workspace
	Policy     authz.WorkspacePolicy
}

func (*FakeService) AddTags added in v0.2.2

func (f *FakeService) AddTags(ctx context.Context, workspaceID resource.ID, tags []TagSpec) error

func (*FakeService) Create added in v0.2.2

func (*FakeService) Delete added in v0.2.2

func (*FakeService) Get added in v0.2.2

func (*FakeService) GetByName added in v0.2.2

func (f *FakeService) GetByName(context.Context, string, string) (*Workspace, error)

func (*FakeService) GetWorkspacePolicy added in v0.3.6

func (f *FakeService) GetWorkspacePolicy(context.Context, resource.ID) (authz.WorkspacePolicy, error)

func (*FakeService) List added in v0.2.2

func (*FakeService) ListConnectedWorkspaces added in v0.2.2

func (f *FakeService) ListConnectedWorkspaces(ctx context.Context, vcsProviderID resource.ID, repoPath string) ([]*Workspace, error)

func (*FakeService) ListTags added in v0.2.2

func (*FakeService) Lock added in v0.2.2

func (*FakeService) RemoveTags added in v0.2.2

func (f *FakeService) RemoveTags(ctx context.Context, workspaceID resource.ID, tags []TagSpec) error

func (*FakeService) SetPermission added in v0.2.2

func (f *FakeService) SetPermission(ctx context.Context, workspaceID, teamID resource.ID, role authz.Role) error

func (*FakeService) Unlock added in v0.2.2

func (*FakeService) UnsetPermission added in v0.2.2

func (f *FakeService) UnsetPermission(ctx context.Context, workspaceID, teamID resource.ID) error

func (*FakeService) Update added in v0.2.2

func (f *FakeService) Update(_ context.Context, _ resource.ID, opts UpdateOptions) (*Workspace, error)

type LatestRun

type LatestRun struct {
	ID     resource.ID
	Status runStatus
}

LatestRun is a summary of the latest run for a workspace

type ListOptions

type ListOptions struct {
	Search       string
	Tags         []string
	Organization *string `schema:"organization_name"`

	resource.PageOptions
}

ListOptions are options for paginating and filtering a list of Workspaces

type ListTagsOptions

type ListTagsOptions struct {
	resource.PageOptions
}

ListTagsOptions are options for paginating and filtering a list of tags

type ListWorkspaceTagsOptions

type ListWorkspaceTagsOptions struct {
	resource.PageOptions
}

ListWorkspaceTagsOptions are options for paginating and filtering a list of workspace tags

type LockButton

type LockButton struct {
	State    string // locked or unlocked
	Text     string // button text
	Tooltip  string // button tooltip
	Disabled bool   // button greyed out or not
	Message  string // message accompanying button
	Action   string // form URL
}

type Options

type Options struct {
	*sql.DB
	*sql.Listener
	*tfeapi.Responder
	*authz.Authorizer
	html.Renderer

	logr.Logger

	OrganizationService *organization.Service
	VCSProviderService  *vcsprovider.Service
	TeamService         *team.Service
	UserService         *user.Service
	ConnectionService   *connections.Service
}

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) AddTags added in v0.2.2

func (s *Service) AddTags(ctx context.Context, workspaceID resource.ID, tags []TagSpec) error

func (*Service) AfterCreateWorkspace added in v0.1.8

func (s *Service) AfterCreateWorkspace(hook func(context.Context, *Workspace) error)

func (*Service) BeforeCreateWorkspace added in v0.2.0

func (s *Service) BeforeCreateWorkspace(hook func(context.Context, *Workspace) error)

func (*Service) BeforeUpdateWorkspace added in v0.2.0

func (s *Service) BeforeUpdateWorkspace(hook func(context.Context, *Workspace) error)

func (*Service) Create added in v0.2.2

func (s *Service) Create(ctx context.Context, opts CreateOptions) (*Workspace, error)

func (*Service) Delete added in v0.2.2

func (s *Service) Delete(ctx context.Context, workspaceID resource.ID) (*Workspace, error)

func (*Service) DeleteTags added in v0.2.2

func (s *Service) DeleteTags(ctx context.Context, organization string, tagIDs []resource.ID) error

func (*Service) Get added in v0.2.2

func (s *Service) Get(ctx context.Context, workspaceID resource.ID) (*Workspace, error)

func (*Service) GetByName added in v0.2.2

func (s *Service) GetByName(ctx context.Context, organization, workspace string) (*Workspace, error)

func (*Service) GetWorkspacePolicy added in v0.3.6

func (s *Service) GetWorkspacePolicy(ctx context.Context, workspaceID resource.ID) (authz.WorkspacePolicy, error)

GetWorkspacePolicy retrieves the authorization policy for a workspace.

NOTE: there is no auth because it is used in the process of making an auth decision.

func (*Service) List added in v0.2.2

func (s *Service) List(ctx context.Context, opts ListOptions) (*resource.Page[*Workspace], error)

func (*Service) ListConnectedWorkspaces added in v0.1.14

func (s *Service) ListConnectedWorkspaces(ctx context.Context, vcsProviderID resource.ID, repoPath string) ([]*Workspace, error)

func (*Service) ListTags added in v0.2.2

func (s *Service) ListTags(ctx context.Context, organization string, opts ListTagsOptions) (*resource.Page[*Tag], error)

func (*Service) ListWorkspaceTags added in v0.2.2

func (s *Service) ListWorkspaceTags(ctx context.Context, workspaceID resource.ID, opts ListWorkspaceTagsOptions) (*resource.Page[*Tag], error)

func (*Service) Lock added in v0.2.2

func (s *Service) Lock(ctx context.Context, workspaceID resource.ID, runID *resource.ID) (*Workspace, error)

Lock locks the workspace. A workspace can only be locked on behalf of a run or a user. If the former then runID must be populated. Otherwise a user is extracted from the context.

func (*Service) RemoveTags added in v0.2.2

func (s *Service) RemoveTags(ctx context.Context, workspaceID resource.ID, tags []TagSpec) error

func (*Service) SetCurrentRun

func (s *Service) SetCurrentRun(ctx context.Context, workspaceID, runID resource.ID) (*Workspace, error)

SetCurrentRun sets the current run for the workspace

func (*Service) SetPermission added in v0.2.2

func (s *Service) SetPermission(ctx context.Context, workspaceID, teamID resource.ID, role authz.Role) error

func (*Service) TagWorkspaces added in v0.2.2

func (s *Service) TagWorkspaces(ctx context.Context, tagID resource.ID, workspaceIDs []resource.ID) error

func (*Service) Unlock added in v0.2.2

func (s *Service) Unlock(ctx context.Context, workspaceID resource.ID, runID *resource.ID, force bool) (*Workspace, error)

Unlock unlocks the workspace. A workspace can only be unlocked on behalf of a run or a user. If the former then runID must be non-nil; otherwise a user is extracted from the context.

func (*Service) UnsetPermission added in v0.2.2

func (s *Service) UnsetPermission(ctx context.Context, workspaceID, teamID resource.ID) error

func (*Service) Update added in v0.2.2

func (s *Service) Update(ctx context.Context, workspaceID resource.ID, opts UpdateOptions) (*Workspace, error)

func (*Service) Watch added in v0.2.2

func (s *Service) Watch(ctx context.Context) (<-chan pubsub.Event[*Workspace], func())

type Tag

type Tag struct {
	ID            resource.ID // ID of the form 'tag-*'. Globally unique.
	Name          string      // Meaningful symbol. Unique to an organization.
	InstanceCount int         // Number of workspaces that have this tag
	Organization  string      // Organization this tag belongs to.
}

Tag is a symbol associated with one or more workspaces. Helps searching and grouping workspaces.

type TagSpec

type TagSpec struct {
	ID   *resource.ID
	Name string
}

TagSpec specifies a tag. Either ID or Name must be non-nil for it to be valid.

func (TagSpec) Valid

func (s TagSpec) Valid() error

type TagSpecs

type TagSpecs []TagSpec

func (TagSpecs) LogValue

func (specs TagSpecs) LogValue() slog.Value

type UpdateOptions

type UpdateOptions struct {
	AgentPoolID                *resource.ID `json:"agent-pool-id,omitempty"`
	AllowDestroyPlan           *bool
	AutoApply                  *bool
	Name                       *string
	Description                *string
	ExecutionMode              *ExecutionMode `json:"execution-mode,omitempty"`
	GlobalRemoteState          *bool
	Operations                 *bool
	QueueAllRuns               *bool
	SpeculativeEnabled         *bool
	StructuredRunOutputEnabled *bool
	TerraformVersion           *string
	TriggerPrefixes            []string
	TriggerPatterns            []string
	WorkingDirectory           *string

	// Always trigger runs. A value of true is mutually exclusive with
	// setting TriggerPatterns or ConnectOptions.TagsRegex.
	AlwaysTrigger *bool

	// Disconnect workspace from repo. It is invalid to specify true for an
	// already disconnected workspace.
	Disconnect bool

	// Specifying ConnectOptions either connects a currently
	// disconnected workspace, or modifies a connection if already
	// connected.
	*ConnectOptions
}

type VCSTriggerStrategy added in v0.1.4

type VCSTriggerStrategy string

VCS trigger strategy determines which VCS events trigger runs

type Workspace

type Workspace struct {
	ID                         resource.ID   `jsonapi:"primary,workspaces"`
	CreatedAt                  time.Time     `jsonapi:"attribute" json:"created_at"`
	UpdatedAt                  time.Time     `jsonapi:"attribute" json:"updated_at"`
	AgentPoolID                *resource.ID  `jsonapi:"attribute" json:"agent-pool-id"`
	AllowDestroyPlan           bool          `jsonapi:"attribute" json:"allow_destroy_plan"`
	AutoApply                  bool          `jsonapi:"attribute" json:"auto_apply"`
	CanQueueDestroyPlan        bool          `jsonapi:"attribute" json:"can_queue_destroy_plan"`
	Description                string        `jsonapi:"attribute" json:"description"`
	Environment                string        `jsonapi:"attribute" json:"environment"`
	ExecutionMode              ExecutionMode `jsonapi:"attribute" json:"execution_mode"`
	GlobalRemoteState          bool          `jsonapi:"attribute" json:"global_remote_state"`
	MigrationEnvironment       string        `jsonapi:"attribute" json:"migration_environment"`
	Name                       string        `jsonapi:"attribute" json:"name"`
	QueueAllRuns               bool          `jsonapi:"attribute" json:"queue_all_runs"`
	SpeculativeEnabled         bool          `jsonapi:"attribute" json:"speculative_enabled"`
	StructuredRunOutputEnabled bool          `jsonapi:"attribute" json:"structured_run_output_enabled"`
	SourceName                 string        `jsonapi:"attribute" json:"source_name"`
	SourceURL                  string        `jsonapi:"attribute" json:"source_url"`
	TerraformVersion           string        `jsonapi:"attribute" json:"terraform_version"`
	WorkingDirectory           string        `jsonapi:"attribute" json:"working_directory"`
	Organization               string        `jsonapi:"attribute" json:"organization"`
	LatestRun                  *LatestRun    `jsonapi:"attribute" json:"latest_run"`
	Tags                       []string      `jsonapi:"attribute" json:"tags"`
	Lock                       *resource.ID  `jsonapi:"attribute" json:"lock"`

	// VCS Connection; nil means the workspace is not connected.
	Connection *Connection

	// TriggerPatterns is mutually exclusive with Connection.TagsRegex.
	//
	// Note: TriggerPatterns ought to belong in Connection but it is included at
	// the root of Workspace because the go-tfe integration tests set
	// this field without setting the connection!
	TriggerPatterns []string

	// TriggerPrefixes exists only to pass the go-tfe integration tests and
	// is not used when determining whether to trigger runs. Use
	// TriggerPatterns instead.
	TriggerPrefixes []string
}

Workspace is a terraform workspace.

func NewWorkspace

func NewWorkspace(opts CreateOptions) (*Workspace, error)

func (*Workspace) Enlock

func (ws *Workspace) Enlock(id resource.ID) error

Enlock locks the workspace with the given ID. The ID must be either a run or user ID.

func (*Workspace) ExecutionModes

func (ws *Workspace) ExecutionModes() []string

ExecutionModes returns a list of possible execution modes

func (*Workspace) Locked

func (ws *Workspace) Locked() bool

func (*Workspace) LogValue

func (ws *Workspace) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (*Workspace) String

func (ws *Workspace) String() string

func (*Workspace) Unlock

func (ws *Workspace) Unlock(id resource.ID, force bool) error

Unlock the workspace with the given ID. The ID must be either a run or user ID.

func (*Workspace) Update

func (ws *Workspace) Update(opts UpdateOptions) (*bool, error)

Update updates the workspace with the given options. A boolean is returned to indicate whether the workspace is to be connected to a repo (true), disconnected from a repo (false), or neither (nil).

type WorkspacePage

type WorkspacePage struct {
	organization.OrganizationPage

	Workspace *Workspace
}

WorkspacePage contains data shared by all workspace-based pages.

func NewPage

func NewPage(r *http.Request, title string, workspace *Workspace) WorkspacePage

Jump to

Keyboard shortcuts

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