File size: 8,465 Bytes
daa8246
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package ionet

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"github.com/samber/lo"
)

// ListContainers retrieves all containers for a specific deployment
func (c *Client) ListContainers(deploymentID string) (*ContainerList, error) {
	if deploymentID == "" {
		return nil, fmt.Errorf("deployment ID cannot be empty")
	}

	endpoint := fmt.Sprintf("/deployment/%s/containers", deploymentID)

	resp, err := c.makeRequest("GET", endpoint, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to list containers: %w", err)
	}

	var containerList ContainerList
	if err := decodeDataWithFlexibleTimes(resp.Body, &containerList); err != nil {
		return nil, fmt.Errorf("failed to parse containers list: %w", err)
	}

	return &containerList, nil
}

// GetContainerDetails retrieves detailed information about a specific container
func (c *Client) GetContainerDetails(deploymentID, containerID string) (*Container, error) {
	if deploymentID == "" {
		return nil, fmt.Errorf("deployment ID cannot be empty")
	}
	if containerID == "" {
		return nil, fmt.Errorf("container ID cannot be empty")
	}

	endpoint := fmt.Sprintf("/deployment/%s/container/%s", deploymentID, containerID)

	resp, err := c.makeRequest("GET", endpoint, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get container details: %w", err)
	}

	// API response format not documented, assuming direct format
	var container Container
	if err := decodeWithFlexibleTimes(resp.Body, &container); err != nil {
		return nil, fmt.Errorf("failed to parse container details: %w", err)
	}

	return &container, nil
}

// GetContainerJobs retrieves containers jobs for a specific container (similar to containers endpoint)
func (c *Client) GetContainerJobs(deploymentID, containerID string) (*ContainerList, error) {
	if deploymentID == "" {
		return nil, fmt.Errorf("deployment ID cannot be empty")
	}
	if containerID == "" {
		return nil, fmt.Errorf("container ID cannot be empty")
	}

	endpoint := fmt.Sprintf("/deployment/%s/containers-jobs/%s", deploymentID, containerID)

	resp, err := c.makeRequest("GET", endpoint, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get container jobs: %w", err)
	}

	var containerList ContainerList
	if err := decodeDataWithFlexibleTimes(resp.Body, &containerList); err != nil {
		return nil, fmt.Errorf("failed to parse container jobs: %w", err)
	}

	return &containerList, nil
}

// buildLogEndpoint constructs the request path for fetching logs
func buildLogEndpoint(deploymentID, containerID string, opts *GetLogsOptions) (string, error) {
	if deploymentID == "" {
		return "", fmt.Errorf("deployment ID cannot be empty")
	}
	if containerID == "" {
		return "", fmt.Errorf("container ID cannot be empty")
	}

	params := make(map[string]interface{})

	if opts != nil {
		if opts.Level != "" {
			params["level"] = opts.Level
		}
		if opts.Stream != "" {
			params["stream"] = opts.Stream
		}
		if opts.Limit > 0 {
			params["limit"] = opts.Limit
		}
		if opts.Cursor != "" {
			params["cursor"] = opts.Cursor
		}
		if opts.Follow {
			params["follow"] = true
		}

		if opts.StartTime != nil {
			params["start_time"] = opts.StartTime
		}
		if opts.EndTime != nil {
			params["end_time"] = opts.EndTime
		}
	}

	endpoint := fmt.Sprintf("/deployment/%s/log/%s", deploymentID, containerID)
	endpoint += buildQueryParams(params)

	return endpoint, nil
}

// GetContainerLogs retrieves logs for containers in a deployment and normalizes them
func (c *Client) GetContainerLogs(deploymentID, containerID string, opts *GetLogsOptions) (*ContainerLogs, error) {
	raw, err := c.GetContainerLogsRaw(deploymentID, containerID, opts)
	if err != nil {
		return nil, err
	}

	logs := &ContainerLogs{
		ContainerID: containerID,
	}

	if raw == "" {
		return logs, nil
	}

	normalized := strings.ReplaceAll(raw, "\r\n", "\n")
	lines := strings.Split(normalized, "\n")
	logs.Logs = lo.FilterMap(lines, func(line string, _ int) (LogEntry, bool) {
		if strings.TrimSpace(line) == "" {
			return LogEntry{}, false
		}
		return LogEntry{Message: line}, true
	})

	return logs, nil
}

// GetContainerLogsRaw retrieves the raw text logs for a specific container
func (c *Client) GetContainerLogsRaw(deploymentID, containerID string, opts *GetLogsOptions) (string, error) {
	endpoint, err := buildLogEndpoint(deploymentID, containerID, opts)
	if err != nil {
		return "", err
	}

	resp, err := c.makeRequest("GET", endpoint, nil)
	if err != nil {
		return "", fmt.Errorf("failed to get container logs: %w", err)
	}

	return string(resp.Body), nil
}

// StreamContainerLogs streams real-time logs for a specific container
// This method uses a callback function to handle incoming log entries
func (c *Client) StreamContainerLogs(deploymentID, containerID string, opts *GetLogsOptions, callback func(*LogEntry) error) error {
	if deploymentID == "" {
		return fmt.Errorf("deployment ID cannot be empty")
	}
	if containerID == "" {
		return fmt.Errorf("container ID cannot be empty")
	}
	if callback == nil {
		return fmt.Errorf("callback function cannot be nil")
	}

	// Set follow to true for streaming
	if opts == nil {
		opts = &GetLogsOptions{}
	}
	opts.Follow = true

	endpoint, err := buildLogEndpoint(deploymentID, containerID, opts)
	if err != nil {
		return err
	}

	// Note: This is a simplified implementation. In a real scenario, you might want to use
	// Server-Sent Events (SSE) or WebSocket for streaming logs
	for {
		resp, err := c.makeRequest("GET", endpoint, nil)
		if err != nil {
			return fmt.Errorf("failed to stream container logs: %w", err)
		}

		var logs ContainerLogs
		if err := decodeWithFlexibleTimes(resp.Body, &logs); err != nil {
			return fmt.Errorf("failed to parse container logs: %w", err)
		}

		// Call the callback for each log entry
		for _, logEntry := range logs.Logs {
			if err := callback(&logEntry); err != nil {
				return fmt.Errorf("callback error: %w", err)
			}
		}

		// If there are no more logs or we have a cursor, continue polling
		if !logs.HasMore && logs.NextCursor == "" {
			break
		}

		// Update cursor for next request
		if logs.NextCursor != "" {
			opts.Cursor = logs.NextCursor
			endpoint, err = buildLogEndpoint(deploymentID, containerID, opts)
			if err != nil {
				return err
			}
		}

		// Wait a bit before next poll to avoid overwhelming the API
		time.Sleep(2 * time.Second)
	}

	return nil
}

// RestartContainer restarts a specific container (if supported by the API)
func (c *Client) RestartContainer(deploymentID, containerID string) error {
	if deploymentID == "" {
		return fmt.Errorf("deployment ID cannot be empty")
	}
	if containerID == "" {
		return fmt.Errorf("container ID cannot be empty")
	}

	endpoint := fmt.Sprintf("/deployment/%s/container/%s/restart", deploymentID, containerID)

	_, err := c.makeRequest("POST", endpoint, nil)
	if err != nil {
		return fmt.Errorf("failed to restart container: %w", err)
	}

	return nil
}

// StopContainer stops a specific container (if supported by the API)
func (c *Client) StopContainer(deploymentID, containerID string) error {
	if deploymentID == "" {
		return fmt.Errorf("deployment ID cannot be empty")
	}
	if containerID == "" {
		return fmt.Errorf("container ID cannot be empty")
	}

	endpoint := fmt.Sprintf("/deployment/%s/container/%s/stop", deploymentID, containerID)

	_, err := c.makeRequest("POST", endpoint, nil)
	if err != nil {
		return fmt.Errorf("failed to stop container: %w", err)
	}

	return nil
}

// ExecuteInContainer executes a command in a specific container (if supported by the API)
func (c *Client) ExecuteInContainer(deploymentID, containerID string, command []string) (string, error) {
	if deploymentID == "" {
		return "", fmt.Errorf("deployment ID cannot be empty")
	}
	if containerID == "" {
		return "", fmt.Errorf("container ID cannot be empty")
	}
	if len(command) == 0 {
		return "", fmt.Errorf("command cannot be empty")
	}

	reqBody := map[string]interface{}{
		"command": command,
	}

	endpoint := fmt.Sprintf("/deployment/%s/container/%s/exec", deploymentID, containerID)

	resp, err := c.makeRequest("POST", endpoint, reqBody)
	if err != nil {
		return "", fmt.Errorf("failed to execute command in container: %w", err)
	}

	var result map[string]interface{}
	if err := json.Unmarshal(resp.Body, &result); err != nil {
		return "", fmt.Errorf("failed to parse execution result: %w", err)
	}

	if output, ok := result["output"].(string); ok {
		return output, nil
	}

	return string(resp.Body), nil
}