Skip to content

Commit

Permalink
feat(flink): Add logs command (#807)
Browse files Browse the repository at this point in the history
* feat(flink): Add logs command

* chore: update docs

* chore: update to upstream meroxa-go
  • Loading branch information
raulb authored Aug 25, 2023
1 parent 2965944 commit f13fca7
Show file tree
Hide file tree
Showing 65 changed files with 367 additions and 111 deletions.
2 changes: 1 addition & 1 deletion cmd/meroxa/root/apps/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (l *Logs) Execute(ctx context.Context) error {
return getErr
}

output := display.AppLogsTable(appLogs)
output := display.LogsTable(appLogs)

l.logger.Info(ctx, output)
l.logger.JSON(ctx, appLogs)
Expand Down
4 changes: 2 additions & 2 deletions cmd/meroxa/root/apps/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestApplicationLogsExecution(t *testing.T) {
}

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := display.AppLogsTable(appLogs)
wantLeveledOutput := display.LogsTable(appLogs)

// N.B. This comparison is undeterminstic when the test data map contains
// more than one key. Maps in golang are not guaranteed ordered so the result
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestApplicationLogsExecutionWithPath(t *testing.T) {
}

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := display.AppLogsTable(appLogs)
wantLeveledOutput := display.LogsTable(appLogs)

if !strings.Contains(gotLeveledOutput, wantLeveledOutput) {
t.Fatalf(cmp.Diff(wantLeveledOutput, gotLeveledOutput))
Expand Down
1 change: 1 addition & 0 deletions cmd/meroxa/root/flink/flink.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (*Job) SubCommands() []*cobra.Command {
return []*cobra.Command{
builder.BuildCobraCommand(&Deploy{}),
builder.BuildCobraCommand(&Describe{}),
builder.BuildCobraCommand(&Logs{}),
builder.BuildCobraCommand(&Remove{}),
builder.BuildCobraCommand(&List{}),
}
Expand Down
98 changes: 98 additions & 0 deletions cmd/meroxa/root/flink/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright © 2022 Meroxa Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package flink

import (
"context"
"errors"

"github.com/meroxa/cli/cmd/meroxa/builder"
"github.com/meroxa/cli/log"
"github.com/meroxa/cli/utils/display"
"github.com/meroxa/meroxa-go/pkg/meroxa"
)

var (
_ builder.CommandWithAliases = (*Logs)(nil)
_ builder.CommandWithDocs = (*Logs)(nil)
_ builder.CommandWithArgs = (*Logs)(nil)
_ builder.CommandWithClient = (*Logs)(nil)
_ builder.CommandWithLogger = (*Logs)(nil)
_ builder.CommandWithExecute = (*Logs)(nil)
)

type Logs struct {
client flinkLogsClient
logger log.Logger

args struct {
NameOrUUID string
}
}

type flinkLogsClient interface {
GetFlinkLogsV2(ctx context.Context, nameOrUUID string) (*meroxa.Logs, error)
}

func (*Logs) Aliases() []string {
return []string{"log"}
}

func (l *Logs) Usage() string {
return `logs [NameOrUUID] [--path pwd]`
}

func (l *Logs) Docs() builder.Docs {
return builder.Docs{
Short: "View relevant logs to the state of the given Flink Job",
Example: `meroxa jobs logs my-flink-job-name
meroxa jobs logs my-flink-job-uuid`,
}
}

func (l *Logs) Execute(ctx context.Context) error {
nameOrUUID := l.args.NameOrUUID

appLogs, getErr := l.client.GetFlinkLogsV2(ctx, nameOrUUID)
if getErr != nil {
return getErr
}

output := display.LogsTable(appLogs)

l.logger.Info(ctx, output)
l.logger.JSON(ctx, appLogs)

return nil
}

func (l *Logs) Client(client meroxa.Client) {
l.client = client
}

func (l *Logs) Logger(logger log.Logger) {
l.logger = logger
}

func (l *Logs) ParseArgs(args []string) error {
if len(args) < 1 {
return errors.New("requires Flink Job name or UUID")
}

l.args.NameOrUUID = args[0]
return nil
}
104 changes: 104 additions & 0 deletions cmd/meroxa/root/flink/logs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package flink

import (
"context"
"encoding/json"
"errors"
"reflect"
"strings"
"testing"
"time"

"github.com/meroxa/cli/utils"

"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/meroxa/cli/log"
"github.com/meroxa/cli/utils/display"
"github.com/meroxa/meroxa-go/pkg/meroxa"
"github.com/meroxa/meroxa-go/pkg/mock"
)

func TestLogsFlinkJobArgs(t *testing.T) {
tests := []struct {
args []string
err error
name string
}{
{args: nil, err: errors.New("requires Flink Job name or UUID"), name: ""},
{args: []string{"job-name"}, err: nil, name: "job-name"},
}

for _, tt := range tests {
l := &Logs{}
err := l.ParseArgs(tt.args)

if err != nil && tt.err.Error() != err.Error() {
t.Fatalf("expected \"%s\" got \"%s\"", tt.err, err)
}

if tt.name != l.args.NameOrUUID {
t.Fatalf("expected \"%s\" got \"%s\"", tt.name, l.args.NameOrUUID)
}
}
}

func TestFlinkLogsExecution(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
client := mock.NewMockClient(ctrl)
logger := log.NewTestLogger()

fj := utils.GenerateFlinkJob()

flinkLogs := &meroxa.Logs{
Data: []meroxa.LogData{
{
Timestamp: time.Now().UTC(),
Log: "log just logging",
Source: "connector",
},
{
Timestamp: time.Now().UTC(),
Log: "another log",
Source: "flink-job",
},
},
Metadata: meroxa.Metadata{
End: time.Now().UTC(),
Start: time.Now().UTC().Add(-12 * time.Hour),
Limit: 10,
},
}

client.EXPECT().GetFlinkLogsV2(ctx, fj.Name).Return(flinkLogs, nil)

l := &Logs{
client: client,
logger: logger,
}
l.args.NameOrUUID = fj.Name

err := l.Execute(ctx)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
}

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := display.LogsTable(flinkLogs)

if !strings.Contains(gotLeveledOutput, wantLeveledOutput) {
t.Fatalf(cmp.Diff(wantLeveledOutput, gotLeveledOutput))
}

gotJSONOutput := logger.JSONOutput()
var gotAppLogs meroxa.Logs
err = json.Unmarshal([]byte(gotJSONOutput), &gotAppLogs)
if err != nil {
t.Fatalf("not expected error, got %q", err.Error())
}

if !reflect.DeepEqual(gotAppLogs, *flinkLogs) {
t.Fatalf(cmp.Diff(*flinkLogs, gotAppLogs))
}
}
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-account-list.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-account-set.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-account.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-api.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-apps-deploy.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-apps-describe.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-apps-init.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-apps-list.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-apps-logs.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-apps-open.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-apps-remove.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-apps-run.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-apps-upgrade.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-apps.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-auth-login.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-auth-logout.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-auth-whoami.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-auth.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-billing.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-builds-describe.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-builds-logs.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-builds.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
2 changes: 1 addition & 1 deletion etc/man/man1/meroxa-completion.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH "Meroxa" "1" "May 2023" "Meroxa CLI " "Meroxa Manual"
.TH "Meroxa" "1" "Aug 2023" "Meroxa CLI " "Meroxa Manual"

.SH NAME
.PP
Expand Down
Loading

0 comments on commit f13fca7

Please sign in to comment.