Skip to content

Commit

Permalink
Merge pull request #241 from meroxa/jj/save-the-environment
Browse files Browse the repository at this point in the history
feat(repair): allow users to repair environments
  • Loading branch information
jayjayjpg authored Feb 2, 2022
2 parents 7e26e11 + 3a28b17 commit f52bcf0
Show file tree
Hide file tree
Showing 9 changed files with 305 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{}),
}
}
89 changes: 89 additions & 0 deletions cmd/meroxa/root/environments/repair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
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"
"errors"

"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)
_ builder.CommandWithHidden = (*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 (r *Repair) Hidden() bool {
return true
}

func (r *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 (r *Repair) Logger(logger log.Logger) {
r.logger = logger
}

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

func (r *Repair) ParseArgs(args []string) error {
if len(args) < 1 {
return errors.New("requires environment name or uuid")
}
r.args.NameOrUUID = args[0]
return nil
}

func (r *Repair) Execute(ctx context.Context) error {
rr, err := r.client.PerformActionOnEnvironment(ctx, r.args.NameOrUUID, &meroxa.RepairEnvironmentInput{Action: meroxa.EnvironmentActionRepair}) // nolint:lll
if err != nil {
return err
}

r.logger.Infof(ctx, `The repairment of your environment %q is now in progress and your environment will be up and running soon.`, r.args.NameOrUUID) // nolint:lll
r.logger.Infof(ctx, `Run "meroxa env describe %s" for status.`, r.args.NameOrUUID)
r.logger.JSON(ctx, rr)

return nil
}
107 changes: 107 additions & 0 deletions cmd/meroxa/root/environments/repair_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
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(
`The repairment of your environment %q is now in progress and your environment will be up and running soon.
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-20220202200635-942cf1c778a4
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-20220202200635-942cf1c778a4 h1:NlyG9imqx9fLuqNFtSSpUajILE45Ms/pAkK/f1AaFw0=
github.com/meroxa/meroxa-go v0.0.0-20220202200635-942cf1c778a4/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.

75 changes: 75 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-20220202200635-942cf1c778a4
## explicit; go 1.17
github.com/meroxa/meroxa-go/pkg/meroxa
github.com/meroxa/meroxa-go/pkg/mock
Expand Down

0 comments on commit f52bcf0

Please sign in to comment.