-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #241 from meroxa/jj/save-the-environment
feat(repair): allow users to repair environments
- Loading branch information
Showing
9 changed files
with
305 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
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.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters