2022-03-07 17:40:54 +00:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
import (
|
2022-07-15 21:12:39 +00:00
|
|
|
"context"
|
2022-03-07 17:40:54 +00:00
|
|
|
"database/sql"
|
2022-06-01 14:44:53 +00:00
|
|
|
"encoding/json"
|
2022-03-07 17:40:54 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/moby/moby/pkg/namesgenerator"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2022-09-09 16:34:23 +00:00
|
|
|
"github.com/coder/coder/coderd/audit"
|
2022-03-25 21:07:45 +00:00
|
|
|
"github.com/coder/coder/coderd/database"
|
|
|
|
"github.com/coder/coder/coderd/httpapi"
|
|
|
|
"github.com/coder/coder/coderd/httpmw"
|
2022-03-07 17:40:54 +00:00
|
|
|
"github.com/coder/coder/coderd/parameter"
|
2022-05-25 16:00:59 +00:00
|
|
|
"github.com/coder/coder/coderd/rbac"
|
2022-03-22 19:17:50 +00:00
|
|
|
"github.com/coder/coder/codersdk"
|
2022-03-07 17:40:54 +00:00
|
|
|
)
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) templateVersion(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-10-10 20:37:06 +00:00
|
|
|
var (
|
|
|
|
templateVersion = httpmw.TemplateVersionParam(r)
|
|
|
|
template = httpmw.TemplateParam(r)
|
|
|
|
)
|
|
|
|
|
|
|
|
if !api.Authorize(r, rbac.ActionRead, templateVersion.RBACObject(template)) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-25 16:00:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)
|
2022-03-07 17:40:54 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-04-12 15:17:33 +00:00
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
createdByName, err := getUsernameByUserID(ctx, api.Database, templateVersion.CreatedBy)
|
2022-07-15 21:12:39 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-07-15 21:12:39 +00:00
|
|
|
Message: "Internal error fetching creator name.",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job), createdByName))
|
2022-03-07 17:40:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) patchCancelTemplateVersion(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-10-10 20:37:06 +00:00
|
|
|
var (
|
|
|
|
templateVersion = httpmw.TemplateVersionParam(r)
|
|
|
|
template = httpmw.TemplateParam(r)
|
|
|
|
)
|
|
|
|
if !api.Authorize(r, rbac.ActionUpdate, templateVersion.RBACObject(template)) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-25 16:00:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)
|
2022-03-22 19:17:50 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-03-22 19:17:50 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if job.CompletedAt.Valid {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{
|
2022-03-22 19:17:50 +00:00
|
|
|
Message: "Job has already completed!",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if job.CanceledAt.Valid {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{
|
2022-03-22 19:17:50 +00:00
|
|
|
Message: "Job has already been marked as canceled!",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-09-21 22:07:00 +00:00
|
|
|
err = api.Database.UpdateProvisionerJobWithCancelByID(ctx, database.UpdateProvisionerJobWithCancelByIDParams{
|
2022-03-22 19:17:50 +00:00
|
|
|
ID: job.ID,
|
|
|
|
CanceledAt: sql.NullTime{
|
|
|
|
Time: database.Now(),
|
|
|
|
Valid: true,
|
|
|
|
},
|
2022-10-03 16:43:11 +00:00
|
|
|
CompletedAt: sql.NullTime{
|
|
|
|
Time: database.Now(),
|
|
|
|
// If the job is running, don't mark it completed!
|
|
|
|
Valid: !job.WorkerID.Valid,
|
|
|
|
},
|
2022-03-22 19:17:50 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error updating provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-03-22 19:17:50 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
|
2022-03-22 19:17:50 +00:00
|
|
|
Message: "Job has been marked as canceled...",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) templateVersionSchema(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-10-10 20:37:06 +00:00
|
|
|
var (
|
|
|
|
templateVersion = httpmw.TemplateVersionParam(r)
|
|
|
|
template = httpmw.TemplateParam(r)
|
|
|
|
)
|
|
|
|
|
|
|
|
if !api.Authorize(r, rbac.ActionRead, templateVersion.RBACObject(template)) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-25 16:00:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)
|
2022-03-07 17:40:54 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !job.CompletedAt.Valid {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{
|
2022-04-06 17:42:40 +00:00
|
|
|
Message: "Template version job hasn't completed!",
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-09-21 22:07:00 +00:00
|
|
|
schemas, err := api.Database.GetParameterSchemasByJobID(ctx, job.ID)
|
2022-03-07 17:40:54 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error listing parameter schemas.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-05-19 13:29:36 +00:00
|
|
|
apiSchemas := make([]codersdk.ParameterSchema, 0)
|
|
|
|
for _, schema := range schemas {
|
|
|
|
apiSchema, err := convertParameterSchema(schema)
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: fmt.Sprintf("Internal error converting schema %s.", schema.Name),
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-05-19 13:29:36 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiSchemas = append(apiSchemas, apiSchema)
|
2022-03-07 17:40:54 +00:00
|
|
|
}
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, apiSchemas)
|
2022-03-07 17:40:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) templateVersionParameters(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-10-10 20:37:06 +00:00
|
|
|
var (
|
|
|
|
templateVersion = httpmw.TemplateVersionParam(r)
|
|
|
|
template = httpmw.TemplateParam(r)
|
|
|
|
)
|
|
|
|
if !api.Authorize(r, rbac.ActionRead, templateVersion.RBACObject(template)) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-25 16:00:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)
|
2022-03-07 17:40:54 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !job.CompletedAt.Valid {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{
|
2022-03-07 17:40:54 +00:00
|
|
|
Message: "Job hasn't completed!",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-09-21 22:07:00 +00:00
|
|
|
values, err := parameter.Compute(ctx, api.Database, parameter.ComputeScope{
|
2022-04-06 17:42:40 +00:00
|
|
|
TemplateImportJobID: job.ID,
|
2022-03-07 17:40:54 +00:00
|
|
|
}, ¶meter.ComputeOptions{
|
|
|
|
// We *never* want to send the client secret parameter values.
|
|
|
|
HideRedisplayValues: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error computing values.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if values == nil {
|
|
|
|
values = []parameter.ComputedValue{}
|
|
|
|
}
|
2022-04-12 15:17:33 +00:00
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, values)
|
2022-03-07 17:40:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 14:44:53 +00:00
|
|
|
func (api *API) postTemplateVersionDryRun(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-10-10 20:37:06 +00:00
|
|
|
var (
|
|
|
|
apiKey = httpmw.APIKey(r)
|
|
|
|
templateVersion = httpmw.TemplateVersionParam(r)
|
|
|
|
template = httpmw.TemplateParam(r)
|
|
|
|
)
|
|
|
|
if !api.Authorize(r, rbac.ActionRead, templateVersion.RBACObject(template)) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-06-01 14:44:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// We use the workspace RBAC check since we don't want to allow dry runs if
|
|
|
|
// the user can't create workspaces.
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionCreate,
|
2022-06-01 14:44:53 +00:00
|
|
|
rbac.ResourceWorkspace.InOrg(templateVersion.OrganizationID).WithOwner(apiKey.UserID.String())) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-06-01 14:44:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req codersdk.CreateTemplateVersionDryRunRequest
|
2022-09-21 22:07:00 +00:00
|
|
|
if !httpapi.Read(ctx, rw, r, &req) {
|
2022-06-01 14:44:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)
|
2022-06-01 14:44:53 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error updating provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !job.CompletedAt.Valid {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{
|
2022-06-01 14:44:53 +00:00
|
|
|
Message: "Template version import job hasn't completed!",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert parameters from request to parameters for the job
|
|
|
|
parameterValues := make([]database.ParameterValue, len(req.ParameterValues))
|
|
|
|
for i, v := range req.ParameterValues {
|
|
|
|
parameterValues[i] = database.ParameterValue{
|
|
|
|
ID: uuid.Nil,
|
|
|
|
Scope: database.ParameterScopeWorkspace,
|
|
|
|
ScopeID: uuid.Nil,
|
|
|
|
Name: v.Name,
|
|
|
|
SourceScheme: database.ParameterSourceSchemeData,
|
|
|
|
SourceValue: v.SourceValue,
|
|
|
|
DestinationScheme: database.ParameterDestinationSchemeProvisionerVariable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal template version dry-run job with the parameters from the
|
|
|
|
// request.
|
|
|
|
input, err := json.Marshal(templateVersionDryRunJob{
|
|
|
|
TemplateVersionID: templateVersion.ID,
|
|
|
|
WorkspaceName: req.WorkspaceName,
|
|
|
|
ParameterValues: parameterValues,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error unmarshalling provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a dry-run job
|
|
|
|
jobID := uuid.New()
|
2022-09-21 22:07:00 +00:00
|
|
|
provisionerJob, err := api.Database.InsertProvisionerJob(ctx, database.InsertProvisionerJobParams{
|
2022-06-01 14:44:53 +00:00
|
|
|
ID: jobID,
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
UpdatedAt: database.Now(),
|
|
|
|
OrganizationID: templateVersion.OrganizationID,
|
|
|
|
InitiatorID: apiKey.UserID,
|
|
|
|
Provisioner: job.Provisioner,
|
|
|
|
StorageMethod: job.StorageMethod,
|
|
|
|
StorageSource: job.StorageSource,
|
|
|
|
Type: database.ProvisionerJobTypeTemplateVersionDryRun,
|
|
|
|
Input: input,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error inserting provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusCreated, convertProvisionerJob(provisionerJob))
|
2022-06-01 14:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) templateVersionDryRun(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-06-01 14:44:53 +00:00
|
|
|
job, ok := api.fetchTemplateVersionDryRunJob(rw, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, convertProvisionerJob(job))
|
2022-06-01 14:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) templateVersionDryRunResources(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
job, ok := api.fetchTemplateVersionDryRunJob(rw, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
api.provisionerJobResources(rw, r, job)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) templateVersionDryRunLogs(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
job, ok := api.fetchTemplateVersionDryRunJob(rw, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
api.provisionerJobLogs(rw, r, job)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) patchTemplateVersionDryRunCancel(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-06-01 14:44:53 +00:00
|
|
|
templateVersion := httpmw.TemplateVersionParam(r)
|
|
|
|
|
|
|
|
job, ok := api.fetchTemplateVersionDryRunJob(rw, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionUpdate,
|
2022-06-01 14:44:53 +00:00
|
|
|
rbac.ResourceWorkspace.InOrg(templateVersion.OrganizationID).WithOwner(job.InitiatorID.String())) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-06-01 14:44:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if job.CompletedAt.Valid {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Job has already completed.",
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if job.CanceledAt.Valid {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusPreconditionFailed, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Job has already been marked as canceled.",
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
err := api.Database.UpdateProvisionerJobWithCancelByID(ctx, database.UpdateProvisionerJobWithCancelByIDParams{
|
2022-06-01 14:44:53 +00:00
|
|
|
ID: job.ID,
|
|
|
|
CanceledAt: sql.NullTime{
|
|
|
|
Time: database.Now(),
|
|
|
|
Valid: true,
|
|
|
|
},
|
2022-10-03 16:43:11 +00:00
|
|
|
CompletedAt: sql.NullTime{
|
|
|
|
Time: database.Now(),
|
|
|
|
// If the job is running, don't mark it completed!
|
|
|
|
Valid: !job.WorkerID.Valid,
|
|
|
|
},
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error updating provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Job has been marked as canceled.",
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) fetchTemplateVersionDryRunJob(rw http.ResponseWriter, r *http.Request) (database.ProvisionerJob, bool) {
|
|
|
|
var (
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx = r.Context()
|
2022-06-01 14:44:53 +00:00
|
|
|
templateVersion = httpmw.TemplateVersionParam(r)
|
2022-10-10 20:37:06 +00:00
|
|
|
template = httpmw.TemplateParam(r)
|
2022-06-01 14:44:53 +00:00
|
|
|
jobID = chi.URLParam(r, "jobID")
|
|
|
|
)
|
2022-10-10 20:37:06 +00:00
|
|
|
|
|
|
|
if !api.Authorize(r, rbac.ActionRead, templateVersion.RBACObject(template)) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-06-01 14:44:53 +00:00
|
|
|
return database.ProvisionerJob{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
jobUUID, err := uuid.Parse(jobID)
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: fmt.Sprintf("Job ID %q must be a valid UUID.", jobID),
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
return database.ProvisionerJob{}, false
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := api.Database.GetProvisionerJobByID(ctx, jobUUID)
|
2022-06-01 14:44:53 +00:00
|
|
|
if xerrors.Is(err, sql.ErrNoRows) {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
|
2022-06-14 15:14:05 +00:00
|
|
|
Message: fmt.Sprintf("Provisioner job %q not found.", jobUUID),
|
|
|
|
})
|
2022-06-01 14:44:53 +00:00
|
|
|
return database.ProvisionerJob{}, false
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
return database.ProvisionerJob{}, false
|
|
|
|
}
|
|
|
|
if job.Type != database.ProvisionerJobTypeTemplateVersionDryRun {
|
|
|
|
httpapi.Forbidden(rw)
|
|
|
|
return database.ProvisionerJob{}, false
|
|
|
|
}
|
2022-10-12 19:33:21 +00:00
|
|
|
|
2022-06-01 14:44:53 +00:00
|
|
|
// Do a workspace resource check since it's basically a workspace dry-run .
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionRead,
|
2022-06-01 14:44:53 +00:00
|
|
|
rbac.ResourceWorkspace.InOrg(templateVersion.OrganizationID).WithOwner(job.InitiatorID.String())) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.Forbidden(rw)
|
2022-06-01 14:44:53 +00:00
|
|
|
return database.ProvisionerJob{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the template version is the one used in the request.
|
|
|
|
var input templateVersionDryRunJob
|
|
|
|
err = json.Unmarshal(job.Input, &input)
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error unmarshaling job metadata.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-06-01 14:44:53 +00:00
|
|
|
})
|
|
|
|
return database.ProvisionerJob{}, false
|
|
|
|
}
|
|
|
|
if input.TemplateVersionID != templateVersion.ID {
|
|
|
|
httpapi.Forbidden(rw)
|
|
|
|
return database.ProvisionerJob{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return job, true
|
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) templateVersionsByTemplate(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-05-16 19:36:27 +00:00
|
|
|
template := httpmw.TemplateParam(r)
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionRead, template) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-25 16:00:59 +00:00
|
|
|
return
|
|
|
|
}
|
2022-05-16 19:36:27 +00:00
|
|
|
|
|
|
|
paginationParams, ok := parsePagination(rw, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-02 14:01:45 +00:00
|
|
|
var err error
|
|
|
|
apiVersions := []codersdk.TemplateVersion{}
|
|
|
|
err = api.Database.InTx(func(store database.Store) error {
|
|
|
|
if paginationParams.AfterID != uuid.Nil {
|
|
|
|
// See if the record exists first. If the record does not exist, the pagination
|
|
|
|
// query will not work.
|
2022-09-21 22:07:00 +00:00
|
|
|
_, err := store.GetTemplateVersionByID(ctx, paginationParams.AfterID)
|
2022-06-02 14:01:45 +00:00
|
|
|
if err != nil && xerrors.Is(err, sql.ErrNoRows) {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: fmt.Sprintf("Record at \"after_id\" (%q) does not exists.", paginationParams.AfterID.String()),
|
2022-06-02 14:01:45 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
} else if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching template version at after_id.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-06-02 14:01:45 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
versions, err := store.GetTemplateVersionsByTemplateID(ctx, database.GetTemplateVersionsByTemplateIDParams{
|
2022-06-02 14:01:45 +00:00
|
|
|
TemplateID: template.ID,
|
|
|
|
AfterID: paginationParams.AfterID,
|
|
|
|
LimitOpt: int32(paginationParams.Limit),
|
|
|
|
OffsetOpt: int32(paginationParams.Offset),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
2022-06-02 14:01:45 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, apiVersions)
|
2022-06-02 14:01:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching template versions.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-06-02 14:01:45 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2022-05-16 19:36:27 +00:00
|
|
|
|
2022-06-02 14:01:45 +00:00
|
|
|
jobIDs := make([]uuid.UUID, 0, len(versions))
|
|
|
|
for _, version := range versions {
|
|
|
|
jobIDs = append(jobIDs, version.JobID)
|
|
|
|
}
|
2022-09-21 22:07:00 +00:00
|
|
|
jobs, err := store.GetProvisionerJobsByIDs(ctx, jobIDs)
|
2022-06-02 14:01:45 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
2022-06-02 14:01:45 +00:00
|
|
|
return err
|
2022-05-16 19:36:27 +00:00
|
|
|
}
|
2022-06-02 14:01:45 +00:00
|
|
|
jobByID := map[string]database.ProvisionerJob{}
|
|
|
|
for _, job := range jobs {
|
|
|
|
jobByID[job.ID.String()] = job
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, version := range versions {
|
|
|
|
job, exists := jobByID[version.JobID.String()]
|
|
|
|
if !exists {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: fmt.Sprintf("Job %q doesn't exist for version %q.", version.JobID, version.ID),
|
2022-06-02 14:01:45 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2022-09-21 22:07:00 +00:00
|
|
|
createdByName, err := getUsernameByUserID(ctx, store, version.CreatedBy)
|
2022-07-15 21:12:39 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-07-15 21:12:39 +00:00
|
|
|
Message: "Internal error fetching creator name.",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
apiVersions = append(apiVersions, convertTemplateVersion(version, convertProvisionerJob(job), createdByName))
|
2022-06-02 14:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
2022-05-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, apiVersions)
|
2022-05-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-05-16 19:36:27 +00:00
|
|
|
template := httpmw.TemplateParam(r)
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionRead, template) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-25 16:00:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
templateVersionName := chi.URLParam(r, "templateversionname")
|
2022-09-21 22:07:00 +00:00
|
|
|
templateVersion, err := api.Database.GetTemplateVersionByTemplateIDAndName(ctx, database.GetTemplateVersionByTemplateIDAndNameParams{
|
2022-05-16 19:36:27 +00:00
|
|
|
TemplateID: uuid.NullUUID{
|
|
|
|
UUID: template.ID,
|
|
|
|
Valid: true,
|
|
|
|
},
|
|
|
|
Name: templateVersionName,
|
|
|
|
})
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: fmt.Sprintf("No template version found by name %q.", templateVersionName),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching template version.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)
|
2022-05-16 19:36:27 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
createdByName, err := getUsernameByUserID(ctx, api.Database, templateVersion.CreatedBy)
|
2022-07-15 21:12:39 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-07-15 21:12:39 +00:00
|
|
|
Message: "Internal error fetching creator name.",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job), createdByName))
|
2022-05-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) patchActiveTemplateVersion(rw http.ResponseWriter, r *http.Request) {
|
2022-09-09 16:34:23 +00:00
|
|
|
var (
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx = r.Context()
|
2022-09-09 16:34:23 +00:00
|
|
|
template = httpmw.TemplateParam(r)
|
2022-09-20 04:11:01 +00:00
|
|
|
auditor = *api.Auditor.Load()
|
2022-09-09 16:34:23 +00:00
|
|
|
aReq, commitAudit = audit.InitRequest[database.Template](rw, &audit.RequestParams{
|
2022-09-20 04:11:01 +00:00
|
|
|
Audit: auditor,
|
|
|
|
Log: api.Logger,
|
|
|
|
Request: r,
|
|
|
|
Action: database.AuditActionWrite,
|
2022-09-09 16:34:23 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
defer commitAudit()
|
|
|
|
aReq.Old = template
|
|
|
|
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionUpdate, template) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-25 16:00:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
var req codersdk.UpdateActiveTemplateVersion
|
2022-09-21 22:07:00 +00:00
|
|
|
if !httpapi.Read(ctx, rw, r, &req) {
|
2022-05-16 19:36:27 +00:00
|
|
|
return
|
|
|
|
}
|
2022-09-21 22:07:00 +00:00
|
|
|
version, err := api.Database.GetTemplateVersionByID(ctx, req.ID)
|
2022-05-16 19:36:27 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Template version not found.",
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching template version.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if version.TemplateID.UUID.String() != template.ID.String() {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
2022-05-16 19:36:27 +00:00
|
|
|
Message: "The provided template version doesn't belong to the specified template.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-06-17 17:22:28 +00:00
|
|
|
|
|
|
|
err = api.Database.InTx(func(store database.Store) error {
|
2022-09-21 22:07:00 +00:00
|
|
|
err = store.UpdateTemplateActiveVersionByID(ctx, database.UpdateTemplateActiveVersionByIDParams{
|
2022-06-17 17:22:28 +00:00
|
|
|
ID: template.ID,
|
|
|
|
ActiveVersionID: req.ID,
|
2022-06-30 12:14:51 +00:00
|
|
|
UpdatedAt: database.Now(),
|
2022-06-17 17:22:28 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("update active version: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error updating active template version.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-09-09 16:34:23 +00:00
|
|
|
newTemplate := template
|
|
|
|
newTemplate.ActiveVersionID = req.ID
|
|
|
|
aReq.New = newTemplate
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
|
2022-05-16 19:36:27 +00:00
|
|
|
Message: "Updated the active template version!",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new version of a template. An import job is queued to parse the storage method provided.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *http.Request) {
|
2022-09-09 16:34:23 +00:00
|
|
|
var (
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx = r.Context()
|
2022-09-09 16:34:23 +00:00
|
|
|
apiKey = httpmw.APIKey(r)
|
|
|
|
organization = httpmw.OrganizationParam(r)
|
2022-09-20 04:11:01 +00:00
|
|
|
auditor = *api.Auditor.Load()
|
2022-09-09 16:34:23 +00:00
|
|
|
aReq, commitAudit = audit.InitRequest[database.TemplateVersion](rw, &audit.RequestParams{
|
2022-09-20 04:11:01 +00:00
|
|
|
Audit: auditor,
|
|
|
|
Log: api.Logger,
|
|
|
|
Request: r,
|
|
|
|
Action: database.AuditActionCreate,
|
2022-09-09 16:34:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
req codersdk.CreateTemplateVersionRequest
|
|
|
|
)
|
|
|
|
defer commitAudit()
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
if !httpapi.Read(ctx, rw, r, &req) {
|
2022-05-16 19:36:27 +00:00
|
|
|
return
|
|
|
|
}
|
2022-05-27 16:19:13 +00:00
|
|
|
|
2022-10-10 20:37:06 +00:00
|
|
|
var template database.Template
|
2022-05-16 19:36:27 +00:00
|
|
|
if req.TemplateID != uuid.Nil {
|
2022-10-10 20:37:06 +00:00
|
|
|
var err error
|
|
|
|
template, err = api.Database.GetTemplateByID(ctx, req.TemplateID)
|
2022-05-16 19:36:27 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Template does not exist.",
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching template.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 20:37:06 +00:00
|
|
|
if template.ID != uuid.Nil {
|
|
|
|
if !api.Authorize(r, rbac.ActionCreate, template) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if !api.Authorize(r, rbac.ActionCreate, rbac.ResourceTemplate.InOrg(organization.ID)) {
|
|
|
|
// Making a new template version is the same permission as creating a new template.
|
|
|
|
httpapi.ResourceNotFound(rw)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
file, err := api.Database.GetFileByHash(ctx, req.StorageSource)
|
2022-05-16 19:36:27 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "File not found.",
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching file.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-10 20:37:06 +00:00
|
|
|
// TODO(JonA): Readd this check once we update the unique constraint
|
|
|
|
// on files to be owner + hash.
|
|
|
|
// if !api.Authorize(r, rbac.ActionRead, file) {
|
|
|
|
// httpapi.ResourceNotFound(rw)
|
|
|
|
// return
|
|
|
|
// }
|
2022-05-27 16:19:13 +00:00
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
var templateVersion database.TemplateVersion
|
|
|
|
var provisionerJob database.ProvisionerJob
|
2022-10-10 20:37:06 +00:00
|
|
|
err = api.Database.InTx(func(tx database.Store) error {
|
2022-05-16 19:36:27 +00:00
|
|
|
jobID := uuid.New()
|
2022-06-17 17:22:28 +00:00
|
|
|
inherits := make([]uuid.UUID, 0)
|
|
|
|
for _, parameterValue := range req.ParameterValues {
|
|
|
|
if parameterValue.CloneID != uuid.Nil {
|
|
|
|
inherits = append(inherits, parameterValue.CloneID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand inherited params
|
|
|
|
if len(inherits) > 0 {
|
|
|
|
if req.TemplateID == uuid.Nil {
|
|
|
|
return xerrors.Errorf("cannot inherit parameters if template_id is not set")
|
|
|
|
}
|
|
|
|
|
2022-10-10 20:37:06 +00:00
|
|
|
inheritedParams, err := tx.ParameterValues(ctx, database.ParameterValuesParams{
|
2022-09-12 23:24:20 +00:00
|
|
|
IDs: inherits,
|
2022-06-17 17:22:28 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("fetch inherited params: %w", err)
|
|
|
|
}
|
|
|
|
for _, copy := range inheritedParams {
|
|
|
|
// This is a bit inefficient, as we make a new db call for each
|
|
|
|
// param.
|
2022-10-10 20:37:06 +00:00
|
|
|
version, err := tx.GetTemplateVersionByJobID(ctx, copy.ScopeID)
|
2022-06-17 17:22:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("fetch template version for param %q: %w", copy.Name, err)
|
|
|
|
}
|
|
|
|
if !version.TemplateID.Valid || version.TemplateID.UUID != req.TemplateID {
|
|
|
|
return xerrors.Errorf("cannot inherit parameters from other templates")
|
|
|
|
}
|
|
|
|
if copy.Scope != database.ParameterScopeImportJob {
|
|
|
|
return xerrors.Errorf("copy parameter scope is %q, must be %q", copy.Scope, database.ParameterScopeImportJob)
|
|
|
|
}
|
|
|
|
// Add the copied param to the list to process
|
|
|
|
req.ParameterValues = append(req.ParameterValues, codersdk.CreateParameterRequest{
|
|
|
|
Name: copy.Name,
|
|
|
|
SourceValue: copy.SourceValue,
|
|
|
|
SourceScheme: codersdk.ParameterSourceScheme(copy.SourceScheme),
|
|
|
|
DestinationScheme: codersdk.ParameterDestinationScheme(copy.DestinationScheme),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
for _, parameterValue := range req.ParameterValues {
|
2022-06-17 17:22:28 +00:00
|
|
|
if parameterValue.CloneID != uuid.Nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-10-10 20:37:06 +00:00
|
|
|
_, err = tx.InsertParameterValue(ctx, database.InsertParameterValueParams{
|
2022-05-16 19:36:27 +00:00
|
|
|
ID: uuid.New(),
|
|
|
|
Name: parameterValue.Name,
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
UpdatedAt: database.Now(),
|
|
|
|
Scope: database.ParameterScopeImportJob,
|
|
|
|
ScopeID: jobID,
|
2022-05-19 18:04:44 +00:00
|
|
|
SourceScheme: database.ParameterSourceScheme(parameterValue.SourceScheme),
|
2022-05-16 19:36:27 +00:00
|
|
|
SourceValue: parameterValue.SourceValue,
|
2022-05-19 18:04:44 +00:00
|
|
|
DestinationScheme: database.ParameterDestinationScheme(parameterValue.DestinationScheme),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("insert parameter value: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 20:37:06 +00:00
|
|
|
provisionerJob, err = tx.InsertProvisionerJob(ctx, database.InsertProvisionerJobParams{
|
2022-05-16 19:36:27 +00:00
|
|
|
ID: jobID,
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
UpdatedAt: database.Now(),
|
|
|
|
OrganizationID: organization.ID,
|
|
|
|
InitiatorID: apiKey.UserID,
|
2022-05-19 18:04:44 +00:00
|
|
|
Provisioner: database.ProvisionerType(req.Provisioner),
|
2022-05-16 19:36:27 +00:00
|
|
|
StorageMethod: database.ProvisionerStorageMethodFile,
|
|
|
|
StorageSource: file.Hash,
|
|
|
|
Type: database.ProvisionerJobTypeTemplateVersionImport,
|
|
|
|
Input: []byte{'{', '}'},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("insert provisioner job: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var templateID uuid.NullUUID
|
|
|
|
if req.TemplateID != uuid.Nil {
|
|
|
|
templateID = uuid.NullUUID{
|
|
|
|
UUID: req.TemplateID,
|
|
|
|
Valid: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 01:17:36 +00:00
|
|
|
if req.Name == "" {
|
|
|
|
req.Name = namesgenerator.GetRandomName(1)
|
|
|
|
}
|
|
|
|
|
2022-10-10 20:37:06 +00:00
|
|
|
templateVersion, err = tx.InsertTemplateVersion(ctx, database.InsertTemplateVersionParams{
|
2022-05-16 19:36:27 +00:00
|
|
|
ID: uuid.New(),
|
|
|
|
TemplateID: templateID,
|
|
|
|
OrganizationID: organization.ID,
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
UpdatedAt: database.Now(),
|
2022-09-24 01:17:36 +00:00
|
|
|
Name: req.Name,
|
2022-05-17 20:00:48 +00:00
|
|
|
Readme: "",
|
2022-05-16 19:36:27 +00:00
|
|
|
JobID: provisionerJob.ID,
|
2022-07-15 21:12:39 +00:00
|
|
|
CreatedBy: uuid.NullUUID{
|
|
|
|
UUID: apiKey.UserID,
|
|
|
|
Valid: true,
|
|
|
|
},
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("insert template version: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-05-16 19:36:27 +00:00
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-09-09 16:34:23 +00:00
|
|
|
aReq.New = templateVersion
|
2022-05-16 19:36:27 +00:00
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
createdByName, err := getUsernameByUserID(ctx, api.Database, templateVersion.CreatedBy)
|
2022-07-15 21:12:39 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-07-15 21:12:39 +00:00
|
|
|
Message: "Internal error fetching creator name.",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusCreated, convertTemplateVersion(templateVersion, convertProvisionerJob(provisionerJob), createdByName))
|
2022-05-16 19:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 16:00:59 +00:00
|
|
|
// templateVersionResources returns the workspace agent resources associated
|
|
|
|
// with a template version. A template can specify more than one resource to be
|
|
|
|
// provisioned, each resource can have an agent that dials back to coderd.
|
|
|
|
// The agents returned are informative of the template version, and do not
|
|
|
|
// return agents associated with any particular workspace.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) templateVersionResources(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-10-10 20:37:06 +00:00
|
|
|
var (
|
|
|
|
templateVersion = httpmw.TemplateVersionParam(r)
|
|
|
|
template = httpmw.TemplateParam(r)
|
|
|
|
)
|
|
|
|
|
|
|
|
if !api.Authorize(r, rbac.ActionRead, templateVersion.RBACObject(template)) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-25 16:00:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)
|
2022-03-07 17:40:54 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
api.provisionerJobResources(rw, r, job)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:00:59 +00:00
|
|
|
// templateVersionLogs returns the logs returned by the provisioner for the given
|
|
|
|
// template version. These logs are only associated with the template version,
|
|
|
|
// and not any build logs for a workspace.
|
|
|
|
// Eg: Logs returned from 'terraform plan' when uploading a new terraform file.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) templateVersionLogs(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-10-10 20:37:06 +00:00
|
|
|
var (
|
|
|
|
templateVersion = httpmw.TemplateVersionParam(r)
|
|
|
|
template = httpmw.TemplateParam(r)
|
|
|
|
)
|
|
|
|
|
|
|
|
if !api.Authorize(r, rbac.ActionRead, templateVersion.RBACObject(template)) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-25 16:00:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)
|
2022-03-07 17:40:54 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-03-07 17:40:54 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
api.provisionerJobLogs(rw, r, job)
|
|
|
|
}
|
|
|
|
|
2022-07-15 21:12:39 +00:00
|
|
|
func getUsernameByUserID(ctx context.Context, db database.Store, userID uuid.NullUUID) (string, error) {
|
|
|
|
if !userID.Valid {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
user, err := db.GetUserByID(ctx, userID.UUID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return user.Username, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertTemplateVersion(version database.TemplateVersion, job codersdk.ProvisionerJob, createdByName string) codersdk.TemplateVersion {
|
2022-04-06 17:42:40 +00:00
|
|
|
return codersdk.TemplateVersion{
|
2022-06-01 14:44:53 +00:00
|
|
|
ID: version.ID,
|
|
|
|
TemplateID: &version.TemplateID.UUID,
|
|
|
|
OrganizationID: version.OrganizationID,
|
|
|
|
CreatedAt: version.CreatedAt,
|
|
|
|
UpdatedAt: version.UpdatedAt,
|
|
|
|
Name: version.Name,
|
|
|
|
Job: job,
|
|
|
|
Readme: version.Readme,
|
2022-07-15 21:12:39 +00:00
|
|
|
CreatedByID: version.CreatedBy.UUID,
|
|
|
|
CreatedByName: createdByName,
|
2022-03-07 17:40:54 +00:00
|
|
|
}
|
|
|
|
}
|