-
Notifications
You must be signed in to change notification settings - Fork 39
Add sqlserverflex database create/delete commands
#387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,34 @@ | ||
| ## stackit beta sqlserverflex database | ||
|
|
||
| Provides functionality for SQLServer Flex databases | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Provides functionality for SQLServer Flex databases. | ||
|
|
||
| ``` | ||
| stackit beta sqlserverflex database [flags] | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit beta sqlserverflex database" | ||
| ``` | ||
|
|
||
| ### 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 | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit beta sqlserverflex](./stackit_beta_sqlserverflex.md) - Provides functionality for SQLServer Flex | ||
| * [stackit beta sqlserverflex database create](./stackit_beta_sqlserverflex_database_create.md) - Creates a SQLServer Flex database | ||
| * [stackit beta sqlserverflex database delete](./stackit_beta_sqlserverflex_database_delete.md) - Deletes a SQLServer Flex database | ||
|
|
This file contains hidden or 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,42 @@ | ||
| ## stackit beta sqlserverflex database create | ||
|
|
||
| Creates a SQLServer Flex database | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Creates a SQLServer Flex database. | ||
| This operation cannot be triggered asynchronously (the "--async" flag will have no effect). | ||
|
|
||
| ``` | ||
| stackit beta sqlserverflex database create DATABASE_NAME [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Create a SQLServer Flex database with name "my-database" on instance with ID "xxx" | ||
| $ stackit beta sqlserverflex database create my-database --instance-id xxx --owner some-username | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit beta sqlserverflex database create" | ||
| --instance-id string SQLServer Flex instance ID | ||
| --owner string Username of the owner user | ||
| ``` | ||
|
|
||
| ### 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 | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit beta sqlserverflex database](./stackit_beta_sqlserverflex_database.md) - Provides functionality for SQLServer Flex databases | ||
|
|
This file contains hidden or 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,41 @@ | ||
| ## stackit beta sqlserverflex database delete | ||
|
|
||
| Deletes a SQLServer Flex database | ||
|
|
||
| ### Synopsis | ||
|
|
||
| Deletes a SQLServer Flex database. | ||
| This operation cannot be triggered asynchronously (the "--async" flag will have no effect). | ||
|
|
||
| ``` | ||
| stackit beta sqlserverflex database delete DATABASE_NAME [flags] | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| ``` | ||
| Delete a SQLServer Flex database with name "my-database" of instance with ID "xxx" | ||
| $ stackit beta sqlserverflex database delete my-database --instance-id xxx | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help Help for "stackit beta sqlserverflex database delete" | ||
| --instance-id string SQLServer Flex instance ID | ||
| ``` | ||
|
|
||
| ### 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 | ||
| --verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info") | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [stackit beta sqlserverflex database](./stackit_beta_sqlserverflex_database.md) - Provides functionality for SQLServer Flex databases | ||
|
|
158 changes: 158 additions & 0 deletions
158
internal/cmd/beta/sqlserverflex/database/create/create.go
This file contains hidden or 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,158 @@ | ||
| package create | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/goccy/go-yaml" | ||
| "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/services/sqlserverflex/client" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" | ||
| "github.com/stackitcloud/stackit-cli/internal/pkg/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const ( | ||
| databaseNameArg = "DATABASE_NAME" | ||
|
|
||
| instanceIdFlag = "instance-id" | ||
| ownerFlag = "owner" | ||
| ) | ||
|
|
||
| type inputModel struct { | ||
| *globalflags.GlobalFlagModel | ||
| DatabaseName string | ||
| InstanceId string | ||
| Owner string | ||
| } | ||
|
|
||
| func NewCmd(p *print.Printer) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: fmt.Sprintf("create %s", databaseNameArg), | ||
| Short: "Creates a SQLServer Flex database", | ||
| Long: fmt.Sprintf("%s\n%s", | ||
| "Creates a SQLServer Flex database.", | ||
| `This operation cannot be triggered asynchronously (the "--async" flag will have no effect).`, | ||
| ), | ||
| Args: args.SingleArg(databaseNameArg, nil), | ||
| Example: examples.Build( | ||
| examples.NewExample( | ||
| `Create a SQLServer Flex database with name "my-database" on instance with ID "xxx"`, | ||
| "$ stackit beta sqlserverflex database create my-database --instance-id xxx --owner some-username"), | ||
| ), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := context.Background() | ||
| model, err := parseInput(p, cmd, args) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Configure API client | ||
| apiClient, err := client.ConfigureClient(p) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !model.AssumeYes { | ||
| prompt := fmt.Sprintf("Are you sure you want to create database %q? (This cannot be undone)", model.DatabaseName) | ||
| err = p.PromptForConfirmation(prompt) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Call API | ||
| req := buildRequest(ctx, model, apiClient) | ||
| s := spinner.New(p) | ||
| s.Start("Creating database") | ||
| resp, err := req.Execute() | ||
| if err != nil { | ||
| s.StopWithError() | ||
| return fmt.Errorf("create SQLServer Flex database: %w", err) | ||
| } | ||
| s.Stop() | ||
|
|
||
| return outputResult(p, model, resp) | ||
| }, | ||
| } | ||
| configureFlags(cmd) | ||
| return cmd | ||
| } | ||
|
|
||
| func configureFlags(cmd *cobra.Command) { | ||
| cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "SQLServer Flex instance ID") | ||
| cmd.Flags().String(ownerFlag, "", "Username of the owner user") | ||
| err := flags.MarkFlagsRequired(cmd, instanceIdFlag, ownerFlag) | ||
| cobra.CheckErr(err) | ||
| } | ||
|
|
||
| func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) { | ||
| databaseName := inputArgs[0] | ||
|
|
||
| globalFlags := globalflags.Parse(p, cmd) | ||
| if globalFlags.ProjectId == "" { | ||
| return nil, &errors.ProjectIdError{} | ||
| } | ||
|
|
||
| model := inputModel{ | ||
| GlobalFlagModel: globalFlags, | ||
| DatabaseName: databaseName, | ||
| InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag), | ||
| Owner: flags.FlagToStringValue(p, cmd, ownerFlag), | ||
| } | ||
|
|
||
| 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 *sqlserverflex.APIClient) sqlserverflex.ApiCreateDatabaseRequest { | ||
| req := apiClient.CreateDatabase(ctx, model.ProjectId, model.InstanceId) | ||
| payload := sqlserverflex.CreateDatabasePayload{ | ||
| Name: &model.DatabaseName, | ||
| Options: utils.Ptr(map[string]string{ | ||
| "owner": model.Owner, | ||
| }), | ||
| } | ||
| req = req.CreateDatabasePayload(payload) | ||
| return req | ||
| } | ||
|
|
||
| func outputResult(p *print.Printer, model *inputModel, resp *sqlserverflex.CreateDatabaseResponse) error { | ||
| switch model.OutputFormat { | ||
| case print.JSONOutputFormat: | ||
| details, err := json.MarshalIndent(resp, "", " ") | ||
| if err != nil { | ||
| return fmt.Errorf("marshal SQLServer Flex database: %w", err) | ||
| } | ||
| p.Outputln(string(details)) | ||
|
|
||
| return nil | ||
| case print.YAMLOutputFormat: | ||
| details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true)) | ||
| if err != nil { | ||
| return fmt.Errorf("marshal SQLServer Flex database: %w", err) | ||
| } | ||
| p.Outputln(string(details)) | ||
|
|
||
| return nil | ||
| default: | ||
| p.Outputf("Created database %q\n", model.DatabaseName) | ||
| return nil | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.