Skip to content

Commit 995a046

Browse files
committed
feat(provisioners) - add endpoint to fetch tags associated to a key using its id
fix: change from database.StringMap to codersdk.ProvisionerKeyTags in endpoint response improve annotations for endpoint move logic to enterprise part generate new doc move logic to enterprise part generate doc
1 parent e55e8ee commit 995a046

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed

coderd/apidoc/docs.go

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codersdk/provisionerdaemons.go

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

371+
// GetProvisionTagsByKey returns the provisioner tags associated with the provisioner key.
372+
func (c *Client) GetProvisionTagsByKey(ctx context.Context, organizationID uuid.UUID, provisionerKey string) (ProvisionerKeyTags, error) {
373+
res, err := c.Request(ctx, http.MethodGet,
374+
fmt.Sprintf("/api/v2/organizations/%s/provisionerkeys/%s/tags", organizationID.String(), provisionerKey),
375+
nil,
376+
)
377+
if err != nil {
378+
return nil, xerrors.Errorf("make request: %w", err)
379+
}
380+
defer res.Body.Close()
381+
382+
if res.StatusCode != http.StatusOK {
383+
return nil, ReadBodyAsError(res)
384+
}
385+
var resp ProvisionerKeyTags
386+
return resp, 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,

docs/reference/api/enterprise.md

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/coderd/coderd.go

+1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
352352
r.Use(
353353
httpmw.ExtractProvisionerKeyParam(options.Database),
354354
)
355+
r.Get("/tags", api.fetchProvisionerKeyTags)
355356
r.Delete("/", api.deleteProvisionerKey)
356357
})
357358
})

enterprise/coderd/provisionerkeys.go

+17
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,23 @@ func (api *API) deleteProvisionerKey(rw http.ResponseWriter, r *http.Request) {
200200
httpapi.Write(ctx, rw, http.StatusNoContent, nil)
201201
}
202202

203+
// @Summary Get provisioner key tags by ID
204+
// @ID get-provisioner-key-tags-by-id
205+
// @Produce json
206+
// @Tags Enterprise
207+
// @Param organization path string true "Organization ID"
208+
// @Param provisionerkeyid path string true "Provisioner Key ID" format(uuid)
209+
// @Success 200 {object} codersdk.ProvisionerKeyTags
210+
// @Router /organizations/{organization}/provisionerkeys/{provisionerkeyid}/tags [get]
211+
func (api *API) fetchProvisionerKeyTags(rw http.ResponseWriter, r *http.Request) {
212+
var (
213+
ctx = r.Context()
214+
pk = httpmw.ProvisionerKeyParam(r)
215+
)
216+
217+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.ProvisionerKeyTags(pk.Tags))
218+
}
219+
203220
func convertProvisionerKeys(dbKeys []database.ProvisionerKey) []codersdk.ProvisionerKey {
204221
keys := make([]codersdk.ProvisionerKey, 0, len(dbKeys))
205222
for _, dbKey := range dbKeys {

enterprise/coderd/provisionerkeys_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,38 @@ func TestProvisionerKeys(t *testing.T) {
133133
err = orgAdmin.DeleteProvisionerKey(ctx, owner.OrganizationID, codersdk.ProvisionerKeyNamePSK)
134134
require.ErrorContains(t, err, "reserved")
135135
}
136+
137+
func TestProvisionerKeyTags(t *testing.T) {
138+
t.Parallel()
139+
t.Run("GetTags", func(t *testing.T) {
140+
t.Parallel()
141+
142+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong*10)
143+
t.Cleanup(cancel)
144+
dv := coderdtest.DeploymentValues(t)
145+
client, owner := coderdenttest.New(t, &coderdenttest.Options{
146+
Options: &coderdtest.Options{
147+
DeploymentValues: dv,
148+
},
149+
LicenseOptions: &coderdenttest.LicenseOptions{
150+
Features: license.Features{
151+
codersdk.FeatureMultipleOrganizations: 1,
152+
},
153+
},
154+
})
155+
156+
//nolint:gocritic // Not the purpose of this test
157+
_, err := client.CreateProvisionerKey(ctx, owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{
158+
Name: "key",
159+
Tags: map[string]string{"key1": "value1", "key2": "value2"},
160+
})
161+
require.NoError(t, err)
162+
163+
tags, err := client.GetProvisionTagsByKeyID(ctx, owner.OrganizationID, "key")
164+
require.NoError(t, err)
165+
require.Equal(t, tags, codersdk.ProvisionerKeyTags{"key1": "value1", "key2": "value2"})
166+
167+
err = client.DeleteProvisionerKey(ctx, owner.OrganizationID, "invalid_key")
168+
require.ErrorContains(t, err, "Resource not found")
169+
})
170+
}

0 commit comments

Comments
 (0)