-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
4,570 additions
and
3 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
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
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,119 @@ | ||
package create | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"stackit/internal/pkg/args" | ||
"stackit/internal/pkg/confirm" | ||
"stackit/internal/pkg/errors" | ||
"stackit/internal/pkg/examples" | ||
"stackit/internal/pkg/flags" | ||
"stackit/internal/pkg/globalflags" | ||
"stackit/internal/pkg/services/opensearch/client" | ||
opensearchUtils "stackit/internal/pkg/services/opensearch/utils" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stackitcloud/stackit-sdk-go/services/opensearch" | ||
) | ||
|
||
const ( | ||
instanceIdFlag = "instance-id" | ||
hidePasswordFlag = "hide-password" | ||
) | ||
|
||
type inputModel struct { | ||
*globalflags.GlobalFlagModel | ||
InstanceId string | ||
HidePassword bool | ||
} | ||
|
||
func NewCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Create credentials for an OpenSearch instance", | ||
Long: "Create credentials (username and password) for an OpenSearch instance", | ||
Args: args.NoArgs, | ||
Example: examples.Build( | ||
examples.NewExample( | ||
`Create credentials for an OpenSearch instance`, | ||
"$ stackit opensearch credentials create --instance-id xxx"), | ||
examples.NewExample( | ||
`Create credentials for an OpenSearch instance and hide the password in the output`, | ||
"$ stackit opensearch credentials create --instance-id xxx --hide-password"), | ||
), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := context.Background() | ||
model, err := parseInput(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Configure API client | ||
apiClient, err := client.ConfigureClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
instanceLabel, err := opensearchUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId) | ||
if err != nil { | ||
instanceLabel = model.InstanceId | ||
} | ||
|
||
if !model.AssumeYes { | ||
prompt := fmt.Sprintf("Are you sure you want to create a credential for instance %s?", instanceLabel) | ||
err = confirm.PromptForConfirmation(cmd, prompt) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Call API | ||
req := buildRequest(ctx, model, apiClient) | ||
resp, err := req.Execute() | ||
if err != nil { | ||
return fmt.Errorf("create OpenSearch credentials: %w", err) | ||
} | ||
|
||
cmd.Printf("Created credential for instance %s. Credential ID: %s\n\n", instanceLabel, *resp.Id) | ||
cmd.Printf("Username: %s\n", *resp.Raw.Credentials.Username) | ||
if model.HidePassword { | ||
cmd.Printf("Password: <hidden>\n") | ||
} else { | ||
cmd.Printf("Password: %s\n", *resp.Raw.Credentials.Password) | ||
} | ||
cmd.Printf("Host: %s\n", *resp.Raw.Credentials.Host) | ||
cmd.Printf("Port: %d\n", *resp.Raw.Credentials.Port) | ||
cmd.Printf("URI: %s\n", *resp.Uri) | ||
return nil | ||
}, | ||
} | ||
configureFlags(cmd) | ||
return cmd | ||
} | ||
|
||
func configureFlags(cmd *cobra.Command) { | ||
cmd.Flags().Var(flags.UUIDFlag(), instanceIdFlag, "Instance ID") | ||
cmd.Flags().Bool(hidePasswordFlag, false, "Hide password in output") | ||
|
||
err := flags.MarkFlagsRequired(cmd, instanceIdFlag) | ||
cobra.CheckErr(err) | ||
} | ||
|
||
func parseInput(cmd *cobra.Command) (*inputModel, error) { | ||
globalFlags := globalflags.Parse(cmd) | ||
if globalFlags.ProjectId == "" { | ||
return nil, &errors.ProjectIdError{} | ||
} | ||
|
||
return &inputModel{ | ||
GlobalFlagModel: globalFlags, | ||
InstanceId: flags.FlagToStringValue(cmd, instanceIdFlag), | ||
HidePassword: flags.FlagToBoolValue(cmd, hidePasswordFlag), | ||
}, nil | ||
} | ||
|
||
func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch.APIClient) opensearch.ApiCreateCredentialsRequest { | ||
req := apiClient.CreateCredentials(ctx, model.ProjectId, model.InstanceId) | ||
return req | ||
} |
Oops, something went wrong.