Skip to content
Closed
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
23 changes: 0 additions & 23 deletions .devcontainer/Dockerfile

This file was deleted.

23 changes: 6 additions & 17 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/debian
{
"name": "Debian",
"build": {
"dockerfile": "Dockerfile"
"name": "Gitpod Flex - Go SDK",
"image": "mcr.microsoft.com/devcontainers/base:ubuntu-24.04",
"features": {
"ghcr.io/devcontainers/features/go:1": {
"version": "1.23"
}
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
1 change: 1 addition & 0 deletions environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func TestEnvironmentUpdate(t *testing.T) {
)
_, err := client.Environments.Update(context.TODO(), gitpod.EnvironmentUpdateParams{
Body: gitpod.EnvironmentUpdateParamsBodyMetadata{

Metadata: gitpod.F[any](map[string]interface{}{}),
},
})
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ module github.com/stainless-sdks/gitpod-go

go 1.21

require github.com/pkg/sftp v1.13.7

require (
github.com/google/uuid v1.3.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
)
54 changes: 54 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/pkg/sftp v1.13.7 h1:uv+I3nNJvlKZIQGSr8JVQLNHFU9YhhNpvC14Y6KgmSM=
github.com/pkg/sftp v1.13.7/go.mod h1:KMKI0t3T6hfA+lTR/ssZdunHo+uwq7ghoN09/FSu3DY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
Expand All @@ -10,3 +21,46 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
227 changes: 227 additions & 0 deletions lib/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package lib

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/stainless-sdks/gitpod-go"
"github.com/stainless-sdks/gitpod-go/internal/apierror"
)

type CreateOptions struct {
ProjectID string
ContextURL string
EnvironmentClass string
}

type EnvironmentService interface {
Create(ctx context.Context, options *CreateOptions) (*gitpod.EnvironmentGetResponseEnvironment, error)
Start(ctx context.Context, environmentID string) (*gitpod.EnvironmentGetResponseEnvironment, error)
Stop(ctx context.Context, environmentID string) (*gitpod.EnvironmentGetResponseEnvironment, error)
Delete(ctx context.Context, environmentID string) error
}

func NewEnvironmentService(client *gitpod.Client) EnvironmentService {
return &environmentService{
Client: client,
}
}

type environmentService struct {
Client *gitpod.Client
}

func (s *environmentService) Create(ctx context.Context, options *CreateOptions) (*gitpod.EnvironmentGetResponseEnvironment, error) {
envID, err := s.create(ctx, options)
if err != nil {
return nil, err
}
return waitForRunning(ctx, s.Client, envID, func(env *gitpod.EnvironmentGetResponseEnvironment) (bool, error) {
return true, nil
})
}

func (s *environmentService) Start(ctx context.Context, environmentID string) (*gitpod.EnvironmentGetResponseEnvironment, error) {
_, err := s.Client.Environments.Start(ctx, gitpod.EnvironmentStartParams{
EnvironmentID: gitpod.String(environmentID),
})
if err != nil {
return nil, err
}
return waitForRunning(ctx, s.Client, environmentID, func(env *gitpod.EnvironmentGetResponseEnvironment) (bool, error) {
return true, nil
})
}

func (s *environmentService) Stop(ctx context.Context, environmentID string) (*gitpod.EnvironmentGetResponseEnvironment, error) {
_, err := s.Client.Environments.Stop(ctx, gitpod.EnvironmentStopParams{
EnvironmentID: gitpod.String(environmentID),
})
if err != nil {
return nil, err
}
return waitForStopped(ctx, s.Client, environmentID)
}

func (s *environmentService) Delete(ctx context.Context, environmentID string) error {
_, err := s.Client.Environments.Delete(ctx, gitpod.EnvironmentDeleteParams{
EnvironmentID: gitpod.String(environmentID),
})
if err != nil {
apierr, ok := err.(*apierror.Error)
if ok && apierr.StatusCode == http.StatusNotFound {
return nil
}
return err
}
return waitForDeleted(ctx, s.Client, environmentID)
}

func waitForDeleted(ctx context.Context, client *gitpod.Client, envID string) error {
_, err := waitForEnvironment(ctx, client, envID, func() (*gitpod.EnvironmentGetResponseEnvironment, bool, error) {
resp, err := client.Environments.Get(ctx, gitpod.EnvironmentGetParams{
EnvironmentID: gitpod.String(envID),
})
if err != nil {
apierr, ok := err.(*apierror.Error)
if ok && apierr.StatusCode == http.StatusNotFound {
return nil, true, nil
}
return nil, false, err
}
return nil, resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseDeleted, nil
})
return err
}

func waitForStopped(ctx context.Context, client *gitpod.Client, envID string) (*gitpod.EnvironmentGetResponseEnvironment, error) {
return waitForEnvironment(ctx, client, envID, func() (*gitpod.EnvironmentGetResponseEnvironment, bool, error) {
resp, err := client.Environments.Get(ctx, gitpod.EnvironmentGetParams{
EnvironmentID: gitpod.String(envID),
})
if err != nil {
return nil, false, err
}
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseStopped ||
resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseDeleting ||
resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseDeleted {
return &resp.Environment, true, nil
}
return nil, false, nil
})
}

func waitForRunning(ctx context.Context, client *gitpod.Client, envID string, check func(*gitpod.EnvironmentGetResponseEnvironment) (bool, error)) (*gitpod.EnvironmentGetResponseEnvironment, error) {
return waitForEnvironment(ctx, client, envID, func() (*gitpod.EnvironmentGetResponseEnvironment, bool, error) {
resp, err := client.Environments.Get(ctx, gitpod.EnvironmentGetParams{
EnvironmentID: gitpod.String(envID),
})
if err != nil {
return nil, false, err
}

if fm := resp.Environment.Status.FailureMessage; len(fm) > 0 {
return nil, false, fmt.Errorf("environment creation failed: %s", fm)
}
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseRunning {
ok, err := check(&resp.Environment)
if err != nil {
return nil, false, err
}
return &resp.Environment, ok, nil
}
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseStopping {
return nil, false, errors.New("environment creation failed: environment is stopping")
}
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseStopped {
return nil, false, errors.New("environment creation failed: environment is stopped")
}
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseDeleting {
return nil, false, errors.New("environment creation failed: environment is deleting")
}
if resp.Environment.Status.Phase == gitpod.EnvironmentGetResponseEnvironmentStatusPhaseEnvironmentPhaseDeleted {
return nil, false, errors.New("environment creation failed: environment is deleted")
}
return nil, false, nil
})
}

func waitForEnvironment(ctx context.Context, client *gitpod.Client, envID string, check func() (*gitpod.EnvironmentGetResponseEnvironment, bool, error)) (*gitpod.EnvironmentGetResponseEnvironment, error) {
return WaitFor(ctx, client, envID, gitpod.EventWatchResponseResourceTypeResourceTypeEnvironment, envID, check)
}

func WaitFor[T any](ctx context.Context, client *gitpod.Client, envID string, resourceType gitpod.EventWatchResponseResourceType, resourceID string, check func() (*T, bool, error)) (*T, error) {
env, ok, err := check()
if err != nil {
return nil, err
}
if ok {
return env, nil
}

stream := client.Events.WatchStreaming(ctx, gitpod.EventWatchParams{
Body: gitpod.EventWatchParamsBodyEnvironmentScopeProducesEventsForTheEnvironmentItselfAllTasksTaskExecutionsAndServicesAssociatedWithThatEnvironment{
EnvironmentID: gitpod.String(envID),
},
})
defer stream.Close()

for stream.Next() {
resp := stream.Current()
if resp.ResourceType == resourceType && resp.ResourceID == resourceID {
env, ok, err := check()
if err != nil {
// TODO: if transient we should retry?
return nil, err
}
if ok {
return env, nil
}
}
}

return nil, stream.Err()
}

func (s *environmentService) create(ctx context.Context, options *CreateOptions) (string, error) {
if options.ProjectID != "" {
resp, err := s.Client.Environments.NewFromProject(ctx, gitpod.EnvironmentNewFromProjectParams{
ProjectID: gitpod.String(options.ProjectID),
})
if err != nil {
return "", err
}
return resp.Environment.ID, nil
}
if options.ContextURL != "" {
if options.EnvironmentClass == "" {
return "", errors.New("environmentClass must be provided when contextURL is provided")
}
resp, err := s.Client.Environments.New(ctx, gitpod.EnvironmentNewParams{
Spec: gitpod.F(gitpod.EnvironmentNewParamsSpec{
DesiredPhase: gitpod.F(gitpod.EnvironmentNewParamsSpecDesiredPhaseEnvironmentPhaseRunning),
Machine: gitpod.F(gitpod.EnvironmentNewParamsSpecMachine{
Class: gitpod.String(options.EnvironmentClass),
}),
Content: gitpod.F(gitpod.EnvironmentNewParamsSpecContent{
Initializer: gitpod.F(gitpod.EnvironmentNewParamsSpecContentInitializer{
Specs: gitpod.F([]gitpod.EnvironmentNewParamsSpecContentInitializerSpecUnion{
gitpod.EnvironmentNewParamsSpecContentInitializerSpecsContextURL{
ContextURL: gitpod.F(gitpod.EnvironmentNewParamsSpecContentInitializerSpecsContextURLContextURL{
URL: gitpod.String(options.ContextURL),
}),
},
}),
}),
}),
}),
})
if err != nil {
return "", err
}
return resp.Environment.ID, nil
}
return "", errors.New("either projectID or contextURL must be provided")
}
Loading
Loading