|
| 1 | +package coderd |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/go-chi/render" |
| 10 | + "github.com/google/uuid" |
| 11 | + |
| 12 | + "github.com/coder/coder/database" |
| 13 | + "github.com/coder/coder/httpapi" |
| 14 | + "github.com/coder/coder/httpmw" |
| 15 | +) |
| 16 | + |
| 17 | +// Project is the JSON representation of a Coder project. |
| 18 | +// This type matches the database object for now, but is |
| 19 | +// abstracted for ease of change later on. |
| 20 | +type Project database.Project |
| 21 | + |
| 22 | +type CreateProjectRequest struct { |
| 23 | + Name string `json:"name" validate:"username,required"` |
| 24 | + Provisioner database.ProvisionerType `json:"provisioner" validate:"oneof=terraform cdr-basic,required"` |
| 25 | +} |
| 26 | + |
| 27 | +type projects struct { |
| 28 | + Database database.Store |
| 29 | +} |
| 30 | + |
| 31 | +// allProjects lists all projects across organizations for a user. |
| 32 | +func (p *projects) allProjects(rw http.ResponseWriter, r *http.Request) { |
| 33 | + apiKey := httpmw.APIKey(r) |
| 34 | + organizations, err := p.Database.GetOrganizationsByUserID(r.Context(), apiKey.UserID) |
| 35 | + if err != nil { |
| 36 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 37 | + Message: fmt.Sprintf("get organizations: %s", err.Error()), |
| 38 | + }) |
| 39 | + return |
| 40 | + } |
| 41 | + organizationIDs := make([]string, 0, len(organizations)) |
| 42 | + for _, organization := range organizations { |
| 43 | + organizationIDs = append(organizationIDs, organization.ID) |
| 44 | + } |
| 45 | + projects, err := p.Database.GetProjectsByOrganizationIDs(r.Context(), organizationIDs) |
| 46 | + if errors.Is(err, sql.ErrNoRows) { |
| 47 | + err = nil |
| 48 | + } |
| 49 | + if err != nil { |
| 50 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 51 | + Message: fmt.Sprintf("get projects: %s", err.Error()), |
| 52 | + }) |
| 53 | + return |
| 54 | + } |
| 55 | + render.Status(r, http.StatusOK) |
| 56 | + render.JSON(rw, r, projects) |
| 57 | +} |
| 58 | + |
| 59 | +// allProjectsForOrganization lists all projects for a specific organization. |
| 60 | +func (p *projects) allProjectsForOrganization(rw http.ResponseWriter, r *http.Request) { |
| 61 | + organization := httpmw.OrganizationParam(r) |
| 62 | + projects, err := p.Database.GetProjectsByOrganizationIDs(r.Context(), []string{organization.ID}) |
| 63 | + if errors.Is(err, sql.ErrNoRows) { |
| 64 | + err = nil |
| 65 | + } |
| 66 | + if err != nil { |
| 67 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 68 | + Message: fmt.Sprintf("get projects: %s", err.Error()), |
| 69 | + }) |
| 70 | + return |
| 71 | + } |
| 72 | + render.Status(r, http.StatusOK) |
| 73 | + render.JSON(rw, r, projects) |
| 74 | +} |
| 75 | + |
| 76 | +// createProject makes a new project in an organization. |
| 77 | +func (p *projects) createProject(rw http.ResponseWriter, r *http.Request) { |
| 78 | + var createProject CreateProjectRequest |
| 79 | + if !httpapi.Read(rw, r, &createProject) { |
| 80 | + return |
| 81 | + } |
| 82 | + organization := httpmw.OrganizationParam(r) |
| 83 | + _, err := p.Database.GetProjectByOrganizationAndName(r.Context(), database.GetProjectByOrganizationAndNameParams{ |
| 84 | + OrganizationID: organization.ID, |
| 85 | + Name: createProject.Name, |
| 86 | + }) |
| 87 | + if err == nil { |
| 88 | + httpapi.Write(rw, http.StatusConflict, httpapi.Response{ |
| 89 | + Message: fmt.Sprintf("project %q already exists", createProject.Name), |
| 90 | + Errors: []httpapi.Error{{ |
| 91 | + Field: "name", |
| 92 | + Code: "exists", |
| 93 | + }}, |
| 94 | + }) |
| 95 | + return |
| 96 | + } |
| 97 | + if !errors.Is(err, sql.ErrNoRows) { |
| 98 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 99 | + Message: fmt.Sprintf("get project by name: %s", err.Error()), |
| 100 | + }) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + project, err := p.Database.InsertProject(r.Context(), database.InsertProjectParams{ |
| 105 | + ID: uuid.New(), |
| 106 | + CreatedAt: database.Now(), |
| 107 | + UpdatedAt: database.Now(), |
| 108 | + OrganizationID: organization.ID, |
| 109 | + Name: createProject.Name, |
| 110 | + Provisioner: createProject.Provisioner, |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 114 | + Message: fmt.Sprintf("insert project: %s", err), |
| 115 | + }) |
| 116 | + return |
| 117 | + } |
| 118 | + render.Status(r, http.StatusCreated) |
| 119 | + render.JSON(rw, r, project) |
| 120 | +} |
0 commit comments