Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita983416

Browse files
authored
feat: add regions endpoint for proxies feature (#7277)
* feat: add regions endpoint for proxies feature
1 parent6e8ff2d commita983416

File tree

13 files changed

+625
-6
lines changed

13 files changed

+625
-6
lines changed

‎coderd/apidoc/docs.go

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/apidoc/swagger.json

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/coderd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,11 @@ func New(options *Options) *API {
461461
r.Post("/csp/reports",api.logReportCSPViolations)
462462

463463
r.Get("/buildinfo",buildInfo(api.AccessURL))
464+
// /regions is overridden in the enterprise version
465+
r.Group(func(r chi.Router) {
466+
r.Use(apiKeyMiddleware)
467+
r.Get("/regions",api.regions)
468+
})
464469
r.Route("/deployment",func(r chi.Router) {
465470
r.Use(apiKeyMiddleware)
466471
r.Get("/config",api.deploymentValues)

‎coderd/workspaceproxies.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package coderd
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"net/http"
7+
8+
"github.com/google/uuid"
9+
"golang.org/x/xerrors"
10+
11+
"github.com/coder/coder/coderd/database/dbauthz"
12+
"github.com/coder/coder/coderd/httpapi"
13+
"github.com/coder/coder/codersdk"
14+
)
15+
16+
func (api*API)PrimaryRegion(ctx context.Context) (codersdk.Region,error) {
17+
deploymentIDStr,err:=api.Database.GetDeploymentID(ctx)
18+
ifxerrors.Is(err,sql.ErrNoRows) {
19+
// This shouldn't happen but it's pretty easy to avoid this causing
20+
// issues by falling back to a nil UUID.
21+
deploymentIDStr=uuid.Nil.String()
22+
}elseiferr!=nil {
23+
return codersdk.Region{},xerrors.Errorf("get deployment ID: %w",err)
24+
}
25+
deploymentID,err:=uuid.Parse(deploymentIDStr)
26+
iferr!=nil {
27+
// This also shouldn't happen but we fallback to nil UUID.
28+
deploymentID=uuid.Nil
29+
}
30+
31+
return codersdk.Region{
32+
ID:deploymentID,
33+
// TODO: provide some way to customize these fields for the primary
34+
// region
35+
Name:"primary",
36+
DisplayName:"Default",
37+
IconURL:"/emojis/1f60e.png",// face with sunglasses
38+
Healthy:true,
39+
PathAppURL:api.AccessURL.String(),
40+
WildcardHostname:api.AppHostname,
41+
},nil
42+
}
43+
44+
// @Summary Get site-wide regions for workspace connections
45+
// @ID get-site-wide-regions-for-workspace-connections
46+
// @Security CoderSessionToken
47+
// @Produce json
48+
// @Tags WorkspaceProxies
49+
// @Success 200 {object} codersdk.RegionsResponse
50+
// @Router /regions [get]
51+
func (api*API)regions(rw http.ResponseWriter,r*http.Request) {
52+
ctx:=r.Context()
53+
//nolint:gocritic // this route intentionally requests resources that users
54+
// cannot usually access in order to give them a full list of available
55+
// regions.
56+
ctx=dbauthz.AsSystemRestricted(ctx)
57+
58+
region,err:=api.PrimaryRegion(ctx)
59+
iferr!=nil {
60+
httpapi.InternalServerError(rw,err)
61+
return
62+
}
63+
64+
httpapi.Write(ctx,rw,http.StatusOK, codersdk.RegionsResponse{
65+
Regions: []codersdk.Region{region},
66+
})
67+
}

‎coderd/workspaceproxies_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package coderd_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/uuid"
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/coder/coder/coderd/coderdtest"
10+
"github.com/coder/coder/coderd/database/dbtestutil"
11+
"github.com/coder/coder/codersdk"
12+
"github.com/coder/coder/testutil"
13+
)
14+
15+
funcTestRegions(t*testing.T) {
16+
t.Parallel()
17+
18+
t.Run("OK",func(t*testing.T) {
19+
t.Parallel()
20+
constappHostname="*.apps.coder.test"
21+
22+
db,pubsub:=dbtestutil.NewDB(t)
23+
deploymentID:=uuid.New()
24+
25+
ctx:=testutil.Context(t,testutil.WaitLong)
26+
err:=db.InsertDeploymentID(ctx,deploymentID.String())
27+
require.NoError(t,err)
28+
29+
client:=coderdtest.New(t,&coderdtest.Options{
30+
AppHostname:appHostname,
31+
Database:db,
32+
Pubsub:pubsub,
33+
})
34+
_=coderdtest.CreateFirstUser(t,client)
35+
36+
regions,err:=client.Regions(ctx)
37+
require.NoError(t,err)
38+
39+
require.Len(t,regions,1)
40+
require.NotEqual(t,uuid.Nil,regions[0].ID)
41+
require.Equal(t,regions[0].ID,deploymentID)
42+
require.Equal(t,"primary",regions[0].Name)
43+
require.Equal(t,"Default",regions[0].DisplayName)
44+
require.NotEmpty(t,regions[0].IconURL)
45+
require.True(t,regions[0].Healthy)
46+
require.Equal(t,client.URL.String(),regions[0].PathAppURL)
47+
require.Equal(t,appHostname,regions[0].WildcardHostname)
48+
49+
// Ensure the primary region ID is constant.
50+
regions2,err:=client.Regions(ctx)
51+
require.NoError(t,err)
52+
require.Equal(t,regions[0].ID,regions2[0].ID)
53+
})
54+
55+
t.Run("RequireAuth",func(t*testing.T) {
56+
t.Parallel()
57+
58+
ctx:=testutil.Context(t,testutil.WaitLong)
59+
client:=coderdtest.New(t,nil)
60+
_=coderdtest.CreateFirstUser(t,client)
61+
62+
unauthedClient:=codersdk.New(client.URL)
63+
regions,err:=unauthedClient.Regions(ctx)
64+
require.Error(t,err)
65+
require.Empty(t,regions)
66+
})
67+
}

‎codersdk/workspaceproxy.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,44 @@ func (c *Client) DeleteWorkspaceProxyByName(ctx context.Context, name string) er
130130
func (c*Client)DeleteWorkspaceProxyByID(ctx context.Context,id uuid.UUID)error {
131131
returnc.DeleteWorkspaceProxyByName(ctx,id.String())
132132
}
133+
134+
typeRegionsResponsestruct {
135+
Regions []Region`json:"regions"`
136+
}
137+
138+
typeRegionstruct {
139+
ID uuid.UUID`json:"id" format:"uuid"`
140+
Namestring`json:"name"`
141+
DisplayNamestring`json:"display_name"`
142+
IconURLstring`json:"icon_url"`
143+
Healthybool`json:"healthy"`
144+
145+
// PathAppURL is the URL to the base path for path apps. Optional
146+
// unless wildcard_hostname is set.
147+
// E.g. https://us.example.com
148+
PathAppURLstring`json:"path_app_url"`
149+
150+
// WildcardHostname is the wildcard hostname for subdomain apps.
151+
// E.g. *.us.example.com
152+
// E.g. *--suffix.au.example.com
153+
// Optional. Does not need to be on the same domain as PathAppURL.
154+
WildcardHostnamestring`json:"wildcard_hostname"`
155+
}
156+
157+
func (c*Client)Regions(ctx context.Context) ([]Region,error) {
158+
res,err:=c.Request(ctx,http.MethodGet,
159+
"/api/v2/regions",
160+
nil,
161+
)
162+
iferr!=nil {
163+
returnnil,xerrors.Errorf("make request: %w",err)
164+
}
165+
deferres.Body.Close()
166+
167+
ifres.StatusCode!=http.StatusOK {
168+
returnnil,ReadBodyAsError(res)
169+
}
170+
171+
varregionsRegionsResponse
172+
returnregions.Regions,json.NewDecoder(res.Body).Decode(&regions)
173+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp