2022-03-07 11:40:54 -06:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2022-04-11 16:06:15 -05:00
|
|
|
"errors"
|
2022-03-07 11:40:54 -06:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2022-04-11 16:06:15 -05:00
|
|
|
"github.com/google/uuid"
|
2022-03-07 11:40:54 -06:00
|
|
|
|
2022-03-25 16:07:45 -05:00
|
|
|
"github.com/coder/coder/coderd/httpapi"
|
|
|
|
|
"github.com/coder/coder/coderd/httpmw"
|
2022-05-25 11:00:59 -05:00
|
|
|
"github.com/coder/coder/coderd/rbac"
|
2022-03-22 13:17:50 -06:00
|
|
|
"github.com/coder/coder/codersdk"
|
2022-03-07 11:40:54 -06:00
|
|
|
)
|
|
|
|
|
|
2022-05-25 22:14:08 -05:00
|
|
|
func (api *API) workspaceResource(rw http.ResponseWriter, r *http.Request) {
|
2022-03-07 11:40:54 -06:00
|
|
|
workspaceBuild := httpmw.WorkspaceBuildParam(r)
|
|
|
|
|
workspaceResource := httpmw.WorkspaceResourceParam(r)
|
2022-05-25 11:00:59 -05:00
|
|
|
workspace := httpmw.WorkspaceParam(r)
|
|
|
|
|
if !api.Authorize(rw, r, rbac.ActionRead, workspace) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 11:40:54 -06:00
|
|
|
job, err := api.Database.GetProvisionerJobByID(r.Context(), workspaceBuild.JobID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
|
Message: fmt.Sprintf("get provisioner job: %s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !job.CompletedAt.Valid {
|
|
|
|
|
httpapi.Write(rw, http.StatusPreconditionFailed, httpapi.Response{
|
|
|
|
|
Message: "Job hasn't completed!",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-04-11 16:06:15 -05:00
|
|
|
agents, err := api.Database.GetWorkspaceAgentsByResourceIDs(r.Context(), []uuid.UUID{workspaceResource.ID})
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
err = nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
|
Message: fmt.Sprintf("get provisioner job agents: %s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
apiAgents := make([]codersdk.WorkspaceAgent, 0)
|
|
|
|
|
for _, agent := range agents {
|
2022-03-22 13:17:50 -06:00
|
|
|
convertedAgent, err := convertWorkspaceAgent(agent, api.AgentConnectionUpdateFrequency)
|
2022-03-07 11:40:54 -06:00
|
|
|
if err != nil {
|
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
|
Message: fmt.Sprintf("convert provisioner job agent: %s", err),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-04-11 16:06:15 -05:00
|
|
|
apiAgents = append(apiAgents, convertedAgent)
|
2022-03-07 11:40:54 -06:00
|
|
|
}
|
|
|
|
|
|
2022-04-12 10:17:33 -05:00
|
|
|
httpapi.Write(rw, http.StatusOK, convertWorkspaceResource(workspaceResource, apiAgents))
|
2022-03-07 11:40:54 -06:00
|
|
|
}
|