Skip to content

Commit

Permalink
create and show for opensearch
Browse files Browse the repository at this point in the history
  • Loading branch information
simisoft-exo committed Sep 11, 2024
1 parent cdec4db commit 572e378
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/dbaas_external_endpoint_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func (c *dbaasExternalEndpointCreateCmd) cmdRun(cmd *cobra.Command, args []strin
return c.createDatadog(cmd, args)
// case "elasticsearch":
// return c.createElasticsearch(cmd, args)
// case "opensearch":
// return c.createOpensearch(cmd, args)
case "opensearch":
return c.createOpensearch(cmd, args)
// case "prometheus":
// return c.createPrometheus(cmd, args)
// case "rsyslog":
Expand Down
59 changes: 59 additions & 0 deletions cmd/dbaas_external_endpoint_create_opensearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
v3 "github.com/exoscale/egoscale/v3"
)

func (c *dbaasExternalEndpointCreateCmd) createOpensearch(_ *cobra.Command, _ []string) error {
ctx := gContext
client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone))
if err != nil {
return err
}

opensearchRequestPayload := v3.CreateDBAASExternalEndpointOpensearchRequest{
Settings: &v3.DBAASEndpointOpensearch{},
}

if c.OpensearchURL != "" {
opensearchRequestPayload.Settings.URL = c.OpensearchURL
}
if c.OpensearchIndexPrefix != "" {
opensearchRequestPayload.Settings.IndexPrefix = c.OpensearchIndexPrefix
}
if c.OpensearchCA != "" {
opensearchRequestPayload.Settings.CA = c.OpensearchCA
}
if c.OpensearchIndexDaysMax != 0 {
opensearchRequestPayload.Settings.IndexDaysMax = c.OpensearchIndexDaysMax
}
if c.OpensearchTimeout != 0 {
opensearchRequestPayload.Settings.Timeout = c.OpensearchTimeout
}

op, err := client.CreateDBAASExternalEndpointOpensearch(ctx, c.Name, opensearchRequestPayload)
if err != nil {
return err
}

decorateAsyncOperation(fmt.Sprintf("Creating DBaaS OpenSearch external Endpoint %q", c.Name), func() {
op, err = client.Wait(ctx, op, v3.OperationStateSuccess)
})
if err != nil {
return err
}

endpointID := op.Reference.ID.String()
if !globalstate.Quiet {
return (&dbaasExternalEndpointShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
EndpointID: endpointID,
Type: "opensearch",
}).cmdRun(nil, nil)
}
return nil
}
2 changes: 2 additions & 0 deletions cmd/dbaas_external_endpoint_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (c *dbaasExternalEndpointShowCmd) cmdRun(cmd *cobra.Command, args []string)
switch c.Type {
case "datadog":
return c.outputFunc(c.showDatadog())
case "opensearch":
return c.outputFunc(c.showOpensearch())
default:
return fmt.Errorf("unsupported external endpoint type %q", c.Type)
}
Expand Down
73 changes: 73 additions & 0 deletions cmd/dbaas_external_endpoint_show_opensearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"fmt"
"os"
"strconv"

"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
v3 "github.com/exoscale/egoscale/v3"
)

type opensearchOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Settings v3.DBAASEndpointOpensearch `json:"settings"`
}

func (o *opensearchOutput) ToJSON() { output.JSON(o) }
func (o *opensearchOutput) ToText() { output.Text(o) }
func (o *opensearchOutput) ToTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"OpenSearch External Endpoint"})
defer t.Render()

t.Append([]string{"Endpoint ID", o.ID})
t.Append([]string{"Endpoint Name", o.Name})
t.Append([]string{"Endpoint Type", o.Type})

settings := o.Settings
t.Append([]string{"OpenSearch URL", settings.URL})
t.Append([]string{"Index Prefix", settings.IndexPrefix})
t.Append([]string{"CA Certificate", truncateString(settings.CA, 50)})
t.Append([]string{"Index Days Max", strconv.FormatInt(settings.IndexDaysMax, 10)})
t.Append([]string{"Timeout", strconv.FormatInt(settings.Timeout, 10)})
}

func truncateString(s string, maxLen int) string {
if len(s) > maxLen {
return s[:maxLen] + "..."
}
return s
}

func (c *dbaasExternalEndpointShowCmd) showOpensearch() (output.Outputter, error) {
ctx := gContext
client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone))
if err != nil {
return nil, err
}

endpointUUID, err := v3.ParseUUID(c.EndpointID)
if err != nil {
return nil, fmt.Errorf("invalid endpoint ID: %w", err)
}

endpointResponse, err := client.GetDBAASExternalEndpointOpensearch(ctx, endpointUUID)
if err != nil {
return nil, fmt.Errorf("error getting OpenSearch external endpoint: %w", err)
}

output := &opensearchOutput{
ID: endpointResponse.ID.String(),
Name: endpointResponse.Name,
Type: string(endpointResponse.Type),
Settings: *endpointResponse.Settings,
}

return output, nil
}

0 comments on commit 572e378

Please sign in to comment.