MCPcopy Index your code
hub / github.com/coder/coder / postProvisionerKey

Method postProvisionerKey

enterprise/coderd/provisionerkeys.go:27–98  ·  view source on GitHub ↗

@Summary Create provisioner key @ID create-provisioner-key @Security CoderSessionToken @Produce json @Tags Enterprise @Param organization path string true "Organization ID" @Success 201 {object} codersdk.CreateProvisionerKeyResponse @Router /api/v2/organizations/{organization}/provisionerkeys [post]

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

25// @Success 201 {object} codersdk.CreateProvisionerKeyResponse
26// @Router /api/v2/organizations/{organization}/provisionerkeys [post]
27func (api *API) postProvisionerKey(rw http.ResponseWriter, r *http.Request) {
28 ctx := r.Context()
29 organization := httpmw.OrganizationParam(r)
30
31 var req codersdk.CreateProvisionerKeyRequest
32 if !httpapi.Read(ctx, rw, r, &req) {
33 return
34 }
35
36 if req.Name == "" {
37 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
38 Message: "Name is required",
39 Validations: []codersdk.ValidationError{
40 {
41 Field: "name",
42 Detail: "Name is required",
43 },
44 },
45 })
46 return
47 }
48
49 if len(req.Name) > 64 {
50 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
51 Message: "Name must be at most 64 characters",
52 Validations: []codersdk.ValidationError{
53 {
54 Field: "name",
55 Detail: "Name must be at most 64 characters",
56 },
57 },
58 })
59 return
60 }
61
62 if slices.ContainsFunc(codersdk.ReservedProvisionerKeyNames(), func(s string) bool {
63 return strings.EqualFold(req.Name, s)
64 }) {
65 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
66 Message: fmt.Sprintf("Name cannot be reserved name '%s'", req.Name),
67 Validations: []codersdk.ValidationError{
68 {
69 Field: "name",
70 Detail: fmt.Sprintf("Name cannot be reserved name '%s'", req.Name),
71 },
72 },
73 })
74 return
75 }
76
77 params, token, err := provisionerkey.New(organization.ID, req.Name, req.Tags)
78 if err != nil {
79 httpapi.InternalServerError(rw, err)
80 return
81 }
82
83 _, err = api.Database.InsertProvisionerKey(ctx, params)
84 if database.IsUniqueViolation(err, database.UniqueProvisionerKeysOrganizationIDNameIndex) {

Callers

nothing calls this directly

Calls 9

OrganizationParamFunction · 0.92
ReadFunction · 0.92
WriteFunction · 0.92
NewFunction · 0.92
InternalServerErrorFunction · 0.92
IsUniqueViolationFunction · 0.92
ContextMethod · 0.65
InsertProvisionerKeyMethod · 0.65

Tested by

no test coverage detected