Skip to content

Commit

Permalink
feat(repair): allow users to repair environments
Browse files Browse the repository at this point in the history
  • Loading branch information
jayjayjpg committed Feb 2, 2022
1 parent 7e26e11 commit bc38d6c
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/meroxa/root/environments/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ func (*Environments) SubCommands() []*cobra.Command {
builder.BuildCobraCommand(&List{}),
builder.BuildCobraCommand(&Remove{}),
builder.BuildCobraCommand(&Update{}),
builder.BuildCobraCommand(&Repair{}),
}
}
82 changes: 82 additions & 0 deletions cmd/meroxa/root/environments/repair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright © 2021 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 environments

import (
"context"

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

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

type repairEnvironmentClient interface {
PerformActionOnEnvironment(ctx context.Context, nameOrUUID string, body *meroxa.RepairEnvironmentInput) (*meroxa.Environment, error)
}

type Repair struct {
client repairEnvironmentClient
logger log.Logger

args struct {
NameOrUUID string
}
}

func (u *Repair) Usage() string {
return "repair NAMEorUUID"
}

func (r *Repair) Docs() builder.Docs {
return builder.Docs{
Short: "Repair environment",
Long: "Repair any environment that is in one of the following states: provisioning_error, deprovisioning_error, repairing_error.",
}
}

func (c *Repair) Logger(logger log.Logger) {
c.logger = logger
}

func (c *Repair) Client(client meroxa.Client) {
c.client = client
}

func (c *Repair) ParseArgs(args []string) error {
if len(args) > 0 {
c.args.NameOrUUID = args[0]
}
return nil
}

func (u *Repair) Execute(ctx context.Context) error {
r, err := u.client.PerformActionOnEnvironment(ctx, u.args.NameOrUUID, &meroxa.RepairEnvironmentInput{Action: "repair"}) /* OPENQ: How do I pass a struct here? */
if err != nil {
return err
}

u.logger.Infof(ctx, "The repairment of your environment %q is now in progress", u.args.NameOrUUID)
u.logger.Info(ctx, "Meroxa will try to resolve the error and your environment should be up and running soon.") //nolint
u.logger.JSON(ctx, r)

return nil
}
106 changes: 106 additions & 0 deletions cmd/meroxa/root/environments/repair_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright © 2021 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 environments

import (
"context"
"encoding/json"
"errors"
"strings"

"fmt"
"reflect"
"testing"

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

func TestRepairEnvironmentArgs(t *testing.T) {
tests := []struct {
args []string
err error
name string
uuid string
}{
{args: nil, err: errors.New("requires environment name or uuid"), name: "", uuid: ""},
{args: []string{"environment-name"}, err: nil, name: "environment-name", uuid: ""},
}

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

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

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

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

r := &Repair{
client: client,
logger: logger,
}

e := utils.GenerateEnvironment("")
r.args.NameOrUUID = e.Name

client.
EXPECT().
PerformActionOnEnvironment(ctx, e.Name, &meroxa.RepairEnvironmentInput{Action: "repair"}).
Return(&e, nil)

err := r.Execute(ctx)

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

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := fmt.Sprintf(
"Repairing environment...\nEnvironment %q has been updated. Run `meroxa env describe %s` for status",
e.Name,
e.Name)

if !strings.Contains(gotLeveledOutput, wantLeveledOutput) {
t.Fatalf("expected output:\n%s\ngot:\n%s", wantLeveledOutput, gotLeveledOutput)
}

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

if !reflect.DeepEqual(gotEnvironment, e) {
t.Fatalf("expected \"%v\", got \"%v\"", e, gotEnvironment)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/manifoldco/promptui v0.8.0
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/meroxa/meroxa-go v0.0.0-20220126011704-43e4ed19207c
github.com/meroxa/meroxa-go v0.0.0-20220131162708-20eac1635900
github.com/nirasan/go-oauth-pkce-code-verifier v0.0.0-20170819232839-0fbfe93532da
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/rivo/uniseg v0.2.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRR
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/meroxa/meroxa-go v0.0.0-20220126011704-43e4ed19207c h1:7L85L+/TmBp8eQHfgxjQ78ydC/+WFukuKi3od9pLMl8=
github.com/meroxa/meroxa-go v0.0.0-20220126011704-43e4ed19207c/go.mod h1:HDFszURCM1cOpKE699o5Hs0T2tEIXqY+vFcsur3RiwY=
github.com/meroxa/meroxa-go v0.0.0-20220131162708-20eac1635900 h1:gKQFFBHqRS0+9DX1Yv1vP8u5OqzGf+R5Vjc7rgHbfmI=
github.com/meroxa/meroxa-go v0.0.0-20220131162708-20eac1635900/go.mod h1:HDFszURCM1cOpKE699o5Hs0T2tEIXqY+vFcsur3RiwY=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand Down
28 changes: 28 additions & 0 deletions vendor/github.com/meroxa/meroxa-go/pkg/meroxa/environment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/meroxa/meroxa-go/pkg/meroxa/meroxa.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/meroxa/meroxa-go/pkg/mock/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ github.com/mattn/go-runewidth
# github.com/mattn/go-shellwords v1.0.12
## explicit; go 1.13
github.com/mattn/go-shellwords
# github.com/meroxa/meroxa-go v0.0.0-20220126011704-43e4ed19207c
# github.com/meroxa/meroxa-go v0.0.0-20220131162708-20eac1635900
## explicit; go 1.17
github.com/meroxa/meroxa-go/pkg/meroxa
github.com/meroxa/meroxa-go/pkg/mock
Expand Down

0 comments on commit bc38d6c

Please sign in to comment.