Thanks to visit codestin.com
Credit goes to github.com

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

Commit f80c366

Browse files
committed
Update create-from-config for compatibility with remote templates
1 parent 79b7089 commit f80c366

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
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: 27 additions & 13 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,28 +103,44 @@ 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 {
143+
if err := c.requestBody(ctx, http.MethodPost, path, req, &tpl, opts...); err != nil {
130144
return tpl, err
131145
}
132146
return tpl, nil
@@ -144,7 +158,7 @@ func (c *DefaultClient) ParseTemplate(ctx context.Context, req ParseTemplateRequ
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
@@ -354,12 +354,14 @@ coder envs create-from-config -f coder.yaml`,
354354
}
355355

356356
req := coder.ParseTemplateRequest{
357-
RepoURL: repo,
358-
Ref: ref,
359-
Local: rd,
357+
RepoURL: repo,
358+
Ref: ref,
359+
Local: rd,
360+
OrgID: org,
361+
Filepath: ".coder/coder.yaml",
360362
}
361363

362-
tpl, err := client.ParseTemplate(ctx, req)
364+
version, err := client.ParseTemplate(ctx, req)
363365
if err != nil {
364366
return handleAPIError(err)
365367
}
@@ -371,7 +373,7 @@ coder envs create-from-config -f coder.yaml`,
371373

372374
env, err := client.CreateEnvironment(ctx, coder.CreateEnvironmentRequest{
373375
OrgID: userOrg.ID,
374-
Template: tpl,
376+
TemplateID: version.TemplateID,
375377
ResourcePoolID: provider.ID,
376378
Namespace: provider.DefaultNamespace,
377379
})

0 commit comments

Comments
 (0)