Skip to content

Commit 300a763

Browse files
committed
feat: add examples to api
1 parent 1cfe5de commit 300a763

File tree

7 files changed

+45
-15
lines changed

7 files changed

+45
-15
lines changed

cli/templateinit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/spf13/cobra"
99

1010
"github.com/coder/coder/cli/cliui"
11+
"github.com/coder/coder/codersdk"
1112
"github.com/coder/coder/examples"
1213
"github.com/coder/coder/provisionersdk"
1314
)
@@ -22,7 +23,7 @@ func templateInit() *cobra.Command {
2223
return err
2324
}
2425
exampleNames := []string{}
25-
exampleByName := map[string]examples.Example{}
26+
exampleByName := map[string]codersdk.TemplateExample{}
2627
for _, example := range exampleList {
2728
name := fmt.Sprintf(
2829
"%s\n%s\n%s\n",

coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ func New(options *Options) *API {
355355
r.Post("/", api.postTemplateByOrganization)
356356
r.Get("/", api.templatesByOrganization)
357357
r.Get("/{templatename}", api.templateByOrganizationAndName)
358+
r.Get("/examples", api.templateExamples)
358359
})
359360
r.Route("/members", func(r chi.Router) {
360361
r.Get("/roles", api.assignableOrgRoles)

coderd/users.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,21 @@ func (api *API) putUserRoles(rw http.ResponseWriter, r *http.Request) {
825825
httpapi.Write(ctx, rw, http.StatusOK, convertUser(updatedUser, organizationIDs))
826826
}
827827

828+
func (_ *API) templateExamples(rw http.ResponseWriter, r *http.Request) {
829+
ctx := r.Context()
830+
831+
ex, err := examples.List()
832+
if err != nil {
833+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
834+
Message: "Internal error fetching examples.",
835+
Detail: err.Error(),
836+
})
837+
return
838+
}
839+
840+
httpapi.Write(ctx, rw, http.StatusOK, ex)
841+
}
842+
828843
// updateSiteUserRoles will ensure only site wide roles are passed in as arguments.
829844
// If an organization role is included, an error is returned.
830845
func (api *API) updateSiteUserRoles(ctx context.Context, args database.UpdateUserRolesParams) (database.User, error) {

codersdk/organizations.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ type CreateTemplateVersionRequest struct {
3838
// TemplateID optionally associates a version with a template.
3939
TemplateID uuid.UUID `json:"template_id,omitempty"`
4040
StorageMethod ProvisionerStorageMethod `json:"storage_method" validate:"oneof=file,required"`
41-
FileID uuid.UUID `json:"file_id" validate:"required"`
41+
FileID uuid.UUID `json:"file_id,omitempty" validate:"required_without=ExampleID"`
42+
ExampleID uuid.UUID `json:"example_id,omitempty" validate:"required_without=FileID"`
4243
Provisioner ProvisionerType `json:"provisioner" validate:"oneof=terraform echo,required"`
4344
ProvisionerTags map[string]string `json:"tags"`
4445

codersdk/templates.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ type UpdateTemplateMeta struct {
8282
AllowUserCancelWorkspaceJobs bool `json:"allow_user_cancel_workspace_jobs,omitempty"`
8383
}
8484

85+
type TemplateExample struct {
86+
ID string `json:"id"`
87+
URL string `json:"url"`
88+
Name string `json:"name"`
89+
Description string `json:"description"`
90+
Markdown string `json:"markdown"`
91+
}
92+
8593
// Template returns a single template.
8694
func (c *Client) Template(ctx context.Context, template uuid.UUID) (Template, error) {
8795
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/templates/%s", template), nil)

examples/examples.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,24 @@ import (
1212
"github.com/gohugoio/hugo/parser/pageparser"
1313
"golang.org/x/sync/singleflight"
1414
"golang.org/x/xerrors"
15+
16+
"github.com/coder/coder/codersdk"
1517
)
1618

1719
var (
1820
//go:embed templates
1921
files embed.FS
2022

2123
exampleBasePath = "https://github.com/coder/coder/tree/main/examples/templates/"
22-
examples = make([]Example, 0)
24+
examples = make([]codersdk.TemplateExample, 0)
2325
parseExamples sync.Once
2426
archives = singleflight.Group{}
2527
)
2628

27-
type Example struct {
28-
ID string `json:"id"`
29-
URL string `json:"url"`
30-
Name string `json:"name"`
31-
Description string `json:"description"`
32-
Markdown string `json:"markdown"`
33-
}
34-
3529
const rootDir = "templates"
3630

3731
// List returns all embedded examples.
38-
func List() ([]Example, error) {
32+
func List() ([]codersdk.TemplateExample, error) {
3933
var returnError error
4034
parseExamples.Do(func() {
4135
files, err := fs.Sub(files, rootDir)
@@ -92,7 +86,7 @@ func List() ([]Example, error) {
9286
return
9387
}
9488

95-
examples = append(examples, Example{
89+
examples = append(examples, codersdk.TemplateExample{
9690
ID: exampleID,
9791
URL: exampleURL,
9892
Name: name,
@@ -112,7 +106,7 @@ func Archive(exampleID string) ([]byte, error) {
112106
return nil, xerrors.Errorf("list: %w", err)
113107
}
114108

115-
var selected Example
109+
var selected codersdk.TemplateExample
116110
for _, example := range examples {
117111
if example.ID != exampleID {
118112
continue

site/src/api/typesGenerated.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ export interface CreateTemplateVersionRequest {
189189
readonly name?: string
190190
readonly template_id?: string
191191
readonly storage_method: ProvisionerStorageMethod
192-
readonly file_id: string
192+
readonly file_id?: string
193+
readonly example_id?: string
193194
readonly provisioner: ProvisionerType
194195
readonly tags: Record<string, string>
195196
readonly parameter_values?: CreateParameterRequest[]
@@ -668,6 +669,15 @@ export interface TemplateDAUsResponse {
668669
readonly entries: DAUEntry[]
669670
}
670671

672+
// From codersdk/templates.go
673+
export interface TemplateExample {
674+
readonly id: string
675+
readonly url: string
676+
readonly name: string
677+
readonly description: string
678+
readonly markdown: string
679+
}
680+
671681
// From codersdk/templates.go
672682
export interface TemplateGroup extends Group {
673683
readonly role: TemplateRole

0 commit comments

Comments
 (0)