Skip to content

Commit

Permalink
Preflight cli changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-cross committed Feb 7, 2022
1 parent 8e5159f commit b5c7793
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 21 deletions.
14 changes: 11 additions & 3 deletions cmd/meroxa/root/environments/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Create struct {
Type string `long:"type" usage:"environment type, when not specified"`
Provider string `long:"provider" usage:"environment cloud provider to use"`
Region string `long:"region" usage:"environment region"`
Config []string `short:"c" long:"config" usage:"environment configuration based on type and provider (e.g.: --config aws_access_key_id=my_access_key --config aws_access_secret=my_access_secret)"` // nolint:lll
Config []string `short:"c" long:"config" usage:"environment configuration based on type and provider (e.g.: --config aws_access_key_id=my_access_key --config aws_secret_access_key=my_access_secret)"` // nolint:lll
}

envCfg map[string]interface{}
Expand Down Expand Up @@ -131,12 +131,20 @@ func (c *Create) Execute(ctx context.Context) error {
environment, err := c.client.CreateEnvironment(ctx, e)

if err != nil {
c.logger.JSON(ctx, environment.Status.State)
if environment.Status.State == meroxa.EnvironmentStatePreflightError {
c.logger.JSON(ctx, environment.Status.PreflightDetails)
}
return err
}

c.logger.Infof(ctx, "Environment %q is being provisioned. Run `meroxa env describe %s` for status", environment.Name, environment.Name)
c.logger.JSON(ctx, environment)

if environment.Status.State == meroxa.EnvironmentStatePreflightSuccess {
environment.Status.PreflightDetails = nil
}

c.logger.JSON(ctx, environment)
return nil
}

Expand Down Expand Up @@ -275,7 +283,7 @@ func (c *Create) Docs() builder.Docs {
return builder.Docs{
Short: "Create an environment",
Example: `
meroxa env create my-env --type self_hosted --provider aws --region us-east-1 --config aws_access_key_id=my_access_key --config aws_access_secret=my_access_secret
meroxa env create my-env --type self_hosted --provider aws --region us-east-1 --config aws_access_key_id=my_access_key --config aws_secret_access_key=my_access_secret
`,
}
}
51 changes: 51 additions & 0 deletions cmd/meroxa/root/environments/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/golang/mock/gomock"
"github.com/meroxa/cli/log"
Expand Down Expand Up @@ -103,3 +104,53 @@ func TestDescribeEnvironmentExecution(t *testing.T) {
t.Fatalf("expected \"%v\", got \"%v\"", e, gotEnvironment)
}
}

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

environmentName := "my-env"

e := utils.GenerateEnvironmentFailed(environmentName)

client.
EXPECT().
GetEnvironment(
ctx,
e.Name,
).
Return(&e, nil)

dc := &Describe{
client: client,
logger: logger,
}
dc.args.NameOrUUID = e.Name

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

gotLeveledOutput := logger.LeveledOutput()
wantLeveledOutput := utils.EnvironmentTable(&e)

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)
}

fmt.Println(wantLeveledOutput)
}
Binary file added kubectl
Binary file not shown.
69 changes: 61 additions & 8 deletions utils/display.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"encoding/json"
"fmt"
"net/url"
"strconv"
Expand Down Expand Up @@ -628,8 +627,6 @@ func EnvironmentsTable(environments []*meroxa.Environment, hideHeaders bool) str
func EnvironmentTable(environment *meroxa.Environment) string {
mainTable := simpletable.New()

bytes, _ := json.Marshal(&environment.Status)

mainTable.Body.Cells = [][]*simpletable.Cell{
{
{Align: simpletable.AlignRight, Text: "UUID:"},
Expand All @@ -651,10 +648,7 @@ func EnvironmentTable(environment *meroxa.Environment) string {
{Align: simpletable.AlignRight, Text: "Type:"},
{Text: string(environment.Type)},
},
{
{Align: simpletable.AlignRight, Text: "Status:"},
{Text: string(bytes)},
},

{
{Align: simpletable.AlignRight, Text: "Created At:"},
{Text: environment.CreatedAt.String()},
Expand All @@ -663,11 +657,70 @@ func EnvironmentTable(environment *meroxa.Environment) string {
{Align: simpletable.AlignRight, Text: "Updated At:"},
{Text: environment.UpdatedAt.String()},
},
{
{Align: simpletable.AlignRight, Text: "Environment Status:"},
{Text: string(environment.Status.State)},
},
}

mainTable.SetStyle(simpletable.StyleCompact)

return mainTable.String()
preflightTable := simpletable.New()

preflightTable.Body.Cells = [][]*simpletable.Cell{
{
{Align: simpletable.AlignRight, Text: " AWS EC2 Permissions Status:"},
{Text: strings.Join(environment.Status.PreflightDetails.PreflightPermissions.EC2, " ; ")},
},
{
{Align: simpletable.AlignRight, Text: "AWS EKS Permissions Status:"},
{Text: strings.Join(environment.Status.PreflightDetails.PreflightPermissions.EKS, " ; ")},
},
{
{Align: simpletable.AlignRight, Text: "AWS IAM Permissions Status:"},
{Text: strings.Join(environment.Status.PreflightDetails.PreflightPermissions.IAM, " ; ")},
},
{
{Align: simpletable.AlignRight, Text: "AWS KMS Permissions Status:"},
{Text: strings.Join(environment.Status.PreflightDetails.PreflightPermissions.KMS, " ; ")},
},
{
{Align: simpletable.AlignRight, Text: "AWS MKS Permissions Status:"},
{Text: strings.Join(environment.Status.PreflightDetails.PreflightPermissions.MSK, " ; ")},
},
{
{Align: simpletable.AlignRight, Text: "AWS S3 Permissions Status:"},
{Text: strings.Join(environment.Status.PreflightDetails.PreflightPermissions.S3, " ; ")},
},
{
{Align: simpletable.AlignRight, Text: "AWS Service Quotas Permissions Status:"},
{Text: strings.Join(environment.Status.PreflightDetails.PreflightPermissions.ServiceQuotas, " ; ")},
},
{
{Align: simpletable.AlignRight, Text: "AWS CloudFormation Permissions Status:"},
{Text: strings.Join(environment.Status.PreflightDetails.PreflightPermissions.Cloudformation, " ; ")},
},
{
{Align: simpletable.AlignRight, Text: "AWS Cloudwatch Permissions Status:"},
{Text: strings.Join(environment.Status.PreflightDetails.PreflightPermissions.Cloudwatch, " ; ")},
},
{
{Align: simpletable.AlignRight, Text: "AWS EIP Limits Status:"},
{Text: string(environment.Status.PreflightDetails.PreflightLimits.EIP)},
},
{
{Align: simpletable.AlignRight, Text: "AWS NAT Limits Status:"},
{Text: string(environment.Status.PreflightDetails.PreflightLimits.NAT)},
},
{
{Align: simpletable.AlignRight, Text: "AWS VPC Limits Status:"},
{Text: string(environment.Status.PreflightDetails.PreflightLimits.VPC)},
},
}

preflightTable.SetStyle(simpletable.StyleCompact)

return mainTable.String() + "\n" + preflightTable.String()
}

func PrintEnvironmentsTable(environments []*meroxa.Environment, hideHeaders bool) {
Expand Down
64 changes: 64 additions & 0 deletions utils/display_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,70 @@ func TestEnvironmentsTable(t *testing.T) {
}
}

func TestEnvironmentsTablePreflightFailed(t *testing.T) {
e := &meroxa.Environment{
Type: meroxa.EnvironmentTypePrivate,
Name: "environment-preflight-failed",
Provider: meroxa.EnvironmentProviderAws,
Region: meroxa.EnvironmentRegionUsEast1,
Status: meroxa.EnvironmentViewStatus{
State: meroxa.EnvironmentStatePreflightError,
Details: "",
PreflightDetails: meroxa.PreflightDetails{
PreflightPermissions: meroxa.PreflightPermissions{
S3: []string{"missing read permission for S3", "missing write permissions for S3"},
EC2: []string{"missing read permission for S3", "missing write permissions for S3"},
},
PreflightLimits: meroxa.PreflightLimits{
EIP: "",
},
},
},
UUID: "531428f7-4e86-4094-8514-d397d49026f7",
}

tests := map[string][]*meroxa.Environment{
"Base": {e},
}

tableHeaders := []string{"ID", "NAME", "TYPE", "PROVIDER", "REGION", "STATE"}

for name, environments := range tests {
t.Run(name, func(t *testing.T) {
out := CaptureOutput(func() {
PrintEnvironmentsTable(environments, false)
})

for _, header := range tableHeaders {
if !strings.Contains(out, header) {
t.Errorf("%s header is missing", header)
}
}

if !strings.Contains(out, e.UUID) {
t.Errorf("%s, not found", e.UUID)
}
if !strings.Contains(out, e.Name) {
t.Errorf("%s, not found", e.Name)
}
if !strings.Contains(out, string(e.Type)) {
t.Errorf("%s, not found", e.Type)
}
if !strings.Contains(out, string(e.Region)) {
t.Errorf("%s, not found", e.Region)
}
if !strings.Contains(out, string(e.Status.State)) {
t.Errorf("%s, not found", e.Status.State)
}
if !strings.Contains(out, e.UUID) {
t.Errorf("%s, not found", e.UUID)
}

fmt.Println(out)
})
}
}

func TestEnvironmentsTableWithoutHeaders(t *testing.T) {
e := &meroxa.Environment{
Type: meroxa.EnvironmentTypePrivate,
Expand Down
29 changes: 28 additions & 1 deletion utils/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,33 @@ func GenerateTransform() meroxa.Transform {
}
}

func GenerateEnvironmentFailed(environmentName string) meroxa.Environment {
if environmentName == "" {
environmentName = "environment-1234"
}

return meroxa.Environment{
UUID: "fd572375-77ce-4448-a071-ee4707a599d6",
Type: meroxa.EnvironmentTypePrivate,
Name: environmentName,
Region: meroxa.EnvironmentRegionUsEast1,
Provider: meroxa.EnvironmentProviderAws,
Status: meroxa.EnvironmentViewStatus{
State: meroxa.EnvironmentStatePreflightError,
Details: "",
PreflightDetails: meroxa.PreflightDetails{
PreflightPermissions: meroxa.PreflightPermissions{
S3: []string{"missing read permission for S3", "missing write permissions for S3"},
EC2: []string{"missing read permission for S3", "missing write permissions for S3"},
},
PreflightLimits: meroxa.PreflightLimits{
EIP: "",
},
},
},
}
}

func GenerateEnvironment(environmentName string) meroxa.Environment {
if environmentName == "" {
environmentName = "environment-1234"
Expand All @@ -137,7 +164,7 @@ func GenerateEnvironment(environmentName string) meroxa.Environment {
Region: meroxa.EnvironmentRegionUsEast1,
Provider: meroxa.EnvironmentProviderAws,
Status: meroxa.EnvironmentViewStatus{
State: meroxa.EnvironmentStateProvisioned,
State: meroxa.EnvironmentStatePreflightError,
},
}
}
Expand Down
44 changes: 35 additions & 9 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.

0 comments on commit b5c7793

Please sign in to comment.