feat: add tags to provisioner keys api (#13989)

This commit is contained in:
Garrett Delfosse
2024-07-25 11:20:45 -04:00
committed by GitHub
parent ca83017dc1
commit 6161d173d3
21 changed files with 120 additions and 49 deletions

6
coderd/apidoc/docs.go generated
View File

@ -11024,6 +11024,12 @@ const docTemplate = `{
"organization": {
"type": "string",
"format": "uuid"
},
"tags": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
},

View File

@ -9950,6 +9950,12 @@
"organization": {
"type": "string",
"format": "uuid"
},
"tags": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
},

View File

@ -472,6 +472,7 @@ func ProvisionerKey(t testing.TB, db database.Store, orig database.ProvisionerKe
OrganizationID: takeFirst(orig.OrganizationID, uuid.New()),
Name: takeFirst(orig.Name, namesgenerator.GetRandomName(1)),
HashedSecret: orig.HashedSecret,
Tags: orig.Tags,
})
require.NoError(t, err, "insert provisioner key")
return key

View File

@ -6586,6 +6586,7 @@ func (q *FakeQuerier) InsertProvisionerKey(_ context.Context, arg database.Inser
OrganizationID: arg.OrganizationID,
Name: strings.ToLower(arg.Name),
HashedSecret: arg.HashedSecret,
Tags: arg.Tags,
}
q.provisionerKeys = append(q.provisionerKeys, provisionerKey)
@ -7276,13 +7277,7 @@ func (q *FakeQuerier) ListProvisionerKeysByOrganization(_ context.Context, organ
keys := make([]database.ProvisionerKey, 0)
for _, key := range q.provisionerKeys {
if key.OrganizationID == organizationID {
keys = append(keys, database.ProvisionerKey{
ID: key.ID,
CreatedAt: key.CreatedAt,
OrganizationID: key.OrganizationID,
Name: key.Name,
HashedSecret: key.HashedSecret,
})
keys = append(keys, key)
}
}

View File

@ -754,7 +754,8 @@ CREATE TABLE provisioner_keys (
created_at timestamp with time zone NOT NULL,
organization_id uuid NOT NULL,
name character varying(64) NOT NULL,
hashed_secret bytea NOT NULL
hashed_secret bytea NOT NULL,
tags jsonb NOT NULL
);
CREATE TABLE replicas (

View File

@ -0,0 +1 @@
ALTER TABLE provisioner_keys DROP COLUMN tags;

View File

@ -0,0 +1,2 @@
ALTER TABLE provisioner_keys ADD COLUMN tags jsonb DEFAULT '{}'::jsonb NOT NULL;
ALTER TABLE provisioner_keys ALTER COLUMN tags DROP DEFAULT;

View File

@ -2191,6 +2191,7 @@ type ProvisionerKey struct {
OrganizationID uuid.UUID `db:"organization_id" json:"organization_id"`
Name string `db:"name" json:"name"`
HashedSecret []byte `db:"hashed_secret" json:"hashed_secret"`
Tags StringMap `db:"tags" json:"tags"`
}
type Replica struct {

View File

@ -5533,7 +5533,7 @@ func (q *sqlQuerier) DeleteProvisionerKey(ctx context.Context, id uuid.UUID) err
const getProvisionerKeyByID = `-- name: GetProvisionerKeyByID :one
SELECT
id, created_at, organization_id, name, hashed_secret
id, created_at, organization_id, name, hashed_secret, tags
FROM
provisioner_keys
WHERE
@ -5549,13 +5549,14 @@ func (q *sqlQuerier) GetProvisionerKeyByID(ctx context.Context, id uuid.UUID) (P
&i.OrganizationID,
&i.Name,
&i.HashedSecret,
&i.Tags,
)
return i, err
}
const getProvisionerKeyByName = `-- name: GetProvisionerKeyByName :one
SELECT
id, created_at, organization_id, name, hashed_secret
id, created_at, organization_id, name, hashed_secret, tags
FROM
provisioner_keys
WHERE
@ -5578,21 +5579,23 @@ func (q *sqlQuerier) GetProvisionerKeyByName(ctx context.Context, arg GetProvisi
&i.OrganizationID,
&i.Name,
&i.HashedSecret,
&i.Tags,
)
return i, err
}
const insertProvisionerKey = `-- name: InsertProvisionerKey :one
INSERT INTO
provisioner_keys (
id,
provisioner_keys (
id,
created_at,
organization_id,
name,
hashed_secret
)
name,
hashed_secret,
tags
)
VALUES
($1, $2, $3, lower($5), $4) RETURNING id, created_at, organization_id, name, hashed_secret
($1, $2, $3, lower($6), $4, $5) RETURNING id, created_at, organization_id, name, hashed_secret, tags
`
type InsertProvisionerKeyParams struct {
@ -5600,6 +5603,7 @@ type InsertProvisionerKeyParams struct {
CreatedAt time.Time `db:"created_at" json:"created_at"`
OrganizationID uuid.UUID `db:"organization_id" json:"organization_id"`
HashedSecret []byte `db:"hashed_secret" json:"hashed_secret"`
Tags StringMap `db:"tags" json:"tags"`
Name string `db:"name" json:"name"`
}
@ -5609,6 +5613,7 @@ func (q *sqlQuerier) InsertProvisionerKey(ctx context.Context, arg InsertProvisi
arg.CreatedAt,
arg.OrganizationID,
arg.HashedSecret,
arg.Tags,
arg.Name,
)
var i ProvisionerKey
@ -5618,13 +5623,14 @@ func (q *sqlQuerier) InsertProvisionerKey(ctx context.Context, arg InsertProvisi
&i.OrganizationID,
&i.Name,
&i.HashedSecret,
&i.Tags,
)
return i, err
}
const listProvisionerKeysByOrganization = `-- name: ListProvisionerKeysByOrganization :many
SELECT
id, created_at, organization_id, name, hashed_secret
id, created_at, organization_id, name, hashed_secret, tags
FROM
provisioner_keys
WHERE
@ -5646,6 +5652,7 @@ func (q *sqlQuerier) ListProvisionerKeysByOrganization(ctx context.Context, orga
&i.OrganizationID,
&i.Name,
&i.HashedSecret,
&i.Tags,
); err != nil {
return nil, err
}

View File

@ -1,14 +1,15 @@
-- name: InsertProvisionerKey :one
INSERT INTO
provisioner_keys (
id,
provisioner_keys (
id,
created_at,
organization_id,
name,
hashed_secret
)
name,
hashed_secret,
tags
)
VALUES
($1, $2, $3, lower(@name), $4) RETURNING *;
($1, $2, $3, lower(@name), $4, $5) RETURNING *;
-- name: GetProvisionerKeyByID :one
SELECT

View File

@ -44,6 +44,9 @@ sql:
- column: "provisioner_daemons.tags"
go_type:
type: "StringMap"
- column: "provisioner_keys.tags"
go_type:
type: "StringMap"
- column: "provisioner_jobs.tags"
go_type:
type: "StringMap"

View File

@ -14,7 +14,7 @@ import (
"github.com/coder/coder/v2/cryptorand"
)
func New(organizationID uuid.UUID, name string) (database.InsertProvisionerKeyParams, string, error) {
func New(organizationID uuid.UUID, name string, tags map[string]string) (database.InsertProvisionerKeyParams, string, error) {
id := uuid.New()
secret, err := cryptorand.HexString(64)
if err != nil {
@ -23,12 +23,17 @@ func New(organizationID uuid.UUID, name string) (database.InsertProvisionerKeyPa
hashedSecret := HashSecret(secret)
token := fmt.Sprintf("%s:%s", id, secret)
if tags == nil {
tags = map[string]string{}
}
return database.InsertProvisionerKeyParams{
ID: id,
CreatedAt: dbtime.Now(),
OrganizationID: organizationID,
Name: name,
HashedSecret: hashedSecret,
Tags: tags,
}, token, nil
}