Skip to content

Commit

Permalink
Onboard OpenSearch service
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopalet committed Jan 30, 2024
1 parent 0459c31 commit 97f1a59
Show file tree
Hide file tree
Showing 33 changed files with 4,570 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/dns v0.8.2
github.com/stackitcloud/stackit-sdk-go/services/membership v0.3.4
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v0.10.3
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.9.2
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.7.5
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.3.4
github.com/stackitcloud/stackit-sdk-go/services/ske v0.9.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ github.com/stackitcloud/stackit-sdk-go/services/membership v0.3.4 h1:0OT/UBP55/G
github.com/stackitcloud/stackit-sdk-go/services/membership v0.3.4/go.mod h1:6ovfcQJ96ivkBpSI933lVl2a/SWprpVGoK6YNKycLps=
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v0.10.3 h1:M7ALIg1tE8MFLLw9Um0iyvdBgIhl83tJ0sWRjP7YqMM=
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v0.10.3/go.mod h1:LWfUBjGQWF3SZivQdUdAC/WxJkx8ImJKy5GFMV3tXHY=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.9.2 h1:dwZ1NDD+AxTaZqAeR/0PY7yt32dbABhQH1Vsnt8A+hg=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.9.2/go.mod h1:M8mjTS5yR0XXoH9EpuULme9fEkLhUz4UOT7XSHUSRQ8=
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.7.5 h1:Gu0z8MpErzBHxb9xx8B/4DduxckDmBRPWNaeoVcE8cQ=
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.7.5/go.mod h1:MQ5eGWFmnDf9wUArqZ2g+nwJgMDkYDQUkoRVutaHrms=
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.3.4 h1:XNL7bk5mwCovV8a3oIIC9PlNpPTUG3XNwdRqHS5V2no=
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/config/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
serviceAccountCustomEndpointFlag = "service-account-custom-endpoint"
skeCustomEndpointFlag = "ske-custom-endpoint"
resourceManagerCustomEndpointFlag = "resource-manager-custom-endpoint"
OpenSearchCustomEndpointFlag = "opensearch-custom-endpoint"
)

type inputModel struct {
Expand Down Expand Up @@ -86,6 +87,7 @@ func configureFlags(cmd *cobra.Command) {
cmd.Flags().String(serviceAccountCustomEndpointFlag, "", "Service Account custom endpoint")
cmd.Flags().String(skeCustomEndpointFlag, "", "SKE custom endpoint")
cmd.Flags().String(resourceManagerCustomEndpointFlag, "", "Resource manager custom endpoint")
cmd.Flags().String(OpenSearchCustomEndpointFlag, "", "OpenSearch custom endpoint")

err := viper.BindPFlag(config.DNSCustomEndpointKey, cmd.Flags().Lookup(dnsCustomEndpointFlag))
cobra.CheckErr(err)
Expand All @@ -99,6 +101,8 @@ func configureFlags(cmd *cobra.Command) {
cobra.CheckErr(err)
err = viper.BindPFlag(config.ResourceManagerEndpointKey, cmd.Flags().Lookup(skeCustomEndpointFlag))
cobra.CheckErr(err)
err = viper.BindPFlag(config.OpenSearchCustomEndpointKey, cmd.Flags().Lookup(OpenSearchCustomEndpointFlag))
cobra.CheckErr(err)
}

func parseInput(cmd *cobra.Command) (*inputModel, error) {
Expand Down
7 changes: 7 additions & 0 deletions internal/cmd/config/unset/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
serviceAccountCustomEndpointFlag = "service-account-custom-endpoint"
skeCustomEndpointFlag = "ske-custom-endpoint"
resourceManagerCustomEndpointFlag = "resource-manager-custom-endpoint"
openSearchCustomEndpointFlag = "opensearch-custom-endpoint"
)

type inputModel struct {
Expand All @@ -37,6 +38,7 @@ type inputModel struct {
ServiceAccountCustomEndpoint bool
SKECustomEndpoint bool
ResourceManagerCustomEndpoint bool
OpenSearchCustomEndpoint bool
}

func NewCmd() *cobra.Command {
Expand Down Expand Up @@ -87,6 +89,9 @@ func NewCmd() *cobra.Command {
if model.ResourceManagerCustomEndpoint {
viper.Set(config.ResourceManagerEndpointKey, "")
}
if model.OpenSearchCustomEndpoint {
viper.Set(config.OpenSearchCustomEndpointKey, "")
}

err := viper.WriteConfig()
if err != nil {
Expand All @@ -110,6 +115,7 @@ func configureFlags(cmd *cobra.Command) {
cmd.Flags().Bool(serviceAccountCustomEndpointFlag, false, "SKE custom endpoint")
cmd.Flags().Bool(skeCustomEndpointFlag, false, "SKE custom endpoint")
cmd.Flags().Bool(resourceManagerCustomEndpointFlag, false, "Resource Manager custom endpoint")
cmd.Flags().Bool(openSearchCustomEndpointFlag, false, "OpenSearch custom endpoint")
}

func parseInput(cmd *cobra.Command) *inputModel {
Expand All @@ -124,5 +130,6 @@ func parseInput(cmd *cobra.Command) *inputModel {
ServiceAccountCustomEndpoint: flags.FlagToBoolValue(cmd, serviceAccountCustomEndpointFlag),
SKECustomEndpoint: flags.FlagToBoolValue(cmd, skeCustomEndpointFlag),
ResourceManagerCustomEndpoint: flags.FlagToBoolValue(cmd, resourceManagerCustomEndpointFlag),
OpenSearchCustomEndpoint: flags.FlagToBoolValue(cmd, openSearchCustomEndpointFlag),
}
}
3 changes: 3 additions & 0 deletions internal/cmd/config/unset/unset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]bool)) map[string]bool
serviceAccountCustomEndpointFlag: true,
skeCustomEndpointFlag: true,
resourceManagerCustomEndpointFlag: true,
openSearchCustomEndpointFlag: true,
}
for _, mod := range mods {
mod(flagValues)
Expand All @@ -30,6 +31,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
ServiceAccountCustomEndpoint: true,
SKECustomEndpoint: true,
ResourceManagerCustomEndpoint: true,
OpenSearchCustomEndpoint: true,
}
for _, mod := range mods {
mod(model)
Expand Down Expand Up @@ -61,6 +63,7 @@ func TestParseInput(t *testing.T) {
model.ServiceAccountCustomEndpoint = false
model.SKECustomEndpoint = false
model.ResourceManagerCustomEndpoint = false
model.OpenSearchCustomEndpoint = false
}),
},
{
Expand Down
119 changes: 119 additions & 0 deletions internal/cmd/opensearch/credentials/create/create.go
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
}
Loading

0 comments on commit 97f1a59

Please sign in to comment.