@@ -34,6 +34,30 @@ type Environment struct {
34
34
AutoOffThreshold xjson.Duration `json:"auto_off_threshold" tab:"-"`
35
35
}
36
36
37
+ // CreateEnvironmentRequest is used to configure a new environment
38
+ type CreateEnvironmentRequest struct {
39
+ Name string `json:"name"`
40
+ ImageID string `json:"image_id"`
41
+ ImageTag string `json:"image_tag"`
42
+ CPUCores float32 `json:"cpu_cores"`
43
+ MemoryGB int `json:"memory_gb"`
44
+ DiskGB int `json:"disk_gb"`
45
+ GPUs int `json:"gpus"`
46
+ Services []string `json:"services"`
47
+ }
48
+
49
+ // CreateEnvironment sends a request to create an environment.
50
+ func (c Client ) CreateEnvironment (ctx context.Context , orgID string , req CreateEnvironmentRequest ) (* Environment , error ) {
51
+ var env * Environment
52
+ err := c .requestBody (
53
+ ctx ,
54
+ http .MethodPost , "/api/orgs/" + orgID + "/environments" ,
55
+ req ,
56
+ env ,
57
+ )
58
+ return env , err
59
+ }
60
+
37
61
// EnvironmentsByOrganization gets the list of environments owned by the given user.
38
62
func (c Client ) EnvironmentsByOrganization (ctx context.Context , userID , orgID string ) ([]Environment , error ) {
39
63
var envs []Environment
@@ -46,32 +70,28 @@ func (c Client) EnvironmentsByOrganization(ctx context.Context, userID, orgID st
46
70
return envs , err
47
71
}
48
72
73
+ // DeleteEnvironment deletes the environment.
74
+ func (c Client ) DeleteEnvironment (ctx context.Context , envID string ) error {
75
+ return c .requestBody (
76
+ ctx ,
77
+ http .MethodDelete , "/api/environments/" + envID ,
78
+ nil ,
79
+ nil ,
80
+ )
81
+ }
82
+
49
83
// DialWsep dials an environments command execution interface
50
84
// See github.com/cdr/wsep for details
51
85
func (c Client ) DialWsep (ctx context.Context , env * Environment ) (* websocket.Conn , error ) {
52
- u := c .copyURL ()
53
- if c .BaseURL .Scheme == "https" {
54
- u .Scheme = "wss"
55
- } else {
56
- u .Scheme = "ws"
57
- }
58
- u .Path = "/proxy/environments/" + env .ID + "/wsep"
86
+ return c .dialWs (ctx , "/proxy/environments/" + env .ID + "/wsep" )
87
+ }
59
88
60
- ctx , cancel := context .WithTimeout (ctx , time .Second * 15 )
61
- defer cancel ()
89
+ // DialEnvironmentBuildLog opens a websocket connection for the environment build log messages
90
+ func (c Client ) DialEnvironmentBuildLog (ctx context.Context , envID string ) (* websocket.Conn , error ) {
91
+ return c .dialWs (ctx , "/api/environments/" + envID + "/watch-update" )
92
+ }
62
93
63
- conn , resp , err := websocket .Dial (ctx , u .String (),
64
- & websocket.DialOptions {
65
- HTTPHeader : map [string ][]string {
66
- "Cookie" : {"session_token=" + c .Token },
67
- },
68
- },
69
- )
70
- if err != nil {
71
- if resp != nil {
72
- return nil , bodyError (resp )
73
- }
74
- return nil , err
75
- }
76
- return conn , nil
94
+ // DialEnvironmentStats opens a websocket connection for environment stats
95
+ func (c Client ) DialEnvironmentStats (ctx context.Context , envID string ) (* websocket.Conn , error ) {
96
+ return c .dialWs (ctx , "/api/environments/" + envID + "/watch-stats" )
77
97
}
0 commit comments