Skip to content

Commit 6161d17

Browse files
authored
feat: add tags to provisioner keys api (coder#13989)
1 parent ca83017 commit 6161d17

21 files changed

+120
-49
lines changed

coderd/apidoc/docs.go

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

coderd/apidoc/swagger.json

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

coderd/database/dbgen/dbgen.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ func ProvisionerKey(t testing.TB, db database.Store, orig database.ProvisionerKe
472472
OrganizationID: takeFirst(orig.OrganizationID, uuid.New()),
473473
Name: takeFirst(orig.Name, namesgenerator.GetRandomName(1)),
474474
HashedSecret: orig.HashedSecret,
475+
Tags: orig.Tags,
475476
})
476477
require.NoError(t, err, "insert provisioner key")
477478
return key

coderd/database/dbmem/dbmem.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6586,6 +6586,7 @@ func (q *FakeQuerier) InsertProvisionerKey(_ context.Context, arg database.Inser
65866586
OrganizationID: arg.OrganizationID,
65876587
Name: strings.ToLower(arg.Name),
65886588
HashedSecret: arg.HashedSecret,
6589+
Tags: arg.Tags,
65896590
}
65906591
q.provisionerKeys = append(q.provisionerKeys, provisionerKey)
65916592

@@ -7276,13 +7277,7 @@ func (q *FakeQuerier) ListProvisionerKeysByOrganization(_ context.Context, organ
72767277
keys := make([]database.ProvisionerKey, 0)
72777278
for _, key := range q.provisionerKeys {
72787279
if key.OrganizationID == organizationID {
7279-
keys = append(keys, database.ProvisionerKey{
7280-
ID: key.ID,
7281-
CreatedAt: key.CreatedAt,
7282-
OrganizationID: key.OrganizationID,
7283-
Name: key.Name,
7284-
HashedSecret: key.HashedSecret,
7285-
})
7280+
keys = append(keys, key)
72867281
}
72877282
}
72887283

coderd/database/dump.sql

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE provisioner_keys DROP COLUMN tags;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE provisioner_keys ADD COLUMN tags jsonb DEFAULT '{}'::jsonb NOT NULL;
2+
ALTER TABLE provisioner_keys ALTER COLUMN tags DROP DEFAULT;

coderd/database/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 16 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/provisionerkeys.sql

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
-- name: InsertProvisionerKey :one
22
INSERT INTO
3-
provisioner_keys (
4-
id,
3+
provisioner_keys (
4+
id,
55
created_at,
66
organization_id,
7-
name,
8-
hashed_secret
9-
)
7+
name,
8+
hashed_secret,
9+
tags
10+
)
1011
VALUES
11-
($1, $2, $3, lower(@name), $4) RETURNING *;
12+
($1, $2, $3, lower(@name), $4, $5) RETURNING *;
1213

1314
-- name: GetProvisionerKeyByID :one
1415
SELECT

coderd/database/sqlc.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ sql:
4444
- column: "provisioner_daemons.tags"
4545
go_type:
4646
type: "StringMap"
47+
- column: "provisioner_keys.tags"
48+
go_type:
49+
type: "StringMap"
4750
- column: "provisioner_jobs.tags"
4851
go_type:
4952
type: "StringMap"

coderd/provisionerkey/provisionerkey.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/coder/coder/v2/cryptorand"
1515
)
1616

17-
func New(organizationID uuid.UUID, name string) (database.InsertProvisionerKeyParams, string, error) {
17+
func New(organizationID uuid.UUID, name string, tags map[string]string) (database.InsertProvisionerKeyParams, string, error) {
1818
id := uuid.New()
1919
secret, err := cryptorand.HexString(64)
2020
if err != nil {
@@ -23,12 +23,17 @@ func New(organizationID uuid.UUID, name string) (database.InsertProvisionerKeyPa
2323
hashedSecret := HashSecret(secret)
2424
token := fmt.Sprintf("%s:%s", id, secret)
2525

26+
if tags == nil {
27+
tags = map[string]string{}
28+
}
29+
2630
return database.InsertProvisionerKeyParams{
2731
ID: id,
2832
CreatedAt: dbtime.Now(),
2933
OrganizationID: organizationID,
3034
Name: name,
3135
HashedSecret: hashedSecret,
36+
Tags: tags,
3237
}, token, nil
3338
}
3439

codersdk/provisionerdaemons.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,17 @@ func (c *Client) ServeProvisionerDaemon(ctx context.Context, req ServeProvisione
274274
}
275275

276276
type ProvisionerKey struct {
277-
ID uuid.UUID `json:"id" table:"-" format:"uuid"`
278-
CreatedAt time.Time `json:"created_at" table:"created_at" format:"date-time"`
279-
OrganizationID uuid.UUID `json:"organization" table:"organization_id" format:"uuid"`
280-
Name string `json:"name" table:"name,default_sort"`
277+
ID uuid.UUID `json:"id" table:"-" format:"uuid"`
278+
CreatedAt time.Time `json:"created_at" table:"created_at" format:"date-time"`
279+
OrganizationID uuid.UUID `json:"organization" table:"organization_id" format:"uuid"`
280+
Name string `json:"name" table:"name,default_sort"`
281+
Tags map[string]string `json:"tags" table:"tags"`
281282
// HashedSecret - never include the access token in the API response
282283
}
283284

284285
type CreateProvisionerKeyRequest struct {
285-
Name string `json:"name"`
286+
Name string `json:"name"`
287+
Tags map[string]string `json:"tags"`
286288
}
287289

288290
type CreateProvisionerKeyResponse struct {

docs/api/enterprise.md

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api/schemas.md

Lines changed: 13 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/cli/provisionerkeys.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ func (r *RootCmd) provisionerKeys() *serpent.Command {
3333
}
3434

3535
func (r *RootCmd) provisionerKeysCreate() *serpent.Command {
36-
orgContext := agpl.NewOrganizationContext()
36+
var (
37+
orgContext = agpl.NewOrganizationContext()
38+
rawTags []string
39+
)
3740

3841
client := new(codersdk.Client)
3942
cmd := &serpent.Command{
@@ -51,8 +54,14 @@ func (r *RootCmd) provisionerKeysCreate() *serpent.Command {
5154
return xerrors.Errorf("current organization: %w", err)
5255
}
5356

57+
tags, err := agpl.ParseProvisionerTags(rawTags)
58+
if err != nil {
59+
return err
60+
}
61+
5462
res, err := client.CreateProvisionerKey(ctx, org.ID, codersdk.CreateProvisionerKeyRequest{
5563
Name: inv.Args[0],
64+
Tags: tags,
5665
})
5766
if err != nil {
5867
return xerrors.Errorf("create provisioner key: %w", err)
@@ -69,7 +78,15 @@ func (r *RootCmd) provisionerKeysCreate() *serpent.Command {
6978
},
7079
}
7180

72-
cmd.Options = serpent.OptionSet{}
81+
cmd.Options = serpent.OptionSet{
82+
{
83+
Flag: "tag",
84+
FlagShorthand: "t",
85+
Env: "CODER_PROVISIONERD_TAGS",
86+
Description: "Tags to filter provisioner jobs by.",
87+
Value: serpent.StringArrayOf(&rawTags),
88+
},
89+
}
7390
orgContext.AttachOptions(cmd)
7491

7592
return cmd

enterprise/cli/provisionerkeys_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestProvisionerKeys(t *testing.T) {
4141
ctx := testutil.Context(t, testutil.WaitMedium)
4242
inv, conf := newCLI(
4343
t,
44-
"provisioner", "keys", "create", name,
44+
"provisioner", "keys", "create", name, "--tag", "foo=bar",
4545
)
4646

4747
pty := ptytest.New(t)
@@ -77,8 +77,10 @@ func TestProvisionerKeys(t *testing.T) {
7777
require.Contains(t, line, "NAME")
7878
require.Contains(t, line, "CREATED AT")
7979
require.Contains(t, line, "ORGANIZATION ID")
80+
require.Contains(t, line, "TAGS")
8081
line = pty.ReadLine(ctx)
8182
require.Contains(t, line, strings.ToLower(name))
83+
require.Contains(t, line, "map[foo:bar]")
8284

8385
inv, conf = newCLI(
8486
t,

enterprise/coderd/provisionerdaemons_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ func TestProvisionerDaemonServe(t *testing.T) {
559559
t.Run("ProvisionerKeyAuth", func(t *testing.T) {
560560
t.Parallel()
561561

562-
insertParams, token, err := provisionerkey.New(uuid.Nil, "dont-TEST-me")
562+
insertParams, token, err := provisionerkey.New(uuid.Nil, "dont-TEST-me", nil)
563563
require.NoError(t, err)
564564

565565
tcs := []struct {

0 commit comments

Comments
 (0)