Skip to content

feat(coderd): add endpoint to fetch provisioner key details #15505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 20, 2024
Next Next commit
feat(provisioners) - add endpoint to fetch tags associated to a key u…
…sing 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
  • Loading branch information
defelmnq committed Nov 14, 2024
commit 995a04679eb42ccdf4c8b751770ce9b3535f56e0
37 changes: 37 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions codersdk/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,24 @@ func (c *Client) ListProvisionerKeys(ctx context.Context, organizationID uuid.UU
return resp, json.NewDecoder(res.Body).Decode(&resp)
}

// GetProvisionTagsByKey returns the provisioner tags associated with the provisioner key.
func (c *Client) GetProvisionTagsByKey(ctx context.Context, organizationID uuid.UUID, provisionerKey string) (ProvisionerKeyTags, error) {
res, err := c.Request(ctx, http.MethodGet,
fmt.Sprintf("/api/v2/organizations/%s/provisionerkeys/%s/tags", organizationID.String(), provisionerKey),
nil,
)
if err != nil {
return nil, xerrors.Errorf("make request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, ReadBodyAsError(res)
}
var resp ProvisionerKeyTags
return resp, json.NewDecoder(res.Body).Decode(&resp)
}

// ListProvisionerKeyDaemons lists all provisioner keys with their associated daemons for an organization.
func (c *Client) ListProvisionerKeyDaemons(ctx context.Context, organizationID uuid.UUID) ([]ProvisionerKeyDaemons, error) {
res, err := c.Request(ctx, http.MethodGet,
Expand Down
36 changes: 36 additions & 0 deletions docs/reference/api/enterprise.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
r.Use(
httpmw.ExtractProvisionerKeyParam(options.Database),
)
r.Get("/tags", api.fetchProvisionerKeyTags)
r.Delete("/", api.deleteProvisionerKey)
})
})
Expand Down
17 changes: 17 additions & 0 deletions enterprise/coderd/provisionerkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,23 @@
httpapi.Write(ctx, rw, http.StatusNoContent, nil)
}

// @Summary Get provisioner key tags by ID
// @ID get-provisioner-key-tags-by-id
// @Produce json
// @Tags Enterprise
// @Param organization path string true "Organization ID"
// @Param provisionerkeyid path string true "Provisioner Key ID" format(uuid)
// @Success 200 {object} codersdk.ProvisionerKeyTags
// @Router /organizations/{organization}/provisionerkeys/{provisionerkeyid}/tags [get]
func (api *API) fetchProvisionerKeyTags(rw http.ResponseWriter, r *http.Request) {

Check failure on line 211 in enterprise/coderd/provisionerkeys.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'api' is not referenced in method's body, consider removing or renaming it as _ (revive)
var (
ctx = r.Context()
pk = httpmw.ProvisionerKeyParam(r)
)

httpapi.Write(ctx, rw, http.StatusOK, codersdk.ProvisionerKeyTags(pk.Tags))
}

func convertProvisionerKeys(dbKeys []database.ProvisionerKey) []codersdk.ProvisionerKey {
keys := make([]codersdk.ProvisionerKey, 0, len(dbKeys))
for _, dbKey := range dbKeys {
Expand Down
35 changes: 35 additions & 0 deletions enterprise/coderd/provisionerkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,38 @@
err = orgAdmin.DeleteProvisionerKey(ctx, owner.OrganizationID, codersdk.ProvisionerKeyNamePSK)
require.ErrorContains(t, err, "reserved")
}

func TestProvisionerKeyTags(t *testing.T) {
t.Parallel()
t.Run("GetTags", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong*10)
t.Cleanup(cancel)
dv := coderdtest.DeploymentValues(t)
client, owner := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
DeploymentValues: dv,
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureMultipleOrganizations: 1,
},
},
})

//nolint:gocritic // Not the purpose of this test
_, err := client.CreateProvisionerKey(ctx, owner.OrganizationID, codersdk.CreateProvisionerKeyRequest{
Name: "key",
Tags: map[string]string{"key1": "value1", "key2": "value2"},
})
require.NoError(t, err)

tags, err := client.GetProvisionTagsByKeyID(ctx, owner.OrganizationID, "key")

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go (ubuntu-latest)

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go (macos-latest)

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / lint

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID) (typecheck)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go-pg

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go-pg

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go (windows-2022)

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go-pg-16

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go-race

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)

Check failure on line 163 in enterprise/coderd/provisionerkeys_test.go

View workflow job for this annotation

GitHub Actions / test-go-race

client.GetProvisionTagsByKeyID undefined (type *codersdk.Client has no field or method GetProvisionTagsByKeyID)
require.NoError(t, err)
require.Equal(t, tags, codersdk.ProvisionerKeyTags{"key1": "value1", "key2": "value2"})

err = client.DeleteProvisionerKey(ctx, owner.OrganizationID, "invalid_key")
require.ErrorContains(t, err, "Resource not found")
})
}
Loading