Skip to content

Commit 6cc4e8a

Browse files
committed
FastEdge API change
1 parent 7cc7beb commit 6cc4e8a

7 files changed

Lines changed: 67 additions & 46 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/G-core/gcore-cli
33
go 1.21.5
44

55
require (
6-
github.com/G-Core/FastEdge-client-sdk-go v0.1.0
6+
github.com/G-Core/FastEdge-client-sdk-go v0.2.0
77
github.com/alecthomas/assert v1.0.0
88
github.com/dustin/go-humanize v1.0.1
99
github.com/fatih/color v1.16.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/G-Core/FastEdge-client-sdk-go v0.1.0 h1:vjkOiencavIb8eztdqCXFrvCvLFM6eJD04/mbU/PTYo=
2-
github.com/G-Core/FastEdge-client-sdk-go v0.1.0/go.mod h1:ggyUVhy8/OCMBY4nbm7n9qDoPioROCk4vHhDJq9w7qE=
1+
github.com/G-Core/FastEdge-client-sdk-go v0.2.0 h1:OHibIpVO/7kEG1eKy+5i0jcb1urPNm9EKUdH84entpk=
2+
github.com/G-Core/FastEdge-client-sdk-go v0.2.0/go.mod h1:ggyUVhy8/OCMBY4nbm7n9qDoPioROCk4vHhDJq9w7qE=
33
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
44
github.com/alecthomas/assert v1.0.0 h1:3XmGh/PSuLzDbK3W2gUbRXwgW5lqPkuqvRgeQ30FI5o=
55
github.com/alecthomas/assert v1.0.0/go.mod h1:va/d2JC+M7F6s+80kl/R3G7FUiW6JzUO+hPhLyJ36ZY=

internal/commands/fastedge/app.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ uploading binary using "--file <filename>". To load file from stdin, use "-" as
5858
return fmt.Errorf("adding the app: %w", err)
5959
}
6060
if rsp.StatusCode() != http.StatusOK {
61-
return fmt.Errorf("adding the app: %s", string(rsp.Body))
61+
return fmt.Errorf("adding the app: %s", extractErrorMessage(rsp.Body))
6262
}
6363

6464
if output.Format(cmd) == output.FmtJSON {
@@ -121,7 +121,7 @@ uploading binary using "--file <filename>". To load file from stdin, use "-" as
121121
return fmt.Errorf("updating the app: %w", err)
122122
}
123123
if rsp.StatusCode() != http.StatusOK {
124-
return fmt.Errorf("updating the app: %s", string(rsp.Body))
124+
return fmt.Errorf("updating the app: %s", extractErrorMessage(rsp.Body))
125125
}
126126

127127
if output.Format(cmd) == output.FmtJSON {
@@ -147,12 +147,12 @@ uploading binary using "--file <filename>". To load file from stdin, use "-" as
147147
Short: "Show list of client's apps",
148148
Args: cobra.NoArgs,
149149
RunE: func(cmd *cobra.Command, args []string) error {
150-
rsp, err := client.ListAppsWithResponse(context.Background())
150+
rsp, err := client.ListAppsWithResponse(context.Background(), &sdk.ListAppsParams{})
151151
if err != nil {
152152
return fmt.Errorf("getting the list of apps: %w", err)
153153
}
154154
if rsp.StatusCode() != http.StatusOK {
155-
return fmt.Errorf("getting the list of apps: %s", string(rsp.Body))
155+
return fmt.Errorf("getting the list of apps: %s", extractErrorMessage(rsp.Body))
156156
}
157157

158158
if output.Format(cmd) == output.FmtJSON {
@@ -201,7 +201,7 @@ commands.`,
201201
return fmt.Errorf("getting app detail: %w", err)
202202
}
203203
if rsp.StatusCode() != http.StatusOK {
204-
return fmt.Errorf("getting app details: %s", string(rsp.Body))
204+
return fmt.Errorf("getting app details: %s", extractErrorMessage(rsp.Body))
205205
}
206206

207207
if output.Format(cmd) == output.FmtJSON {
@@ -244,7 +244,7 @@ commands.`,
244244
return fmt.Errorf("enabling app: %w", err)
245245
}
246246
if rsp.StatusCode() != http.StatusOK {
247-
return fmt.Errorf("enabling app: %s", string(rsp.Body))
247+
return fmt.Errorf("enabling app: %s", extractErrorMessage(rsp.Body))
248248
}
249249

250250
if output.Format(cmd) == output.FmtJSON {
@@ -275,7 +275,7 @@ commands.`,
275275
return fmt.Errorf("disabling app: %w", err)
276276
}
277277
if rsp.StatusCode() != http.StatusOK {
278-
return fmt.Errorf("disabling app: %s", string(rsp.Body))
278+
return fmt.Errorf("disabling app: %s", extractErrorMessage(rsp.Body))
279279
}
280280

281281
if output.Format(cmd) == output.FmtJSON {
@@ -311,7 +311,7 @@ so if you don't want this to happen, consider disabling the app to keep binary r
311311
return fmt.Errorf("deleting app: %w", err)
312312
}
313313
if rsp.StatusCode() != http.StatusOK {
314-
return fmt.Errorf("deleting app: %s", string(rsp.Body))
314+
return fmt.Errorf("deleting app: %s", extractErrorMessage(rsp.Body))
315315
}
316316

317317
if output.Format(cmd) == output.FmtJSON {

internal/commands/fastedge/binary.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func binary() *cobra.Command {
3737
return fmt.Errorf("getting the list of binaries: %w", err)
3838
}
3939
if rsp.StatusCode() != http.StatusOK {
40-
return fmt.Errorf("getting the list of binaries: %s", string(rsp.Body))
40+
return fmt.Errorf("getting the list of binaries: %s", extractErrorMessage(rsp.Body))
4141
}
4242

4343
if output.Format(cmd) == output.FmtJSON {
@@ -107,7 +107,7 @@ If this flag is omitted, file contant is read from stdin.`,
107107
return fmt.Errorf("getting the list of plans: %w", err)
108108
}
109109
if rsp.StatusCode() != http.StatusOK {
110-
return fmt.Errorf("getting the list of plans: %s", string(rsp.Body))
110+
return fmt.Errorf("getting the list of plans: %s", extractErrorMessage(rsp.Body))
111111
}
112112

113113
if output.Format(cmd) == output.FmtJSON {
@@ -118,7 +118,7 @@ If this flag is omitted, file contant is read from stdin.`,
118118
fmt.Printf(
119119
"Status:\t\t%s\nSource lang:\t%s\n",
120120
binStatusToString(rsp.JSON200.Status),
121-
srcLangToString(rsp.JSON200.Type),
121+
srcLangToString(rsp.JSON200.Source),
122122
)
123123
if rsp.JSON200.Name != nil && *rsp.JSON200.Name != "" {
124124
fmt.Printf("Name:\t\t%s\n", *rsp.JSON200.Name)
@@ -154,7 +154,7 @@ If this flag is omitted, file contant is read from stdin.`,
154154
return fmt.Errorf("getting the list of plans: %w", err)
155155
}
156156
if rsp.StatusCode() != http.StatusOK {
157-
return fmt.Errorf("getting the list of plans: %s", string(rsp.Body))
157+
return fmt.Errorf("getting the list of plans: %s", extractErrorMessage(rsp.Body))
158158
}
159159

160160
if output.Format(cmd) == output.FmtJSON {
@@ -192,10 +192,10 @@ func uploadBinary(src string) (int64, error) {
192192
return 0, fmt.Errorf("cannot upload the binary: %w", err)
193193
}
194194
if rsp.StatusCode() != http.StatusOK {
195-
return 0, fmt.Errorf("cannot upload the binary: %s", string(rsp.Body))
195+
return 0, fmt.Errorf("cannot upload the binary: %s", extractErrorMessage(rsp.Body))
196196
}
197197

198-
return *rsp.JSON200, nil
198+
return rsp.JSON200.Id, nil
199199
}
200200

201201
func binStatusToString(s int) string {

internal/commands/fastedge/fastedge.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fastedge
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"net/http"
78
"runtime/debug"
@@ -75,3 +76,15 @@ func addSDKversionHeader(ctx context.Context, req *http.Request) error {
7576
}
7677
return nil
7778
}
79+
80+
type errResponse struct {
81+
Error string `json:"error"`
82+
}
83+
84+
func extractErrorMessage(rspBuf []byte) string {
85+
var rsp errResponse
86+
if err := json.Unmarshal(rspBuf, &rsp); err == nil {
87+
return rsp.Error
88+
}
89+
return string(rspBuf)
90+
}

internal/commands/fastedge/logs.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func appLogsFilterFlags(cmd *cobra.Command) {
2929
func logs() *cobra.Command {
3030
var (
3131
from, to time.Time
32-
sort *sdk.GetV1AppsIdLogsParamsSort
32+
sort *sdk.ListLogsParamsSort
3333
edge *string
3434
clientIp *string
3535
)
@@ -72,7 +72,7 @@ This command allows you filtering by edge name, client ip and time range.`,
7272
}
7373

7474
if sortFlag != "" {
75-
logParamSort := sdk.GetV1AppsIdLogsParamsSort(sortFlag)
75+
logParamSort := sdk.ListLogsParamsSort(sortFlag)
7676
if logParamSort != sdk.Asc && logParamSort != sdk.Desc {
7777
return errors.New("invalid value for `sort` expected asc or desc")
7878
}
@@ -94,10 +94,10 @@ This command allows you filtering by edge name, client ip and time range.`,
9494
return fmt.Errorf("cannot find app by name: %w", err)
9595
}
9696

97-
rsp, err := client.GetV1AppsIdLogsWithResponse(
97+
rsp, err := client.ListLogsWithResponse(
9898
context.Background(),
9999
id,
100-
&sdk.GetV1AppsIdLogsParams{
100+
&sdk.ListLogsParams{
101101
From: &from,
102102
To: &to,
103103
Edge: edge,
@@ -109,7 +109,7 @@ This command allows you filtering by edge name, client ip and time range.`,
109109
return fmt.Errorf("getting app logs: %w", err)
110110
}
111111
if rsp.StatusCode() != http.StatusOK {
112-
return fmt.Errorf("getting app logs: %s", string(rsp.Body))
112+
return fmt.Errorf("getting app logs: %s", extractErrorMessage(rsp.Body))
113113
}
114114

115115
if output.Format(cmd) == output.FmtJSON {
@@ -145,10 +145,10 @@ This command allows you filtering by edge name, client ip and time range.`,
145145
)
146146

147147
// Call the API again with the new page number
148-
rsp, err = client.GetV1AppsIdLogsWithResponse(
148+
rsp, err = client.ListLogsWithResponse(
149149
context.Background(),
150150
id,
151-
&sdk.GetV1AppsIdLogsParams{
151+
&sdk.ListLogsParams{
152152
From: &from,
153153
To: &to,
154154
Edge: edge,
@@ -191,7 +191,7 @@ This command allows you filtering by edge name, client ip and time range.`,
191191
return fmt.Errorf("enabling logging: %w", err)
192192
}
193193
if rsp.StatusCode() != http.StatusOK {
194-
return fmt.Errorf("enabling logging: %s", string(rsp.Body))
194+
return fmt.Errorf("enabling logging: %s", extractErrorMessage(rsp.Body))
195195
}
196196

197197
rsp1, err := client.GetAppWithResponse(
@@ -202,7 +202,7 @@ This command allows you filtering by edge name, client ip and time range.`,
202202
return fmt.Errorf("getting app detail: %w", err)
203203
}
204204
if rsp.StatusCode() != http.StatusOK {
205-
return fmt.Errorf("getting app details: %s", string(rsp.Body))
205+
return fmt.Errorf("getting app details: %s", extractErrorMessage(rsp.Body))
206206
}
207207

208208
if rsp1.JSON200.DebugUntil == nil {
@@ -232,7 +232,7 @@ This command allows you filtering by edge name, client ip and time range.`,
232232
return fmt.Errorf("disabling logging: %w", err)
233233
}
234234
if rsp.StatusCode() != http.StatusOK {
235-
return fmt.Errorf("disabling logging: %s", string(rsp.Body))
235+
return fmt.Errorf("disabling logging: %s", extractErrorMessage(rsp.Body))
236236
}
237237

238238
fmt.Printf("Logging for app %d disabled\n", id)

internal/commands/fastedge/stats.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func stat() *cobra.Command {
2626
return fmt.Errorf("getting the statistics: %w", err)
2727
}
2828
if rsp.StatusCode() != http.StatusOK {
29-
return fmt.Errorf("getting the statistics: %s", string(rsp.Body))
29+
return fmt.Errorf("getting the statistics: %s", extractErrorMessage(rsp.Body))
3030
}
3131

3232
if output.Format(cmd) == output.FmtJSON {
@@ -56,7 +56,7 @@ func stat() *cobra.Command {
5656
}
5757

5858
var cmdCalls = &cobra.Command{
59-
Use: "calls <app_name>",
59+
Use: "calls [<app_name>]",
6060
Aliases: []string{"calls"},
6161
Short: "Show app calls statistic",
6262
Long: `Show number of app calls, grouped by time slots and HTTP statuses.
@@ -65,11 +65,15 @@ but you can change reporting interval using "--from" and "--to" flags
6565
(specifying date/time in format "YYYY-MM-DD HH:mm:SS", where either date or time,
6666
can be omitted, or as UNIX timestamp) and reporting step duration with flag
6767
"--step" (in seconds).`,
68-
Args: cobra.ExactArgs(1),
68+
Args: cobra.MaximumNArgs(1),
6969
RunE: func(cmd *cobra.Command, args []string) error {
70-
id, err := getAppIdByName(args[0])
71-
if err != nil {
72-
return fmt.Errorf("cannot find app by name: %w", err)
70+
var appId *int64
71+
if len(args) > 0 {
72+
id, err := getAppIdByName(args[0])
73+
if err != nil {
74+
return fmt.Errorf("cannot find app by name: %w", err)
75+
}
76+
appId = &id
7377
}
7478

7579
from, err := parseTimeFlag(cmd, "from")
@@ -87,10 +91,10 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag
8791
return fmt.Errorf("cannot parse reporting step: %w", err)
8892
}
8993

90-
rsp, err := client.AppCallsWithResponse(
94+
rsp, err := client.StatsCallsWithResponse(
9195
context.Background(),
92-
id,
93-
&sdk.AppCallsParams{
96+
&sdk.StatsCallsParams{
97+
Id: appId,
9498
From: from,
9599
To: to,
96100
Step: step,
@@ -100,7 +104,7 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag
100104
return fmt.Errorf("cannot get statistics: %w", err)
101105
}
102106
if rsp.StatusCode() != http.StatusOK {
103-
return fmt.Errorf("cannot get statistics: %s", string(rsp.Body))
107+
return fmt.Errorf("cannot get statistics: %s", extractErrorMessage(rsp.Body))
104108
}
105109

106110
if output.Format(cmd) == output.FmtJSON {
@@ -168,7 +172,7 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag
168172
statFlags(cmdCalls)
169173

170174
var cmdDuration = &cobra.Command{
171-
Use: "duration <app_name>",
175+
Use: "duration [<app_name>]",
172176
Aliases: []string{"duration", "time", "timing"},
173177
Short: "Show app execution duration",
174178
Long: `Show duration of app calls, grouped by time slots. All times are in msec
@@ -177,11 +181,15 @@ but you can change reporting interval using "--from" and "--to" flags
177181
(specifying date/time in format "YYYY-MM-DD HH:mm:SS", where either date or time,
178182
can be omitted, or as UNIX timestamp) and reporting step duration with flag
179183
"--step" (in seconds).`,
180-
Args: cobra.ExactArgs(1),
184+
Args: cobra.MaximumNArgs(1),
181185
RunE: func(cmd *cobra.Command, args []string) error {
182-
id, err := getAppIdByName(args[0])
183-
if err != nil {
184-
return fmt.Errorf("cannot find app by name: %w", err)
186+
var appId *int64
187+
if len(args) > 0 {
188+
id, err := getAppIdByName(args[0])
189+
if err != nil {
190+
return fmt.Errorf("cannot find app by name: %w", err)
191+
}
192+
appId = &id
185193
}
186194

187195
from, err := parseTimeFlag(cmd, "from")
@@ -199,10 +207,10 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag
199207
return fmt.Errorf("cannot parse reporting step: %w", err)
200208
}
201209

202-
rsp, err := client.AppDurationWithResponse(
210+
rsp, err := client.StatsDurationWithResponse(
203211
context.Background(),
204-
id,
205-
&sdk.AppDurationParams{
212+
&sdk.StatsDurationParams{
213+
Id: appId,
206214
From: from,
207215
To: to,
208216
Step: step,
@@ -212,7 +220,7 @@ can be omitted, or as UNIX timestamp) and reporting step duration with flag
212220
return fmt.Errorf("cannot get statistics: %w", err)
213221
}
214222
if rsp.StatusCode() != http.StatusOK {
215-
return fmt.Errorf("cannot get statistics: %s", string(rsp.Body))
223+
return fmt.Errorf("cannot get statistics: %s", extractErrorMessage(rsp.Body))
216224
}
217225

218226
if output.Format(cmd) == output.FmtJSON {

0 commit comments

Comments
 (0)