Skip to content

Commit ddce8ef

Browse files
committed
finishing touch + c/g + doc + lint
1 parent e2bdedf commit ddce8ef

9 files changed

Lines changed: 1348 additions & 1849 deletions
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
Display the logs of a container from the last 2 hours
4+
5+
USAGE:
6+
scw container container logs <container-id ...> [arg=value ...]
7+
8+
ARGS:
9+
container-id ID of the container which logs are to be displayed
10+
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
11+
12+
FLAGS:
13+
-h, --help help for logs
14+
--list-sub-commands List all subcommands
15+
16+
GLOBAL FLAGS:
17+
-c, --config string The path to the config file
18+
-D, --debug Enable debug mode
19+
-o, --output string Output format: json or human, see 'scw help output' for more info (default "human")
20+
-p, --profile string The config profile to use

cmd/scw/testdata/test-all-usage-container-container-usage.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ AVAILABLE COMMANDS:
1313
redeploy Redeploy a container
1414
update Update the container associated with the specified ID.
1515

16+
UTILITY COMMANDS:
17+
logs Show container logs
18+
1619
FLAGS:
1720
-h, --help help for container
1821
--list-sub-commands List all subcommands

docs/commands/container.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This API allows you to manage your Serverless Containers.
77
- [Delete the container associated with the specified ID.](#delete-the-container-associated-with-the-specified-id.)
88
- [Get the container associated with the specified ID.](#get-the-container-associated-with-the-specified-id.)
99
- [List all containers the caller can access (read permission).](#list-all-containers-the-caller-can-access-(read-permission).)
10+
- [Show container logs](#show-container-logs)
1011
- [Redeploy a container](#redeploy-a-container)
1112
- [Update the container associated with the specified ID.](#update-the-container-associated-with-the-specified-id.)
1213
- [Deploy a container](#deploy-a-container)
@@ -159,6 +160,26 @@ scw container container list [arg=value ...]
159160

160161

161162

163+
### Show container logs
164+
165+
Display the logs of a container from the last 2 hours
166+
167+
**Usage:**
168+
169+
```
170+
scw container container logs <container-id ...> [arg=value ...]
171+
```
172+
173+
174+
**Args:**
175+
176+
| Name | | Description |
177+
|------|---|-------------|
178+
| container-id | | ID of the container which logs are to be displayed |
179+
| region | Default: `fr-par`<br />One of: `fr-par`, `nl-ams`, `pl-waw` | Region to target. If none is passed will use default region from the config |
180+
181+
182+
162183
### Redeploy a container
163184

164185
Performs a rollout of the container by creating new instances with the latest image version and terminating the old instances.

internal/namespaces/container/v1/custom.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ func GetCommands() *core.Commands {
3535
cmds.MustFind("container", "namespace", "update").Override(containerNamespaceUpdateBuilder)
3636
cmds.MustFind("container", "namespace", "delete").Override(containerNamespaceDeleteBuilder)
3737

38+
// Logs and Metrics
39+
cmds.Merge(core.NewCommands(
40+
containerLogs(),
41+
// containerMetrics(), // TODO: coming soon
42+
))
43+
44+
// Deploy
3845
if cmdDeploy := containerDeployCommand(); cmdDeploy != nil {
3946
cmds.Add(cmdDeploy)
4047
}

internal/namespaces/container/v1/custom_container_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66

77
"github.com/scaleway/scaleway-cli/v2/core"
8-
container "github.com/scaleway/scaleway-cli/v2/internal/namespaces/container/v1"
8+
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/container/v1"
99
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/registry/v1"
1010
"github.com/scaleway/scaleway-cli/v2/internal/testhelpers"
1111
containerSDK "github.com/scaleway/scaleway-sdk-go/api/container/v1"

internal/namespaces/container/v1beta1/custom_logs.go renamed to internal/namespaces/container/v1/custom_logs.go

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ type containerLogsRequest struct {
2525
func containerLogs() *core.Command {
2626
return &core.Command{
2727
Short: `Show container logs`,
28-
Long: ``, // TODO
28+
Long: `Display the logs of a container from the last 2 hours`,
2929
Namespace: "container",
3030
Resource: "container",
3131
Verb: "logs",
32-
// Groups: []string{"workflow"}, // TODO
33-
ArgsType: reflect.TypeOf(containerLogsRequest{}),
32+
Groups: []string{"utility"},
33+
ArgsType: reflect.TypeOf(containerLogsRequest{}),
3434
ArgSpecs: core.ArgSpecs{
3535
{
3636
Name: "container-id",
@@ -41,7 +41,6 @@ func containerLogs() *core.Command {
4141
scw.RegionFrPar,
4242
scw.RegionNlAms,
4343
scw.RegionPlWaw,
44-
scw.Region(core.AllLocalities), // TODO: test region=all
4544
),
4645
},
4746
Run: containerLogsRun,
@@ -118,10 +117,10 @@ func buildLokiQuery(datasourceURL, containerID string) (*http.Request, error) {
118117
`{resource_type="serverless_container", resource_id="%s"}`,
119118
containerID,
120119
)
121-
start := time.Now().Add(-2 * time.Hour).Format(time.RFC3339) //"2026-01-26T16:00:00Z"
122-
end := time.Now().Format(time.RFC3339) //"2026-01-26T16:30:00Z"
120+
start := time.Now().Add(-2 * time.Hour).Format(time.RFC3339) // TODO: customize timespan ?
121+
end := time.Now().Format(time.RFC3339)
123122

124-
reqURL := fmt.Sprintf("%s/loki/api/v1/query_range", datasourceURL)
123+
reqURL := datasourceURL + "/loki/api/v1/query_range"
125124

126125
formData := url.Values{}
127126
formData.Set("query", query)
@@ -165,8 +164,9 @@ func readLokiResponseBody(requestBody io.ReadCloser) ([]LogEntry, error) {
165164
return nil, fmt.Errorf("Error parsing JSON: %v\n", err)
166165
}
167166

168-
if len(lokiResp.Data.Result) == 0 { //|| len(lokiResp.Data.Result[0].Values) == 0 {
169-
return nil, fmt.Errorf("No results found\n")
167+
if len(lokiResp.Data.Result) == 0 {
168+
// || len(lokiResp.Data.Result[0].Values) == 0 { TODO: rework no-data case
169+
return nil, errors.New("No results found\n")
170170
}
171171

172172
var response []LogEntry
@@ -209,45 +209,9 @@ func deleteToken(
209209
func getOrCreateToken(
210210
ctx context.Context,
211211
cockpitAPI *cockpit.RegionalAPI,
212-
region scw.Region, scope cockpit.TokenScope,
212+
region scw.Region,
213+
scope cockpit.TokenScope,
213214
) (*cockpit.Token, bool, error) {
214-
// var tokenToUse *cockpit.Token
215-
216-
readOnlyTokens, err := cockpitAPI.ListTokens(&cockpit.RegionalAPIListTokensRequest{
217-
Region: region,
218-
// ProjectID: "",
219-
TokenScopes: []cockpit.TokenScope{scope},
220-
}, scw.WithAllPages(), scw.WithContext(ctx))
221-
if err != nil {
222-
return nil, false, err
223-
}
224-
225-
for _, roToken := range readOnlyTokens.Tokens {
226-
token, err := cockpitAPI.GetToken(&cockpit.RegionalAPIGetTokenRequest{
227-
Region: region,
228-
TokenID: roToken.ID,
229-
}, scw.WithContext(ctx))
230-
if err != nil {
231-
return nil, false, err
232-
}
233-
234-
if token.SecretKey != nil {
235-
return token, false, nil
236-
}
237-
}
238-
239-
// fullAccessTokens, err := cockpitAPI.ListTokens(&cockpit.RegionalAPIListTokensRequest{
240-
// Region: region,
241-
// // ProjectID: "",
242-
// TokenScopes: []cockpit.TokenScope{cockpit.TokenScopeFullAccessLogsRules},
243-
// }, scw.WithAllPages(), scw.WithContext(ctx))
244-
// if err != nil {
245-
// return nil, err
246-
// }
247-
//
248-
// if len(fullAccessTokens.Tokens) > 0 {
249-
// tokenToUse = fullAccessTokens.Tokens[0]
250-
// } else {
251215
token, err := cockpitAPI.CreateToken(&cockpit.RegionalAPICreateTokenRequest{
252216
Region: region,
253217
// ProjectID: "",

internal/namespaces/container/v1beta1/custom_logs_test.go renamed to internal/namespaces/container/v1/custom_logs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66

77
"github.com/scaleway/scaleway-cli/v2/core"
8-
container "github.com/scaleway/scaleway-cli/v2/internal/namespaces/container/v1beta1"
8+
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/container/v1"
99
)
1010

1111
func Test_ContainerLogs(t *testing.T) {
@@ -16,7 +16,7 @@ func Test_ContainerLogs(t *testing.T) {
1616
BeforeFunc: core.BeforeFuncCombine(
1717
createNamespace("Namespace"),
1818
core.ExecStoreBeforeCmd("Container", fmt.Sprintf(
19-
"scw container container create namespace-id={{ .Namespace.ID }} name=%s registry-image=%s -w",
19+
"scw container container create namespace-id={{ .Namespace.ID }} name=%s image=%s -w",
2020
core.GetRandomName("test-logs"),
2121
image,
2222
)),

0 commit comments

Comments
 (0)