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

Commitf40cbf3

Browse files
committed
feat(provisioners) - add endpoint to fetch tags associated to a key using its id
1 parente55e8ee commitf40cbf3

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

‎coderd/coderd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,11 @@ func New(options *Options) *API {
11351135
})
11361136
})
11371137
})
1138+
r.Route("/provisionerkeys",func(r chi.Router) {
1139+
r.Route("/{provisionerkeyid}",func(r chi.Router) {
1140+
r.Get("/tags",api.getProvisionKeyTags)
1141+
})
1142+
})
11381143
r.Route("/workspaceagents",func(r chi.Router) {
11391144
r.Post("/azure-instance-identity",api.postWorkspaceAuthAzureInstanceIdentity)
11401145
r.Post("/aws-instance-identity",api.postWorkspaceAuthAWSInstanceIdentity)

‎coderd/provisionerkeys.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package coderd
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/go-chi/chi/v5"
7+
"github.com/google/uuid"
8+
9+
"github.com/coder/coder/v2/coderd/httpapi"
10+
"github.com/coder/coder/v2/codersdk"
11+
)
12+
13+
// Returns tags from a provisioner key.
14+
// The purpose of this function is to return the provisioner tags
15+
// associated with the provisioner key using the ID.
16+
// GET /provisionerkeys/{provisionerkeyid}/tags
17+
func (api*API)getProvisionKeyTags(rw http.ResponseWriter,r*http.Request) {
18+
var (
19+
ctx=r.Context()
20+
keyID=chi.URLParam(r,"provisionerkeyid")
21+
)
22+
23+
parsedKeyID,err:=uuid.Parse(keyID)
24+
iferr!=nil {
25+
httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{
26+
Message:"Invalid provisioner key.",
27+
Detail:err.Error(),
28+
})
29+
return
30+
}
31+
32+
key,err:=api.Database.GetProvisionerKeyByID(ctx,parsedKeyID)
33+
iferr!=nil {
34+
httpapi.Write(ctx,rw,http.StatusInternalServerError, codersdk.Response{
35+
Message:"Internal error fetching provisioner tags.",
36+
Detail:err.Error(),
37+
})
38+
return
39+
}
40+
41+
httpapi.Write(ctx,rw,http.StatusOK,key.Tags)
42+
}

‎coderd/provisionerkeys_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package coderd_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/google/uuid"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/coder/coder/v2/coderd/coderdtest"
11+
"github.com/coder/coder/v2/codersdk"
12+
"github.com/coder/coder/v2/testutil"
13+
)
14+
15+
funcTestProvisionerKeyTags(t*testing.T) {
16+
t.Parallel()
17+
t.Run("GetTags",func(t*testing.T) {
18+
t.Parallel()
19+
20+
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitShort)
21+
defercancel()
22+
23+
client:=coderdtest.New(t,&coderdtest.Options{IncludeProvisionerDaemon:true})
24+
user:=coderdtest.CreateFirstUser(t,client)
25+
26+
key,err:=client.CreateProvisionerKey(ctx,user.OrganizationID, codersdk.CreateProvisionerKeyRequest{
27+
Tags:map[string]string{"key1":"value1","key2":"value2"},
28+
})
29+
require.NoError(t,err)
30+
31+
keyID,err:=uuid.Parse(key.Key)
32+
require.NoError(t,err)
33+
34+
tags,err:=client.GetProvisionTagsByKeyID(ctx,keyID)
35+
require.NoError(t,err)
36+
require.Equal(t,tags, codersdk.ProvisionerKeyTags{"key1":"value1","key2":"value2"})
37+
})
38+
39+
t.Run("InvalidKeyID",func(t*testing.T) {
40+
t.Parallel()
41+
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitShort)
42+
defercancel()
43+
44+
client:=coderdtest.New(t,&coderdtest.Options{IncludeProvisionerDaemon:true})
45+
_,err:=client.GetProvisionTagsByKeyID(ctx,uuid.MustParse("00000000-0000-0000-0000-000000000000"))
46+
require.Error(t,err)
47+
})
48+
}

‎codersdk/provisionerdaemons.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,24 @@ func (c *Client) ListProvisionerKeys(ctx context.Context, organizationID uuid.UU
368368
returnresp,json.NewDecoder(res.Body).Decode(&resp)
369369
}
370370

371+
// GetProvisionTagsByKeyID returns the provisioner tags associated with the provisioner key ID.
372+
func (c*Client)GetProvisionTagsByKeyID(ctx context.Context,provisionerKeyID uuid.UUID) (ProvisionerKeyTags,error) {
373+
res,err:=c.Request(ctx,http.MethodGet,
374+
fmt.Sprintf("/api/v2/provisionerkeys/%s/tags",provisionerKeyID.String()),
375+
nil,
376+
)
377+
iferr!=nil {
378+
returnnil,xerrors.Errorf("make request: %w",err)
379+
}
380+
deferres.Body.Close()
381+
382+
ifres.StatusCode!=http.StatusOK {
383+
returnnil,ReadBodyAsError(res)
384+
}
385+
varrespProvisionerKeyTags
386+
returnresp,json.NewDecoder(res.Body).Decode(&resp)
387+
}
388+
371389
// ListProvisionerKeyDaemons lists all provisioner keys with their associated daemons for an organization.
372390
func (c*Client)ListProvisionerKeyDaemons(ctx context.Context,organizationID uuid.UUID) ([]ProvisionerKeyDaemons,error) {
373391
res,err:=c.Request(ctx,http.MethodGet,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp