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

chore: provide resource pool id and namespace in env create #258

Merged
merged 3 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 17 additions & 18 deletions coder-sdk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ const (

// CreateEnvironmentRequest is used to configure a new environment.
type CreateEnvironmentRequest struct {
Name string `json:"name"`
ImageID string `json:"image_id"`
OrgID string `json:"org_id"`
ImageTag string `json:"image_tag"`
CPUCores float32 `json:"cpu_cores"`
MemoryGB float32 `json:"memory_gb"`
DiskGB int `json:"disk_gb"`
GPUs int `json:"gpus"`
Services []string `json:"services"`
UseContainerVM bool `json:"use_container_vm"`
Name string `json:"name"`
ImageID string `json:"image_id"`
OrgID string `json:"org_id"`
ImageTag string `json:"image_tag"`
CPUCores float32 `json:"cpu_cores"`
MemoryGB float32 `json:"memory_gb"`
DiskGB int `json:"disk_gb"`
GPUs int `json:"gpus"`
UseContainerVM bool `json:"use_container_vm"`
ResourcePoolID string `json:"resource_pool_id"`
Namespace string `json:"namespace"`

// Template comes from the parse template route on cemanager.
// This field should never be manually populated
Expand Down Expand Up @@ -189,14 +190,12 @@ func (c *DefaultClient) StopEnvironment(ctx context.Context, envID string) error
// UpdateEnvironmentReq defines the update operation, only setting
// nil-fields.
type UpdateEnvironmentReq struct {
ImageID *string `json:"image_id"`
ImageTag *string `json:"image_tag"`
CPUCores *float32 `json:"cpu_cores"`
MemoryGB *float32 `json:"memory_gb"`
DiskGB *int `json:"disk_gb"`
GPUs *int `json:"gpus"`
Services *[]string `json:"services"`
CodeServerReleaseURL *string `json:"code_server_release_url"`
ImageID *string `json:"image_id"`
ImageTag *string `json:"image_tag"`
CPUCores *float32 `json:"cpu_cores"`
MemoryGB *float32 `json:"memory_gb"`
DiskGB *int `json:"disk_gb"`
GPUs *int `json:"gpus"`
}

// RebuildEnvironment requests that the given envID is rebuilt with no changes to its specification.
Expand Down
19 changes: 17 additions & 2 deletions internal/cmd/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"

"cdr.dev/coder-cli/coder-sdk"
"cdr.dev/coder-cli/internal/coderutil"
"cdr.dev/coder-cli/internal/x/xcobra"
"cdr.dev/coder-cli/pkg/clog"
"cdr.dev/coder-cli/pkg/tablewriter"
Expand Down Expand Up @@ -198,6 +199,11 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub
return err
}

provider, err := coderutil.DefaultWorkspaceProvider(ctx, client)
if err != nil {
return xerrors.Errorf("default workspace provider: %w", err)
}

// ExactArgs(1) ensures our name value can't panic on an out of bounds.
createReq := &coder.CreateEnvironmentRequest{
Name: args[0],
Expand All @@ -209,6 +215,8 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub
DiskGB: disk,
GPUs: gpus,
UseContainerVM: useCVM,
ResourcePoolID: provider.ID,
Namespace: provider.DefaultNamespace,
}

// if any of these defaulted to their zero value we provision
Expand Down Expand Up @@ -339,9 +347,16 @@ coder envs create-from-repo -f coder.yaml`,
return xerrors.Errorf("parse environment template config: %w", err)
}

provider, err := coderutil.DefaultWorkspaceProvider(ctx, client)
if err != nil {
return xerrors.Errorf("default workspace provider: %w", err)
}

env, err := client.CreateEnvironment(ctx, coder.CreateEnvironmentRequest{
OrgID: userOrg.ID,
Template: tpl,
OrgID: userOrg.ID,
Template: tpl,
ResourcePoolID: provider.ID,
Namespace: provider.DefaultNamespace,
})
if err != nil {
return xerrors.Errorf("create environment: %w", err)
Expand Down
14 changes: 14 additions & 0 deletions internal/coderutil/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ func EnvsWithProvider(ctx context.Context, client coder.Client, envs []coder.Env
}
return pooledEnvs, nil
}

// DefaultWorkspaceProvider returns the default provider with which to create environments.
func DefaultWorkspaceProvider(ctx context.Context, c coder.Client) (*coder.WorkspaceProvider, error) {
provider, err := c.WorkspaceProviders(ctx)
if err != nil {
return nil, err
}
for _, p := range provider {
if p.Local {
return &p, nil
}
}
return nil, coder.ErrNotFound
}
Comment on lines +63 to +74
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @f0ssel