Skip to content

Commit c462a69

Browse files
committed
Refactor init-script endpoint to use path params instead of query params
1 parent 7d07857 commit c462a69

File tree

5 files changed

+19
-34
lines changed

5 files changed

+19
-34
lines changed

cli/external_workspaces.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,7 @@ func (r *RootCmd) externalWorkspaceAgentInstructions() *serpent.Command {
133133
return xerrors.Errorf("get external agent token for agent %q: %w", workspaceAgent.Name, err)
134134
}
135135

136-
initScriptURL := fmt.Sprintf("%s/api/v2/init-script", client.URL)
137-
if workspaceAgent.OperatingSystem != "linux" || workspaceAgent.Architecture != "amd64" {
138-
initScriptURL = fmt.Sprintf("%s/api/v2/init-script?os=%s&arch=%s", client.URL, workspaceAgent.OperatingSystem, workspaceAgent.Architecture)
139-
}
140-
136+
initScriptURL := fmt.Sprintf("%s/api/v2/init-script/%s/%s", client.URL, workspaceAgent.OperatingSystem, workspaceAgent.Architecture)
141137
agentInfo := externalAgent{
142138
WorkspaceName: workspace.Name,
143139
AgentName: workspaceAgent.Name,
@@ -244,11 +240,7 @@ func fetchExternalAgents(inv *serpent.Invocation, client *codersdk.Client, works
244240
return nil, xerrors.Errorf("get external agent token for agent %q: %w", agent.Name, err)
245241
}
246242

247-
initScriptURL := fmt.Sprintf("%s/api/v2/init-script", client.URL)
248-
if agent.OperatingSystem != "linux" || agent.Architecture != "amd64" {
249-
initScriptURL = fmt.Sprintf("%s/api/v2/init-script?os=%s&arch=%s", client.URL, agent.OperatingSystem, agent.Architecture)
250-
}
251-
243+
initScriptURL := fmt.Sprintf("%s/api/v2/init-script/%s/%s", client.URL, agent.OperatingSystem, agent.Architecture)
252244
externalAgents = append(externalAgents, externalAgent{
253245
AgentName: agent.Name,
254246
AuthType: "token",

cli/external_workspaces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func TestExternalWorkspaces(t *testing.T) {
374374
require.NoError(t, json.Unmarshal(out.Bytes(), &agentInfo))
375375
assert.Equal(t, "token", agentInfo["auth_type"])
376376
assert.NotEmpty(t, agentInfo["auth_token"])
377-
assert.Contains(t, agentInfo["init_script"], "/api/v2/init-script")
377+
assert.Contains(t, agentInfo["init_script"], "/api/v2/init-script/linux/amd64")
378378
})
379379

380380
t.Run("AgentInstructionsNonExistentWorkspace", func(t *testing.T) {

coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ func New(options *Options) *API {
15511551
r.Get("/", api.tailnetRPCConn)
15521552
})
15531553
r.Route("/init-script", func(r chi.Router) {
1554-
r.Get("/", api.initScript)
1554+
r.Get("/{os}/{arch}", api.initScript)
15551555
})
15561556
})
15571557

coderd/init_script.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,30 @@ import (
55
"net/http"
66
"strings"
77

8+
"github.com/go-chi/chi/v5"
9+
810
"github.com/coder/coder/v2/provisionersdk"
911
)
1012

1113
// @Summary Get agent init script
1214
// @ID get-agent-init-script
1315
// @Produce text/plain
1416
// @Tags InitScript
15-
// @Param os query string false "Operating system" default "linux"
16-
// @Param arch query string false "Architecture" default "amd64"
17+
// @Param os path string true "Operating system"
18+
// @Param arch path string true "Architecture"
1719
// @Success 200 "Success"
18-
// @Router /init-script [get]
20+
// @Router /init-script/{os}/{arch} [get]
1921
func (api *API) initScript(rw http.ResponseWriter, r *http.Request) {
20-
os := "linux"
21-
arch := "amd64"
22-
if os := r.URL.Query().Get("os"); os != "" {
23-
os = strings.ToLower(os)
24-
if os != "linux" && os != "darwin" && os != "windows" {
25-
rw.WriteHeader(http.StatusBadRequest)
26-
return
27-
}
22+
os := strings.ToLower(chi.URLParam(r, "os"))
23+
arch := strings.ToLower(chi.URLParam(r, "arch"))
24+
25+
if os != "linux" && os != "darwin" && os != "windows" {
26+
rw.WriteHeader(http.StatusBadRequest)
27+
return
2828
}
29-
if arch := r.URL.Query().Get("arch"); arch != "" {
30-
arch = strings.ToLower(arch)
31-
if arch != "amd64" && arch != "arm64" && arch != "armv7" {
32-
rw.WriteHeader(http.StatusBadRequest)
33-
return
34-
}
29+
if arch != "amd64" && arch != "arm64" && arch != "armv7" {
30+
rw.WriteHeader(http.StatusBadRequest)
31+
return
3532
}
3633

3734
script, exists := provisionersdk.AgentScriptEnv()[fmt.Sprintf("CODER_AGENT_SCRIPT_%s_%s", os, arch)]

site/src/modules/resources/AgentExternal.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ export const AgentExternal: FC<AgentExternalProps> = ({
2121
);
2222

2323
const origin = isChromatic() ? "https://example.com" : window.location.origin;
24-
let initScriptURL = `${origin}/api/v2/init-script`;
25-
if (agent.operating_system !== "linux" || agent.architecture !== "amd64") {
26-
initScriptURL = `${initScriptURL}?os=${agent.operating_system}&arch=${agent.architecture}`;
27-
}
28-
24+
const initScriptURL = `${origin}/api/v2/init-script/${agent.operating_system}/${agent.architecture}`;
2925
useEffect(() => {
3026
if (
3127
isExternalAgent &&

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy