Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 9d75db2

Browse files
authored
Update create-from-config for compatibility with remote templates (#285)
1 parent e04bba8 commit 9d75db2

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ ci/bin
33
cmd/coder/coder
44
ci/integration/bin
55
ci/integration/env.sh
6-
coder-sdk/env.sh
6+
coder-sdk/env.sh
7+
.vscode

coder-sdk/env.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package coder
22

33
import (
44
"context"
5-
"encoding/json"
65
"io"
76
"net/http"
87
"net/url"
@@ -88,9 +87,8 @@ type CreateEnvironmentRequest struct {
8887
Namespace string `json:"namespace"`
8988
EnableAutoStart bool `json:"autostart_enabled"`
9089

91-
// Template comes from the parse template route on cemanager.
92-
// This field should never be manually populated
93-
Template Template `json:"template,omitempty"`
90+
// TemplateID comes from the parse template route on cemanager.
91+
TemplateID string `json:"template_id,omitempty"`
9492
}
9593

9694
// CreateEnvironment sends a request to create an environment.
@@ -105,46 +103,62 @@ func (c *DefaultClient) CreateEnvironment(ctx context.Context, req CreateEnviron
105103
// ParseTemplateRequest parses a template. If Local is a non-nil reader
106104
// it will obviate any other fields on the request.
107105
type ParseTemplateRequest struct {
108-
RepoURL string `json:"repo_url"`
109-
Ref string `json:"ref"`
110-
Local io.Reader `json:"-"`
106+
RepoURL string `json:"repo_url"`
107+
Ref string `json:"ref"`
108+
Filepath string `json:"filepath"`
109+
OrgID string `json:"-"`
110+
Local io.Reader `json:"-"`
111111
}
112112

113-
// Template is a Workspaces As Code (WAC) template.
113+
// TemplateVersion is a Workspaces As Code (WAC) template.
114114
// For now, let's not interpret it on the CLI level. We just need
115115
// to forward this as part of the create env request.
116-
type Template = json.RawMessage
116+
type TemplateVersion struct {
117+
ID string `json:"id"`
118+
TemplateID string `json:"template_id"`
119+
// FileHash is the sha256 hash of the template's file contents.
120+
FileHash string `json:"file_hash"`
121+
// Commit is the git commit from which the template was derived.
122+
Commit string `json:"commit"`
123+
CommitMessage string `json:"commit_message"`
124+
CreatedAt time.Time `json:"created_at"`
125+
}
117126

118127
// ParseTemplate parses a template config. It support both remote repositories and local files.
119128
// If a local file is specified then all other values in the request are ignored.
120-
func (c *DefaultClient) ParseTemplate(ctx context.Context, req ParseTemplateRequest) (Template, error) {
129+
func (c *DefaultClient) ParseTemplate(ctx context.Context, req ParseTemplateRequest) (*TemplateVersion, error) {
121130
const path = "/api/private/environments/template/parse"
122131
var (
123-
tpl Template
132+
tpl TemplateVersion
124133
opts []requestOption
125134
headers = http.Header{}
135+
query = url.Values{}
126136
)
127137

138+
query.Set("org-id", req.OrgID)
139+
140+
opts = append(opts, withQueryParams(query))
141+
128142
if req.Local == nil {
129-
if err := c.requestBody(ctx, http.MethodPost, path, req, &tpl); err != nil {
130-
return tpl, err
143+
if err := c.requestBody(ctx, http.MethodPost, path, req, &tpl, opts...); err != nil {
144+
return &tpl, err
131145
}
132-
return tpl, nil
146+
return &tpl, nil
133147
}
134148

135149
headers.Set("Content-Type", "application/octet-stream")
136150
opts = append(opts, withBody(req.Local), withHeaders(headers))
137151

138152
err := c.requestBody(ctx, http.MethodPost, path, nil, &tpl, opts...)
139153
if err != nil {
140-
return tpl, err
154+
return &tpl, err
141155
}
142156

143-
return tpl, nil
157+
return &tpl, nil
144158
}
145159

146160
// CreateEnvironmentFromRepo sends a request to create an environment from a repository.
147-
func (c *DefaultClient) CreateEnvironmentFromRepo(ctx context.Context, orgID string, req Template) (*Environment, error) {
161+
func (c *DefaultClient) CreateEnvironmentFromRepo(ctx context.Context, orgID string, req TemplateVersion) (*Environment, error) {
148162
var env Environment
149163
if err := c.requestBody(ctx, http.MethodPost, "/api/private/orgs/"+orgID+"/environments/from-repo", req, &env); err != nil {
150164
return nil, err

coder-sdk/interface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ type Client interface {
7979

8080
// ParseTemplate parses a template config. It support both remote repositories and local files.
8181
// If a local file is specified then all other values in the request are ignored.
82-
ParseTemplate(ctx context.Context, req ParseTemplateRequest) (Template, error)
82+
ParseTemplate(ctx context.Context, req ParseTemplateRequest) (*TemplateVersion, error)
8383

8484
// CreateEnvironmentFromRepo sends a request to create an environment from a repository.
85-
CreateEnvironmentFromRepo(ctx context.Context, orgID string, req Template) (*Environment, error)
85+
CreateEnvironmentFromRepo(ctx context.Context, orgID string, req TemplateVersion) (*Environment, error)
8686

8787
// Environments lists environments returned by the given filter.
8888
Environments(ctx context.Context) ([]Environment, error)

internal/cmd/envs.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,14 @@ coder envs create-from-config -f coder.yaml`,
362362
}
363363

364364
req := coder.ParseTemplateRequest{
365-
RepoURL: repo,
366-
Ref: ref,
367-
Local: rd,
365+
RepoURL: repo,
366+
Ref: ref,
367+
Local: rd,
368+
OrgID: userOrg.ID,
369+
Filepath: ".coder/coder.yaml",
368370
}
369371

370-
tpl, err := client.ParseTemplate(ctx, req)
372+
version, err := client.ParseTemplate(ctx, req)
371373
if err != nil {
372374
return handleAPIError(err)
373375
}
@@ -379,7 +381,7 @@ coder envs create-from-config -f coder.yaml`,
379381

380382
env, err := client.CreateEnvironment(ctx, coder.CreateEnvironmentRequest{
381383
OrgID: userOrg.ID,
382-
Template: tpl,
384+
TemplateID: version.TemplateID,
383385
ResourcePoolID: provider.ID,
384386
Namespace: provider.DefaultNamespace,
385387
})

0 commit comments

Comments
 (0)