diff --git a/cmd/dbaas_external_endpoint_create.go b/cmd/dbaas_external_endpoint_create.go index 8c17e8a4..00b33453 100644 --- a/cmd/dbaas_external_endpoint_create.go +++ b/cmd/dbaas_external_endpoint_create.go @@ -95,10 +95,10 @@ func (c *dbaasExternalEndpointCreateCmd) cmdRun(cmd *cobra.Command, args []strin switch c.Type { case "datadog": return c.createDatadog(cmd, args) - // case "elasticsearch": - // return c.createElasticsearch(cmd, args) case "opensearch": return c.createOpensearch(cmd, args) + case "elasticsearch": + return c.createElasticsearch(cmd, args) // case "prometheus": // return c.createPrometheus(cmd, args) // case "rsyslog": diff --git a/cmd/dbaas_external_endpoint_create_elasticsearch.go b/cmd/dbaas_external_endpoint_create_elasticsearch.go new file mode 100644 index 00000000..abae84f2 --- /dev/null +++ b/cmd/dbaas_external_endpoint_create_elasticsearch.go @@ -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) createElasticsearch(_ *cobra.Command, _ []string) error { + ctx := gContext + client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(account.CurrentAccount.DefaultZone)) + if err != nil { + return err + } + + elasticsearchRequestPayload := v3.CreateDBAASExternalEndpointElasticsearchRequest{ + Settings: &v3.DBAASEndpointElasticsearch{}, + } + + if c.ElasticsearchURL != "" { + elasticsearchRequestPayload.Settings.URL = c.ElasticsearchURL + } + if c.ElasticsearchIndexPrefix != "" { + elasticsearchRequestPayload.Settings.IndexPrefix = c.ElasticsearchIndexPrefix + } + if c.ElasticsearchCA != "" { + elasticsearchRequestPayload.Settings.CA = c.ElasticsearchCA + } + if c.ElasticsearchIndexDaysMax != 0 { + elasticsearchRequestPayload.Settings.IndexDaysMax = c.ElasticsearchIndexDaysMax + } + if c.ElasticsearchTimeout != 0 { + elasticsearchRequestPayload.Settings.Timeout = c.ElasticsearchTimeout + } + + op, err := client.CreateDBAASExternalEndpointElasticsearch(ctx, c.Name, elasticsearchRequestPayload) + if err != nil { + return err + } + + decorateAsyncOperation(fmt.Sprintf("Creating DBaaS ElasticSearch 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: "elasticsearch", + }).cmdRun(nil, nil) + } + return nil +} diff --git a/cmd/dbaas_external_endpoint_show.go b/cmd/dbaas_external_endpoint_show.go index f4c37dc9..f7ea9598 100644 --- a/cmd/dbaas_external_endpoint_show.go +++ b/cmd/dbaas_external_endpoint_show.go @@ -36,6 +36,8 @@ func (c *dbaasExternalEndpointShowCmd) cmdRun(cmd *cobra.Command, args []string) return c.outputFunc(c.showDatadog()) case "opensearch": return c.outputFunc(c.showOpensearch()) + case "elasticsearch": + return c.outputFunc(c.showElasticsearch()) default: return fmt.Errorf("unsupported external endpoint type %q", c.Type) } diff --git a/cmd/dbaas_external_endpoint_show_elasticsearch.go b/cmd/dbaas_external_endpoint_show_elasticsearch.go new file mode 100644 index 00000000..bead36f1 --- /dev/null +++ b/cmd/dbaas_external_endpoint_show_elasticsearch.go @@ -0,0 +1,66 @@ +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 elasticsearchOutput struct { + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + Settings v3.DBAASEndpointElasticsearch `json:"settings"` +} + +func (o *elasticsearchOutput) ToJSON() { output.JSON(o) } +func (o *elasticsearchOutput) ToText() { output.Text(o) } +func (o *elasticsearchOutput) ToTable() { + t := table.NewTable(os.Stdout) + t.SetHeader([]string{"Elasticsearch 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{"Elasticsearch 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 (c *dbaasExternalEndpointShowCmd) showElasticsearch() (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.GetDBAASExternalEndpointElasticsearch(ctx, endpointUUID) + if err != nil { + return nil, fmt.Errorf("error getting ElasticSearch external endpoint: %w", err) + } + + output := &elasticsearchOutput{ + ID: endpointResponse.ID.String(), + Name: endpointResponse.Name, + Type: string(endpointResponse.Type), + Settings: *endpointResponse.Settings, + } + + return output, nil +}