Skip to content

Commit 2d2dfec

Browse files
committed
Refactor init-script endpoint, apply review suggestions for db
1 parent c462a69 commit 2d2dfec

File tree

6 files changed

+63
-14
lines changed

6 files changed

+63
-14
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4707,7 +4707,7 @@ func (q *querier) UpdateTemplateVersionDescriptionByJobID(ctx context.Context, a
47074707
}
47084708

47094709
func (q *querier) UpdateTemplateVersionExternalAgentByJobID(ctx context.Context, arg database.UpdateTemplateVersionExternalAgentByJobIDParams) error {
4710-
// An actor is allowed to update the template version AI task flag if they are authorized to update the template.
4710+
// An actor is allowed to update the template version external agent flag if they are authorized to update the template.
47114711
tv, err := q.db.GetTemplateVersionByJobID(ctx, arg.JobID)
47124712
if err != nil {
47134713
return err

coderd/database/migrations/000356_external_agents.down.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ ALTER TABLE workspace_builds DROP COLUMN has_external_agent;
3636

3737
DROP VIEW workspace_build_with_user;
3838

39-
-- We're adding the has_external_agent column.
4039
CREATE VIEW workspace_build_with_user AS
4140
SELECT
4241
workspace_builds.id,

coderd/database/migrations/000356_external_agents.up.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SELECT
2323
template_versions.archived,
2424
template_versions.source_example_id,
2525
template_versions.has_ai_task,
26-
template_versions.has_external_agent,
26+
template_versions.has_external_agent,
2727
COALESCE(visible_users.avatar_url, '' :: text) AS created_by_avatar_url,
2828
COALESCE(visible_users.username, '' :: text) AS created_by_username,
2929
COALESCE(visible_users.name, '' :: text) AS created_by_name
@@ -66,7 +66,7 @@ SELECT
6666
workspace_builds.template_version_preset_id,
6767
workspace_builds.has_ai_task,
6868
workspace_builds.ai_task_sidebar_app_id,
69-
workspace_builds.has_external_agent,
69+
workspace_builds.has_external_agent,
7070
COALESCE(
7171
visible_users.avatar_url,
7272
'' :: text

coderd/init_script.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,9 @@ func (api *API) initScript(rw http.ResponseWriter, r *http.Request) {
2222
os := strings.ToLower(chi.URLParam(r, "os"))
2323
arch := strings.ToLower(chi.URLParam(r, "arch"))
2424

25-
if os != "linux" && os != "darwin" && os != "windows" {
26-
rw.WriteHeader(http.StatusBadRequest)
27-
return
28-
}
29-
if arch != "amd64" && arch != "arm64" && arch != "armv7" {
30-
rw.WriteHeader(http.StatusBadRequest)
31-
return
32-
}
33-
3425
script, exists := provisionersdk.AgentScriptEnv()[fmt.Sprintf("CODER_AGENT_SCRIPT_%s_%s", os, arch)]
3526
if !exists {
36-
rw.WriteHeader(http.StatusNotFound)
27+
rw.WriteHeader(http.StatusBadRequest)
3728
return
3829
}
3930
script = strings.ReplaceAll(script, "${ACCESS_URL}", api.AccessURL.String()+"/")

coderd/init_script_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package coderd_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/coder/coder/v2/coderd/coderdtest"
11+
)
12+
13+
func TestInitScript(t *testing.T) {
14+
t.Parallel()
15+
16+
t.Run("OK", func(t *testing.T) {
17+
t.Parallel()
18+
client := coderdtest.New(t, nil)
19+
script, err := client.InitScript(context.Background(), "windows", "amd64")
20+
require.NoError(t, err)
21+
require.NotEmpty(t, script)
22+
})
23+
24+
t.Run("BadRequest", func(t *testing.T) {
25+
t.Parallel()
26+
client := coderdtest.New(t, nil)
27+
_, err := client.InitScript(context.Background(), "darwin", "armv7")
28+
require.Error(t, err)
29+
fmt.Printf("err: %+v\n", err)
30+
})
31+
}

codersdk/init_script.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package codersdk
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
)
9+
10+
func (c *Client) InitScript(ctx context.Context, os, arch string) (string, error) {
11+
url := fmt.Sprintf("/api/v2/init-script/%s/%s", os, arch)
12+
res, err := c.Request(ctx, http.MethodGet, url, nil)
13+
if err != nil {
14+
return "", err
15+
}
16+
defer res.Body.Close()
17+
18+
if res.StatusCode != http.StatusOK {
19+
return "", ReadBodyAsError(res)
20+
}
21+
22+
script, err := io.ReadAll(res.Body)
23+
if err != nil {
24+
return "", err
25+
}
26+
27+
return string(script), nil
28+
}

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