diff --git a/docs/stackit_git.md b/docs/stackit_git.md index 8a959c7d2..2a9b072e2 100644 --- a/docs/stackit_git.md +++ b/docs/stackit_git.md @@ -30,8 +30,6 @@ stackit git [flags] ### SEE ALSO * [stackit](./stackit.md) - Manage STACKIT resources using the command line -* [stackit git create](./stackit_git_create.md) - Creates STACKIT Git instance -* [stackit git delete](./stackit_git_delete.md) - Deletes STACKIT Git instance -* [stackit git describe](./stackit_git_describe.md) - Describes STACKIT Git instance -* [stackit git list](./stackit_git_list.md) - Lists all instances of STACKIT Git. +* [stackit git flavor](./stackit_git_flavor.md) - Provides functionality for STACKIT Git flavors +* [stackit git instance](./stackit_git_instance.md) - Provides functionality for STACKIT Git instances diff --git a/docs/stackit_git_create.md b/docs/stackit_git_flavor.md similarity index 65% rename from docs/stackit_git_create.md rename to docs/stackit_git_flavor.md index fa7a2a97d..c2ec85a08 100644 --- a/docs/stackit_git_create.md +++ b/docs/stackit_git_flavor.md @@ -1,27 +1,19 @@ -## stackit git create +## stackit git flavor -Creates STACKIT Git instance +Provides functionality for STACKIT Git flavors ### Synopsis -Create a STACKIT Git instance by name. +Provides functionality for STACKIT Git flavors. ``` -stackit git create [flags] -``` - -### Examples - -``` - Create a instance with name 'my-new-instance' - $ stackit git create --name my-new-instance +stackit git flavor [flags] ``` ### Options ``` - -h, --help Help for "stackit git create" - --name string The name of the instance. + -h, --help Help for "stackit git flavor" ``` ### Options inherited from parent commands @@ -38,4 +30,5 @@ stackit git create [flags] ### SEE ALSO * [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git +* [stackit git flavor list](./stackit_git_flavor_list.md) - Lists instances flavors of STACKIT Git. diff --git a/docs/stackit_git_flavor_list.md b/docs/stackit_git_flavor_list.md new file mode 100644 index 000000000..a8fc54b0f --- /dev/null +++ b/docs/stackit_git_flavor_list.md @@ -0,0 +1,44 @@ +## stackit git flavor list + +Lists instances flavors of STACKIT Git. + +### Synopsis + +Lists instances flavors of STACKIT Git for the current project. + +``` +stackit git flavor list [flags] +``` + +### Examples + +``` + List STACKIT Git flavors + $ stackit git flavor list + + Lists up to 10 STACKIT Git flavors + $ stackit git flavor list --limit=10 +``` + +### Options + +``` + -h, --help Help for "stackit git flavor list" + --limit int Limit the output to the first n elements +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit git flavor](./stackit_git_flavor.md) - Provides functionality for STACKIT Git flavors + diff --git a/docs/stackit_git_instance.md b/docs/stackit_git_instance.md new file mode 100644 index 000000000..5f7c6d243 --- /dev/null +++ b/docs/stackit_git_instance.md @@ -0,0 +1,37 @@ +## stackit git instance + +Provides functionality for STACKIT Git instances + +### Synopsis + +Provides functionality for STACKIT Git instances. + +``` +stackit git instance [flags] +``` + +### Options + +``` + -h, --help Help for "stackit git instance" +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git +* [stackit git instance create](./stackit_git_instance_create.md) - Creates STACKIT Git instance +* [stackit git instance delete](./stackit_git_instance_delete.md) - Deletes STACKIT Git instance +* [stackit git instance describe](./stackit_git_instance_describe.md) - Describes STACKIT Git instance +* [stackit git instance list](./stackit_git_instance_list.md) - Lists all instances of STACKIT Git. + diff --git a/docs/stackit_git_instance_create.md b/docs/stackit_git_instance_create.md new file mode 100644 index 000000000..e5d8bba93 --- /dev/null +++ b/docs/stackit_git_instance_create.md @@ -0,0 +1,49 @@ +## stackit git instance create + +Creates STACKIT Git instance + +### Synopsis + +Create a STACKIT Git instance by name. + +``` +stackit git instance create [flags] +``` + +### Examples + +``` + Create a instance with name 'my-new-instance' + $ stackit git instance create --name my-new-instance + + Create a instance with name 'my-new-instance' and flavor + $ stackit git instance create --name my-new-instance --flavor git-100' + + Create a instance with name 'my-new-instance' and acl + $ stackit git instance create --name my-new-instance --acl 1.1.1.1/1' +``` + +### Options + +``` + --acl strings Acl for the instance. + --flavor string Flavor of the instance. + -h, --help Help for "stackit git instance create" + --name string The name of the instance. +``` + +### Options inherited from parent commands + +``` + -y, --assume-yes If set, skips all confirmation prompts + --async If set, runs the command asynchronously + -o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"] + -p, --project-id string Project ID + --region string Target region for region-specific requests + --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") +``` + +### SEE ALSO + +* [stackit git instance](./stackit_git_instance.md) - Provides functionality for STACKIT Git instances + diff --git a/docs/stackit_git_delete.md b/docs/stackit_git_instance_delete.md similarity index 72% rename from docs/stackit_git_delete.md rename to docs/stackit_git_instance_delete.md index 6f863ee60..df0a6d46a 100644 --- a/docs/stackit_git_delete.md +++ b/docs/stackit_git_instance_delete.md @@ -1,4 +1,4 @@ -## stackit git delete +## stackit git instance delete Deletes STACKIT Git instance @@ -7,20 +7,20 @@ Deletes STACKIT Git instance Deletes a STACKIT Git instance by its internal ID. ``` -stackit git delete INSTANCE_ID [flags] +stackit git instance delete INSTANCE_ID [flags] ``` ### Examples ``` Delete a instance with ID "xxx" - $ stackit git delete xxx + $ stackit git instance delete xxx ``` ### Options ``` - -h, --help Help for "stackit git delete" + -h, --help Help for "stackit git instance delete" ``` ### Options inherited from parent commands @@ -36,5 +36,5 @@ stackit git delete INSTANCE_ID [flags] ### SEE ALSO -* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git +* [stackit git instance](./stackit_git_instance.md) - Provides functionality for STACKIT Git instances diff --git a/docs/stackit_git_describe.md b/docs/stackit_git_instance_describe.md similarity index 75% rename from docs/stackit_git_describe.md rename to docs/stackit_git_instance_describe.md index e2aedcc40..90716803e 100644 --- a/docs/stackit_git_describe.md +++ b/docs/stackit_git_instance_describe.md @@ -1,4 +1,4 @@ -## stackit git describe +## stackit git instance describe Describes STACKIT Git instance @@ -7,7 +7,7 @@ Describes STACKIT Git instance Describes a STACKIT Git instance by its internal ID. ``` -stackit git describe INSTANCE_ID [flags] +stackit git instance describe INSTANCE_ID [flags] ``` ### Examples @@ -20,7 +20,7 @@ stackit git describe INSTANCE_ID [flags] ### Options ``` - -h, --help Help for "stackit git describe" + -h, --help Help for "stackit git instance describe" ``` ### Options inherited from parent commands @@ -36,5 +36,5 @@ stackit git describe INSTANCE_ID [flags] ### SEE ALSO -* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git +* [stackit git instance](./stackit_git_instance.md) - Provides functionality for STACKIT Git instances diff --git a/docs/stackit_git_list.md b/docs/stackit_git_instance_list.md similarity index 79% rename from docs/stackit_git_list.md rename to docs/stackit_git_instance_list.md index c0f65927b..96afe06d4 100644 --- a/docs/stackit_git_list.md +++ b/docs/stackit_git_instance_list.md @@ -1,4 +1,4 @@ -## stackit git list +## stackit git instance list Lists all instances of STACKIT Git. @@ -7,7 +7,7 @@ Lists all instances of STACKIT Git. Lists all instances of STACKIT Git for the current project. ``` -stackit git list [flags] +stackit git instance list [flags] ``` ### Examples @@ -23,7 +23,7 @@ stackit git list [flags] ### Options ``` - -h, --help Help for "stackit git list" + -h, --help Help for "stackit git instance list" --limit int Limit the output to the first n elements ``` @@ -40,5 +40,5 @@ stackit git list [flags] ### SEE ALSO -* [stackit git](./stackit_git.md) - Provides functionality for STACKIT Git +* [stackit git instance](./stackit_git_instance.md) - Provides functionality for STACKIT Git instances diff --git a/internal/cmd/git/flavor/flavor.go b/internal/cmd/git/flavor/flavor.go new file mode 100644 index 000000000..c6f4c512f --- /dev/null +++ b/internal/cmd/git/flavor/flavor.go @@ -0,0 +1,28 @@ +package flavor + +import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/git/flavor/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + + "github.com/spf13/cobra" +) + +func NewCmd(params *params.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: "flavor", + Short: "Provides functionality for STACKIT Git flavors", + Long: "Provides functionality for STACKIT Git flavors.", + Args: args.NoArgs, + Run: utils.CmdHelp, + } + addSubcommands(cmd, params) + return cmd +} + +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand( + list.NewCmd(params), + ) +} diff --git a/internal/cmd/git/flavor/list/list.go b/internal/cmd/git/flavor/list/list.go new file mode 100644 index 000000000..aec140d80 --- /dev/null +++ b/internal/cmd/git/flavor/list/list.go @@ -0,0 +1,161 @@ +package list + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/goccy/go-yaml" + "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/projectname" + "github.com/stackitcloud/stackit-cli/internal/pkg/services/git/client" + "github.com/stackitcloud/stackit-cli/internal/pkg/tables" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + "github.com/stackitcloud/stackit-sdk-go/services/git" +) + +type inputModel struct { + *globalflags.GlobalFlagModel + Limit *int64 +} + +const limitFlag = "limit" + +func NewCmd(params *params.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "Lists instances flavors of STACKIT Git.", + Long: "Lists instances flavors of STACKIT Git for the current project.", + Args: args.NoArgs, + Example: examples.Build( + examples.NewExample( + `List STACKIT Git flavors`, + "$ stackit git flavor list"), + examples.NewExample( + "Lists up to 10 STACKIT Git flavors", + "$ stackit git flavor list --limit=10", + ), + ), + RunE: func(cmd *cobra.Command, _ []string) error { + ctx := context.Background() + model, err := parseInput(params.Printer, cmd) + if err != nil { + return err + } + + // Configure API client + apiClient, err := client.ConfigureClient(params.Printer) + if err != nil { + return err + } + + // Call API + req := buildRequest(ctx, model, apiClient) + resp, err := req.Execute() + if err != nil { + return fmt.Errorf("get STACKIT Git flavors: %w", err) + } + flavors := *resp.Flavors + if len(flavors) == 0 { + projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd) + if err != nil { + params.Printer.Debug(print.ErrorLevel, "get project name: %v", err) + projectLabel = model.ProjectId + } + params.Printer.Info("No flavors found for project %q\n", projectLabel) + return nil + } else if model.Limit != nil && len(flavors) > int(*model.Limit) { + flavors = (flavors)[:*model.Limit] + } + return outputResult(params.Printer, model.OutputFormat, flavors) + }, + } + configureFlags(cmd) + return cmd +} + +func configureFlags(cmd *cobra.Command) { + cmd.Flags().Int64(limitFlag, 0, "Limit the output to the first n elements") +} + +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { + globalFlags := globalflags.Parse(p, cmd) + if globalFlags.ProjectId == "" { + return nil, &errors.ProjectIdError{} + } + + limit := flags.FlagToInt64Pointer(p, cmd, limitFlag) + if limit != nil && *limit < 1 { + return nil, &errors.FlagValidationError{ + Flag: limitFlag, + Details: "must be greater than 0", + } + } + + model := inputModel{ + GlobalFlagModel: globalFlags, + Limit: limit, + } + + if p.IsVerbosityDebug() { + modelStr, err := print.BuildDebugStrFromInputModel(model) + if err != nil { + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) + } else { + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) + } + } + + return &model, nil +} + +func buildRequest(ctx context.Context, model *inputModel, apiClient *git.APIClient) git.ApiListFlavorsRequest { + return apiClient.ListFlavors(ctx, model.ProjectId) +} + +func outputResult(p *print.Printer, outputFormat string, flavors []git.Flavor) error { + switch outputFormat { + case print.JSONOutputFormat: + details, err := json.MarshalIndent(flavors, "", " ") + if err != nil { + return fmt.Errorf("marshal Observability flavor list: %w", err) + } + p.Outputln(string(details)) + + return nil + case print.YAMLOutputFormat: + details, err := yaml.MarshalWithOptions(flavors, yaml.IndentSequence(true), yaml.UseJSONMarshaler()) + if err != nil { + return fmt.Errorf("marshal Observability flavor list: %w", err) + } + p.Outputln(string(details)) + + return nil + default: + table := tables.NewTable() + table.SetHeader("ID", "DESCRIPTION", "DISPLAY_NAME", "AVAILABLE", "SKU") + for i := range flavors { + flavor := (flavors)[i] + table.AddRow( + utils.PtrString(flavor.Id), + utils.PtrString(flavor.Description), + utils.PtrString(flavor.DisplayName), + utils.PtrString(flavor.Availability), + utils.PtrString(flavor.Sku), + ) + } + err := table.Display(p) + if err != nil { + return fmt.Errorf("render table: %w", err) + } + + return nil + } +} diff --git a/internal/cmd/git/flavor/list/list_test.go b/internal/cmd/git/flavor/list/list_test.go new file mode 100644 index 000000000..1413630b0 --- /dev/null +++ b/internal/cmd/git/flavor/list/list_test.go @@ -0,0 +1,237 @@ +package list + +import ( + "context" + "strconv" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/google/uuid" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" + "github.com/stackitcloud/stackit-cli/internal/pkg/print" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + "github.com/stackitcloud/stackit-sdk-go/services/git" +) + +type testCtxKey struct{} + +var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") +var testClient = &git.APIClient{} +var testProjectId = uuid.NewString() + +const ( + testLimit = 10 +) + +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { + flagValues := map[string]string{ + globalflags.ProjectIdFlag: testProjectId, + } + for _, mod := range mods { + mod(flagValues) + } + return flagValues +} + +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { + model := &inputModel{ + GlobalFlagModel: &globalflags.GlobalFlagModel{ + ProjectId: testProjectId, + Verbosity: globalflags.VerbosityDefault, + }, + } + for _, mod := range mods { + mod(model) + } + return model +} + +func fixtureRequest(mods ...func(request *git.ApiListFlavorsRequest)) git.ApiListFlavorsRequest { + request := testClient.ListFlavors(testCtx, testProjectId) + for _, mod := range mods { + mod(&request) + } + return request +} + +func TestParseInput(t *testing.T) { + tests := []struct { + description string + flagValues map[string]string + isValid bool + expectedModel *inputModel + }{ + { + description: "base", + flagValues: fixtureFlagValues(), + isValid: true, + expectedModel: fixtureInputModel(), + }, + { + description: "no values", + flagValues: map[string]string{}, + isValid: false, + }, + { + description: "project id missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, globalflags.ProjectIdFlag) + }), + isValid: false, + }, + { + description: "project id invalid 1", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "" + }), + isValid: false, + }, + { + description: "project id invalid 2", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" + }), + isValid: false, + }, + { + description: "with limit flag", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues["limit"] = strconv.Itoa(testLimit) + }), + isValid: true, + expectedModel: fixtureInputModel(func(model *inputModel) { + model.Limit = utils.Ptr(int64(testLimit)) + }), + }, + { + description: "with limit flag == 0", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues["limit"] = strconv.Itoa(0) + }), + isValid: false, + }, + { + description: "with limit flag < 0", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + flagValues["limit"] = strconv.Itoa(-1) + }), + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + p := print.NewPrinter() + cmd := NewCmd(¶ms.CmdParams{Printer: p}) + err := globalflags.Configure(cmd.Flags()) + if err != nil { + t.Fatalf("configure global flags: %v", err) + } + + for flag, value := range tt.flagValues { + err := cmd.Flags().Set(flag, value) + if err != nil { + if !tt.isValid { + return + } + t.Fatalf("setting flag --%s=%s: %v", flag, value, err) + } + } + + err = cmd.ValidateRequiredFlags() + if err != nil { + if !tt.isValid { + return + } + t.Fatalf("error validating flags: %v", err) + } + + model, err := parseInput(p, cmd) + if err != nil { + if !tt.isValid { + return + } + t.Fatalf("error parsing flags: %v", err) + } + + if !tt.isValid { + t.Fatalf("did not fail on invalid input") + } + diff := cmp.Diff(model, tt.expectedModel) + if diff != "" { + t.Fatalf("Data does not match: %s", diff) + } + }) + } +} + +func TestBuildRequest(t *testing.T) { + tests := []struct { + description string + model *inputModel + expectedRequest git.ApiListFlavorsRequest + }{ + { + description: "base", + model: fixtureInputModel(), + expectedRequest: fixtureRequest(), + }, + } + + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + request := buildRequest(testCtx, tt.model, testClient) + + diff := cmp.Diff(request, tt.expectedRequest, + cmp.AllowUnexported(tt.expectedRequest), + cmpopts.EquateComparable(testCtx), + ) + if diff != "" { + t.Fatalf("Data does not match: %s", diff) + } + }) + } +} + +func TestOutputResult(t *testing.T) { + type args struct { + outputFormat string + flavors []git.Flavor + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "empty", + args: args{}, + wantErr: false, + }, + { + name: "set empty flavors slice", + args: args{ + flavors: []git.Flavor{}, + }, + wantErr: false, + }, + { + name: "set empty flavors in flavors slice", + args: args{ + flavors: []git.Flavor{{}}, + }, + wantErr: false, + }, + } + p := print.NewPrinter() + p.Cmd = NewCmd(¶ms.CmdParams{Printer: p}) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := outputResult(p, tt.args.outputFormat, tt.args.flavors); (err != nil) != tt.wantErr { + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/internal/cmd/git/git.go b/internal/cmd/git/git.go index fcacb4fcd..72605d968 100644 --- a/internal/cmd/git/git.go +++ b/internal/cmd/git/git.go @@ -1,10 +1,8 @@ package git import ( - "github.com/stackitcloud/stackit-cli/internal/cmd/git/create" - "github.com/stackitcloud/stackit-cli/internal/cmd/git/delete" - "github.com/stackitcloud/stackit-cli/internal/cmd/git/describe" - "github.com/stackitcloud/stackit-cli/internal/cmd/git/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/git/flavor" + "github.com/stackitcloud/stackit-cli/internal/cmd/git/instance" "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/utils" @@ -26,9 +24,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command { func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { cmd.AddCommand( - list.NewCmd(params), - describe.NewCmd(params), - create.NewCmd(params), - delete.NewCmd(params), + instance.NewCmd(params), + flavor.NewCmd(params), ) } diff --git a/internal/cmd/git/create/create.go b/internal/cmd/git/instance/create/create.go similarity index 82% rename from internal/cmd/git/create/create.go rename to internal/cmd/git/instance/create/create.go index 6df750b37..761c2be61 100644 --- a/internal/cmd/git/create/create.go +++ b/internal/cmd/git/instance/create/create.go @@ -5,10 +5,9 @@ import ( "encoding/json" "fmt" - "github.com/stackitcloud/stackit-cli/internal/cmd/params" - "github.com/goccy/go-yaml" "github.com/spf13/cobra" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" "github.com/stackitcloud/stackit-cli/internal/pkg/args" "github.com/stackitcloud/stackit-cli/internal/pkg/errors" "github.com/stackitcloud/stackit-cli/internal/pkg/examples" @@ -23,13 +22,17 @@ import ( ) const ( - nameFlag = "name" + nameFlag = "name" + flavorFlag = "flavor" + aclFlag = "acl" ) type inputModel struct { *globalflags.GlobalFlagModel - Id *string - Name string + Id *string + Name string + Flavor string + Acl []string } func NewCmd(params *params.CmdParams) *cobra.Command { @@ -41,7 +44,15 @@ func NewCmd(params *params.CmdParams) *cobra.Command { Example: examples.Build( examples.NewExample( `Create a instance with name 'my-new-instance'`, - `$ stackit git create --name my-new-instance`, + `$ stackit git instance create --name my-new-instance`, + ), + examples.NewExample( + `Create a instance with name 'my-new-instance' and flavor`, + `$ stackit git instance create --name my-new-instance --flavor git-100'`, + ), + examples.NewExample( + `Create a instance with name 'my-new-instance' and acl`, + `$ stackit git instance create --name my-new-instance --acl 1.1.1.1/1'`, ), ), RunE: func(cmd *cobra.Command, _ []string) (err error) { @@ -95,6 +106,8 @@ func NewCmd(params *params.CmdParams) *cobra.Command { func configureFlags(cmd *cobra.Command) { cmd.Flags().String(nameFlag, "", "The name of the instance.") + cmd.Flags().String(flavorFlag, "", "Flavor of the instance.") + cmd.Flags().StringSlice(aclFlag, []string{}, "Acl for the instance.") if err := flags.MarkFlagsRequired(cmd, nameFlag); err != nil { cobra.CheckErr(err) } @@ -107,10 +120,14 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { return nil, &errors.ProjectIdError{} } name := flags.FlagToStringValue(p, cmd, nameFlag) + flavor := flags.FlagToStringValue(p, cmd, flavorFlag) + acl := flags.FlagToStringSliceValue(p, cmd, aclFlag) model := inputModel{ GlobalFlagModel: globalFlags, Name: name, + Flavor: flavor, + Acl: acl, } if p.IsVerbosityDebug() { @@ -131,7 +148,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *git.APIClie func createPayload(model *inputModel) git.CreateInstancePayload { return git.CreateInstancePayload{ - Name: &model.Name, + Name: &model.Name, + Flavor: git.CreateInstancePayloadGetFlavorAttributeType(&model.Flavor), + Acl: &model.Acl, } } diff --git a/internal/cmd/git/create/create_test.go b/internal/cmd/git/instance/create/create_test.go similarity index 94% rename from internal/cmd/git/create/create_test.go rename to internal/cmd/git/instance/create/create_test.go index 26499bf8b..5b46e1eef 100644 --- a/internal/cmd/git/create/create_test.go +++ b/internal/cmd/git/instance/create/create_test.go @@ -21,14 +21,18 @@ var ( testClient = &git.APIClient{} testProjectId = uuid.NewString() - testName = "test-instance" + testName = "test-instance" + testFlavor = "git-100" + testAcl = []string{"0.0.0.0/0"} ) func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { flagValues := map[string]string{ globalflags.ProjectIdFlag: testProjectId, - nameFlag: testName, + nameFlag: testName, + flavorFlag: testFlavor, + aclFlag: testAcl[0], } for _, mod := range mods { mod(flagValues) @@ -40,6 +44,8 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { model := &inputModel{ GlobalFlagModel: &globalflags.GlobalFlagModel{ProjectId: testProjectId, Verbosity: globalflags.VerbosityDefault}, Name: testName, + Flavor: testFlavor, + Acl: testAcl, } for _, mod := range mods { mod(model) @@ -49,7 +55,9 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { func fixtureCreatePayload(mods ...func(payload *git.CreateInstancePayload)) (payload git.CreateInstancePayload) { payload = git.CreateInstancePayload{ - Name: &testName, + Name: &testName, + Flavor: git.CreateInstancePayloadGetFlavorAttributeType(&testFlavor), + Acl: &testAcl, } for _, mod := range mods { mod(&payload) diff --git a/internal/cmd/git/delete/delete.go b/internal/cmd/git/instance/delete/delete.go similarity index 97% rename from internal/cmd/git/delete/delete.go rename to internal/cmd/git/instance/delete/delete.go index 67ae11438..c16a6b09e 100644 --- a/internal/cmd/git/delete/delete.go +++ b/internal/cmd/git/instance/delete/delete.go @@ -35,7 +35,8 @@ func NewCmd(params *params.CmdParams) *cobra.Command { Long: "Deletes a STACKIT Git instance by its internal ID.", Args: args.SingleArg(instanceIdArg, utils.ValidateUUID), Example: examples.Build( - examples.NewExample(`Delete a instance with ID "xxx"`, `$ stackit git delete xxx`), + examples.NewExample(`Delete a instance with ID "xxx"`, + `$ stackit git instance delete xxx`), ), RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() diff --git a/internal/cmd/git/delete/delete_test.go b/internal/cmd/git/instance/delete/delete_test.go similarity index 100% rename from internal/cmd/git/delete/delete_test.go rename to internal/cmd/git/instance/delete/delete_test.go diff --git a/internal/cmd/git/describe/describe.go b/internal/cmd/git/instance/describe/describe.go similarity index 100% rename from internal/cmd/git/describe/describe.go rename to internal/cmd/git/instance/describe/describe.go diff --git a/internal/cmd/git/describe/describe_test.go b/internal/cmd/git/instance/describe/describe_test.go similarity index 100% rename from internal/cmd/git/describe/describe_test.go rename to internal/cmd/git/instance/describe/describe_test.go diff --git a/internal/cmd/git/instance/instance.go b/internal/cmd/git/instance/instance.go new file mode 100644 index 000000000..bb9cbf0f4 --- /dev/null +++ b/internal/cmd/git/instance/instance.go @@ -0,0 +1,34 @@ +package instance + +import ( + "github.com/stackitcloud/stackit-cli/internal/cmd/git/instance/create" + delete2 "github.com/stackitcloud/stackit-cli/internal/cmd/git/instance/delete" + "github.com/stackitcloud/stackit-cli/internal/cmd/git/instance/describe" + "github.com/stackitcloud/stackit-cli/internal/cmd/git/instance/list" + "github.com/stackitcloud/stackit-cli/internal/cmd/params" + "github.com/stackitcloud/stackit-cli/internal/pkg/args" + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" + + "github.com/spf13/cobra" +) + +func NewCmd(params *params.CmdParams) *cobra.Command { + cmd := &cobra.Command{ + Use: "instance", + Short: "Provides functionality for STACKIT Git instances", + Long: "Provides functionality for STACKIT Git instances.", + Args: args.NoArgs, + Run: utils.CmdHelp, + } + addSubcommands(cmd, params) + return cmd +} + +func addSubcommands(cmd *cobra.Command, params *params.CmdParams) { + cmd.AddCommand( + list.NewCmd(params), + describe.NewCmd(params), + create.NewCmd(params), + delete2.NewCmd(params), + ) +} diff --git a/internal/cmd/git/list/list.go b/internal/cmd/git/instance/list/list.go similarity index 100% rename from internal/cmd/git/list/list.go rename to internal/cmd/git/instance/list/list.go diff --git a/internal/cmd/git/list/list_test.go b/internal/cmd/git/instance/list/list_test.go similarity index 100% rename from internal/cmd/git/list/list_test.go rename to internal/cmd/git/instance/list/list_test.go