Skip to content

Commit 0625956

Browse files
committed
add sandbox create
1 parent 5d9b63a commit 0625956

File tree

4 files changed

+126
-40
lines changed

4 files changed

+126
-40
lines changed

.devcontainer/Dockerfile

Lines changed: 0 additions & 23 deletions
This file was deleted.

.devcontainer/devcontainer.json

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2-
// README at: https://github.com/devcontainers/templates/tree/main/src/debian
31
{
4-
"name": "Debian",
5-
"build": {
6-
"dockerfile": "Dockerfile"
2+
"name": "Gitpod Flex - Go SDK",
3+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu-24.04",
4+
"features": {
5+
"ghcr.io/devcontainers/features/go:1": {
6+
"version": "1.23"
7+
}
78
}
8-
9-
// Features to add to the dev container. More info: https://containers.dev/features.
10-
// "features": {},
11-
12-
// Use 'forwardPorts' to make a list of ports inside the container available locally.
13-
// "forwardPorts": [],
14-
15-
// Configure tool-specific properties.
16-
// "customizations": {},
17-
18-
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
19-
// "remoteUser": "root"
209
}

lib/sandbox/sandbox.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package sandbox
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"time"
8+
9+
"github.com/stainless-sdks/gitpod-go"
10+
)
11+
12+
type EnvironmentSandbox struct {
13+
Client *gitpod.Client
14+
}
15+
16+
func NewEnvironmentSandbox(client *gitpod.Client) *EnvironmentSandbox {
17+
return &EnvironmentSandbox{
18+
Client: client,
19+
}
20+
}
21+
22+
type CreateEnvironmentParams struct {
23+
ProjectID string
24+
ContextURL string
25+
EnvironmentClass string
26+
}
27+
28+
func (s *EnvironmentSandbox) Create(ctx context.Context, params *CreateEnvironmentParams) (res *gitpod.EnvironmentGetResponseEnvironment, err error) {
29+
envID, err := s.create(ctx, params)
30+
if err != nil {
31+
return nil, err
32+
}
33+
return s.waitForRunning(ctx, envID)
34+
}
35+
36+
func (s *EnvironmentSandbox) waitForRunning(ctx context.Context, envID string) (*gitpod.EnvironmentGetResponseEnvironment, error) {
37+
tick := time.NewTicker(500 * time.Microsecond)
38+
defer tick.Stop()
39+
for {
40+
select {
41+
case <-ctx.Done():
42+
return nil, ctx.Err()
43+
case <-tick.C:
44+
resp, err := s.Client.Environments.Get(ctx, gitpod.EnvironmentGetParams{
45+
EnvironmentID: gitpod.String(envID),
46+
})
47+
if err != nil {
48+
// TODO: if transient we should retry?
49+
return nil, err
50+
}
51+
52+
if fm := resp.Environment.Status.FailureMessage; len(fm) > 0 {
53+
return nil, fmt.Errorf("environment creation failed: %s", fm)
54+
}
55+
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseRunning {
56+
return &resp.Environment, nil
57+
}
58+
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseStopping {
59+
return nil, errors.New("environment creation failed: environment is stopping")
60+
}
61+
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseStopped {
62+
return nil, errors.New("environment creation failed: environment is stopped")
63+
}
64+
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseDeleting {
65+
return nil, errors.New("environment creation failed: environment is deleting")
66+
}
67+
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseDeleted {
68+
return nil, errors.New("environment creation failed: environment is deleted")
69+
}
70+
}
71+
}
72+
}
73+
74+
func (s *EnvironmentSandbox) create(ctx context.Context, params *CreateEnvironmentParams) (string, error) {
75+
if params.ProjectID != "" {
76+
resp, err := s.Client.Environments.NewFromProject(ctx, gitpod.EnvironmentNewFromProjectParams{
77+
ProjectID: gitpod.String(params.ProjectID),
78+
})
79+
if err != nil {
80+
return "", err
81+
}
82+
return resp.Environment.ID, nil
83+
}
84+
if params.ContextURL != "" {
85+
if params.EnvironmentClass == "" {
86+
return "", errors.New("environmentClass must be provided when contextURL is provided")
87+
}
88+
resp, err := s.Client.Environments.New(ctx, gitpod.EnvironmentNewParams{
89+
Spec: gitpod.F(gitpod.EnvironmentNewParamsSpec{
90+
DesiredPhase: gitpod.F(gitpod.EnvironmentNewParamsSpecDesiredPhaseEnvironmentPhaseRunning),
91+
Machine: gitpod.F(gitpod.EnvironmentNewParamsSpecMachine{
92+
Class: gitpod.String(params.EnvironmentClass),
93+
}),
94+
Content: gitpod.F(gitpod.EnvironmentNewParamsSpecContent{
95+
Initializer: gitpod.F(gitpod.EnvironmentNewParamsSpecContentInitializer{
96+
Specs: gitpod.F([]gitpod.EnvironmentNewParamsSpecContentInitializerSpecUnion{
97+
gitpod.EnvironmentNewParamsSpecContentInitializerSpecsContextURL{
98+
ContextURL: gitpod.F(gitpod.EnvironmentNewParamsSpecContentInitializerSpecsContextURLContextURL{
99+
URL: gitpod.String(params.ContextURL),
100+
}),
101+
},
102+
}),
103+
}),
104+
}),
105+
}),
106+
})
107+
if err != nil {
108+
return "", err
109+
}
110+
return resp.Environment.ID, nil
111+
}
112+
return "", errors.New("either projectID or contextURL must be provided")
113+
}

lib/sandbox/sandbox_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package sandbox
2+
3+
import "testing"
4+
5+
func TestSandbox(t *testing.T) {
6+
7+
}

0 commit comments

Comments
 (0)