Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update create-from-config for compatibility with remote templates
  • Loading branch information
sreya committed Mar 11, 2021
commit cfd46085cdb2a0594a7894007c7d149a8200ee5f
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ ci/bin
cmd/coder/coder
ci/integration/bin
ci/integration/env.sh
coder-sdk/env.sh
coder-sdk/env.sh
.vscode
40 changes: 27 additions & 13 deletions coder-sdk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package coder

import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -88,9 +87,8 @@ type CreateEnvironmentRequest struct {
Namespace string `json:"namespace"`
EnableAutoStart bool `json:"autostart_enabled"`

// Template comes from the parse template route on cemanager.
// This field should never be manually populated
Template Template `json:"template,omitempty"`
// TemplateID comes from the parse template route on cemanager.
TemplateID string `json:"template_id,omitempty"`
}

// CreateEnvironment sends a request to create an environment.
Expand All @@ -105,28 +103,44 @@ func (c *DefaultClient) CreateEnvironment(ctx context.Context, req CreateEnviron
// ParseTemplateRequest parses a template. If Local is a non-nil reader
// it will obviate any other fields on the request.
type ParseTemplateRequest struct {
RepoURL string `json:"repo_url"`
Ref string `json:"ref"`
Local io.Reader `json:"-"`
RepoURL string `json:"repo_url"`
Ref string `json:"ref"`
Filepath string `json:"filepath"`
OrgID string `json:"-"`
Local io.Reader `json:"-"`
}

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

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

query.Set("org-id", req.OrgID)

opts = append(opts, withQueryParams(query))

if req.Local == nil {
if err := c.requestBody(ctx, http.MethodPost, path, req, &tpl); err != nil {
if err := c.requestBody(ctx, http.MethodPost, path, req, &tpl, opts...); err != nil {
return tpl, err
}
return tpl, nil
Expand All @@ -144,7 +158,7 @@ func (c *DefaultClient) ParseTemplate(ctx context.Context, req ParseTemplateRequ
}

// CreateEnvironmentFromRepo sends a request to create an environment from a repository.
func (c *DefaultClient) CreateEnvironmentFromRepo(ctx context.Context, orgID string, req Template) (*Environment, error) {
func (c *DefaultClient) CreateEnvironmentFromRepo(ctx context.Context, orgID string, req TemplateVersion) (*Environment, error) {
var env Environment
if err := c.requestBody(ctx, http.MethodPost, "/api/private/orgs/"+orgID+"/environments/from-repo", req, &env); err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions coder-sdk/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ type Client interface {

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

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

// Environments lists environments returned by the given filter.
Environments(ctx context.Context) ([]Environment, error)
Expand Down
12 changes: 7 additions & 5 deletions internal/cmd/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,14 @@ coder envs create-from-config -f coder.yaml`,
}

req := coder.ParseTemplateRequest{
RepoURL: repo,
Ref: ref,
Local: rd,
RepoURL: repo,
Ref: ref,
Local: rd,
OrgID: org,
Filepath: ".coder/coder.yaml",
}

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

env, err := client.CreateEnvironment(ctx, coder.CreateEnvironmentRequest{
OrgID: userOrg.ID,
Template: tpl,
TemplateID: version.TemplateID,
ResourcePoolID: provider.ID,
Namespace: provider.DefaultNamespace,
})
Expand Down