Skip to content

Commit 7329cca

Browse files
committed
Add placeholder API and projectService
1 parent 18dd18f commit 7329cca

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

coderd/api.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package coderd
2+
3+
import "context"
4+
5+
// API offers an HTTP API. Routes are located in routes.go.
6+
type API struct {
7+
// Services.
8+
projectService *projectService
9+
}
10+
11+
// New returns an instantiated API.
12+
func NewAPI(ctx context.Context) *API {
13+
api := &API{
14+
projectService: newProjectService(),
15+
}
16+
return api
17+
}

coderd/coderd.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package coderd
22

33
import (
4+
"context"
45
"net/http"
56

67
"cdr.dev/slog"
@@ -16,8 +17,14 @@ type Options struct {
1617
Database database.Store
1718
}
1819

20+
const (
21+
provisionerTerraform = "provisioner:terraform"
22+
provisionerBasic = "provisioner:basic"
23+
)
24+
1925
// New constructs the Coder API into an HTTP handler.
2026
func New(options *Options) http.Handler {
27+
api := NewAPI(context.Background())
2128
r := chi.NewRouter()
2229
r.Route("/api/v2", func(r chi.Router) {
2330
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
@@ -27,6 +34,18 @@ func New(options *Options) http.Handler {
2734
Message: "👋",
2835
})
2936
})
37+
38+
// Projects endpoint
39+
r.Route("/projects", func(r chi.Router) {
40+
r.Route("/{organization}", func(r chi.Router) {
41+
// TODO: Authentication
42+
// TODO: User extraction
43+
// TODO: Extract organization and add to context
44+
r.Get("/", api.projectService.getProjects)
45+
r.Post("/", api.projectService.createProject)
46+
})
47+
})
48+
3049
})
3150
r.NotFound(site.Handler().ServeHTTP)
3251
return r

coderd/projects.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package coderd
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/coder/coder/xjson"
7+
)
8+
9+
type ProjectParameter struct {
10+
Id string `json:"id" validate:"required"`
11+
Name string `json:"name" validate:"required"`
12+
Description string `json:"description"`
13+
14+
// Validation Parameters
15+
ValueType string `json:"validation_value_type"`
16+
}
17+
18+
// Project is a Go representation of the workspaces v2 project,
19+
// defined here: https://www.notion.so/coderhq/Workspaces-v2-e908a8cd54804ddd910367abf03c8d0a#befa328add894231979e6cf8a378d2ec
20+
type Project struct {
21+
Id string `json:"id" validate:"required"`
22+
Name string `json:"name" validate:"required"`
23+
Description string `json:"description" validate:"required"`
24+
ProvisionerType string `json:"provisioner_type" validate:"required"`
25+
26+
Parameters []ProjectParameter `json:"parameters" validate:"required"`
27+
}
28+
29+
// Placeholder type of projectService
30+
type projectService struct {
31+
}
32+
33+
func newProjectService() *projectService {
34+
projectService := &projectService{}
35+
return projectService
36+
}
37+
38+
func (ps *projectService) getProjects(w http.ResponseWriter, r *http.Request) {
39+
40+
// Construct a couple hard-coded projects to return the UI
41+
42+
terraformProject := Project{
43+
Id: "test_terraform_project_id",
44+
Name: "Terraform",
45+
Description: "Kubernetes on Terraform",
46+
Parameters: []ProjectParameter{
47+
{
48+
Id: "parameter_cluster_namespace",
49+
Name: "Namespace",
50+
Description: "Kubernetes namespace to host workspace pod",
51+
ValueType: "string",
52+
},
53+
{
54+
Id: "parameter_cpu",
55+
Name: "CPU",
56+
Description: "CPU Cores to Allocate",
57+
ValueType: "number",
58+
},
59+
},
60+
}
61+
62+
echoProject := Project{
63+
Id: "test_echo_project_id",
64+
Name: "Echo Project",
65+
Description: "A simple echo provider",
66+
Parameters: []ProjectParameter{
67+
{
68+
Id: "parameter_echo_string",
69+
Name: "Echo String",
70+
Description: "String that should be echo'd out in build log",
71+
ValueType: "string",
72+
},
73+
},
74+
}
75+
76+
projects := []Project{
77+
terraformProject,
78+
echoProject,
79+
}
80+
81+
xjson.Write(w, http.StatusOK, projects)
82+
}
83+
84+
func (ps *projectService) createProject(w http.ResponseWriter, r *http.Request) {
85+
// TODO: Validate arguments
86+
// Organization context
87+
// User
88+
// Parameter values
89+
// Submit to provisioner
90+
xjson.Write(w, http.StatusOK, nil)
91+
}

0 commit comments

Comments
 (0)