-
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.
feat(repair): allow users to repair environments
- Loading branch information
Showing
9 changed files
with
231 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,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 | ||
} |
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,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) | ||
} | ||
} |
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.
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