|
| 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 | +} |
0 commit comments