Skip to content

Commit

Permalink
feat: variable sets (#574)
Browse files Browse the repository at this point in the history
Fixes #306
  • Loading branch information
leg100 authored Sep 2, 2023
1 parent 8268643 commit 419e2fb
Show file tree
Hide file tree
Showing 67 changed files with 4,406 additions and 828 deletions.
7 changes: 7 additions & 0 deletions hack/go-tfe-tests.bash
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ tests+=('TestConfigurationVersionsCreate')
tests+=('TestConfigurationVersionsUpload')
tests+=('TestConfigurationVersionsDownload')
tests+=('TestVariables')
tests+=('TestVariableSetsCreate')
tests+=('TestVariableSetsUpdate$')
tests+=('TestVariableSetsList$')
tests+=('TestVariableSetsListForWorkspace')
tests+=('TestVariableSetsRead')
tests+=('TestVariableSetsApplyToAndRemoveFromWorkspaces')
tests+=('TestVariableSetsDelete')
tests+=('TestStateVersion')

# only run these tests if env vars are present - otherwise the tests fail early
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type (
// client allows the agent to communicate with the server endpoints.
client interface {
GetWorkspace(ctx context.Context, workspaceID string) (*workspace.Workspace, error)
ListVariables(ctx context.Context, workspaceID string) ([]*variable.Variable, error)
ListEffectiveVariables(ctx context.Context, runID string) ([]*variable.Variable, error)
GetPlanFile(ctx context.Context, id string, format run.PlanFormat) ([]byte, error)
UploadPlanFile(ctx context.Context, id string, plan []byte, format run.PlanFormat) error
GetLockFile(ctx context.Context, id string) ([]byte, error)
Expand Down
4 changes: 2 additions & 2 deletions internal/agent/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func newEnvironment(
}
envs := internal.SafeAppend(agent.envs, internal.CredentialEnv(agent.Hostname(), token))

// retrieve workspace variables and add them to the environment
variables, err := agent.ListVariables(ctx, run.WorkspaceID)
// retrieve variables and add them to the environment
variables, err := agent.ListEffectiveVariables(ctx, run.ID)
if err != nil {
return nil, errors.Wrap(err, "retrieving workspace variables")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/auth/team_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (h *webHandlers) getTeam(w http.ResponseWriter, r *http.Request) {
Existing: usernames,
Action: paths.AddMemberTeam(team.ID),
Placeholder: "Add user",
Width: "wide",
Width: html.WideDropDown,
},
})
}
Expand Down
1 change: 1 addition & 0 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func New(ctx context.Context, logger logr.Logger, cfg Config) (*Daemon, error) {
Responder: responder,
WorkspaceAuthorizer: workspaceService,
WorkspaceService: workspaceService,
RunService: runService,
})

agent, err := agent.NewAgent(
Expand Down
18 changes: 0 additions & 18 deletions internal/http/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@ const (
pathCookie = "path"
)

type (
// DropdownUI populates a search/dropdown UI component.
DropdownUI struct {
// Name to send along with value in the POST form
Name string
// Existing values to NOT show in the dropdown
Existing []string
// Available values to show in the dropdown
Available []string
// Action is the form action URL
Action string
// Placeholder to show in the input element.
Placeholder string
// Width: "narrow" or "wide"
Width string
}
)

func MarkdownToHTML(md []byte) template.HTML {
return template.HTML(string(markdown.ToHTML(md, nil, nil)))
}
Expand Down
16 changes: 16 additions & 0 deletions internal/http/html/paths/funcmap.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions internal/http/html/paths/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ var specs = []controllerSpec{
Name: "agent_token",
controllerType: resourcePath,
},
{
Name: "variable_set",
controllerType: resourcePath,
nested: []controllerSpec{
{
Name: "variable_set_variable",
controllerType: resourcePath,
},
},
},
{
Name: "organization_token",
controllerType: resourcePath,
Expand Down
33 changes: 33 additions & 0 deletions internal/http/html/paths/variable_set_paths.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions internal/http/html/paths/variable_set_variable_paths.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions internal/http/html/static/css/output.css
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,18 @@ th, td {
border-color: rgb(15 23 42 / var(--tw-border-opacity));
}

.border-x-black {
--tw-border-opacity: 1;
border-left-color: rgb(0 0 0 / var(--tw-border-opacity));
border-right-color: rgb(0 0 0 / var(--tw-border-opacity));
}

.border-y-black {
--tw-border-opacity: 1;
border-top-color: rgb(0 0 0 / var(--tw-border-opacity));
border-bottom-color: rgb(0 0 0 / var(--tw-border-opacity));
}

.bg-black {
--tw-bg-opacity: 1;
background-color: rgb(0 0 0 / var(--tw-bg-opacity));
Expand Down Expand Up @@ -1382,6 +1394,11 @@ th, td {
background-color: rgb(229 231 235 / var(--tw-bg-opacity));
}

.group:hover .group-hover\:bg-gray-400 {
--tw-bg-opacity: 1;
background-color: rgb(156 163 175 / var(--tw-bg-opacity));
}

.peer:checked ~ .peer-checked\:block {
display: block;
}
Expand Down
37 changes: 37 additions & 0 deletions internal/http/html/static/js/variable_set_workspaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
document.addEventListener('alpine:init', () => {
Alpine.data('variable_set_workspaces', (existing = [], available = []) => ({
open: false,
close(focusAfter) {
if (! this.open) return
this.open = false
focusAfter && focusAfter.focus()
},
search: '',
existing: existing,
available: available,
get filterAvailable() {
return this.available?.filter(
i => i.Name.includes(this.search)
).slice(0, 3)
},
get showPanel() {
return (this.open && this.filterAvailable?.length > 0)
},
addWorkspace(workspace) {
// move workspace from available to existing
this.available = this.available.filter(
i => i !== workspace
)
this.existing.push(workspace)
// hide dropdown box
this.close()
},
deleteWorkspace(workspace) {
// move workspace from existing to available
this.existing = this.existing.filter(
i => i !== workspace
)
this.available.push(workspace)
},
}))
})
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<span id="agent_tokens">
<a href="{{ agentTokensPath .Name }}">agent tokens</a>
</span>
<span id="variable_sets">
<a href="{{ variableSetsPath .Name }}">variable sets</a>
</span>
<span id="vcs_providers">
<a href="{{ vcsProvidersPath .Name }}">VCS providers</a>
</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{ template "layout" . }}

{{ define "content-header-title" }}
{{ template "variables-breadcrumb" . }} / edit
{{ template "workspace-variables-breadcrumb" . }} / edit
{{ end }}

{{ define "content" }}
Expand Down
44 changes: 12 additions & 32 deletions internal/http/html/static/templates/content/variable_list.tmpl
Original file line number Diff line number Diff line change
@@ -1,47 +1,27 @@
{{ template "layout" . }}

{{ define "content-header-title" }}
{{ template "variables-breadcrumb" . }}
{{ template "workspace-variables-breadcrumb" . }}
{{ end }}

{{ define "content-header-links" }}
{{ template "workspace-header-links" . }}
{{ end }}

{{ define "content" }}
<table class="table-fixed w-full text-left break-words border-collapse" id="variables-table">
<thead class="bg-gray-200 border-t border-b border-slate-900">
<tr>
<th class="p-2 w-[25%]">Key</th>
<th class="p-2 w-[50%]">Value</th>
<th class="p-2 w-[15%]">Category</th>
<th class="p-2 w-[10%]"></th>
</tr>
</thead>
<tbody class="border-b border-slate-900">
{{ range .Variables }}
<tr class="even:bg-gray-100">
<td class="p-2"><a class="show-underline" href="{{ editVariablePath .ID }}">{{ .Key }}</a></td>
<td class="p-2">{{ if .Sensitive }}<span class="data">hidden</span>{{ else }}{{ .Value }}{{ end }}</td>
<td class="p-2">{{ .Category }}</td>
<td class="p-2 text-right">
{{ if $.CanDeleteVariable }}
<form action="{{ deleteVariablePath .ID }}" method="POST">
<button id="delete-variable-button" class="btn-danger" onclick="return confirm('Are you sure you want to delete?')">Delete</button>
</form>
{{ end }}
</td>
</tr>
{{ else }}
<tr>
<td>No variables currently exist.</td>
</tr>
{{ end }}
</tbody>
</table>
<span class="text-lg my-2">Workspace Variables ({{ len .WorkspaceVariableTable.Variables }})</span>
{{ template "variable-table" .WorkspaceVariableTable }}
{{ if .CanCreateVariable }}
<form action="{{ newVariablePath $.Workspace.ID }}" method="GET">
<form class="mt-2" action="{{ newVariablePath $.Workspace.ID }}" method="GET">
<button class="btn">Add variable</button>
</form>
{{ end }}
<span class="text-lg mt-4">Variable Sets ({{ len .VariableSetTables }})</span>
{{ range .VariableSetTables }}
<div class="flex flex-col gap-2" id="variable-set-{{ .Name }}">
{{ template "variable-set-item" . }}
{{ template "variable-table" . }}
</div>
<div class="my-2"></div>
{{ end }}
{{ end }}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{ template "layout" . }}

{{ define "content-header-title" }}
{{ template "variables-breadcrumb" . }} / new
{{ template "workspace-variables-breadcrumb" . }} / new
{{ end }}

{{ define "content" }}
Expand Down
21 changes: 21 additions & 0 deletions internal/http/html/static/templates/content/variable_set_edit.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ template "layout" . }}

{{ define "content-header-title" }}
<a href="{{ variableSetsPath .VariableSet.Organization }}">variable sets</a> /
{{ .VariableSet.Name }} /
edit
{{ end }}

{{ define "content" }}
<span class="text-xl">Edit variable set</span>

{{ template "variable-set-form" . }}

<hr class="my-4">

<h3 class="text-xl">Variables</h3>
{{ template "variable-table" .VariableTable }}
<form class="mt-2" action="{{ newVariableSetVariablePath $.VariableSet.ID }}" method="GET">
<button class="btn" id="add-variable-button">Add variable</button>
</form>
{{ end }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{{ template "layout" . }}

{{ define "content-header-title" }}
<a href="{{ variableSetsPath .VariableSet.Organization }}">variable sets</a> /
<a href="{{ editVariableSetPath .VariableSet.ID }}">{{ .VariableSet.Name }}</a> /
variables /
{{ .Variable.ID }} /
edit
{{ end }}

{{ define "content" }}
Edit workspace variable.

{{ template "variable-form" . }}
{{ end }}
Loading

0 comments on commit 419e2fb

Please sign in to comment.